Exemple #1
0
    def test_list_widgets_with_formid(self):
        self._create_widget()
        self._publish_xls_form_to_project()

        data = {
            'content_object': 'http://testserver/api/v1/forms/%s' %
                              self.xform.pk,
            'widget_type': "charts",
            'view_type': "horizontal-bar",
            'column': "_submitted_by",
        }

        self._create_widget(data=data)

        view = WidgetViewSet.as_view({
            'get': 'list',
        })

        data = {
            "xform": self.xform.pk
        }

        request = self.factory.get('/', data=data, **self.extra)
        response = view(request)

        self.assertEquals(response.status_code, 200)
        self.assertEquals(len(response.data), 1)
Exemple #2
0
    def test_list_widgets_with_formid(self):
        self._create_widget()
        self._publish_xls_form_to_project()

        data = {
            'content_object':
            'http://testserver/api/v1/forms/%s' % self.xform.pk,
            'widget_type': "charts",
            'view_type': "horizontal-bar",
            'column': "_submitted_by",
        }

        self._create_widget(data=data)

        view = WidgetViewSet.as_view({
            'get': 'list',
        })

        data = {"xform": self.xform.pk}

        request = self.factory.get('/', data=data, **self.extra)
        response = view(request)

        self.assertEquals(response.status_code, 200)
        self.assertEquals(len(response.data), 1)
Exemple #3
0
    def test_widget_permission_create(self):

        alice_data = {'username': '******', 'email': '*****@*****.**'}
        self._login_user_and_profile(alice_data)

        view = WidgetViewSet.as_view({'post': 'create'})

        data = {
            'title': "Widget that",
            'content_object':
            'http://testserver/api/v1/forms/%s' % self.xform.pk,
            'description': "Test widget",
            'aggregation': "Sum",
            'widget_type': "charts",
            'view_type': "horizontal-bar",
            'column': "age",
            'group_by': ''
        }

        # to do: test random user with auth but no perms
        request = self.factory.post('/',
                                    data=json.dumps(data),
                                    content_type="application/json",
                                    **self.extra)
        response = view(request)
        self.assertEquals(response.status_code, 400)

        # owner
        OwnerRole.add(self.user, self.project)
        request = self.factory.post('/',
                                    data=json.dumps(data),
                                    content_type="application/json",
                                    **self.extra)
        response = view(request)
        self.assertEquals(response.status_code, 201)

        # readonly
        ReadOnlyRole.add(self.user, self.project)
        request = self.factory.post('/',
                                    data=json.dumps(data),
                                    content_type="application/json",
                                    **self.extra)
        response = view(request)
        self.assertEquals(response.status_code, 201)

        # dataentryonlyrole
        DataEntryOnlyRole.add(self.user, self.project)
        request = self.factory.post('/',
                                    data=json.dumps(data),
                                    content_type="application/json",
                                    **self.extra)

        response = view(request)
        self.assertEquals(response.status_code, 201)
