def test_files_are_converted_to_urls(self): form_instance = FormInstanceFactory.create(form_json=[{ "schema": { "slug": "first", "properties": { "name": { "type": "string" }, "picture": { "type": "string", "format": "data-url" }, } } }]) FormResponseFactory.create(form_instance=form_instance, json={ "name": "Katharina", "picture": "data-url...." }) form_instance.form.investigation.add_user(self.investigation_owner, "O") self.client.force_login(self.investigation_owner) response = self.client.get(make_url(form_instance)) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 1) self.assertEqual(response.data[0]["json"]["name"], "Katharina") # check that data-url has been converted to a real url for file download self.assertTrue( response.data[0]["json"]["picture"].endswith("/files/picture"))
def test_create_form_csv(self): buffer = StringIO() build_absolute_uri = lambda x: "http://example.com" form_instance = FormInstance.objects.create(form=self.form, form_json=[{ "schema": { "slug": "first", "properties": { "name": { "type": "string" }, "email": { "type": "string" } } } }]) response_1 = FormResponseFactory.create( form_instance=form_instance, submission_date=datetime(2018, 1, 1, tzinfo=pytz.utc), json={ "name": "Peter", "email": "*****@*****.**" }) response_2 = FormResponseFactory.create(form_instance=form_instance, submission_date=datetime( 2018, 1, 2, tzinfo=pytz.utc), json={ "name": "Katharina", "email": "*****@*****.**" }) create_form_csv(self.form, self.investigation.slug, build_absolute_uri, buffer) lines = buffer.getvalue().split('\n') header = lines[0].strip() expected_header = "email,meta_comments,meta_id,meta_status,meta_submission_date,meta_tags,meta_url,meta_version,name" self.assertEquals(header, expected_header) first = lines[1].strip() expected_first = "[email protected],,{},Inbox,2018-01-01 00:00:00+00:00,,http://example.com,0,Peter".format( response_1.id) self.assertEquals(first, expected_first) second = lines[2].strip() expected_second = "[email protected],,{},Inbox,2018-01-02 00:00:00+00:00,,http://example.com,0,Katharina".format( response_2.id) self.assertEquals(second, expected_second)
def setUp(self): self.form_instance = FormInstanceFactory.create() for index, status in enumerate(["V", "V", "S", "S"]): json = {"has_car": True} if index % 2 == 0 else {} FormResponseFactory.create(status=status, form_instance=self.form_instance, json=json) User.objects.create_superuser('*****@*****.**', 'password') self.client = Client() self.client.login(email='*****@*****.**', password='******')
def test_email_filters(self, *args): form = self.form_instance.form edwards_response = FormResponseFactory.create(form_instance=self.form_instance, json={"email": "*****@*****.**"}) FormResponseFactory.create(form_instance=self.form_instance, json={"email": "*****@*****.**"}) response = self.client.get( "/forms/admin/investigations/{}/forms/{}/responses/[email protected]".format(form.investigation.slug, form.slug)) self.assertListEqual( list(response.context_data["formresponse_list"].all()), [edwards_response] )
def setUp(self): self.investigation_owner = UserFactory.create() self.investigation = InvestigationFactory.create() self.form_instance = FormInstanceFactory.create() self.form_instance.form.investigation.add_user( self.investigation_owner, "O") for i in range(5): FormResponseFactory.create(form_instance=self.form_instance, status="S") for i in range(5): FormResponseFactory.create(form_instance=self.form_instance, status="V")
def setUp(self): self.user = UserFactory.create() self.form_instance = FormInstanceFactory.create() self.form_response1 = FormResponseFactory.create(form_instance=self.form_instance) self.form_response2 = FormResponseFactory.create(form_instance=self.form_instance) self.comment1 = CommentFactory.create(author=self.user, text="bla", date=timezone.datetime(2018, 1, 12, tzinfo=pytz.utc), form_response=self.form_response1) self.comment2 = CommentFactory.create(author=self.user, text="blup", date=timezone.datetime(2018, 1, 15, tzinfo=pytz.utc), form_response=self.form_response1) User.objects.create_superuser('*****@*****.**', 'password') self.client.login(email='*****@*****.**', password='******')
def setUp(self): form_instance = FormInstanceFactory.create() response_data = [(timezone.datetime(2018, 1, 2, tzinfo=pytz.utc), "S"), (timezone.datetime(2018, 1, 5, tzinfo=pytz.utc), "S"), (timezone.datetime(2018, 1, 1, tzinfo=pytz.utc), "I"), (timezone.datetime(2018, 1, 1, tzinfo=pytz.utc), "V"), (timezone.datetime(2018, 1, 1, tzinfo=pytz.utc), "V")] for date, status in response_data: FormResponseFactory.create(submission_date=date, form_instance=form_instance, status=status) self.form = form_instance.form
def setUp(self): form_instance = FormInstanceFactory.create() submission_data = [ (make_date(1), "S"), (make_date(2), "V"), (make_date(2), "V"), (make_date(5), "I"), ] for (date, status) in submission_data: FormResponseFactory.create(submission_date=date, status=status, form_instance=form_instance) self.investigation = form_instance.form.investigation
def test_assignee_filters(self, *args): form = self.form_instance.form user = UserFactory.create() form_response = FormResponseFactory.create(form_instance=self.form_instance) form_response.assignees.set([user]) # create another one to make sure that this one is not returned later FormResponseFactory.create(form_instance=self.form_instance) response = self.client.get( "/forms/admin/investigations/{}/forms/{}/responses/inbox?assignee={}".format(form.investigation.slug, form.slug, user.email)) self.assertListEqual( list(response.context_data["formresponse_list"].all()), [form_response] )
def test_priority_order_rendered_fields(self): form_json = [{"schema": { "name": "Step 1", "slug": "step-1", "properties": { "firstname": { "type": "string" }, "lastname": { "type": "string" }, "email": { "type": "string" }, "city": { "type": "string" } } } }] form_instance = FormInstanceFactory.create(priority_fields=["email", "city"], form_json=form_json) response_json = {"firstname": "nerdy", "lastname": "mcnerdface", "email": "*****@*****.**", "city": "Buxtehude"} response = FormResponseFactory.create(form_instance=form_instance, json=response_json) output_fields = list(response.rendered_fields()) self.assertEqual(output_fields[0]["json_name"], "email") self.assertEqual(output_fields[1]["json_name"], "city")
def test_404_when_not_existing(self): form_response = FormResponseFactory.create() url = url_for_response(form_response) # got URL but response will be deleted, so should 404 later form_response.delete() response = self.client.get(url) self.assertEqual(response.status_code, 404)
def test_tag_in_investigation(self): investigation = InvestigationFactory.create() tag = TagFactory.create(investigation=investigation) form_response = FormResponseFactory.create() self.assertNotEqual(form_response.form_instance.form.investigation, investigation) self.assertNotIn(tag, form_response.taglist)
def test_file_download_fails_if_not_logged_in(self): form_response = FormResponseFactory.create( json={"file_field": ["data:image/png;base64,abc123"]}, form_instance=self.form_instance) response = self.client.get( "/forms/admin/investigations/{}/forms/{}/responses/{}/files/file_field/2" .format(self.investigation.slug, self.form.slug, form_response.id)) self.assertEquals(response.status_code, 302) self.assertTrue(response.url.startswith("/admin/login"))
def setUp(self): self.admin_user = UserFactory.create() self.response = FormResponseFactory.create() admin_user_group = UserGroup.objects.filter( investigation=self.response.form_instance.form.investigation, role=INVESTIGATION_ROLES.ADMIN).first() admin_user_group.group.user_set.add(self.admin_user) self.client.force_login(self.admin_user)
def test_create_form_csv_file_filter_json(self): buffer = StringIO() build_absolute_uri = lambda x: "https://example.com{}".format(x) form_instance = FormInstance.objects.create(form=self.form, form_json=[{ "schema": { "slug": "first", "properties": { "name": { "type": "string" }, "picture": { "type": "string", "format": "data-url" }, } } }]) FormResponseFactory.create(form_instance=form_instance, json={ "name": "Katharina", "picture": "data-url...." }) FormResponseFactory.create(form_instance=form_instance, status="I", json={ "name": "Peter", }) create_form_csv(form_instance.form, form_instance.form.investigation.slug, build_absolute_uri, buffer, {"json__picture__isnull": False}) lines = [ line for line in buffer.getvalue().split('\r\n') if line != "" ] self.assertEqual(len(lines), 2)
def test_create_form_csv_file(self): buffer = StringIO() build_absolute_uri = lambda x: "https://example.com{}".format(x) form_instance = FormInstance.objects.create(form=self.form, form_json=[{ "schema": { "slug": "first", "properties": { "name": { "type": "string" }, "picture": { "type": "string", "format": "data-url" }, } } }]) response = FormResponseFactory.create(form_instance=form_instance, submission_date=datetime( 2018, 1, 2, tzinfo=pytz.utc), json={ "name": "Katharina", "picture": "data-url...." }) create_form_csv(form_instance.form, form_instance.form.investigation.slug, build_absolute_uri, buffer) lines = buffer.getvalue().split('\n') header = lines[0].strip() expected_header = "meta_comments,meta_id,meta_status,meta_submission_date,meta_tags,meta_url,meta_version,name,picture" self.assertEquals(header, expected_header) first = lines[1].strip() expected_first = ",".join([ "", str(response.id), "Inbox", "2018-01-02 00:00:00+00:00", "", # no tags "https://example.com/forms/admin/investigations/first-investigation/forms/first-form/responses/{}" .format(response.id), "0", "Katharina", "https://example.com/forms/admin/investigations/first-investigation/forms/first-form/responses/{}/files/picture" .format(response.id) ]) self.assertEquals(first, expected_first)
def test_tag_filters(self, *args): form = self.form_instance.form tag = TagFactory.create(investigation=form.investigation, name="Avocado") form_response = FormResponseFactory.create(form_instance=self.form_instance) form_response.tags.set([tag]) response = self.client.get( "/forms/admin/investigations/{}/forms/{}/responses/inbox?tag={}".format(form.investigation.slug, form.slug, tag.id)) self.assertListEqual( list(response.context_data["formresponse_list"].all()), [form_response] )
def test_edit_non_existing_field(self): properties = {"name": {"type": "string"}} form_instance = self.make_singe_step_instance(properties) form_response = FormResponseFactory.create(json={"name": "Gödel"}, form_instance=form_instance) response = self.client.post(url_for_response(form_response), data={"json__color": "Red"}) self.assertEqual(response.status_code, 400) updated_response = FormResponse.objects.get(id=form_response.id) self.assertDictEqual(updated_response.json, {"name": "Gödel"})
def test_edit(self): form_instance = self.make_singe_step_instance( {"name": { "type": "string" }}) form_response = FormResponseFactory.create(json={"name": "Gödel"}, form_instance=form_instance) response = self.client.post(url_for_response(form_response), data={"json__name": "Escher"}) self.assertEqual(response.status_code, 302) updated_response = FormResponse.objects.get(id=form_response.id) self.assertEqual(updated_response.json["name"], "Escher")
def test_file_download_fails_for_wrong_user(self): other_owner = UserFactory.create() other_investigation = InvestigationFactory.create() other_investigation.add_user(other_owner, INVESTIGATION_ROLES.OWNER) form_response = FormResponseFactory.create( json={"file_field": ["data:image/png;base64,abc123"]}, form_instance=self.form_instance) self.client.force_login(other_owner) response = self.client.get( "/forms/admin/investigations/{}/forms/{}/responses/{}/files/file_field/2" .format(self.investigation.slug, self.form.slug, form_response.id)) self.assertEquals(response.status_code, 403)
def test_file_download(self, mock_get_file_data): form_response = FormResponseFactory.create( json={"file_field": "data:image/png;base64,abc123"}, form_instance=self.form_instance) self.client.force_login(self.owner) response = self.client.get( "/forms/admin/investigations/{}/forms/{}/responses/{}/files/file_field" .format(self.investigation.slug, self.form.slug, form_response.id)) self.assertEquals(response.status_code, 200) expected_content_disposition = 'inline; filename="{}-filename.png"'.format( form_response.id) self.assertEquals(response['Content-Disposition'], expected_content_disposition)
def test_with_json_schema(self): properties = {"email": {"type": "string", "format": "email"}} form_instance = self.make_singe_step_instance(properties) form_response = FormResponseFactory.create( json={"email": "*****@*****.**"}, form_instance=form_instance) response = self.client.post( url_for_response(form_response), data={"json__email": "not an email address"}) self.assertEqual(response.status_code, 400) self.assertEqual(response.content, b"'not an email address' is not a 'email'") updated_response = FormResponse.objects.get(id=form_response.id) self.assertDictEqual(updated_response.json, {"email": "*****@*****.**"})
def test_file_download_multiple_off_by_one(self): form_response = FormResponseFactory.create( json={ "file_field": [ "data:image/png;base64,abc123", "data:image/png;base64,abc123" ] }, form_instance=self.form_instance) self.client.force_login(self.owner) response = self.client.get( "/forms/admin/investigations/{}/forms/{}/responses/{}/files/file_field/2" .format(self.investigation.slug, self.form.slug, form_response.id)) self.assertEquals( response.status_code, 404, "should return 404 if user is trying to get file 3 but there are only 2" )
def setUp(self): form_instances = [FormInstanceFactory.create() for _ in range(2)] investigations = [instance.form.investigation for instance in form_instances] responses = [[], []] for index, instance in enumerate(form_instances): for _ in range(5): response = FormResponseFactory.create(form_instance=instance) responses[index].append(response) user = UserFactory.create() self.investigation = investigations[0] self.investigation.add_user(user, INVESTIGATION_ROLES.EDITOR) self.responses = responses self.admin_user = user self.client.force_login(self.admin_user)
def test_edit_fails_for_viewer(self): form_instance = self.make_singe_step_instance( {"name": { "type": "string" }}) form_response = FormResponseFactory.create(json={"name": "Gödel"}, form_instance=form_instance) viewer = UserFactory.create() self.investigation.add_user(viewer, "V") self.client.force_login(viewer) response = self.client.post(url_for_response(form_response), data={"json__name": "Escher"}) self.assertEqual(response.status_code, 403) updated_response = FormResponse.objects.get(id=form_response.id) self.assertEqual(updated_response.json["name"], "Gödel")
def setUp(self): self.response = FormResponseFactory.create() form_instance = self.response.form_instance form_instance.form_json = [{"schema": { "name": "Step 1", "slug": "step-1", "properties": { "files": { "type": "array", "items": { "type": "string", "format": "data-url" } } } } }] form_instance.save()
def test_tag_in_response(self): tag = TagFactory.create() form_response = FormResponseFactory.create() form_response.tags.set([tag]) self.assertEqual(list(form_response.tags.all()), [tag])
def setUp(self): self.form_response_1 = FormResponseFactory.create() self.form_response_2 = FormResponseFactory.create() User.objects.create_superuser('*****@*****.**', 'password')
def setUp(self): User.objects.create_superuser('*****@*****.**', 'password') self.client = Client() self.client.login(email='*****@*****.**', password='******') self.response = FormResponseFactory.create()