コード例 #1
0
class FormExportTest(TestCase):
    def setUp(self):
        self.app_id = 'kasdlfkjsldfkjsdlkjf'
        self.domain_name = 'form-export-test'
        self.domain = create_domain(self.domain_name)
        self.username = '******'
        self.couch_user = CommCareUser.create(self.domain_name, self.username,
                                              password='******')
        self.couch_user.save()
        self.client = Client()
        self.client.login(username=self.couch_user.username, password='******')
        self.url = reverse("receiver_post_with_app_id",
                           args=[self.domain_name, self.app_id])

        def post_it():
            f = StringIO(XML_DATA)
            f.name = 'form.xml'
            response = self.client.post(self.url, {'xml_submission_file': f})
        self.form1 = post_it()
        self.form2 = post_it()

        self.custom_export = FormExportSchema.wrap({
            'type': 'form',
            'app_id': self.app_id,
            'default_format': Format.JSON,
            'index': json.dumps([self.domain_name, XMLNS]),
            'tables': [{
                'index': '#',
                'display': 'Export',
                'columns': [{'index': 'form.name', 'display': 'Name'}],
            }]
        })

    def test_include_duplicates(self):

        self.custom_export.include_errors = True
        tmp, _ = self.custom_export.get_export_files()
        data = tmp.getvalue()
        data = json.loads(data)
        self.assertEqual(data['Export']['headers'], ['Name'])
        self.assertEqual(len(data['Export']['rows']), 2)

        self.custom_export.include_errors = False
        tmp, _ = self.custom_export.get_export_files()
        data = tmp.getvalue()
        data = json.loads(data)
        self.assertEqual(data['Export']['headers'], ['Name'])
        self.assertEqual(len(data['Export']['rows']), 1)
コード例 #2
0
    def test_pages(self):
        """
        Confirm that all the groups/locations/users appear on the correct pages
        """
        client = Client()
        client.login(username=self.username, password=self.password)

        # expected_id_sets is a list of sets.
        # expected_id_sets is constructed such that
        # For option with index x yielded by the view:
        #   the option's id should be in expected_ids[x]
        expected_id_sets = [{"user_location"}, {"user_parent_location"}]

        for i in self.groups:
            expected_id_sets.append(self.group_ids)
        for i in self.locations:
            expected_id_sets.append(self.location_ids)
        for i in self.users:
            expected_id_sets.append(self.user_ids)

        page_size = 3  # using a small number because more pages will hopefully be more likely to reveal bugs
        expected_num_pages = int(
            math.ceil(len(expected_id_sets) / float(page_size)))
        for i in range(expected_num_pages):
            page = i + 1
            response = client.get(reverse(CallCenterOwnerOptionsView.url_name,
                                          args=[self.domain.name]),
                                  data={
                                      "page": page,
                                      "page_limit": page_size,
                                      "q": ""
                                  })
            response_json = json.loads(response.content)
            self.assertEqual(response_json['total'], len(expected_id_sets))

            for item_index, item in enumerate(response_json['results']):
                id_ = item['id']
                option_index = ((page - 1) * page_size) + item_index
                self.assertTrue(
                    id_ in expected_id_sets[option_index],
                    "Unexpected item {} at index {}.".format(
                        item, option_index))
コード例 #3
0
    def test_SMS_API_Users_not_shown_on_user_list_page(self):
        client = Client()
        client.login(username=DEFAULT_TEST_USER,
                     password=DEFAULT_TEST_PASSWORD)
        request = HttpRequest()
        request.method = 'GET'
        request.user = User.objects.get(username="******")

        with patch('datawinners.accountmanagement.views.User') as user_class:
            with patch('datawinners.accountmanagement.views.RequestContext'
                       ) as context:
                with patch(
                        "datawinners.accountmanagement.views.render_to_response"
                ) as render_response_patch:
                    objects = Mock()
                    type(user_class).objects = PropertyMock(
                        return_value=objects)
                    users(request)
                    objects.exclude.assert_called_once_with(
                        groups__name__in=['Data Senders', 'SMS API Users'])
