Пример #1
0
    def test_check_discovery_error(self, status_code, expected_error_message):
        """
        Validate that `check_discovery` function works as expected.
        """
        @mock_api_response(responses.GET,
                           Path(settings.COURSE_CATALOG_URL_ROOT) / 'health',
                           json=fake_health(all_okay=False),
                           status=status_code)
        def _test_check_discovery_error():

            with raises(DiscoveryNotAvailable) as error:
                check_discovery()
                assert error.message == expected_error_message

        # Run the tests
        _test_check_discovery_error()
Пример #2
0
    def test_check_ecommerce_error(self, status_code, expected_error_message):
        """
        Validate that `check_ecommerce` function works as expected.
        """
        @mock_api_response(responses.GET,
                           Path(settings.ECOMMERCE_PUBLIC_URL_ROOT) / 'health',
                           json=fake_health(all_okay=False),
                           status=status_code)
        def _test_check_ecommerce_error():

            with raises(EcommerceNotAvailable) as error:
                check_ecommerce()
                assert error.message == expected_error_message

        # Run the tests
        _test_check_ecommerce_error()
Пример #3
0
    def test_check_enterprise_catalog_error(self, status_code,
                                            expected_error_message):
        """
        Validate that `check_enterprise_catalog` function works as expected.
        """
        @mock_api_response(
            responses.GET,
            Path(settings.ENTERPRISE_CATALOG_INTERNAL_ROOT_URL) / 'health',
            json=fake_health(all_okay=False),
            status=status_code)
        def _test_check_enterprise_catalog_error():

            with raises(EnterpriseCatalogNotAvailable) as error:
                check_enterprise_catalog()
                assert error.message == expected_error_message

        # Run the tests
        _test_check_enterprise_catalog_error()
Пример #4
0
class TestChecks(unittest.TestCase):
    """
    Validate the behavior of service checks functions.
    """
    @mock_api_response(
        responses.GET,
        Path(settings.LMS_INTERNAL_ROOT_URL) / 'heartbeat',
        json=fake_lms_heartbeat(),
        additional_responses=[
            {
                'method': responses.GET,
                'url': Path(settings.ECOMMERCE_PUBLIC_URL_ROOT) / 'health',
                'json': fake_health(),
            },
            {
                'method': responses.GET,
                'url': Path(settings.COURSE_CATALOG_URL_ROOT) / 'health',
                'json': fake_health(),
            },
            {
                'method': responses.GET,
                'url':
                Path(settings.ENTERPRISE_CATALOG_INTERNAL_ROOT_URL) / 'health',
                'json': fake_health(),
            },
        ])
    def test_run_checks_all_okay(self):
        """
        Validate that `run_checks` returns success if all services are up.
        """

        all_okay, response = run_checks()
        assert all_okay
        assert response['status'] == Status.OK
        assert response['message'] == 'Service is up and running.'
        assert all(service['status'] == Status.OK
                   for service in response['services'])

    @ddt.unpack
    @ddt.data(
        (mock_api_response_with_callback(
            responses.GET,
            Path(settings.LMS_INTERNAL_ROOT_URL) / 'heartbeat',
            callback=lambda _: (200, {}, json.dumps(fake_health())),
            additional_responses=[
                {
                    'method': responses.GET,
                    'url': Path(settings.ECOMMERCE_PUBLIC_URL_ROOT) / 'health',
                    'callback': lambda _: (503, {}, json.dumps(fake_health())),
                },
                {
                    'method': responses.GET,
                    'url': Path(settings.COURSE_CATALOG_URL_ROOT) / 'health',
                    'callback': lambda _: (400, {}, json.dumps(fake_health())),
                },
                {
                    'method': responses.GET,
                    'url':
                    Path(settings.ENTERPRISE_CATALOG_INTERNAL_ROOT_URL) /
                    'health',
                    'callback': Timeout,
                },
            ]), {'E-Commerce', 'Course Discovery', 'Enterprise Catalog'}),
        (mock_api_response_with_callback(
            responses.GET,
            Path(settings.LMS_INTERNAL_ROOT_URL) / 'heartbeat',
            callback=ConnectionError,
            additional_responses=[
                {
                    'method': responses.GET,
                    'url': Path(settings.ECOMMERCE_PUBLIC_URL_ROOT) / 'health',
                    'callback': lambda _: (500, {}, json.dumps(fake_health())),
                },
                {
                    'method': responses.GET,
                    'url': Path(settings.COURSE_CATALOG_URL_ROOT) / 'health',
                    'callback': lambda _: (403, {}, json.dumps(fake_health())),
                },
                {
                    'method': responses.GET,
                    'url':
                    Path(settings.ENTERPRISE_CATALOG_INTERNAL_ROOT_URL) /
                    'health',
                    'callback': Timeout,
                },
            ]), {
                'E-Commerce', 'Course Discovery', 'Enterprise Catalog',
                'Learning Management System (LMS)'
            }),
    )
    def test_run_checks_errors(self, responses_decorator, downed_services):
        """
        Validate that `run_checks` returns failure if some or all services are down.
        """
        @responses_decorator
        def _test_run_checks_errors():
            all_okay, response = run_checks()
            assert not all_okay
            assert response['status'] == Status.UNAVAILABLE
            assert response[
                'message'] == 'Some or all of the dependant services are down.'
            assert all(service['status'] == Status.OK
                       for service in response['services']
                       if service['service'] not in downed_services)
            assert all(service['status'] == Status.UNAVAILABLE
                       for service in response['services']
                       if service['service'] in downed_services)

        # Run the tests
        _test_run_checks_errors()
