def test_assign_tags_multiple(self): responses = self.responses[0] form = responses[0].form_instance.form investigation = responses[0].form_instance.form.investigation tag = TagFactory.create(name='Pasta', investigation=investigation) payload = { "selected_responses": [responses[3].id, responses[4].id], "tag": tag.id } self.client.post(make_url(form), data=payload) self.assertQuerysetEqual(responses[0].tags.all(), []) self.assertQuerysetEqual(responses[1].tags.all(), []) self.assertQuerysetEqual(responses[2].tags.all(), []) self.assertQuerysetEqual(responses[3].tags.all(), ['<Tag: Pasta>']) self.assertQuerysetEqual(responses[4].tags.all(), ['<Tag: Pasta>']) payload = { "selected_responses": [responses[1].id, responses[3].id], "tag": "clear_tags" } self.client.post(make_url(form), data=payload) self.assertQuerysetEqual(responses[0].tags.all(), []) self.assertQuerysetEqual(responses[1].tags.all(), []) self.assertQuerysetEqual(responses[2].tags.all(), []) self.assertQuerysetEqual(responses[3].tags.all(), []) self.assertQuerysetEqual(responses[4].tags.all(), ['<Tag: Pasta>'])
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_list_with_results(self): tag = TagFactory.create() self.investigation.tag_set.add(tag) other_investigation = InvestigationFactory.create() other_tag = TagFactory.create() other_investigation.tag_set.add(other_tag) self.client.force_login(self.investigation_owner) response = self.client.get(make_url(self.investigation)) self.assertEqual(response.status_code, 200) self.assertEqual(response.data, [{ "id": tag.id, "name": tag.name, "investigation": tag.investigation.id }])
def test_edit_tag(self): tag = TagFactory.create(name="Valdi") self.investigation.tag_set.add(tag) self.client.force_login(self.investigation_owner) response = self.client.patch(make_details_url(tag), {"name": "Valid"}) self.assertEqual(response.status_code, 200) updated_tag = Tag.objects.get(id=tag.id) self.assertEqual(updated_tag.name, "Valid")
def test_remove_tag(self): tag = TagFactory.create() self.investigation.tag_set.add(tag) self.client.force_login(self.investigation_owner) self.assertEqual(self.investigation.tag_set.count(), 1) response = self.client.delete(make_details_url(tag)) self.assertEqual(response.status_code, 204) self.assertEqual(self.investigation.tag_set.count(), 0)
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_remove_tag_fails_if_unauthorized(self): other_user = UserFactory.create() tag = TagFactory.create() self.investigation.tag_set.add(tag) self.client.force_login(other_user) self.assertEqual(self.investigation.tag_set.count(), 1) response = self.client.delete(make_details_url(tag)) self.assertEqual(response.status_code, 403) self.assertEqual(self.investigation.tag_set.count(), 1)
def test_assign_tags_from_other_investigation_fails(self): responses = self.responses[0] investigation = InvestigationFactory.create() other_tag = TagFactory.create(investigation=investigation) form = responses[0].form_instance.form payload = { "selected_responses": [responses[2].id], "tag": other_tag.id } self.client.post(make_url(form), data=payload) self.assertQuerysetEqual(responses[2].tags.all(), [])
def test_assign_tags_for_editor(self): editor = UserFactory.create() self.investigation.add_user(editor, "E") self.client.force_login(editor) responses = self.responses[0] form = responses[0].form_instance.form tag = TagFactory.create(name='Pasta', investigation=self.investigation) payload = { "selected_responses": [responses[3].id, responses[4].id], "tag": tag.id } response = self.client.post(make_url(form), data=payload) self.assertEqual(response.status_code, 302)
def test_create_form_csv_includes_tags(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" } } } }]) first_tag = TagFactory.create(name="First Tag") second_tag = TagFactory.create(name="Second Tag, with, commas") response_1 = FormResponseFactory.create( form_instance=form_instance, submission_date=datetime(2018, 1, 1, tzinfo=pytz.utc), json={ "name": "Peter", "email": "*****@*****.**" }) response_1.tags.set([first_tag, second_tag]) 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,"First Tag, Second Tag with commas",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 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])