コード例 #4
0
    def test_pages(self):
        """
        Confirm that all the groups/locations/users appear on the correct pages
        """
        client = Client()
        client.login(username=self.username, password=self.password)

        # expected_id_sets is a list of sets.
        # expected_id_sets is constructed such that
        # For option with index x yielded by the view:
        #   the option's id should be in expected_ids[x]
        expected_id_sets = [{"user_location"}, {"user_parent_location"}]

        for i in self.groups:
            expected_id_sets.append(self.group_ids)
        for i in self.locations:
            expected_id_sets.append(self.location_ids)
        for i in self.users:
            expected_id_sets.append(self.user_ids)

        page_size = 3  # using a small number because more pages will hopefully be more likely to reveal bugs
        expected_num_pages = int(math.ceil(len(expected_id_sets) / float(page_size)))
        for i in range(expected_num_pages):
            page = i + 1
            response = client.get(reverse(
                CallCenterOwnerOptionsView.url_name, args=[self.domain.name]),
                data={"page": page, "page_limit": page_size, "q": ""}
            )
            response_json = json.loads(response.content)
            self.assertEqual(response_json['total'], len(expected_id_sets))

            for item_index, item in enumerate(response_json['results']):
                id_ = item['id']
                option_index = ((page - 1) * page_size) + item_index
                self.assertTrue(
                    id_ in expected_id_sets[option_index],
                    "Unexpected item {} at index {}.".format(item, option_index)
                )
コード例 #5
0
class FormExportTest(TestCase):
    def setUp(self):
        self.app_id = 'kasdlfkjsldfkjsdlkjf'
        self.domain_name = 'form-export-test'
        self.domain = create_domain(self.domain_name)
        self.username = '******'
        self.couch_user = CommCareUser.create(self.domain_name, self.username,
                                              password='******')
        self.couch_user.save()
        self.client = Client()
        self.client.login(username=self.couch_user.username, password='******')
        self.url = reverse("receiver_post_with_app_id",
                           args=[self.domain_name, self.app_id])
        self.custom_export = FormExportSchema.wrap({
            'type': 'form',
            'app_id': self.app_id,
            'default_format': Format.JSON,
            'index': json.dumps([self.domain_name, XMLNS]),
            'tables': [{
                'index': '#',
                'display': 'Export',
                'columns': [{'index': 'form.name', 'display': 'Name'}],
            }]
        })

    def tearDown(self):
        self.couch_user.delete()

    def post_it(self, user_id=None, form_id=XFORM_ID):
        user_id = user_id or self.couch_user._id
        f = StringIO(XML_DATA.format(
            user_id=user_id,
            xmlns=XMLNS,
            xform_id=form_id,
        ))
        f.name = 'form.xml'
        return self.client.post(self.url, {'xml_submission_file': f})

    def test_include_duplicates(self):
        self.post_it()
        self.post_it()

        self.custom_export.include_errors = True
        files = self.custom_export.get_export_files()
        data = json.loads(files.file.payload)
        self.assertEqual(data['Export']['headers'], ['Name'])
        self.assertEqual(len(data['Export']['rows']), 2)

        self.custom_export.include_errors = False
        files = self.custom_export.get_export_files()
        data = json.loads(files.file.payload)
        self.assertEqual(data['Export']['headers'], ['Name'])
        self.assertEqual(len(data['Export']['rows']), 1)

    def test_exclude_unknown_users(self):
        self.post_it(form_id='good', user_id=self.couch_user._id)
        files = self.custom_export.get_export_files()
        data = json.loads(files.file.payload)
        self.assertEqual(len(data['Export']['rows']), 1)

        # posting from a non-real user shouldn't update
        self.post_it(form_id='bad', user_id='notarealuser')
        files = self.custom_export.get_export_files()
        data = json.loads(files.file.payload)
        self.assertEqual(len(data['Export']['rows']), 1)

        # posting from the real user should update
        self.post_it(form_id='stillgood', user_id=self.couch_user._id)
        files = self.custom_export.get_export_files()
        data = json.loads(files.file.payload)
        self.assertEqual(len(data['Export']['rows']), 2)
