コード例 #1
0
ファイル: test_serializers.py プロジェクト: rhelmer/normandy
    def test_validation_with_wrong_action(self):
        serializer = RecipeSerializer(data={
            'action': 'action-that-doesnt-exist',
            'arguments': {}
        })

        with pytest.raises(serializers.ValidationError):
            serializer.is_valid(raise_exception=True)

        assert serializer.errors['arguments'] == [
            'Could not find arguments schema.'
        ]
コード例 #2
0
    def test_validation_with_wrong_action(self):
        serializer = RecipeSerializer(data={
            "action_id": "9999",
            "arguments": {}
        })

        with pytest.raises(serializers.ValidationError):
            serializer.is_valid(raise_exception=True)

        assert serializer.errors["action_id"] == [
            serializers.PrimaryKeyRelatedField.
            default_error_messages["does_not_exist"].format(pk_value=9999)
        ]
コード例 #3
0
    def test_validation_with_invalid_action(self):
        serializer = RecipeSerializer(data={
            "action_id": "action-that-doesnt-exist",
            "arguments": {}
        })

        with pytest.raises(serializers.ValidationError):
            serializer.is_valid(raise_exception=True)

        assert serializer.errors["action_id"] == [
            serializers.PrimaryKeyRelatedField.
            default_error_messages["incorrect_type"].format(data_type="str")
        ]
コード例 #4
0
    def test_validation_with_wrong_arguments(self):
        action = ActionFactory(name="show-heartbeat",
                               arguments_schema=ARGUMENTS_SCHEMA)

        serializer = RecipeSerializer(
            data={
                "action_id": action.id,
                "name": "Any name",
                "extra_filter_expression": "true",
                "arguments": {
                    "surveyId":
                    "",
                    "surveys": [
                        {
                            "title": "",
                            "weight": 1
                        },
                        {
                            "title": "bar",
                            "weight": 1
                        },
                        {
                            "title": "foo",
                            "weight": 0
                        },
                        {
                            "title": "baz",
                            "weight": "lorem ipsum"
                        },
                    ],
                },
            })

        with pytest.raises(serializers.ValidationError):
            serializer.is_valid(raise_exception=True)

        assert serializer.errors["arguments"] == {
            "surveyId": "This field may not be blank.",
            "surveys": {
                0: {
                    "title": "This field may not be blank."
                },
                2: {
                    "weight": "0 is less than the minimum of 1"
                },
                3: {
                    "weight": "'lorem ipsum' is not of type 'integer'"
                },
            },
        }
コード例 #5
0
    def test_validation_with_jexl_exception(self):
        serializer = RecipeSerializer(
            data={
                "name": "bar",
                "enabled": True,
                "extra_filter_expression": '"\\',
                "action": "show-heartbeat",
                "arguments": {
                    "surveyId":
                    "lorem-ipsum-dolor",
                    "surveys": [
                        {
                            "title": "adipscing",
                            "weight": 1
                        },
                        {
                            "title": "consequetar",
                            "weight": 1
                        },
                    ],
                },
            })

        assert not serializer.is_valid()
        assert serializer.errors["extra_filter_expression"] == [
            'Could not parse expression: "\\'
        ]
コード例 #6
0
    def test_validation_with_invalid_filter_expression(self):
        ActionFactory(name="show-heartbeat", arguments_schema=ARGUMENTS_SCHEMA)

        serializer = RecipeSerializer(
            data={
                "name": "bar",
                "enabled": True,
                "extra_filter_expression": "inv(-alsid",
                "action": "show-heartbeat",
                "arguments": {
                    "surveyId":
                    "lorem-ipsum-dolor",
                    "surveys": [
                        {
                            "title": "adipscing",
                            "weight": 1
                        },
                        {
                            "title": "consequetar",
                            "weight": 1
                        },
                    ],
                },
            })

        assert not serializer.is_valid()
        assert serializer.errors["extra_filter_expression"] == [
            "Could not parse expression: inv(-alsid"
        ]
