[IMP] survey: perform a quick linting in tests
Notably
* have tools and helpers in a SurveyCase and let SurveyCommon bet the
one with data;
* remove unnecessary with_user, replaced by standard one;
* move all tools in SurveyCase to help reusing them;
This commit is contained in:
@@ -5,16 +5,14 @@ import re
|
||||
|
||||
from collections import Counter
|
||||
from contextlib import contextmanager
|
||||
from functools import partial
|
||||
|
||||
from odoo.tests import common, new_test_user
|
||||
|
||||
survey_new_test_user = partial(new_test_user, context={'mail_create_nolog': True, 'mail_create_nosubscribe': True, 'mail_notrack': True, 'no_reset_password': True})
|
||||
from odoo.addons.mail.tests.common import mail_new_test_user
|
||||
from odoo.tests import common
|
||||
|
||||
|
||||
class TestSurveyCommon(common.SavepointCase):
|
||||
class SurveyCase(common.SavepointCase):
|
||||
def setUp(self):
|
||||
super(TestSurveyCommon, self).setUp()
|
||||
super(SurveyCase, self).setUp()
|
||||
|
||||
""" Some custom stuff to make the matching between questions and answers
|
||||
:param dict _type_match: dict
|
||||
@@ -31,75 +29,9 @@ class TestSurveyCommon(common.SavepointCase):
|
||||
'matrix': ('suggestion', ('suggested_answer_id', 'matrix_row_id')), # TDE: still unclear
|
||||
}
|
||||
|
||||
""" Create test data: a survey with some pre-defined questions and various test users for ACL """
|
||||
self.survey_manager = survey_new_test_user(
|
||||
self.env, name='Gustave Doré', login='survey_manager', email='survey.manager@example.com',
|
||||
groups='survey.group_survey_manager,base.group_user'
|
||||
)
|
||||
|
||||
self.survey_user = survey_new_test_user(
|
||||
self.env, name='Lukas Peeters', login='survey_user', email='survey.user@example.com',
|
||||
groups='survey.group_survey_user,base.group_user'
|
||||
)
|
||||
|
||||
self.user_emp = survey_new_test_user(
|
||||
self.env, name='Eglantine Employee', login='user_emp', email='employee@example.com',
|
||||
groups='base.group_user', password='user_emp'
|
||||
)
|
||||
|
||||
self.user_portal = survey_new_test_user(
|
||||
self.env, name='Patrick Portal', login='user_portal', email='portal@example.com',
|
||||
groups='base.group_portal'
|
||||
)
|
||||
|
||||
self.user_public = survey_new_test_user(
|
||||
self.env, name='Pauline Public', login='user_public', email='public@example.com',
|
||||
groups='base.group_public'
|
||||
)
|
||||
|
||||
self.customer = self.env['res.partner'].create({
|
||||
'name': 'Caroline Customer',
|
||||
'email': 'customer@example.com',
|
||||
})
|
||||
|
||||
self.survey = self.env['survey.survey'].with_user(self.survey_manager).create({
|
||||
'title': 'Test Survey',
|
||||
'access_mode': 'public',
|
||||
'users_login_required': True,
|
||||
'users_can_go_back': False,
|
||||
'state': 'open',
|
||||
})
|
||||
self.page_0 = self.env['survey.question'].with_user(self.survey_manager).create({
|
||||
'title': 'First page',
|
||||
'survey_id': self.survey.id,
|
||||
'sequence': 1,
|
||||
'is_page': True,
|
||||
})
|
||||
self.question_ft = self.env['survey.question'].with_user(self.survey_manager).create({
|
||||
'title': 'Test Free Text',
|
||||
'survey_id': self.survey.id,
|
||||
'sequence': 2,
|
||||
'question_type': 'text_box',
|
||||
})
|
||||
self.question_num = self.env['survey.question'].with_user(self.survey_manager).create({
|
||||
'title': 'Test NUmerical Box',
|
||||
'survey_id': self.survey.id,
|
||||
'sequence': 3,
|
||||
'question_type': 'numerical_box',
|
||||
})
|
||||
|
||||
@contextmanager
|
||||
def with_user(self, user):
|
||||
""" Quick with_user environment """
|
||||
old_uid = self.uid
|
||||
try:
|
||||
self.uid = user.id
|
||||
self.env = self.env(user=self.uid)
|
||||
yield
|
||||
finally:
|
||||
# back
|
||||
self.uid = old_uid
|
||||
self.env = self.env(user=self.uid)
|
||||
# ------------------------------------------------------------
|
||||
# ASSERTS
|
||||
# ------------------------------------------------------------
|
||||
|
||||
def assertAnswer(self, answer, state, page):
|
||||
self.assertEqual(answer.state, state)
|
||||
@@ -147,6 +79,10 @@ class TestSurveyCommon(common.SavepointCase):
|
||||
for text in text_bits or []:
|
||||
self.assertIn(text, response.text)
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# DATA CREATION
|
||||
# ------------------------------------------------------------
|
||||
|
||||
def _add_question(self, page, name, qtype, **kwargs):
|
||||
constr_mandatory = kwargs.pop('constr_mandatory', True)
|
||||
constr_error_msg = kwargs.pop('constr_error_msg', 'TestError')
|
||||
@@ -210,6 +146,10 @@ class TestSurveyCommon(common.SavepointCase):
|
||||
base_alvals.update(kwargs)
|
||||
return self.env['survey.user_input.line'].create(base_alvals)
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# UTILS
|
||||
# ------------------------------------------------------------
|
||||
|
||||
def _access_start(self, survey):
|
||||
return self.url_open('/survey/start/%s' % survey.access_token)
|
||||
|
||||
@@ -246,3 +186,84 @@ class TestSurveyCommon(common.SavepointCase):
|
||||
[values] = values
|
||||
post_data[question.id] = str(values)
|
||||
return post_data
|
||||
|
||||
def _answer_question(self, question, answer, answer_token, csrf_token, button_submit='next'):
|
||||
# Employee submits the question answer
|
||||
post_data = self._format_submission_data(question, answer, {'csrf_token': csrf_token, 'token': answer_token, 'button_submit': button_submit})
|
||||
response = self._access_submit(question.survey_id, answer_token, post_data)
|
||||
self.assertResponse(response, 200)
|
||||
|
||||
# Employee is redirected on next question
|
||||
response = self._access_page(question.survey_id, answer_token)
|
||||
self.assertResponse(response, 200)
|
||||
|
||||
def _format_submission_data(self, question, answer, additional_post_data):
|
||||
post_data = {}
|
||||
post_data['question_id'] = question.id
|
||||
post_data.update(self._prepare_post_data(question, answer, post_data))
|
||||
if question.page_id:
|
||||
post_data['page_id'] = question.page_id.id
|
||||
post_data.update(**additional_post_data)
|
||||
return post_data
|
||||
|
||||
|
||||
class TestSurveyCommon(SurveyCase):
|
||||
def setUp(self):
|
||||
super(TestSurveyCommon, self).setUp()
|
||||
|
||||
""" Create test data: a survey with some pre-defined questions and various test users for ACL """
|
||||
self.survey_manager = mail_new_test_user(
|
||||
self.env, name='Gustave Doré', login='survey_manager', email='survey.manager@example.com',
|
||||
groups='survey.group_survey_manager,base.group_user'
|
||||
)
|
||||
|
||||
self.survey_user = mail_new_test_user(
|
||||
self.env, name='Lukas Peeters', login='survey_user', email='survey.user@example.com',
|
||||
groups='survey.group_survey_user,base.group_user'
|
||||
)
|
||||
|
||||
self.user_emp = mail_new_test_user(
|
||||
self.env, name='Eglantine Employee', login='user_emp', email='employee@example.com',
|
||||
groups='base.group_user', password='user_emp'
|
||||
)
|
||||
|
||||
self.user_portal = mail_new_test_user(
|
||||
self.env, name='Patrick Portal', login='user_portal', email='portal@example.com',
|
||||
groups='base.group_portal'
|
||||
)
|
||||
|
||||
self.user_public = mail_new_test_user(
|
||||
self.env, name='Pauline Public', login='user_public', email='public@example.com',
|
||||
groups='base.group_public'
|
||||
)
|
||||
|
||||
self.customer = self.env['res.partner'].create({
|
||||
'name': 'Caroline Customer',
|
||||
'email': 'customer@example.com',
|
||||
})
|
||||
|
||||
self.survey = self.env['survey.survey'].with_user(self.survey_manager).create({
|
||||
'title': 'Test Survey',
|
||||
'access_mode': 'public',
|
||||
'users_login_required': True,
|
||||
'users_can_go_back': False,
|
||||
'state': 'open',
|
||||
})
|
||||
self.page_0 = self.env['survey.question'].with_user(self.survey_manager).create({
|
||||
'title': 'First page',
|
||||
'survey_id': self.survey.id,
|
||||
'sequence': 1,
|
||||
'is_page': True,
|
||||
})
|
||||
self.question_ft = self.env['survey.question'].with_user(self.survey_manager).create({
|
||||
'title': 'Test Free Text',
|
||||
'survey_id': self.survey.id,
|
||||
'sequence': 2,
|
||||
'question_type': 'text_box',
|
||||
})
|
||||
self.question_num = self.env['survey.question'].with_user(self.survey_manager).create({
|
||||
'title': 'Test NUmerical Box',
|
||||
'survey_id': self.survey.id,
|
||||
'sequence': 3,
|
||||
'question_type': 'numerical_box',
|
||||
})
|
||||
|
||||
@@ -11,29 +11,11 @@ from odoo.tests.common import HttpCase
|
||||
|
||||
@tagged('-at_install', 'post_install', 'functional')
|
||||
class TestCertificationFlow(common.TestSurveyCommon, HttpCase):
|
||||
def _answer_question(self, question, answer, answer_token, csrf_token, button_submit='next'):
|
||||
# Employee submits the question answer
|
||||
post_data = self._format_submission_data(question, answer, {'csrf_token': csrf_token, 'token': answer_token, 'button_submit': button_submit})
|
||||
response = self._access_submit(question.survey_id, answer_token, post_data)
|
||||
self.assertResponse(response, 200)
|
||||
|
||||
# Employee is redirected on next question
|
||||
response = self._access_page(question.survey_id, answer_token)
|
||||
self.assertResponse(response, 200)
|
||||
|
||||
def _format_submission_data(self, question, answer, additional_post_data):
|
||||
post_data = {}
|
||||
post_data['question_id'] = question.id
|
||||
post_data.update(self._prepare_post_data(question, answer, post_data))
|
||||
if question.page_id:
|
||||
post_data['page_id'] = question.page_id.id
|
||||
post_data.update(**additional_post_data)
|
||||
return post_data
|
||||
|
||||
def test_flow_certification(self):
|
||||
# Step: survey user creates the certification
|
||||
# --------------------------------------------------
|
||||
with self.with_user(self.survey_user):
|
||||
with self.with_user('survey_user'):
|
||||
certification = self.env['survey.survey'].create({
|
||||
'title': 'User Certification for SO lines',
|
||||
'access_mode': 'public',
|
||||
@@ -148,7 +130,7 @@ class TestCertificationFlow(common.TestSurveyCommon, HttpCase):
|
||||
def test_randomized_certification(self):
|
||||
# Step: survey user creates the randomized certification
|
||||
# --------------------------------------------------
|
||||
with self.with_user(self.survey_user):
|
||||
with self.with_user('survey_user'):
|
||||
certification = self.env['survey.survey'].create({
|
||||
'title': 'User randomized Certification',
|
||||
'questions_layout': 'page_per_section',
|
||||
|
||||
@@ -1,14 +1,7 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
import random
|
||||
|
||||
from collections import Counter
|
||||
from itertools import product
|
||||
from werkzeug import urls
|
||||
|
||||
from odoo import _
|
||||
from odoo.addons.http_routing.models.ir_http import slug
|
||||
from odoo.addons.survey.tests import common
|
||||
from odoo.tests.common import users
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ from odoo.addons.survey.tests import common
|
||||
|
||||
class TestSurveyComputePagesQuestions(common.TestSurveyCommon):
|
||||
def test_compute_pages_questions(self):
|
||||
with self.with_user(self.survey_manager):
|
||||
with self.with_user('survey_manager'):
|
||||
survey = self.env['survey.survey'].create({
|
||||
'title': 'Test compute survey',
|
||||
'state': 'open',
|
||||
|
||||
@@ -20,7 +20,7 @@ class TestSurveyFlow(common.TestSurveyCommon, HttpCase):
|
||||
def test_flow_public(self):
|
||||
# Step: survey manager creates the survey
|
||||
# --------------------------------------------------
|
||||
with self.with_user(self.survey_manager):
|
||||
with self.with_user('survey_manager'):
|
||||
survey = self.env['survey.survey'].create({
|
||||
'title': 'Public Survey for Tarte Al Djotte',
|
||||
'access_mode': 'public',
|
||||
|
||||
@@ -290,7 +290,7 @@ class TestAccess(common.TestSurveyCommon):
|
||||
@users('survey_manager')
|
||||
def test_access_answers_survey_manager(self):
|
||||
admin = self.env.ref('base.user_admin')
|
||||
with self.with_user(admin):
|
||||
with self.with_user(admin.login):
|
||||
survey_other = self.env['survey.survey'].create({'title': 'Other'})
|
||||
self.env['survey.question'].create({'title': 'Other', 'sequence': 0, 'is_page': True, 'survey_id': survey_other.id})
|
||||
question_other = self.env['survey.question'].create({'title': 'Other Question', 'sequence': 1, 'survey_id': survey_other.id})
|
||||
|
||||
@@ -8,7 +8,7 @@ class TestCourseCertificationFailureFlow(TestSurveyCommon):
|
||||
def test_course_certification_failure_flow(self):
|
||||
# Step 1: create a simple certification
|
||||
# --------------------------------------------------
|
||||
with self.with_user(self.survey_user):
|
||||
with self.with_user('survey_user'):
|
||||
certification = self.env['survey.survey'].create({
|
||||
'title': 'Small course certification',
|
||||
'access_mode': 'public',
|
||||
|
||||
Reference in New Issue
Block a user