Exemplo n.º 1
0
    async def test_it_uses_the_connection_provided_by_the_route_if_one_exists(
            self):
        conn = AMQPConnection(
            hostname="127.0.0.1",
            username="******",
            password="******",
            prefetch=1024,
        )
        app = App(connections=[])

        @app.route(
            routes=["a_queue_name"],
            type=RouteTypes.AMQP_RABBITMQ,
            options={"connection": conn},
        )
        async def mock_handler(*args, **kwargs):
            pass

        MockedConsumer = Mock(return_value=Mock(spec=Consumer, queue=Mock()))
        with patch("asyncworker.signals.handlers.rabbitmq.Consumer",
                   MockedConsumer):
            await self.signal_handler.startup(app)

        MockedConsumer.assert_called_once_with(
            route_info=ANY,
            host=conn.hostname,
            username=conn.username,
            password=conn.password,
            prefetch_count=conn.prefetch,
        )
Exemplo n.º 2
0
    async def test_rotation_filename_delegates_to_the_namer_if_a_namer_is_provided(
            self):
        namer = Mock(return_value="Xena")
        handler = BaseAsyncRotatingFileHandler(filename=self.temp_file.name,
                                               namer=namer)

        self.assertEqual(handler.rotation_filename("Xablau"), "Xena")
        namer.assert_called_once_with("Xablau")
class TestValidateDecoratorTestCase(TestCase):
    def setUp(self):
        self.dummy_func = Mock()
        self.self_dummy = Mock(
            request=Mock(),
            _schemas={
                "post": Schema(
                    {"job_id": Coerce(int), "email": Email(), "template": str}
                )
            },
        )

    def test_validation_passed(self):
        self.self_dummy.request.method = "POST"
        self.self_dummy.request.body = (
            b'{"job_id": 12, "email": "*****@*****.**", "template": "Hello!"}'
        )

        validate_json(self.dummy_func)(self.self_dummy)

        self.dummy_func.assert_called_once_with(
            self.self_dummy,
            {"job_id": 12, "email": "*****@*****.**", "template": "Hello!"},
        )

    def test_validation_invalid(self):
        self.self_dummy.request.method = "POST"
        self.self_dummy.request.body = (
            b'{"job_id": "test", "email": "*****@*****.**", "template": "Hello!"}'
        )

        with self.assertRaises(ValidationError):
            validate_json(self.dummy_func)(self.self_dummy)

        self.dummy_func.assert_not_called()

    def test_schema_not_found(self):
        self.self_dummy.request.method = "PUT"
        self.self_dummy.request.body = (
            b'{"job_id": 12, "email": "*****@*****.**", "template": "Hello!"}'
        )

        with self.assertRaises(ValidationError):
            validate_json(self.dummy_func)(self.self_dummy)

        self.dummy_func.assert_not_called()

    def test_invalid_json(self):
        self.self_dummy.request.method = "POST"
        self.self_dummy.request.body = b"Hello"

        with self.assertRaises(UnsupportedFormatError):
            validate_json(self.dummy_func)(self.self_dummy)

        self.dummy_func.assert_not_called()
Exemplo n.º 4
0
    async def test_app_middleware_hook(self):
        mock = Mock()

        class Middleware(BaseMiddleware):
            def on_app(self, app):
                mock(app)
            def on_handler(self, handler):
                mock(handler)

        @Middleware(self.resolver)
        class App(jj.App):
            resolver = self.resolver

        async with run(App()) as client:
            response = await client.get("/")
            self.assertEqual(response.status, 404)

        mock.assert_called_once_with(App)
Exemplo n.º 5
0
    async def test_handler_middleware_hook(self):
        mock = Mock()

        class Middleware(BaseMiddleware):
            def on_app(self, app):
                mock(app)
            def on_handler(self, handler):
                mock(handler)

        class App(jj.App):
            resolver = self.resolver
            @Middleware(resolver)
            @MethodMatcher(resolver, "*")
            async def handler(request):
                return Response(status=200)

        async with run(App()) as client:
            response = await client.get("/")
            self.assertEqual(response.status, 200)

        mock.assert_called_once_with(App.handler)
Exemplo n.º 6
0
    async def test_send_emails_with_retry(self):
        params = (
            ("user", "pass"),
            {"reply-to": "*****@*****.**"},
            {"name": "Admin", "email": "*****@*****.**"},
        )
        self.settings_storage.get_gateway_credentials_headers_and_from.return_value = (
            params
        )
        send_email_batch_mock = CoroutineMock(
            side_effect=[
                [],
                [
                    {"id": 3, "email": "*****@*****.**", "name": "Guy"},
                    {"id": 4, "email": "*****@*****.**", "name": "Guy"},
                ],
                [],
            ]
        )
        manage_retry_mock = Mock()

        service = SendEmailService(
            self.web_client, self.email_client, self.settings_storage, 2
        )

        with patch.object(
            service, "send_email_batch", send_email_batch_mock
        ), patch.object(service, "manage_retry", manage_retry_mock):
            await service.send_emails(
                [
                    {"id": i, "email": f"guy_{i}@co.co", "name": "Guy"}
                    for i in range(1, 6)
                ],
                "Hi {name}! Have a nice day",
                "Hello!",
            )

        self.settings_storage.get_gateway_credentials_headers_and_from.assert_awaited_once()
        send_email_batch_mock.assert_has_calls(
            [
                call(
                    [
                        {"id": 1, "email": "*****@*****.**", "name": "Guy"},
                        {"id": 2, "email": "*****@*****.**", "name": "Guy"},
                    ],
                    "Hi {name}! Have a nice day",
                    "Hello!",
                    *params,
                ),
                call(
                    [
                        {"id": 3, "email": "*****@*****.**", "name": "Guy"},
                        {"id": 4, "email": "*****@*****.**", "name": "Guy"},
                    ],
                    "Hi {name}! Have a nice day",
                    "Hello!",
                    *params,
                ),
                call(
                    [{"id": 5, "email": "*****@*****.**", "name": "Guy"}],
                    "Hi {name}! Have a nice day",
                    "Hello!",
                    *params,
                ),
            ]
        )
        manage_retry_mock.assert_called_once_with(
            [
                {"id": 3, "email": "*****@*****.**", "name": "Guy"},
                {"id": 4, "email": "*****@*****.**", "name": "Guy"},
            ],
            "Hi {name}! Have a nice day",
            "Hello!",
            1,
        )
        self.ensure_future_mock.assert_called_once_with(manage_retry_mock.return_value)