Exemple #4
0
    def test_widget_permission_create(self):

        alice_data = {'username': '******', 'email': '*****@*****.**'}
        self._login_user_and_profile(alice_data)

        view = WidgetViewSet.as_view({
            'post': 'create'
        })

        data = {
            'title': "Widget that",
            'content_object': 'http://testserver/api/v1/forms/%s' %
                              self.xform.pk,
            'description': "Test widget",
            'aggregation': "Sum",
            'widget_type': "charts",
            'view_type': "horizontal-bar",
            'column': "age",
            'group_by': ''
        }

        # to do: test random user with auth but no perms
        request = self.factory.post('/', data=json.dumps(data),
                                    content_type="application/json",
                                    **self.extra)
        response = view(request)
        self.assertEquals(response.status_code, 400)

        # owner
        OwnerRole.add(self.user, self.project)
        request = self.factory.post('/', data=json.dumps(data),
                                    content_type="application/json",
                                    **self.extra)
        response = view(request)
        self.assertEquals(response.status_code, 201)

        # readonly
        ReadOnlyRole.add(self.user, self.project)
        request = self.factory.post('/', data=json.dumps(data),
                                    content_type="application/json",
                                    **self.extra)
        response = view(request)
        self.assertEquals(response.status_code, 201)

        # dataentryonlyrole
        DataEntryOnlyRole.add(self.user, self.project)
        request = self.factory.post('/', data=json.dumps(data),
                                    content_type="application/json",
                                    **self.extra)

        response = view(request)
        self.assertEquals(response.status_code, 201)
    def _create_widget(self, data=None, group_by=''):
        view = WidgetViewSet.as_view({'post': 'create'})

        if not data:
            data = {
                'title':
                "Widget that",
                'content_object':
                'http://testserver/api/v1/forms/%s' % self.xform.pk,
                'description':
                "Test widget",
                'aggregation':
                "Sum",
                'widget_type':
                "charts",
                'view_type':
                "horizontal-bar",
                'column':
                "age",
                'group_by':
                group_by
            }

        count = Widget.objects.all().count()

        request = self.factory.post('/',
                                    data=json.dumps(data),
                                    content_type="application/json",
                                    **self.extra)
        response = view(request)

        self.assertEquals(response.status_code, 201)
        self.assertEquals(count + 1, Widget.objects.all().count())

        self.widget = Widget.objects.all().order_by('pk').reverse()[0]

        self.assertEquals(response.data['id'], self.widget.id)
        self.assertEquals(response.data['title'], data.get('title'))
        self.assertEquals(response.data['content_object'],
                          data['content_object'])
        self.assertEquals(response.data['widget_type'], data['widget_type'])
        self.assertEquals(response.data['view_type'], data['view_type'])
        self.assertEquals(response.data['column'], data['column'])
        self.assertEquals(response.data['description'],
                          data.get('description'))
        self.assertEquals(response.data['group_by'], data.get('group_by'))
        self.assertEquals(response.data['aggregation'],
                          data.get('aggregation'))
        self.assertEquals(response.data['order'], self.widget.order)
        self.assertEquals(response.data['data'], [])
        self.assertEquals(response.data['metadata'], data.get('metadata', {}))
Exemple #6
0
    def test_filter_widgets_by_dataview(self):
        self._create_widget()
        self._publish_xls_form_to_project()

        data = {
            'content_object':
            'http://testserver/api/v1/dataviews/%s' % self.data_view.pk,
            'widget_type':
            "charts",
            'view_type':
            "horizontal-bar",
            'column':
            "_submitted_by",
        }

        self._create_widget(data=data)

        data = {
            'content_object':
            'http://testserver/api/v1/dataviews/%s' % self.data_view.pk,
            'widget_type':
            "charts",
            'view_type':
            "horizontal-bar",
            'column':
            "_submission_time",
        }

        self._create_widget(data)

        view = WidgetViewSet.as_view({
            'get': 'list',
        })

        data = {"dataview": self.data_view.pk}

        request = self.factory.get('/', data=data, **self.extra)
        response = view(request)

        self.assertEquals(response.status_code, 200)
        self.assertEquals(len(response.data), 2)

        data = {"dataview": "so_invalid"}

        request = self.factory.get('/', data=data, **self.extra)
        response = view(request)

        self.assertEquals(response.status_code, 400)
        self.assertEquals(response.data['detail'],
                          u"Invalid value for dataview %s." % "so_invalid")
Exemple #7
0
    def test_widget_with_nonexistance_key(self):
        self._create_widget()

        view = WidgetViewSet.as_view({
            'get': 'list',
        })

        data = {"key": "randomkeythatdoesnotexist"}

        self.extra = {}

        request = self.factory.get('/', data=data, **self.extra)
        response = view(request, pk=self.xform.pk)

        self.assertEqual(response.status_code, 404)