Пример #5
0
class TestHeartbeatViews(unittest.TestCase):
    """
    Validate heartbeat views works as expected.
    """
    @mock_api_response(
        responses.GET,
        Path(settings.LMS_INTERNAL_ROOT_URL) / 'heartbeat',
        json=fake_lms_heartbeat(),
        additional_responses=[
            {
                'method': responses.GET,
                'url': Path(settings.ECOMMERCE_PUBLIC_URL_ROOT) / 'health',
                'json': fake_health(),
            },
            {
                'method': responses.GET,
                'url': Path(settings.COURSE_CATALOG_URL_ROOT) / 'health',
                'json': fake_health(),
            },
            {
                'method': responses.GET,
                'url':
                Path(settings.ENTERPRISE_CATALOG_INTERNAL_ROOT_URL) / 'health',
                'json': fake_health(),
            },
        ])
    def test_ok(self):
        """
        Test that a response with 200 status code is returned if all services are up and running.
        """
        request = RequestFactory().get(reverse('enterprise_heartbeat'))
        response = heartbeat(request)
        assert response.status_code == 200

        data = json.loads(response.content.decode('utf-8'))
        assert data['status'] == Status.OK
        assert data['message'] == 'Service is up and running.'
        assert all(service['status'] == Status.OK
                   for service in data['services'])

    @ddt.unpack
    @ddt.data(
        (mock_api_response_with_callback(
            responses.GET,
            Path(settings.LMS_INTERNAL_ROOT_URL) / 'heartbeat',
            callback=lambda _: (200, {}, json.dumps(fake_health())),
            additional_responses=[
                {
                    'method': responses.GET,
                    'url': Path(settings.ECOMMERCE_PUBLIC_URL_ROOT) / 'health',
                    'callback': lambda _: (503, {}, json.dumps(fake_health())),
                },
                {
                    'method': responses.GET,
                    'url': Path(settings.COURSE_CATALOG_URL_ROOT) / 'health',
                    'callback': lambda _: (400, {}, json.dumps(fake_health())),
                },
                {
                    'method': responses.GET,
                    'url':
                    Path(settings.ENTERPRISE_CATALOG_INTERNAL_ROOT_URL) /
                    'health',
                    'callback': Timeout,
                },
            ]), {'E-Commerce', 'Course Discovery', 'Enterprise Catalog'}),
        (mock_api_response_with_callback(
            responses.GET,
            Path(settings.LMS_INTERNAL_ROOT_URL) / 'heartbeat',
            callback=ConnectionError,
            additional_responses=[
                {
                    'method': responses.GET,
                    'url': Path(settings.ECOMMERCE_PUBLIC_URL_ROOT) / 'health',
                    'callback': lambda _: (500, {}, json.dumps(fake_health())),
                },
                {
                    'method': responses.GET,
                    'url': Path(settings.COURSE_CATALOG_URL_ROOT) / 'health',
                    'callback': lambda _: (403, {}, json.dumps(fake_health())),
                },
                {
                    'method': responses.GET,
                    'url':
                    Path(settings.ENTERPRISE_CATALOG_INTERNAL_ROOT_URL) /
                    'health',
                    'callback': Timeout,
                },
            ]), {
                'E-Commerce', 'Course Discovery', 'Enterprise Catalog',
                'Learning Management System (LMS)'
            }),
    )
    def test_run_checks_errors(self, responses_decorator, downed_services):
        """
        Validate that `run_checks` returns failure if some or all services are down.
        """
        @responses_decorator
        def _test_run_checks_errors():
            request = RequestFactory().get(reverse('enterprise_heartbeat'))
            response = heartbeat(request)
            assert response.status_code == 503

            data = json.loads(response.content.decode('utf-8'))
            assert data['status'] == Status.UNAVAILABLE
            assert data[
                'message'] == 'Some or all of the dependant services are down.'
            assert all(service['status'] == Status.OK
                       for service in data['services']
                       if service['service'] not in downed_services)
            assert all(service['status'] == Status.UNAVAILABLE
                       for service in data['services']
                       if service['service'] in downed_services)

        # Run the tests
        _test_run_checks_errors()
