Example #1
0
 def setUp(self):
     super(self.__class__, self).setUp()
     self.view = XFormSubmissionViewSet.as_view({
         "head": "create",
         "post": "create"
     })
     self._publish_xls_form_to_project()
 def setUp(self):
     super(TestXFormSubmissionViewSet, self).setUp()
     self.view = XFormSubmissionViewSet.as_view({
         "head": "create",
         "post": "create"
     })
     self._publish_xls_form_to_project()
Example #3
0
 def test_submission_with_instance_id_on_root_node(self):
     view = XFormSubmissionViewSet.as_view({'post': 'create'})
     self._publish_xml_form()
     message = u"Successful submission."
     instanceId = u'5b2cc313-fc09-437e-8149-fcd32f695d41'
     self.assertRaises(
         Instance.DoesNotExist, Instance.objects.get, uuid=instanceId)
     submission_path = os.path.join(
         self.main_directory, 'fixtures', 'transportation',
         'view', 'submission.xml')
     count = Instance.objects.count()
     with codecs.open(submission_path, encoding='utf-8') as f:
         post_data = {'xml_submission_file': f}
         request = self.factory.post(self._submission_list_url, post_data)
         response = view(request)
         self.assertEqual(response.status_code, 401)
         auth = DigestAuth('bob', 'bobbob')
         request.META.update(auth(request.META, response))
         response = view(request, username=self.user.username)
         self.assertContains(response, message, status_code=201)
         self.assertContains(response, instanceId, status_code=201)
         self.assertEqual(Instance.objects.count(), count + 1)
Example #4
0
 def test_submission_with_instance_id_on_root_node(self):
     view = XFormSubmissionViewSet.as_view({'post': 'create'})
     self._publish_xml_form()
     message = u"Successful submission."
     instanceId = u'5b2cc313-fc09-437e-8149-fcd32f695d41'
     self.assertRaises(
         Instance.DoesNotExist, Instance.objects.get, uuid=instanceId)
     submission_path = os.path.join(
         self.main_directory, 'fixtures', 'transportation',
         'view', 'submission.xml')
     count = Instance.objects.count()
     with codecs.open(submission_path, encoding='utf-8') as f:
         post_data = {'xml_submission_file': f}
         request = self.factory.post(self._submission_list_url, post_data)
         response = view(request)
         self.assertEqual(response.status_code, 401)
         auth = DigestAuth('bob', 'bobbob')
         request.META.update(auth(request.META, response))
         response = view(request, username=self.user.username)
         self.assertContains(response, message, status_code=201)
         self.assertContains(response, instanceId, status_code=201)
         self.assertEqual(Instance.objects.count(), count + 1)
Example #5
0
    def test_submission_tracking(self):
        """Test that submissions are tracked"""
        segment_mock = MagicMock()
        appoptics_mock = MagicMock()
        onadata.libs.utils.analytics.segment_analytics = segment_mock
        onadata.libs.utils.analytics.init_analytics()
        self.assertEqual(segment_mock.write_key, '123')

        # Test out that the track_object_event decorator
        # Tracks created submissions
        view = XFormSubmissionViewSet.as_view({
            'post': 'create',
            'head': 'create'
        })
        self._publish_xls_form_to_project()
        onadata.libs.utils.analytics.appoptics_api = appoptics_mock
        s = self.surveys[0]
        media_file = "1335783522563.jpg"
        path = os.path.join(self.main_directory, 'fixtures', 'transportation',
                            'instances', s, media_file)
        request_path = f"/{self.user.username}/submission"
        with open(path, 'rb') as f:
            f = InMemoryUploadedFile(f, 'media_file', media_file, 'image/jpg',
                                     os.path.getsize(path), None)
            submission_path = os.path.join(self.main_directory, 'fixtures',
                                           'transportation', 'instances', s,
                                           s + '.xml')
            with open(submission_path, 'rb') as sf:
                data = {'xml_submission_file': sf, 'media_file': f}
                request = self.factory.post(request_path, data)
                request.user = AnonymousUser()
                response = view(request, username=self.user.username)
                self.assertContains(response,
                                    'Successful submission',
                                    status_code=201)
                self.assertTrue(response.has_header('X-OpenRosa-Version'))
                self.assertTrue(
                    response.has_header('X-OpenRosa-Accept-Content-Length'))
                self.assertTrue(response.has_header('Date'))
                self.assertEqual(response['Content-Type'],
                                 'text/xml; charset=utf-8')
                self.assertEqual(response['Location'],
                                 'http://testserver' + request_path)
        form_id = self.xform.pk
        username = self.user.username
        segment_mock.track.assert_called_with(
            '*****@*****.**', 'Submission created', {
                'xform_id': self.xform.pk,
                'organization': 'Bob Inc.',
                'from': 'XML Submissions',
                'label': f'form-{form_id}-owned-by-{username}',
                'value': 1,
                'event_by': 'anonymous'
            }, {
                'source': 'test-server',
                'organization': 'Bob Inc.',
                'event_by': 'anonymous',
                'action_from': 'XML Submissions',
                'xform_id': self.xform.pk,
                'path': f'/{username}/submission',
                'url': f'http://testserver/{username}/submission',
                'ip': '127.0.0.1',
                'userId': self.user.id
            })

        appoptics_mock.submit_measurement.assert_called_with(
            'Submission created',
            1,
            tags={
                'source': 'test-server',
                'event_by': 'anonymous',
                'organization': 'Bob_Inc.',
                'action_from': 'XML_Submissions',
                'xform_id': self.xform.pk,
                'path': f'/{username}/submission',
                'url': f'http://testserver/{username}/submission',
                'ip': '127.0.0.1',
                'userId': self.user.id
            })