Exemple #8
0
    def test_filter_widgets_by_dataview(self):
        self._create_widget()
        self._publish_xls_form_to_project()

        data = {
            'content_object': 'http://testserver/api/v1/dataviews/%s' %
                              self.data_view.pk,
            'widget_type': "charts",
            'view_type': "horizontal-bar",
            'column': "_submitted_by",
        }

        self._create_widget(data=data)

        data = {
            'content_object': 'http://testserver/api/v1/dataviews/%s' %
                              self.data_view.pk,
            'widget_type': "charts",
            'view_type': "horizontal-bar",
            'column': "_submission_time",
        }

        self._create_widget(data)

        view = WidgetViewSet.as_view({
            'get': 'list',
        })

        data = {
            "dataview": self.data_view.pk
        }

        request = self.factory.get('/', data=data, **self.extra)
        response = view(request)

        self.assertEquals(response.status_code, 200)
        self.assertEquals(len(response.data), 2)

        data = {
            "dataview": "so_invalid"
        }

        request = self.factory.get('/', data=data, **self.extra)
        response = view(request)

        self.assertEquals(response.status_code, 400)
        self.assertEquals(response.data['detail'],
                          u"Invalid value for dataview %s." % "so_invalid")
Exemple #9
0
    def test_widget_create_by_org_admin(self):
        self.project.organization = self.organization.user
        self.project.save()
        chuck_data = {'username': '******', 'email': '*****@*****.**'}
        chuck_profile = self._create_user_profile(chuck_data)

        view = OrganizationProfileViewSet.as_view({'post': 'members'})

        data = {
            'username': chuck_profile.user.username,
            'role': OwnerRole.name
        }
        request = self.factory.post('/',
                                    data=json.dumps(data),
                                    content_type="application/json",
                                    **self.extra)

        response = view(request, user=self.organization.user.username)

        self.assertEqual(response.status_code, 201)

        owners_team = get_or_create_organization_owners_team(self.organization)
        self.assertIn(chuck_profile.user, owners_team.user_set.all())

        extra = {
            'HTTP_AUTHORIZATION': 'Token %s' % chuck_profile.user.auth_token
        }

        view = WidgetViewSet.as_view({'post': 'create'})

        data = {
            'content_object':
            'http://testserver/api/v1/dataviews/%s' % self.data_view.pk,
            'widget_type':
            "charts",
            'view_type':
            "horizontal-bar",
            'column':
            "_submission_time",
        }

        request = self.factory.post('/',
                                    data=json.dumps(data),
                                    content_type="application/json",
                                    **extra)
        response = view(request)

        self.assertEquals(response.status_code, 201)
Exemple #10
0
    def test_widget_with_nonexistance_key(self):
        self._create_widget()

        view = WidgetViewSet.as_view({
            'get': 'list',
        })

        data = {
            "key": "randomkeythatdoesnotexist"
        }

        self.extra = {}

        request = self.factory.get('/', data=data, **self.extra)
        response = view(request, pk=self.xform.pk)

        self.assertEqual(response.status_code, 404)
    def _create_widget(self, data=None, group_by=''):
        view = WidgetViewSet.as_view({
            'post': 'create'
        })

        if not data:
            data = {
                'title': "Widget that",
                'content_object': 'http://testserver/api/v1/forms/%s' %
                                  self.xform.pk,
                'description': "Test widget",
                'aggregation': "Sum",
                'widget_type': "charts",
                'view_type': "horizontal-bar",
                'column': "age",
                'group_by': group_by
            }

        count = Widget.objects.all().count()

        request = self.factory.post('/', data=json.dumps(data),
                                    content_type="application/json",
                                    **self.extra)
        response = view(request)

        self.assertEquals(response.status_code, 201)
        self.assertEquals(count + 1, Widget.objects.all().count())

        self.widget = Widget.objects.all().order_by('pk').reverse()[0]

        self.assertEquals(response.data['id'], self.widget.id)
        self.assertEquals(response.data['title'], data.get('title'))
        self.assertEquals(response.data['content_object'],
                          data['content_object'])
        self.assertEquals(response.data['widget_type'], data['widget_type'])
        self.assertEquals(response.data['view_type'], data['view_type'])
        self.assertEquals(response.data['column'], data['column'])
        self.assertEquals(response.data['description'],
                          data.get('description'))
        self.assertEquals(response.data['group_by'], data.get('group_by'))
        self.assertEquals(response.data['aggregation'],
                          data.get('aggregation'))
        self.assertEquals(response.data['order'], self.widget.order)
        self.assertEquals(response.data['data'], [])
        self.assertEquals(response.data['metadata'], data.get('metadata', {}))
