Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions addons/survey/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ def _check_validity(self, survey_token, answer_token, ensure_token=True, check_p
if answer_token and not answer_sudo:
return 'token_wrong'

if not answer_sudo and ensure_token:
if not answer_sudo and ensure_token is True:
return 'token_required'
if not answer_sudo and survey_sudo.access_mode == 'token':
if not answer_sudo and ensure_token != 'survey_only' and survey_sudo.access_mode == 'token':
return 'token_required'

if survey_sudo.users_login_required and request.env.user._is_public():
Expand Down Expand Up @@ -118,7 +118,8 @@ def _get_access_data(self, survey_token, answer_token, ensure_token=True, check_
has_survey_access = True
can_answer = bool(answer_sudo)
if not can_answer:
can_answer = survey_sudo.access_mode == 'public'
can_answer = survey_sudo.access_mode == 'public' or (
has_survey_access and ensure_token == 'survey_only')

return {
'survey_sudo': survey_sudo,
Expand Down Expand Up @@ -635,7 +636,7 @@ def _extract_comment_from_answers(self, question, answers):
def survey_print(self, survey_token, review=False, answer_token=None, **post):
'''Display an survey in printable view; if <answer_token> is set, it will
grab the answers of the user_input_id that has <answer_token>.'''
access_data = self._get_access_data(survey_token, answer_token, ensure_token=False, check_partner=False)
access_data = self._get_access_data(survey_token, answer_token, ensure_token='survey_only', check_partner=False)
if access_data['validity_code'] is not True and (
access_data['has_survey_access'] or
access_data['validity_code'] not in ['token_required', 'survey_closed', 'survey_void', 'answer_deadline']):
Expand Down
50 changes: 50 additions & 0 deletions addons/survey/tests/test_survey_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,53 @@ def test_submit_route_scoring_after_page(self):
self.assertEqual(response.json()['result'][0], expected_correct_answers)

user_input.invalidate_recordset() # TDE note: necessary as lots of sudo in controllers messing with cache

def test_print_survey_access_mode_token(self):
"""Check that a survey with access_mode=token with questions defined can always be printed."""
# Case: No questions, no answers -> general print informs the user "your survey is empty"
survey = self.env['survey.survey'].with_user(self.survey_manager).create({
'title': 'Test Survey without answers',
'access_mode': 'token',
'users_login_required': False,
'users_can_go_back': False,
})
self.authenticate(self.survey_manager.login, self.survey_manager.login)
response = self.url_open(f'/survey/print/{survey.access_token}')
self.assertEqual(response.status_code, 200,
"Print request to shall succeed for a survey without questions nor answers")
self.assertIn("survey is empty", str(response.content),
"Survey print without questions nor answers should inform user that the survey is empty")

# Case: a question, no answers -> general print shows the question
question = self.env['survey.question'].with_user(self.survey_manager).create({
'title': 'Test Question',
'survey_id': survey.id,
'sequence': 1,
'is_page': False,
'question_type': 'char_box',
})
response = self.url_open(f'/survey/print/{survey.access_token}')
self.assertEqual(response.status_code, 200,
"Print request to shall succeed for a survey with questions but no answers")
self.assertIn(question.title, str(response.content),
"Should be possible to print a survey with a question and without answers")

# Case: a question, an answers -> general print shows the question
user_input = self._add_answer(survey, self.survey_manager.partner_id, state='done')
self._add_answer_line(question, user_input, "Test Answer")
response = self.url_open(f'/survey/print/{survey.access_token}')
self.assertEqual(response.status_code, 200,
"Print request without answer token, should be possible for a survey with questions and answers")
self.assertIn(question.title, str(response.content),
"Survey question should be visible in general print, even when answers exist and no answer_token is provided")
self.assertNotIn("Test Answer", str(response.content),
"Survey answer should not be in general print, when no answer_token is provided")

# Case: a question, an answers -> print with answer_token shows both
response = self.url_open(f'/survey/print/{survey.access_token}?answer_token={user_input.access_token}')
self.assertEqual(response.status_code, 200,
"Should be possible to print a sruvey with questions and answers")
self.assertIn(question.title, str(response.content),
"Question should appear when printing survey with using an answer_token")
self.assertIn("Test Answer", str(response.content),
"Answer should appear when printing survey with using an answer_token")