From ba89f88b7cec45b17e0c6036884998cdad873f0f Mon Sep 17 00:00:00 2001 From: Jonas Lehmann Date: Mon, 4 Nov 2019 18:51:10 +0100 Subject: [PATCH] fix(data souce): add user and group to dynamic option Add the user and the group when a dynamic option was created. --- caluma/data_source/tests/test_data_source.py | 54 ++++++++++++++++++++ caluma/form/validators.py | 7 ++- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/caluma/data_source/tests/test_data_source.py b/caluma/data_source/tests/test_data_source.py index 77a8ae721..08a33743c 100644 --- a/caluma/data_source/tests/test_data_source.py +++ b/caluma/data_source/tests/test_data_source.py @@ -2,6 +2,9 @@ from django.core.cache import cache from django.utils import translation +from caluma.form.models import DynamicOption, Question +from caluma.user.models import BaseUser + def test_fetch_data_sources(snapshot, schema_executor, settings): settings.DATA_SOURCE_CLASSES = [ @@ -198,3 +201,54 @@ def test_fetch_data_from_non_existing_data_source(schema_executor, settings, con result = schema_executor(query) assert result.errors + + +@pytest.mark.parametrize( + "question__type,question__data_source", + [(Question.TYPE_DYNAMIC_CHOICE, "MyDataSource")], +) +def test_data_sources_stores_user( + db, schema_executor, info, settings, form, question, document +): + class FakeUser(BaseUser): + def __init__(self): + self.groups = ["foobar"] + self.username = "asdf" + + @property + def group(self): + return "foobar" + + def __str__(self): + return "asdf" + + settings.DATA_SOURCE_CLASSES = [ + "caluma.data_source.tests.data_sources.MyDataSource" + ] + query = """ + mutation createAnswer($input:SaveDocumentStringAnswerInput!){ + saveDocumentStringAnswer(input:$input){ + answer{ + id + } + } + } + """ + info.context.user = FakeUser() + variables = { + "input": { + "question": question.slug, + "document": document.id, + "value": "something", + } + } + assert not DynamicOption.objects.exists() + result = schema_executor(query, variables=variables, info=info) + assert not result.errors + assert DynamicOption.objects.filter( + document=document, + question=question, + slug="something", + created_by_user="asdf", + created_by_group="foobar", + ).exists() diff --git a/caluma/form/validators.py b/caluma/form/validators.py index c8797aa50..e8fbbd0d4 100644 --- a/caluma/form/validators.py +++ b/caluma/form/validators.py @@ -117,7 +117,12 @@ def _validate_dynamic_option(self, question, document, option, info): ) DynamicOption.objects.get_or_create( - document=document, question=question, slug=option, label=valid_label + document=document, + question=question, + slug=option, + label=valid_label, + created_by_user=info.context.user.username, + created_by_group=info.context.user.group, ) def _validate_question_dynamic_multiple_choice(