Exemple #12
0
    def test_widget_with_key(self):
        self._create_widget()

        view = WidgetViewSet.as_view({
            'get': 'list',
        })

        data = {"key": self.widget.key}

        request = self.factory.get('/', data=data, **self.extra)
        response = view(request, formid=self.xform.pk)

        self.assertEqual(response.status_code, 200)
        self.assertIsNotNone(response.data.get('data'))
        self.assertIn('data', response.data.get('data'))
        self.assertEquals(len(response.data.get('data')['data']), 8)
        self.assertIn('age', response.data.get('data')['data'][0])
        self.assertIn('count', response.data.get('data')['data'][0])
Exemple #13
0
    def test_widget_create_by_org_admin(self):
        self.project.organization = self.organization.user
        self.project.save()
        chuck_data = {'username': '******', 'email': '*****@*****.**'}
        chuck_profile = self._create_user_profile(chuck_data)

        view = OrganizationProfileViewSet.as_view({
            'post': 'members'
        })

        data = {'username': chuck_profile.user.username,
                'role': OwnerRole.name}
        request = self.factory.post(
            '/', data=json.dumps(data),
            content_type="application/json", **self.extra)

        response = view(request, user=self.organization.user.username)

        self.assertEqual(response.status_code, 201)

        owners_team = get_organization_owners_team(self.organization)
        self.assertIn(chuck_profile.user, owners_team.user_set.all())

        extra = {
            'HTTP_AUTHORIZATION': 'Token %s' % chuck_profile.user.auth_token}

        view = WidgetViewSet.as_view({
            'post': 'create'
        })

        data = {
            'content_object': 'http://testserver/api/v1/dataviews/%s' %
                              self.data_view.pk,
            'widget_type': "charts",
            'view_type': "horizontal-bar",
            'column': "_submission_time",
        }

        request = self.factory.post('/', data=json.dumps(data),
                                    content_type="application/json",
                                    **extra)
        response = view(request)

        self.assertEquals(response.status_code, 201)
Exemple #14
0
    def test_widget_with_key(self):
        self._create_widget()

        view = WidgetViewSet.as_view({
            'get': 'list',
        })

        data = {
            "key": self.widget.key
        }

        request = self.factory.get('/', data=data, **self.extra)
        response = view(request, formid=self.xform.pk)

        self.assertEqual(response.status_code, 200)
        self.assertIsNotNone(response.data.get('data'))
        self.assertIn('data', response.data.get('data'))
        self.assertEquals(len(response.data.get('data')['data']), 8)
        self.assertIn('age', response.data.get('data')['data'][0])
        self.assertIn('count', response.data.get('data')['data'][0])
Exemple #15
0
    def setUp(self):
        super(self.__class__, self).setUp()
        xlsform_path = os.path.join(settings.PROJECT_ROOT, 'libs', 'tests',
                                    "utils", "fixtures", "tutorial.xls")
        self._org_create()
        self._publish_xls_form_to_project(xlsform_path=xlsform_path)
        for x in range(1, 9):
            path = os.path.join(settings.PROJECT_ROOT, 'libs', 'tests',
                                "utils", 'fixtures', 'tutorial', 'instances',
                                'uuid{}'.format(x), 'submission.xml')
            self._make_submission(path)
            x += 1
        self._create_dataview()

        self.view = WidgetViewSet.as_view({
            'post': 'create',
            'put': 'update',
            'patch': 'partial_update',
            'delete': 'destroy',
            'get': 'retrieve',
        })
