add simple survey support
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
import pytest
|
||||
from django.urls import reverse
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from survey.models import Survey, SurveyQuestion, SurveyResponse, SurveyAnswer
|
||||
from survey.views import check_survey_interception
|
||||
from atlas.models import CaseCollection
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_survey_creation():
|
||||
survey = Survey.objects.create(name="Test Course Survey", description="Feedback on course")
|
||||
assert survey.name == "Test Course Survey"
|
||||
assert survey.active is True
|
||||
assert survey.anonymous is False
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_survey_questions():
|
||||
survey = Survey.objects.create(name="Test Survey")
|
||||
q1 = SurveyQuestion.objects.create(
|
||||
survey=survey,
|
||||
text="Rate this case",
|
||||
question_type=SurveyQuestion.QuestionType.RATING,
|
||||
position=10
|
||||
)
|
||||
q2 = SurveyQuestion.objects.create(
|
||||
survey=survey,
|
||||
text="Any comments?",
|
||||
question_type=SurveyQuestion.QuestionType.TEXT,
|
||||
position=20
|
||||
)
|
||||
assert survey.questions.count() == 2
|
||||
assert q1.get_choices_list() == []
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_survey_response_and_answers(admin_user):
|
||||
survey = Survey.objects.create(name="Test Survey")
|
||||
q1 = SurveyQuestion.objects.create(
|
||||
survey=survey, text="Rate this case", question_type=SurveyQuestion.QuestionType.RATING
|
||||
)
|
||||
|
||||
# Simulate a survey response save
|
||||
response = SurveyResponse.objects.create(
|
||||
survey=survey,
|
||||
user=admin_user,
|
||||
pre_or_post="PRE"
|
||||
)
|
||||
answer = SurveyAnswer.objects.create(
|
||||
response=response,
|
||||
question=q1,
|
||||
rating_answer=4
|
||||
)
|
||||
|
||||
assert response.answers.count() == 1
|
||||
assert answer.rating_answer == 4
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_interception_helper(rf, admin_user):
|
||||
survey = Survey.objects.create(name="Pre-take survey")
|
||||
collection = CaseCollection.objects.create(name="Test Collection", pre_survey=survey)
|
||||
|
||||
request = rf.get('/atlas/collection/1/take/')
|
||||
request.user = admin_user
|
||||
|
||||
# 1. First check: should redirect to pre-survey since they haven't filled it out
|
||||
redirect_response = check_survey_interception(request, collection)
|
||||
assert redirect_response is not None
|
||||
assert "take/" in redirect_response.url
|
||||
|
||||
# 2. Add response and check again: should not redirect
|
||||
ct = ContentType.objects.get_for_model(collection)
|
||||
resp = SurveyResponse.objects.create(
|
||||
survey=survey,
|
||||
user=admin_user,
|
||||
content_type=ct,
|
||||
object_id=collection.pk,
|
||||
pre_or_post="PRE"
|
||||
)
|
||||
|
||||
no_redirect_response = check_survey_interception(request, collection)
|
||||
assert no_redirect_response is None
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_survey_authorship(admin_user):
|
||||
survey = Survey.objects.create(name="Author Test Survey")
|
||||
survey.add_author(admin_user)
|
||||
assert survey.is_author(admin_user) is True
|
||||
assert admin_user in survey.get_author_objects()
|
||||
assert survey.get_authors() == admin_user.username
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_can_manage_survey_permission(admin_user, django_user_model):
|
||||
from survey.views import can_manage_survey
|
||||
from django.contrib.auth.models import Group
|
||||
|
||||
survey = Survey.objects.create(name="Permissions Test Survey", authors_only=True)
|
||||
|
||||
# 1. Non-staff user should not have permission
|
||||
regular_user = django_user_model.objects.create_user(username="trainee", password="pw")
|
||||
assert can_manage_survey(regular_user, survey) is False
|
||||
|
||||
# 2. Staff user in cid_user_manager but not author should not have permission for private survey
|
||||
manager_group, _ = Group.objects.get_or_create(name="cid_user_manager")
|
||||
manager_user = django_user_model.objects.create_user(username="manager", password="pw")
|
||||
manager_user.groups.add(manager_group)
|
||||
assert can_manage_survey(manager_user, survey) is False
|
||||
|
||||
# 3. Superuser should always have permission
|
||||
superuser = django_user_model.objects.create_superuser(username="admin2", password="pw")
|
||||
assert can_manage_survey(superuser, survey) is True
|
||||
|
||||
# 4. Author should have permission even if private
|
||||
survey.add_author(manager_user)
|
||||
assert can_manage_survey(manager_user, survey) is True
|
||||
|
||||
# 5. Non-author manager should have permission if survey is public
|
||||
survey.authors_only = False
|
||||
survey.save()
|
||||
other_manager = django_user_model.objects.create_user(username="other_manager", password="pw")
|
||||
other_manager.groups.add(manager_group)
|
||||
assert can_manage_survey(other_manager, survey) is True
|
||||
|
||||
Reference in New Issue
Block a user