Пример #6
0
class TestChecks(unittest.TestCase):
    """
    Validate the behavior of service checks functions.
    """
    @mock_api_response(
        responses.GET,
        Path(settings.LMS_INTERNAL_ROOT_URL) / 'heartbeat',
        json=fake_lms_heartbeat(),
    )
    def test_check_lms(self):
        """
        Validate that `check_lms` function works as expected.
        """
        service, message = check_lms()

        assert service == 'Learning Management System (LMS)'
        assert message == 'Service is up and running.'

    @ddt.unpack
    @ddt.data(
        (503, 'Service is down'),
        (400, 'An error occurred while checking service status.'),
    )
    def test_check_lms_error(self, status_code, expected_error_message):
        """
        Validate that `check_lms` function works as expected.
        """
        @mock_api_response(responses.GET,
                           Path(settings.LMS_INTERNAL_ROOT_URL) / 'heartbeat',
                           json=fake_lms_heartbeat(all_okay=False),
                           status=status_code)
        def _test_check_lms_error():

            with raises(LMSNotAvailable) as error:
                check_lms()
                assert error.message == expected_error_message

        # Run the tests
        _test_check_lms_error()

    @ddt.data(Timeout, ConnectionError)
    def test_check_lms_connection_errors(self, exception):
        """
        Validate that `check_lms` function works as expected.
        """
        @mock_api_response_with_callback(responses.GET,
                                         Path(settings.LMS_INTERNAL_ROOT_URL) /
                                         'heartbeat',
                                         callback=exception,
                                         content_type='application/json')
        def _test_check_lms_connection_errors():

            with raises(LMSNotAvailable) as error:
                check_lms()
                assert error.message == 'Service is not accessible.'

        # Run the tests
        _test_check_lms_connection_errors()

    @mock_api_response(
        responses.GET,
        Path(settings.ECOMMERCE_PUBLIC_URL_ROOT) / 'health',
        json=fake_lms_heartbeat(),
    )
    def test_check_ecommerce(self):
        """
        Validate that `check_ecommerce` function works as expected.
        """
        service, message = check_ecommerce()

        assert service == 'E-Commerce'
        assert message == 'Service is up and running.'

    @ddt.unpack
    @ddt.data(
        (503, 'Service is down'),
        (400, 'An error occurred while checking service status.'),
    )
    def test_check_ecommerce_error(self, status_code, expected_error_message):
        """
        Validate that `check_ecommerce` function works as expected.
        """
        @mock_api_response(responses.GET,
                           Path(settings.ECOMMERCE_PUBLIC_URL_ROOT) / 'health',
                           json=fake_health(all_okay=False),
                           status=status_code)
        def _test_check_ecommerce_error():

            with raises(EcommerceNotAvailable) as error:
                check_ecommerce()
                assert error.message == expected_error_message

        # Run the tests
        _test_check_ecommerce_error()

    @ddt.data(Timeout, ConnectionError)
    def test_check_ecommerce_connection_errors(self, exception):
        """
        Validate that `check_ecommerce` function works as expected.
        """
        @mock_api_response_with_callback(
            responses.GET,
            Path(settings.ECOMMERCE_PUBLIC_URL_ROOT) / 'health',
            callback=exception,
            content_type='application/json')
        def _test_check_ecommerce_connection_errors():

            with raises(EcommerceNotAvailable) as error:
                check_ecommerce()
                assert error.message == 'Service is not accessible.'

        # Run the tests
        _test_check_ecommerce_connection_errors()

    @mock_api_response(
        responses.GET,
        Path(settings.COURSE_CATALOG_URL_ROOT) / 'health',
        json=fake_health(),
    )
    def test_check_discovery(self):
        """
        Validate that `check_discovery` function works as expected.
        """
        service, message = check_discovery()

        assert service == 'Course Discovery'
        assert message == 'Service is up and running.'

    @ddt.unpack
    @ddt.data(
        (503, 'Service is down'),
        (400, 'An error occurred while checking service status.'),
    )
    def test_check_discovery_error(self, status_code, expected_error_message):
        """
        Validate that `check_discovery` function works as expected.
        """
        @mock_api_response(responses.GET,
                           Path(settings.COURSE_CATALOG_URL_ROOT) / 'health',
                           json=fake_health(all_okay=False),
                           status=status_code)
        def _test_check_discovery_error():

            with raises(DiscoveryNotAvailable) as error:
                check_discovery()
                assert error.message == expected_error_message

        # Run the tests
        _test_check_discovery_error()

    @ddt.data(Timeout, ConnectionError)
    def test_check_discovery_connection_errors(self, exception):
        """
        Validate that `check_discovery` function works as expected.
        """
        @mock_api_response_with_callback(
            responses.GET,
            Path(settings.COURSE_CATALOG_URL_ROOT) / 'health',
            callback=exception,
            content_type='application/json')
        def _test_check_discovery_connection_errors():

            with raises(DiscoveryNotAvailable) as error:
                check_discovery()
                assert error.message == 'Service is not accessible.'

        # Run the tests
        _test_check_discovery_connection_errors()

    @mock_api_response(
        responses.GET,
        Path(settings.ENTERPRISE_CATALOG_INTERNAL_ROOT_URL) / 'health',
        json=fake_health(),
    )
    def test_check_enterprise_catalog(self):
        """
        Validate that `check_enterprise_catalog` function works as expected.
        """
        service, message = check_enterprise_catalog()

        assert service == 'Enterprise Catalog'
        assert message == 'Service is up and running.'

    @ddt.unpack
    @ddt.data(
        (503, 'Service is down'),
        (400, 'An error occurred while checking service status.'),
    )
    def test_check_enterprise_catalog_error(self, status_code,
                                            expected_error_message):
        """
        Validate that `check_enterprise_catalog` function works as expected.
        """
        @mock_api_response(
            responses.GET,
            Path(settings.ENTERPRISE_CATALOG_INTERNAL_ROOT_URL) / 'health',
            json=fake_health(all_okay=False),
            status=status_code)
        def _test_check_enterprise_catalog_error():

            with raises(EnterpriseCatalogNotAvailable) as error:
                check_enterprise_catalog()
                assert error.message == expected_error_message

        # Run the tests
        _test_check_enterprise_catalog_error()

    @ddt.data(Timeout, ConnectionError)
    def test_check_enterprise_catalog_connection_errors(self, exception):
        """
        Validate that `check_enterprise_catalog` function works as expected.
        """
        @mock_api_response_with_callback(
            responses.GET,
            Path(settings.ENTERPRISE_CATALOG_INTERNAL_ROOT_URL) / 'health',
            callback=exception,
            content_type='application/json')
        def _test_check_enterprise_catalog_errors():

            with raises(EnterpriseCatalogNotAvailable) as error:
                check_enterprise_catalog()
                assert error.message == 'Service is not accessible.'

        # Run the tests
        _test_check_enterprise_catalog_errors()