Example #6
0
    def test_submission_tracking(self):
        """Test that submissions are tracked"""
        segment_mock = MagicMock()
        onadata.libs.utils.analytics.segment_analytics = segment_mock
        onadata.libs.utils.analytics.init_analytics()
        self.assertEqual(segment_mock.write_key, '123')

        # Test out that the track_object_event decorator
        # Tracks created submissions, XForms and Projects
        view = XFormSubmissionViewSet.as_view({
            'post': 'create',
            'head': 'create'
        })
        self._publish_xls_form_to_project()
        segment_mock.track.assert_called_with(
            '*****@*****.**', 'XForm created', {
                'created_by': self.xform.user,
                'xform_id': self.xform.pk,
                'xform_name': self.xform.title,
                'from': 'Publish XLS Form',
                'value': 1
            }, {
                'page': {},
                'campaign': {},
                'active': True
            })
        s = self.surveys[0]
        media_file = "1335783522563.jpg"
        path = os.path.join(self.main_directory, 'fixtures', 'transportation',
                            'instances', s, media_file)
        request_path = f"/{self.user.username}/submission"
        with open(path, 'rb') as f:
            f = InMemoryUploadedFile(f, 'media_file', media_file, 'image/jpg',
                                     os.path.getsize(path), None)
            submission_path = os.path.join(self.main_directory, 'fixtures',
                                           'transportation', 'instances', s,
                                           s + '.xml')
            with open(submission_path, 'rb') as sf:
                data = {'xml_submission_file': sf, 'media_file': f}
                request = self.factory.post(request_path, data)
                request.user = AnonymousUser()
                request.META['HTTP_DATE'] = '2020-09-10T11:56:32.424726+00:00'
                request.META['HTTP_REFERER'] = settings.HOSTNAME +\
                    ':8000'
                request.META['HTTP_USER_AGENT'] =\
                    'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit'\
                    '/537.36 (KHTML, like Gecko) Chrome'\
                    '/83.0.4103.61 Safari/537.36'
                response = view(request, username=self.user.username)
                self.assertContains(response,
                                    'Successful submission',
                                    status_code=201)
                self.assertTrue(response.has_header('X-OpenRosa-Version'))
                self.assertTrue(
                    response.has_header('X-OpenRosa-Accept-Content-Length'))
                self.assertTrue(response.has_header('Date'))
                self.assertEqual(response['Content-Type'],
                                 'text/xml; charset=utf-8')
                self.assertEqual(response['Location'],
                                 'http://testserver' + request_path)
        form_id = self.xform.pk
        username = self.user.username
        segment_mock.track.assert_called_with(
            '*****@*****.**', 'Submission created', {
                'xform_id': self.xform.pk,
                'project_id': self.xform.project.pk,
                'organization': 'Bob Inc.',
                'from': 'Submission collected from Enketo',
                'label': f'form-{form_id}-owned-by-{username}',
                'value': 1,
                'event_by': 'anonymous'
            }, {
                'page': {
                    'path': '/bob/submission',
                    'referrer': settings.HOSTNAME + ':8000',
                    'url': 'http://testserver/bob/submission'
                },
                'campaign': {
                    'source': settings.HOSTNAME
                },
                'active':
                True,
                'ip':
                '127.0.0.1',
                'userId':
                self.xform.user.pk,
                'receivedAt':
                '2020-09-10T11:56:32.424726+00:00',
                'userAgent':
                'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit'
                '/537.36 (KHTML, like Gecko) Chrome'
                '/83.0.4103.61 Safari/537.36'
            })