Beispiel #1
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'
        ]
Beispiel #2
0
    def test_validation_with_jexl_exception(self):
        serializer = RecipeSerializer(
            data={
                'name': 'bar',
                'enabled': True,
                'extra_filter_expression': 'aces',
                'action': 'show-heartbeat',
                'arguments': {
                    'surveyId':
                    'lorem-ipsum-dolor',
                    'surveys': [{
                        'title': 'adipscing',
                        'weight': 1
                    }, {
                        'title': 'consequetar',
                        'weight': 1
                    }]
                }
            })

        jexl_mock = mock.Mock()
        jexl_mock().validate.side_effect = Exception("didn't like this")
        with mock.patch('normandy.recipes.api.v1.serializers.JEXL', jexl_mock):
            assert not serializer.is_valid()
            assert serializer.errors['extra_filter_expression'] == [
                'The JEXL parser failed to validate aces'
            ]
Beispiel #3
0
    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.'
        ]
Beispiel #4
0
    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': 'show-heartbeat',
                '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 == {}
Beispiel #5
0
    def test_it_works(self, rf):
        recipe = RecipeFactory(arguments={"foo": "bar"})
        approval = ApprovalRequestFactory(revision=recipe.latest_revision)
        action = recipe.action
        serializer = RecipeSerializer(recipe, context={"request": rf.get("/")})

        assert serializer.data == {
            "name": recipe.name,
            "id": recipe.id,
            "last_updated": Whatever(),
            "enabled": recipe.enabled,
            "extra_filter_expression": recipe.extra_filter_expression,
            "filter_expression": recipe.filter_expression,
            "revision_id": recipe.revision_id,
            "action": action.name,
            "arguments": {
                "foo": "bar"
            },
            "is_approved": False,
            "latest_revision_id": recipe.latest_revision.id,
            "approved_revision_id": None,
            "approval_request": {
                "id": approval.id,
                "created": Whatever(),
                "creator": Whatever(),
                "approved": None,
                "approver": None,
                "comment": None,
            },
            "identicon_seed": Whatever.startswith("v1:"),
            "capabilities": sorted(recipe.capabilities),
        }
Beispiel #6
0
    def test_validation_with_wrong_arguments(self):
        ActionFactory(name='show-heartbeat', arguments_schema=ARGUMENTS_SCHEMA)

        serializer = RecipeSerializer(
            data={
                'action': 'show-heartbeat',
                '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\''
                }
            }
        }
Beispiel #7
0
    def enable(self, request, pk=None):
        recipe = self.get_object()
        recipe.enabled = True

        try:
            recipe.save()
        except Recipe.NotApproved as e:
            return Response({'enabled': str(e)},
                            status=status.HTTP_409_CONFLICT)

        return Response(RecipeSerializer(recipe).data)
Beispiel #8
0
    def test_it_works(self, rf):
        channel = ChannelFactory()
        country = CountryFactory()
        locale = LocaleFactory()
        recipe = RecipeFactory(arguments={'foo': 'bar'},
                               channels=[channel],
                               countries=[country],
                               locales=[locale])
        approval = ApprovalRequestFactory(revision=recipe.latest_revision)
        action = recipe.action
        serializer = RecipeSerializer(recipe, context={'request': rf.get('/')})

        assert serializer.data == {
            'name': recipe.name,
            'id': recipe.id,
            'last_updated': Whatever(),
            'enabled': recipe.enabled,
            'extra_filter_expression': recipe.extra_filter_expression,
            'filter_expression': recipe.filter_expression,
            'revision_id': recipe.revision_id,
            'action': action.name,
            'arguments': {
                'foo': 'bar',
            },
            'channels': [channel.slug],
            'countries': [country.code],
            'locales': [locale.code],
            'is_approved': False,
            'latest_revision_id': recipe.latest_revision.id,
            'approved_revision_id': recipe.approved_revision_id,
            'approval_request': {
                'id': approval.id,
                'created': Whatever(),
                'creator': Whatever(),
                'approved': None,
                'approver': None,
                'comment': None,
            },
            'identicon_seed': Whatever.startswith('v1:'),
        }
Beispiel #9
0
 def disable(self, request, pk=None):
     recipe = self.get_object()
     recipe.enabled = False
     recipe.save()
     return Response(RecipeSerializer(recipe).data)