Example #1
0
    def test_POST_send_to_plugin_creates_zip_file(self, mockServer):
        set_limesurvey_api_mocks(mockServer)

        self._create_basic_objects()
        patient2 = UtilTests.create_patient(self.user)
        for survey in Survey.objects.all():
            UtilTests.create_response_survey(self.user, patient2, survey, 50)
        # Update mocks for calling the two patients
        update_limesurvey_api_mocks(mockServer)

        self.client.post(reverse('send-to-plugin'),
                         data={
                             'headings': ['code'],
                             'opt_floresta': ['on'],
                             'patient_selected':
                             ['age*age', 'gender__name*gender'],
                             'patients_selected[]':
                             [str(self.patient.id),
                              str(patient2.id)]
                         })

        export = Export.objects.last()
        with open(
                os.path.join(settings.MEDIA_ROOT, 'export', str(self.user.id),
                             str(export.id), 'export.zip'), 'rb') as file:
            zipped_file = zipfile.ZipFile(file, 'r')
            self.assertIsNone(zipped_file.testzip())
            zipped_file.close()
Example #2
0
    def test_POST_send_to_plugin_does_not_select_any_patient_display_warning_message(
            self, mockServer):
        set_limesurvey_api_mocks(mockServer)

        self._create_basic_objects()
        response = self.client.post(reverse('send-to-plugin'),
                                    data={
                                        'opt_floresta': ['on'],
                                        'patient_selected': ['age*age'],
                                        'patients_selected[]': []
                                    })
        self.assertRedirects(response, reverse('send-to-plugin'))
        message = str(list(get_messages(response.wsgi_request))[0])
        self.assertEqual(message, _('Please select at least one patient'))
Example #3
0
    def test_POST_send_to_plugin_redirect_to_send_to_plugin_view_and_remove_plugin_url_key_from_session(
            self, mockServer):
        set_limesurvey_api_mocks(mockServer)

        self._create_basic_objects()
        self.client.post(reverse('send-to-plugin'),
                         data={
                             'headings': ['code'],
                             'opt_floresta': ['on'],
                             'patient_selected':
                             ['age*age', 'gender__name*gender'],
                             'patients_selected[]': [str(self.patient.id)]
                         },
                         follow=True)

        self.assertIsNone(self.client.session.get('plugin_url'), None)
Example #4
0
    def test_POST_send_to_plugin_display_success_message(self, mockServer):
        set_limesurvey_api_mocks(mockServer)

        self._create_basic_objects()
        response = self.client.post(reverse('send-to-plugin'),
                                    data={
                                        'headings': ['code'],
                                        'opt_floresta': ['on'],
                                        'patient_selected':
                                        ['age*age', 'gender__name*gender'],
                                        'patients_selected[]':
                                        [str(self.patient.id)]
                                    },
                                    follow=True)

        message = str(list(get_messages(response.wsgi_request))[0])
        self.assertEqual(
            message, _('Data from questionnaires was sent to Forest Plugin'))
Example #5
0
    def test_group_selected_list_in_request_session_removes_session_key(
            self, mockServer):
        # Simulate 'group_selected_list' already in request session when
        # sending to Plugin in Per Participant way
        self.append_session_variable('group_selected_list', 21)

        set_limesurvey_api_mocks(mockServer)
        self._create_basic_objects()

        self.client.post(reverse('send-to-plugin'),
                         data={
                             'headings': ['code'],
                             'opt_floresta': ['on'],
                             'patient_selected':
                             ['age*age', 'gender__name*gender'],
                             'patients_selected[]': [str(self.patient.id)]
                         })

        self.assertIsNone(self.client.session.get('group_selected_list', None))