Exemple #16
0
    def setUp(self):
        super(self.__class__, self).setUp()
        xlsform_path = os.path.join(
            settings.PROJECT_ROOT, 'libs', 'tests', "utils", "fixtures",
            "tutorial.xls")
        self._org_create()
        self._publish_xls_form_to_project(xlsform_path=xlsform_path)
        for x in range(1, 9):
            path = os.path.join(
                settings.PROJECT_ROOT, 'libs', 'tests', "utils", 'fixtures',
                'tutorial', 'instances', 'uuid{}'.format(x), 'submission.xml')
            self._make_submission(path)
            x += 1
        self._create_dataview()

        self.view = WidgetViewSet.as_view({
            'post': 'create',
            'put': 'update',
            'patch': 'partial_update',
            'delete': 'destroy',
            'get': 'retrieve',
        })
Exemple #17
0
    def test_widget_data_public_form(self):
        self._create_widget()

        view = WidgetViewSet.as_view({
            'get': 'list',
        })
        self.extra = {}

        request = self.factory.get('/', **self.extra)
        response = view(request)

        self.assertEqual(response.status_code, 200)
        self.assertEquals(len(response.data), 0)

        # Anonymous user can access widget in public form
        self.xform.shared_data = True
        self.xform.save()

        request = self.factory.get('/', **self.extra)
        response = view(request, formid=self.xform.pk)

        self.assertEqual(response.status_code, 200)
        self.assertEquals(len(response.data), 1)
Exemple #18
0
    def test_widget_data_public_form(self):
        self._create_widget()

        view = WidgetViewSet.as_view({
            'get': 'list',
        })
        self.extra = {}

        request = self.factory.get('/', **self.extra)
        response = view(request)

        self.assertEqual(response.status_code, 200)
        self.assertEquals(len(response.data), 0)

        # Anonymous user can access widget in public form
        self.xform.shared_data = True
        self.xform.save()

        request = self.factory.get('/', **self.extra)
        response = view(request, formid=self.xform.pk)

        self.assertEqual(response.status_code, 200)
        self.assertEquals(len(response.data), 1)
Exemple #19
0
    def test_widget_permission_list(self):
        self._create_widget()

        alice_data = {'username': '******', 'email': '*****@*****.**'}
        self._login_user_and_profile(alice_data)

        view = WidgetViewSet.as_view({
            'get': 'list',
        })

        request = self.factory.get('/', **self.extra)
        response = view(request)

        self.assertEquals(response.status_code, 200)
        self.assertEquals(len(response.data), 0)

        # assign alice the perms
        ReadOnlyRole.add(self.user, self.xform)

        request = self.factory.get('/', **self.extra)
        response = view(request)

        self.assertEquals(response.status_code, 200)
        self.assertEquals(len(response.data), 1)
Exemple #20
0
    def test_widget_permission_list(self):
        self._create_widget()

        alice_data = {'username': '******', 'email': '*****@*****.**'}
        self._login_user_and_profile(alice_data)

        view = WidgetViewSet.as_view({
            'get': 'list',
        })

        request = self.factory.get('/', **self.extra)
        response = view(request)

        self.assertEquals(response.status_code, 200)
        self.assertEquals(len(response.data), 0)

        # assign alice the perms
        ReadOnlyRole.add(self.user, self.xform)

        request = self.factory.get('/', **self.extra)
        response = view(request)

        self.assertEquals(response.status_code, 200)
        self.assertEquals(len(response.data), 1)