コード例 #6
0
ファイル: test_form_export.py プロジェクト: zbidi/commcare-hq
class FormExportTest(TestCase):
    def setUp(self):
        super(FormExportTest, self).setUp()
        self.app_id = 'kasdlfkjsldfkjsdlkjf'
        self.domain_name = 'form-export-test'
        self.domain = create_domain(self.domain_name)
        self.username = '******'
        self.couch_user = CommCareUser.create(self.domain_name,
                                              self.username,
                                              password='******')
        self.couch_user.save()
        self.client = Client()
        self.client.login(username=self.couch_user.username, password='******')
        self.url = reverse("receiver_post_with_app_id",
                           args=[self.domain_name, self.app_id])
        self.custom_export = FormExportSchema.wrap({
            'type':
            'form',
            'app_id':
            self.app_id,
            'default_format':
            Format.JSON,
            'index':
            json.dumps([self.domain_name, XMLNS]),
            'tables': [{
                'index': '#',
                'display': 'Export',
                'columns': [{
                    'index': 'form.name',
                    'display': 'Name'
                }],
            }]
        })

    def tearDown(self):
        self.couch_user.delete()
        super(FormExportTest, self).tearDown()

    def post_it(self, user_id=None, form_id=XFORM_ID):
        user_id = user_id or self.couch_user._id
        f = StringIO(
            XML_DATA.format(
                user_id=user_id,
                xmlns=XMLNS,
                xform_id=form_id,
            ))
        f.name = 'form.xml'
        return self.client.post(self.url, {'xml_submission_file': f})

    def test_include_duplicates(self):
        self.post_it()
        self.post_it()

        self.custom_export.include_errors = True
        files = self.custom_export.get_export_files()
        data = json.loads(files.file.payload)
        self.assertEqual(data['Export']['headers'], ['Name'])
        self.assertEqual(len(data['Export']['rows']), 2)

        self.custom_export.include_errors = False
        files = self.custom_export.get_export_files()
        data = json.loads(files.file.payload)
        self.assertEqual(data['Export']['headers'], ['Name'])
        self.assertEqual(len(data['Export']['rows']), 1)

    def test_exclude_unknown_users(self):
        self.post_it(form_id='good', user_id=self.couch_user._id)
        files = self.custom_export.get_export_files()
        data = json.loads(files.file.payload)
        self.assertEqual(len(data['Export']['rows']), 1)

        # posting from a non-real user shouldn't update
        self.post_it(form_id='bad', user_id='notarealuser')
        files = self.custom_export.get_export_files()
        data = json.loads(files.file.payload)
        self.assertEqual(len(data['Export']['rows']), 1)

        # posting from the real user should update
        self.post_it(form_id='stillgood', user_id=self.couch_user._id)
        files = self.custom_export.get_export_files()
        data = json.loads(files.file.payload)
        self.assertEqual(len(data['Export']['rows']), 2)
コード例 #7
0
class FormExportTest(TestCase):
    def setUp(self):
        self.app_id = "kasdlfkjsldfkjsdlkjf"
        self.domain_name = "form-export-test"
        self.domain = create_domain(self.domain_name)
        self.username = "******"
        self.couch_user = CommCareUser.create(self.domain_name, self.username, password="******")
        self.couch_user.save()
        self.client = Client()
        self.client.login(username=self.couch_user.username, password="******")
        self.url = reverse("receiver_post_with_app_id", args=[self.domain_name, self.app_id])
        self.custom_export = FormExportSchema.wrap(
            {
                "type": "form",
                "app_id": self.app_id,
                "default_format": Format.JSON,
                "index": json.dumps([self.domain_name, XMLNS]),
                "tables": [{"index": "#", "display": "Export", "columns": [{"index": "form.name", "display": "Name"}]}],
            }
        )

    def tearDown(self):
        self.couch_user.delete()

    def post_it(self, user_id=None, form_id=XFORM_ID):
        user_id = user_id or self.couch_user._id
        f = StringIO(XML_DATA.format(user_id=user_id, xmlns=XMLNS, xform_id=form_id))
        f.name = "form.xml"
        return self.client.post(self.url, {"xml_submission_file": f})

    def test_include_duplicates(self):
        self.post_it()
        self.post_it()

        self.custom_export.include_errors = True
        tmp, _ = self.custom_export.get_export_files()
        data = json.loads(tmp.getvalue())
        self.assertEqual(data["Export"]["headers"], ["Name"])
        self.assertEqual(len(data["Export"]["rows"]), 2)

        self.custom_export.include_errors = False
        tmp, _ = self.custom_export.get_export_files()
        data = json.loads(tmp.getvalue())
        self.assertEqual(data["Export"]["headers"], ["Name"])
        self.assertEqual(len(data["Export"]["rows"]), 1)

    def test_exclude_unknown_users(self):
        self.post_it(form_id="good", user_id=self.couch_user._id)
        tmp, _ = self.custom_export.get_export_files()
        data = json.loads(tmp.getvalue())
        self.assertEqual(len(data["Export"]["rows"]), 1)

        # posting from a non-real user shouldn't update
        self.post_it(form_id="bad", user_id="notarealuser")
        tmp, _ = self.custom_export.get_export_files()
        data = json.loads(tmp.getvalue())
        self.assertEqual(len(data["Export"]["rows"]), 1)

        # posting from the real user should update
        self.post_it(form_id="stillgood", user_id=self.couch_user._id)
        tmp, _ = self.custom_export.get_export_files()
        data = json.loads(tmp.getvalue())
        self.assertEqual(len(data["Export"]["rows"]), 2)