Example #6
0
 def test_POST_send_to_plugin_get_error_in_consuming_limesurvey_api_returns_error_message4(
         self, mockServer):
     set_limesurvey_api_mocks(mockServer)
     # Could not get responses by token
     mockServer.return_value.export_responses_by_token.side_effect = \
         4 * [{'status': 'No Data, survey table does not exist.'}]
     self._create_basic_objects()
     response = self.client.post(reverse('send-to-plugin'),
                                 data={
                                     'opt_floresta': ['on'],
                                     'patient_selected':
                                     ['age*age', 'gender__name*gender'],
                                     'patients_selected[]':
                                     [str(self.patient.id)]
                                 })
     self.assertRedirects(response, reverse('send-to-plugin'))
     message = str(list(get_messages(response.wsgi_request))[0])
     self.assertEqual(
         message,
         _('Error: some thing went wrong consuming LimeSurvey API. Please try again. If '
           'problem persists please contact System Administrator.'))
Example #7
0
    def test_POST_send_to_plugin_redirect_to_send_to_plugin_view_with_right_context(
            self, mockServer):
        set_limesurvey_api_mocks(mockServer)

        self._create_basic_objects()
        response = self.client.post(reverse('send-to-plugin'),
                                    data={
                                        'headings': ['code'],
                                        'opt_floresta': ['on'],
                                        'patient_selected':
                                        ['age*age', 'gender__name*gender'],
                                        'patients_selected[]':
                                        [str(self.patient.id)]
                                    },
                                    follow=True)
        self.assertEqual(response.status_code, 200)

        export = Export.objects.last()
        plugin_url = 'http://plugin_url?user_id=' + str(
            self.user.id) + '&export_id=' + str(export.id)
        self.assertEqual(response.context['plugin_url'], plugin_url)
Example #8
0
    def test_POST_send_to_plugin_adds_plugin_url_session_key_and_redirect_to_send_to_plugin_view(
            self, mockServer):
        set_limesurvey_api_mocks(mockServer)

        self._create_basic_objects()
        response = self.client.post(reverse('send-to-plugin'),
                                    data={
                                        'headings': ['code'],
                                        'opt_floresta': ['on'],
                                        'patient_selected':
                                        ['age*age', 'gender__name*gender'],
                                        'patients_selected[]':
                                        [str(self.patient.id)]
                                    })

        export = Export.objects.last()
        plugin = RandomForests.objects.last()
        plugin_url = plugin.plugin_url + '?user_id=' + str(
            self.user.id) + '&export_id=' + str(export.id)
        self.assertEqual(self.client.session.get('plugin_url'), plugin_url)
        self.assertRedirects(response, reverse('send-to-plugin'))
Example #9
0
 def test_POST_send_to_plugin_get_error_in_consuming_limesurvey_api_returns_error_message2(
         self, mockServer):
     set_limesurvey_api_mocks(mockServer)
     # Could not get survey properties
     mockServer.return_value.get_survey_properties.return_value = {
         'status': 'Error: Invalid survey ID'
     }
     self._create_basic_objects()
     response = self.client.post(reverse('send-to-plugin'),
                                 data={
                                     'opt_floresta': ['on'],
                                     'patient_selected':
                                     ['age*age', 'gender__name*gender'],
                                     'patients_selected[]':
                                     [str(self.patient.id)]
                                 })
     self.assertRedirects(response, reverse('send-to-plugin'))
     message = str(list(get_messages(response.wsgi_request))[0])
     self.assertEqual(
         message,
         _('Error: some thing went wrong consuming LimeSurvey API. Please try again. If '
           'problem persists please contact System Administrator.'))
Example #10
0
    def test_POST_send_to_plugin_does_not_build_zip_file_display_error_message(
        self,
        mock_build_zip_file,
        mockServer,
    ):
        set_limesurvey_api_mocks(mockServer)
        # Simulate an empty file path to represent that zip file was not created
        mock_build_zip_file.return_value = 0, ''

        self._create_basic_objects()
        response = self.client.post(reverse('send-to-plugin'),
                                    data={
                                        'opt_floresta': ['on'],
                                        'patient_selected':
                                        ['age*age', 'gender__name*gender'],
                                        'patients_selected[]':
                                        [str(self.patient.id)]
                                    },
                                    follow=True)

        self.assertRedirects(response, reverse('send-to-plugin'))
        message = str(list(get_messages(response.wsgi_request))[0])
        self.assertEqual(message,
                         _('Could not open zip file to send to Forest Plugin'))
Example #11
0
    def test_POST_send_to_plugin_returns_zip_file_with_only_data_from_participants_selected(
            self, mockServer):
        set_limesurvey_api_mocks(mockServer)

        # Reset get_participants_properties to deal with only one participant
        # selected
        mockServer.return_value.get_participant_properties.side_effect = [
            {
                'token': 'sIbj3gwjvwpa2QY'
            },
            {
                'token': 'OSSMaFVewVl8D0J'
            },
            {
                'token': 'OSSMaFVewVl8D0J'
            },
            {
                'token': 'fFPnTsNUJwRye3g'
            },
            {
                'token': 'fFPnTsNUJwRye3g'
            },
            {
                'token': 'fFPnTsNUJwRye3g'
            },
        ]

        self._create_basic_objects()
        patient2 = UtilTests.create_patient(self.user)
        for survey in Survey.objects.all():
            UtilTests.create_response_survey(self.user, patient2, survey, 50)
        self.client.post(
            reverse('send-to-plugin'),
            data={
                'headings': ['code'],
                # TODO (NES-963): about 'patient_selected see TODO (NES-963) in export.views
                'opt_floresta': ['on'],
                'patient_selected': ['age*age', 'gender__name*gender'],
                'patients_selected[]': [str(self.patient.id)],
            })

        export = Export.objects.last()
        with open(
                os.path.join(settings.MEDIA_ROOT, 'export', str(self.user.id),
                             str(export.id), 'export.zip'), 'rb') as file:
            zipped_file = zipfile.ZipFile(file, 'r')
            self.assertIsNone(zipped_file.testzip())
            # Tests for Per_participant subdir
            list_items = zipped_file.namelist()
            in_items = re.compile(input_export.BASE_DIRECTORY +
                                  '/Per_participant/Participant_%s' %
                                  self.patient.code)
            in_items = [in_items.match(item) for item in list_items]
            in_items = [item for item in in_items if item is not None]
            self.assertEqual(3, len(in_items))
            out_items = re.compile('data/Per_participant/Participant_%s' %
                                   patient2.code)
            out_items = [out_items.match(item) for item in list_items]
            out_items = [item for item in out_items if item is not None]
            self.assertEqual(0, len(out_items))

            # Tests for Per_questionnaire subdir
            questionnaire1 = zipped_file.extract(
                input_export.BASE_DIRECTORY +
                '/Per_questionnaire/QA_unified_admission_assessment/Responses_QA_en.csv',
                TEMP_MEDIA_ROOT)
            with open(questionnaire1) as q1_file:
                reader = list(csv.reader(q1_file))
                self.assertEqual(2, len(reader))
                self.assertEqual(self.patient.code, reader[1][0])
            questionnaire2 = zipped_file.extract(
                input_export.BASE_DIRECTORY +
                '/Per_questionnaire/QS_surgical_evaluation/Responses_QS_en.csv',
                TEMP_MEDIA_ROOT)
            with open(questionnaire2) as q3_file:
                reader = list(csv.reader(q3_file))
                self.assertEqual(2, len(reader))
                self.assertEqual(self.patient.code, reader[1][0])
            questionnaire3 = zipped_file.extract(
                input_export.BASE_DIRECTORY +
                '/Per_questionnaire/QF_unified_followup_assessment/Responses_QF_en.csv',
                TEMP_MEDIA_ROOT)
            with open(questionnaire3) as q3_file:
                reader = list(csv.reader(q3_file))
                self.assertEqual(2, len(reader))
                self.assertEqual(self.patient.code, reader[1][0])