Exemplo n.º 1
0
def test_400_for_invalid_data_on_viewsets(req_factory: APIRequestFactory,
                                          viewset_cls: ViewSet):
    viewset_cls.list = validate_request(
        query_params={
            'username': serializers.CharField(allow_blank=False, required=True)
        })(viewset_cls.list)

    request = req_factory.get(URL, data={'username': ''})
    response = viewset_cls.as_view({'get': 'list'})(request)
    assert response.status_code == 400
Exemplo n.º 2
0
def test_valid_data_on_viewsets(req_factory: APIRequestFactory,
                                viewset_cls: ViewSet):
    viewset_cls.create = validate_request(
        data={
            'username': serializers.CharField(allow_blank=False, required=True)
        })(viewset_cls.create)

    request = req_factory.post(URL, data={'username': '******'})
    response = viewset_cls.as_view({'post': 'create'})(request)
    assert response.status_code == 200
    assert response.data['validated_data']['username'] == 'maksim'
    assert 'validated_query_params' not in response.data
Exemplo n.º 3
0
def test_has_data_if_passed_and_valid(req_factory: APIRequestFactory,
                                      apiview_cls: Type[APIView]):
    apiview_cls.post = validate_request(
        data={
            'username': serializers.CharField(allow_blank=False, required=True)
        })(apiview_cls.post)

    response = apiview_cls.as_view()(req_factory.post(
        URL, data={'username': '******'}))
    assert response.status_code == 200
    assert response.data['validated_data']['username'] == 'maksim'
    assert 'validated_query_params' not in response.data
Exemplo n.º 4
0
def test_raises_400_if_query_params_are_invalid(req_factory: APIRequestFactory,
                                                apiview_cls: Type[APIView]):
    apiview_cls.get = validate_request(
        query_params={
            'username': serializers.CharField(allow_blank=False, required=True)
        })(apiview_cls.get)

    response = apiview_cls.as_view()(req_factory.get(URL))
    assert response.status_code == 400

    response = apiview_cls.as_view()(req_factory.get(URL, data={}))
    assert response.status_code == 400

    response = apiview_cls.as_view()(req_factory.get(URL,
                                                     data={'username': ''}))
    assert response.status_code == 400
Exemplo n.º 5
0
def test_raises_400_if_data_is_invalid_as_serializer(
        req_factory: APIRequestFactory, apiview_cls: Type[APIView]):
    class MySerializer(serializers.Serializer):
        username = serializers.CharField(allow_blank=False, required=True)

    apiview_cls.post = validate_request(data=MySerializer)(apiview_cls.post)

    response = apiview_cls.as_view()(req_factory.post(URL))
    assert response.status_code == 400

    response = apiview_cls.as_view()(req_factory.post(URL, data={}))
    assert response.status_code == 400

    response = apiview_cls.as_view()(req_factory.post(URL,
                                                      data={'username': ''}))
    assert response.status_code == 400
Exemplo n.º 6
0
def test_no_error_if_no_data_is_passed_and_no_serializers_defined(
        req_factory: APIRequestFactory, apiview_cls: Type[APIView]):
    apiview_cls.get = validate_request()(apiview_cls.get)
    request = req_factory.get(URL)
    assert apiview_cls.as_view()(request).status_code == 200