test_survey_flow.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo.addons.survey.tests import common
  4. from odoo.tests import tagged
  5. from odoo.tests.common import HttpCase
  6. @tagged('-at_install', 'post_install', 'functional')
  7. class TestSurveyFlow(common.TestSurveyCommon, HttpCase):
  8. def _format_submission_data(self, page, answer_data, additional_post_data):
  9. post_data = {}
  10. post_data['page_id'] = page.id
  11. for question_id, answer_vals in answer_data.items():
  12. question = page.question_ids.filtered(lambda q: q.id == question_id)
  13. post_data.update(self._prepare_post_data(question, answer_vals['value'], post_data))
  14. post_data.update(**additional_post_data)
  15. return post_data
  16. def test_flow_public(self):
  17. # Step: survey manager creates the survey
  18. # --------------------------------------------------
  19. with self.with_user('survey_manager'):
  20. survey = self.env['survey.survey'].create({
  21. 'title': 'Public Survey for Tarte Al Djotte',
  22. 'access_mode': 'public',
  23. 'users_login_required': False,
  24. 'questions_layout': 'page_per_section',
  25. })
  26. # First page is about customer data
  27. page_0 = self.env['survey.question'].create({
  28. 'is_page': True,
  29. 'question_type': False,
  30. 'sequence': 1,
  31. 'title': 'Page1: Your Data',
  32. 'survey_id': survey.id,
  33. })
  34. page0_q0 = self._add_question(
  35. page_0, 'What is your name', 'text_box',
  36. comments_allowed=False,
  37. constr_mandatory=True, constr_error_msg='Please enter your name', survey_id=survey.id)
  38. page0_q1 = self._add_question(
  39. page_0, 'What is your age', 'numerical_box',
  40. comments_allowed=False,
  41. constr_mandatory=True, constr_error_msg='Please enter your name', survey_id=survey.id)
  42. # Second page is about tarte al djotte
  43. page_1 = self.env['survey.question'].create({
  44. 'is_page': True,
  45. 'question_type': False,
  46. 'sequence': 4,
  47. 'title': 'Page2: Tarte Al Djotte',
  48. 'survey_id': survey.id,
  49. })
  50. page1_q0 = self._add_question(
  51. page_1, 'What do you like most in our tarte al djotte', 'multiple_choice',
  52. labels=[{'value': 'The gras'},
  53. {'value': 'The bette'},
  54. {'value': 'The tout'},
  55. {'value': 'The regime is fucked up'}], survey_id=survey.id)
  56. # fetch starting data to check only newly created data during this flow
  57. answers = self.env['survey.user_input'].search([('survey_id', '=', survey.id)])
  58. answer_lines = self.env['survey.user_input.line'].search([('survey_id', '=', survey.id)])
  59. self.assertEqual(answers, self.env['survey.user_input'])
  60. self.assertEqual(answer_lines, self.env['survey.user_input.line'])
  61. # Step: customer takes the survey
  62. # --------------------------------------------------
  63. # Customer opens start page
  64. r = self._access_start(survey)
  65. self.assertResponse(r, 200, [survey.title])
  66. # -> this should have generated a new answer with a token
  67. answers = self.env['survey.user_input'].search([('survey_id', '=', survey.id)])
  68. self.assertEqual(len(answers), 1)
  69. answer_token = answers.access_token
  70. self.assertTrue(answer_token)
  71. self.assertAnswer(answers, 'new', self.env['survey.question'])
  72. # Customer begins survey with first page
  73. r = self._access_page(survey, answer_token)
  74. self.assertResponse(r, 200)
  75. self.assertAnswer(answers, 'new', self.env['survey.question'])
  76. csrf_token = self._find_csrf_token(r.text)
  77. r = self._access_begin(survey, answer_token)
  78. self.assertResponse(r, 200)
  79. # Customer submit first page answers
  80. answer_data = {
  81. page0_q0.id: {'value': ['Alfred Poilvache']},
  82. page0_q1.id: {'value': ['44.0']},
  83. }
  84. post_data = self._format_submission_data(page_0, answer_data, {'csrf_token': csrf_token, 'token': answer_token, 'button_submit': 'next'})
  85. r = self._access_submit(survey, answer_token, post_data)
  86. self.assertResponse(r, 200)
  87. answers.invalidate_recordset() # TDE note: necessary as lots of sudo in controllers messing with cache
  88. # -> this should have generated answer lines
  89. self.assertAnswer(answers, 'in_progress', page_0)
  90. self.assertAnswerLines(page_0, answers, answer_data)
  91. # Customer is redirected on second page and begins filling it
  92. r = self._access_page(survey, answer_token)
  93. self.assertResponse(r, 200)
  94. csrf_token = self._find_csrf_token(r.text)
  95. # Customer submit second page answers
  96. answer_data = {
  97. page1_q0.id: {'value': [page1_q0.suggested_answer_ids.ids[0], page1_q0.suggested_answer_ids.ids[1]]},
  98. }
  99. post_data = self._format_submission_data(page_1, answer_data, {'csrf_token': csrf_token, 'token': answer_token, 'button_submit': 'next'})
  100. r = self._access_submit(survey, answer_token, post_data)
  101. self.assertResponse(r, 200)
  102. answers.invalidate_recordset() # TDE note: necessary as lots of sudo in controllers messing with cache
  103. # -> this should have generated answer lines and closed the answer
  104. self.assertAnswer(answers, 'done', page_1)
  105. self.assertAnswerLines(page_1, answers, answer_data)