Skip to content

Commit

Permalink
fix(data souce): add user and group to dynamic option
Browse files Browse the repository at this point in the history
Add the user and the group when a dynamic option was created.
  • Loading branch information
JohnL17 committed Nov 4, 2019
1 parent a84ea08 commit ba89f88
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
54 changes: 54 additions & 0 deletions caluma/data_source/tests/test_data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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()
7 changes: 6 additions & 1 deletion caluma/form/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit ba89f88

Please sign in to comment.