コード例 #7
0
ファイル: test_serializers.py プロジェクト: rhelmer/normandy
    def test_validation_with_invalid_filter_expression(self):
        ActionFactory(name='show-heartbeat', arguments_schema=ARGUMENTS_SCHEMA)

        serializer = RecipeSerializer(
            data={
                'name': 'bar',
                'enabled': True,
                'extra_filter_expression': 'inv(-alsid',
                'action': 'show-heartbeat',
                'arguments': {
                    'surveyId':
                    'lorem-ipsum-dolor',
                    'surveys': [{
                        'title': 'adipscing',
                        'weight': 1
                    }, {
                        'title': 'consequetar',
                        'weight': 1
                    }]
                }
            })

        assert not serializer.is_valid()
        assert serializer.errors['extra_filter_expression'] == [
            'Could not parse expression: inv(-alsid'
        ]
コード例 #8
0
ファイル: test_serializers.py プロジェクト: rhelmer/normandy
    def test_validation_with_wrong_arguments(self):
        action = ActionFactory(name='show-heartbeat',
                               arguments_schema=ARGUMENTS_SCHEMA)

        serializer = RecipeSerializer(
            data={
                'action_id': action.id,
                'arguments': {
                    'surveyId':
                    '',
                    'surveys': [{
                        'title': '',
                        'weight': 1
                    }, {
                        'title': 'bar',
                        'weight': 1
                    }, {
                        'title': 'foo',
                        'weight': 0
                    }, {
                        'title': 'baz',
                        'weight': 'lorem ipsum'
                    }]
                }
            })

        with pytest.raises(serializers.ValidationError):
            serializer.is_valid(raise_exception=True)

        assert serializer.errors['arguments'] == {
            'surveyId': 'This field may not be blank.',
            'surveys': {
                0: {
                    'title': 'This field may not be blank.'
                },
                2: {
                    'weight': '0 is less than the minimum of 1'
                },
                3: {
                    'weight': '\'lorem ipsum\' is not of type \'integer\''
                }
            }
        }
コード例 #9
0
ファイル: test_serializers.py プロジェクト: rhelmer/normandy
    def test_validation_with_valid_data(self):
        mockAction = ActionFactory(name='show-heartbeat',
                                   arguments_schema=ARGUMENTS_SCHEMA)

        channel = ChannelFactory(slug='release')
        country = CountryFactory(code='CA')
        locale = LocaleFactory(code='en-US')

        serializer = RecipeSerializer(
            data={
                'name': 'bar',
                'enabled': True,
                'extra_filter_expression': '[]',
                'action_id': mockAction.id,
                'channels': ['release'],
                'countries': ['CA'],
                'locales': ['en-US'],
                'arguments': {
                    'surveyId':
                    'lorem-ipsum-dolor',
                    'surveys': [{
                        'title': 'adipscing',
                        'weight': 1
                    }, {
                        'title': 'consequetar',
                        'weight': 1
                    }]
                }
            })

        assert serializer.is_valid()
        assert serializer.validated_data == {
            'name': 'bar',
            'extra_filter_expression': '[]',
            'action': mockAction,
            'arguments': {
                'surveyId':
                'lorem-ipsum-dolor',
                'surveys': [{
                    'title': 'adipscing',
                    'weight': 1
                }, {
                    'title': 'consequetar',
                    'weight': 1
                }]
            },
            'channels': [channel],
            'countries': [country],
            'locales': [locale],
        }
        assert serializer.errors == {}
コード例 #10
0
    def test_validation_with_valid_data(self):
        mockAction = ActionFactory(name="show-heartbeat",
                                   arguments_schema=ARGUMENTS_SCHEMA)

        serializer = RecipeSerializer(
            data={
                "name": "bar",
                "enabled": True,
                "extra_filter_expression": "[]",
                "action_id": mockAction.id,
                "arguments": {
                    "surveyId":
                    "lorem-ipsum-dolor",
                    "surveys": [
                        {
                            "title": "adipscing",
                            "weight": 1
                        },
                        {
                            "title": "consequetar",
                            "weight": 1
                        },
                    ],
                },
            })

        assert serializer.is_valid()
        assert serializer.validated_data == {
            "name": "bar",
            "extra_filter_expression": "[]",
            "action": mockAction,
            "arguments": {
                "surveyId":
                "lorem-ipsum-dolor",
                "surveys": [
                    {
                        "title": "adipscing",
                        "weight": 1
                    },
                    {
                        "title": "consequetar",
                        "weight": 1
                    },
                ],
            },
        }
        assert serializer.errors == {}