def test_endpoints_dynamic_schema(client) -> None:  # noqa: TYP001
    """
    Asserts that the validate_response function validates correct schemas successfully.
    """
    for item in good_test_data:
        response = client.get(item['url'])
        assert response.status_code == 200
        assert response.json() == item['expected_response']

        # Test Swagger documentation
        validate_response(response=response, method='GET', route=item['url'])  # type: ignore
Exemplo n.º 2
0
    def test_get_200(self):
        expected_response = [
            {'name': 'Saab', 'color': 'Yellow', 'height': 'Medium height', 'width': 'Very wide', 'length': '2 meters'},
            {'name': 'Volvo', 'color': 'Red', 'height': 'Medium height', 'width': 'Not wide', 'length': '2 meters'},
            {'name': 'Tesla', 'color': 'black', 'height': 'Medium height', 'width': 'Wide', 'length': '2 meters'},
        ]
        response = self.get(self.path + '/correct/')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json(), expected_response)

        # Test Swagger documentation
        validate_response(response=response, method='GET', route=self.path + '/correct/')
def test_missing_status_code_match(client, monkeypatch) -> None:  # noqa: TYP001
    """
    When we fail to index the schema by status code, we need to raise an exception.
    """

    def mocked_unpack_response(*args, **kwargs):
        return {}, 'bad status code'

    monkeypatch.setattr('django_swagger_tester.response_validation.validation.unpack_response', mocked_unpack_response)
    for item in bad_test_data:
        response = client.get(item['url'])
        with pytest.raises(ImproperlyConfigured, match='`status_code` should be an integer'):
            validate_response(response=response, method='GET', route=item['url'])  # type: ignore
def test_bad_endpoints_dynamic_schema(client) -> None:  # noqa: TYP001
    """
    Asserts that the validate_response function validates incorrect schemas successfully.
    """
    for item in bad_test_data:
        response = client.get(item['url'])
        assert response.status_code == 200
        assert response.json() == item['expected_response']

        # Test Swagger documentation
        with pytest.raises(
            SwaggerDocumentationError, match='The following properties seem to be missing from your response body:'
        ):
            validate_response(response, 'GET', item['url'])  # type: ignore
def test_missing_method_match(client, monkeypatch) -> None:  # noqa: TYP001
    """
    When we fail to index the schema by method, we need to raise an exception.
    """

    def mocked_validate_method(*args, **kwargs):
        pass

    monkeypatch.setattr('django_swagger_tester.drf_yasg.loader.validate_inputs', mocked_validate_method)
    for item in bad_test_data:
        response = client.get(item['url'])
        assert response.status_code == 200
        assert response.json() == item['expected_response']

        # Test Swagger documentation
        with pytest.raises(SwaggerDocumentationError, match='Failed indexing schema.'):
            validate_response(response=response, method='gets', route=item['url'])  # type: ignore
def test_endpoints_dynamic_schema(client, caplog) -> None:  # noqa: TYP001
    """
    Asserts that the validate_response function validates correct schemas successfully.
    """
    response = client.get('/api/v1/trucks/correct/')
    validate_response(response=response,
                      method='GET',
                      route='/api/v1/trucks/correct/',
                      ignore_case=['name', 'width', 'height'])
    assert [
        i in [record.message for record in caplog.records] for i in [
            'Skipping case check for key `name`',
            'Skipping case check for key `name`',
            'Skipping case check for key `height`',
            'Skipping case check for key `height`',
            'Skipping case check for key `width`',
            'Skipping case check for key `width`',
        ]
    ]