diff --git a/patientregistrationsystem/qdc/experiment/tests/test_views.py b/patientregistrationsystem/qdc/experiment/tests/test_views.py index d689be1c5..736799ee9 100644 --- a/patientregistrationsystem/qdc/experiment/tests/test_views.py +++ b/patientregistrationsystem/qdc/experiment/tests/test_views.py @@ -5,6 +5,7 @@ import tempfile import zipfile import os +from datetime import datetime from unittest import skip from unittest.mock import patch, call @@ -45,7 +46,7 @@ from experiment.models import Group as ExperimentGroup from configuration.models import LocalInstitution from custom_user.models import Institution -from experiment.tests.tests_helper import ObjectsFactory +from experiment.tests.tests_helper import ObjectsFactory, ExperimentTestCase from patient.models import Patient, Telephone, SocialDemographicData, AmountCigarettes, AlcoholFrequency, \ AlcoholPeriod, SocialHistoryData, MedicalRecordData, Diagnosis, ClassificationOfDiseases, FleshTone, Payment, \ Religion, Schooling, ExamFile @@ -321,7 +322,6 @@ def setUp(self): ) user_profile = self.user.user_profile user_profile.login_enabled = True - user_profile.force_password_change = False user_profile.save() @@ -329,11 +329,8 @@ def setUp(self): group.user_set.add(self.user) self.client.login(username=self.user.username, password='passwd') - self.research_project = ObjectsFactory.create_research_project() - self.experiment = ObjectsFactory.create_experiment(self.research_project) - self.researcher = ObjectsFactory.create_experiment_researcher(self.experiment) # create experiment/experimental protocol/group @@ -5283,3 +5280,59 @@ def test_import_survey_call_update_response_fails_display_warning_message(self, message = str(list(get_messages(response.wsgi_request))[0]) self.assertEqual(message, 'Não foi possível atualizar todas as respostas.') + + +class ExperimentQuestionnaireTest(ExperimentTestCase): + + def setUp(self): + super(ExperimentQuestionnaireTest, self).setUp() + + # Create questionnaire data collection in NES + # TODO: use method already existent in patient.tests. See other places + survey = create_survey(212121) + self.component_config = self._create_nes_questionnaire( + self.root_component, survey) + + self.client.login( + username=self.user.username, password=self.user_passwd) + + def tearDown(self): + self.client.logout() + + def _create_nes_questionnaire(self, root_component, survey): + """Create questionnaire component in experimental protocol and return + data configuration tree associated to that questionnaire component + :param root_component: Block(Component) model instance + :return: DataConfigurationTree model instance + """ + questionnaire = ObjectsFactory.create_component( + self.experiment, Component.QUESTIONNAIRE, kwargs={'survey': survey}) + component_config = ObjectsFactory.create_component_configuration( + root_component, questionnaire) + ObjectsFactory.create_data_configuration_tree(component_config) + + return component_config + + @patch('survey.abc_search_engine.Server') + @patch('experiment.views.check_required_fields') + def test_create_questionnaire_response_sent_patient_id_in_redirect_url( + self, mockCheckRequiredFields, mockServer): + mockServer.return_value.get_session_key.return_value = 'abc' + mockServer.return_value.get_summary.return_value = 1 + mockServer.return_value.add_participants.return_value = [ + {'token': 'abc', 'tid': 1} + ] + mockCheckRequiredFields.return_value = True + + response = self.client.post( + reverse('subject_questionnaire_response', + kwargs={ + 'group_id': self.group.id, + 'subject_id': self.subject_of_group.subject_id, + 'questionnaire_id': self.component_config.id + }), + data={'action': 'save', 'date': datetime.now().strftime( + '%d/%m/%Y'), }) + + redirect_url = response.context['URL'] + self.assertIn(str(self.patient.id), redirect_url) diff --git a/patientregistrationsystem/qdc/experiment/tests/tests_helper.py b/patientregistrationsystem/qdc/experiment/tests/tests_helper.py index 229a31f48..0318db942 100644 --- a/patientregistrationsystem/qdc/experiment/tests/tests_helper.py +++ b/patientregistrationsystem/qdc/experiment/tests/tests_helper.py @@ -5,10 +5,10 @@ import zipfile from django.apps import apps -from django.contrib.auth.models import User +from django.contrib.auth.models import User, Group as UserGroup from django.core.files import File from django.db import IntegrityError -from django.test import RequestFactory +from django.test import RequestFactory, TestCase from faker import Factory from custom_user.tests_helper import create_user @@ -25,8 +25,34 @@ TMSLocalizationSystem, EMGFile, DigitalGamePhaseData, DigitalGamePhaseFile, AdditionalData, AdditionalDataFile, \ StimulusType, Stimulus from patient.models import ClassificationOfDiseases, MedicalRecordData, Diagnosis, ComplementaryExam, ExamFile +from patient.tests.tests_orig import UtilTests from survey.tests.tests_helper import create_survey + +class ExperimentTestCase(TestCase): + + def setUp(self): + super(ExperimentTestCase, self).setUp() + + # create the groups of users and their permissions + exec(open('add_initial_data.py').read()) + + self.user, self.user_passwd = create_user(UserGroup.objects.all()) + + self.research_project = ObjectsFactory.create_research_project( + self.user) + self.experiment = ObjectsFactory.create_experiment( + self.research_project) + self.root_component = ObjectsFactory.create_block(self.experiment) + self.group = ObjectsFactory.create_group( + self.experiment, self.root_component) + + self.patient = UtilTests().create_patient(changed_by=self.user) + subject = ObjectsFactory.create_subject(self.patient) + self.subject_of_group = ObjectsFactory.create_subject_of_group( + self.group, subject) + + USER_USERNAME = 'myadmin' USER_PWD = 'mypassword' @@ -807,4 +833,4 @@ def create_exam_file(patient, user): with File(open(bin_file.name, 'rb')) as f: exam_file.content.save('file.bin', f) exam_file.save() - return exam_file \ No newline at end of file + return exam_file diff --git a/patientregistrationsystem/qdc/experiment/views.py b/patientregistrationsystem/qdc/experiment/views.py index ba0583d99..3f85ac4a8 100755 --- a/patientregistrationsystem/qdc/experiment/views.py +++ b/patientregistrationsystem/qdc/experiment/views.py @@ -5347,8 +5347,9 @@ def get_limesurvey_response_url(questionnaire_response): id=questionnaire_response.data_configuration_tree.component_configuration.component.id) questionnaire_lime_survey = Questionnaires() - token = questionnaire_lime_survey.get_participant_properties(questionnaire.survey.lime_survey_id, - questionnaire_response.token_id, "token") + token = questionnaire_lime_survey.get_participant_properties( + questionnaire.survey.lime_survey_id, + questionnaire_response.token_id, "token") questionnaire_lime_survey.release_session_key() redirect_url = \ @@ -5359,7 +5360,7 @@ def get_limesurvey_response_url(questionnaire_response): token, str(questionnaire_response.questionnaire_responsible.id), questionnaire_response.date.strftime('%d-%m-%Y'), - str(questionnaire_response.subject_of_group.subject.id)) + str(questionnaire_response.subject_of_group.subject.patient.id)) return redirect_url @@ -5456,8 +5457,9 @@ def subject_questionnaire_response_reuse(request, group_id, subject_id, question @login_required @permission_required('experiment.change_questionnaireresponse') -def questionnaire_response_edit(request, questionnaire_response_id, - template_name="experiment/subject_questionnaire_response_form.html"): +def questionnaire_response_edit( + request, questionnaire_response_id, + template_name="experiment/subject_questionnaire_response_form.html"): questionnaire_response = get_object_or_404(QuestionnaireResponse, id=questionnaire_response_id) questionnaire = Questionnaire.objects.get( id=questionnaire_response.data_configuration_tree.component_configuration.component.id) diff --git a/patientregistrationsystem/qdc/export/tests/tests_helper.py b/patientregistrationsystem/qdc/export/tests/tests_helper.py index 4c547e55e..88c605bd5 100644 --- a/patientregistrationsystem/qdc/export/tests/tests_helper.py +++ b/patientregistrationsystem/qdc/export/tests/tests_helper.py @@ -8,31 +8,17 @@ from django.test import TestCase from custom_user.tests_helper import create_user -from experiment.tests.tests_helper import ObjectsFactory +from experiment.tests.tests_helper import ObjectsFactory, ExperimentTestCase from patient.tests.tests_orig import UtilTests -class ExportTestCase(TestCase): +class ExportTestCase(ExperimentTestCase): def setUp(self): + super(ExportTestCase, self).setUp() - # create the groups of users and their permissions - exec(open('add_initial_data.py').read()) - - # return user password to use when necessary in subclasses - self.user, self.user_passwd = create_user(Group.objects.all()) - self.client.login(username=self.user.username, password=self.user_passwd) - - # create experiment/experimental protocol/group - self.research_project = ObjectsFactory.create_research_project(self.user) - self.experiment = ObjectsFactory.create_experiment(self.research_project) - self.root_component = ObjectsFactory.create_block(self.experiment) - self.group = ObjectsFactory.create_group(self.experiment, self.root_component) - - # create patient/subject/subject_of_group - self.patient = UtilTests().create_patient(changed_by=self.user) - subject = ObjectsFactory.create_subject(self.patient) - self.subject_of_group = ObjectsFactory.create_subject_of_group(self.group, subject) + self.client.login(username=self.user.username, + password=self.user_passwd) def append_session_variable(self, key, value): """See: