Esempio n. 1
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: "\\'
        ]
Esempio n. 2
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'"},
            },
        }
Esempio n. 3
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"
        ]
Esempio n. 4
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},
                    ],
                },
                "bug_number": 1436113,
            }
        )

        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},
                ],
            },
            "bug_number": 1436113,
        }
        assert serializer.errors == {}
Esempio n. 5
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
            )
        ]
Esempio n. 6
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)
        ]
Esempio n. 7
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")
        ]
Esempio n. 8
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"
            )
        ]
Esempio n. 9
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'"
                },
            },
        }
Esempio n. 10
0
    def test_valid_with_null_experimenter_slug(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
                        },
                    ],
                },
                "experimenter_slug": None,
            })

        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
                    },
                ],
            },
            "experimenter_slug": None,
        }
        assert serializer.errors == {}
Esempio n. 11
0
    def disable(self, request, pk=None):
        recipe = self.get_object()

        try:
            recipe.approved_revision.disable(user=request.user)
        except EnabledState.NotActionable as e:
            return Response({"error": str(e)}, status=status.HTTP_409_CONFLICT)

        return Response(RecipeSerializer(recipe).data)
Esempio n. 12
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: "\\']
Esempio n. 13
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"
        ]
Esempio n. 14
0
    def enable(self, request, pk=None):
        recipe = self.get_object()

        if recipe.approved_revision:
            try:
                recipe.approved_revision.enable(user=request.user)
            except EnabledState.NotActionable as e:
                return Response({"error": str(e)},
                                status=status.HTTP_409_CONFLICT)
        else:
            return Response(
                {"error": "Cannot enable a recipe that is not approved."},
                status=status.HTTP_409_CONFLICT,
            )

        recipe.latest_revision.refresh_from_db()
        return Response(RecipeSerializer(recipe).data)
Esempio n. 15
0
    def test_it_works(self, rf):
        recipe = RecipeFactory(arguments={"foo": "bar"}, bug_number=1436113)
        ApprovalRequestFactory(revision=recipe.latest_revision)
        request = rf.get("/")
        serializer = RecipeSerializer(recipe, context={"request": rf.get("/")})

        assert serializer.data == {
            "id":
            recipe.id,
            "latest_revision":
            RecipeRevisionSerializer(recipe.latest_revision,
                                     context={
                                         "request": request
                                     }).data,
            "approved_revision":
            None,
            "signature":
            None,
        }
Esempio n. 16
0
    def test_it_works(self, rf):
        recipe = RecipeFactory(arguments={"foo": "bar"},
                               experimenter_slug="some-experimenter-slug")
        ApprovalRequestFactory(revision=recipe.latest_revision)
        request = rf.get("/")
        serializer = RecipeSerializer(recipe, context={"request": rf.get("/")})

        assert serializer.data == {
            "id":
            recipe.id,
            "latest_revision":
            RecipeRevisionSerializer(recipe.latest_revision,
                                     context={
                                         "request": request
                                     }).data,
            "approved_revision":
            None,
            "signature":
            None,
            "uses_only_baseline_capabilities":
            False,
        }