def test_list_anonymous(self): """List people as anonymous user.""" create_person() # anonymous user resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_403_FORBIDDEN, resp.status_code)
def test_name_complete(self): people = [create_person(name='Test name'), create_person(name=None)] response = self.client.get(reverse('api-submitters'), {'q': 'name'}) self.assertEqual(response.status_code, 200) data = json.loads(response.content.decode()) self.assertEqual(len(data), 1) self.assertEqual(data[0]['name'], people[0].name)
def test_update_authorized(self): """Update an existing cover letter comment as an authorized user. To be authorized users must meet at least one of the following: - project maintainer, cover letter submitter, or cover letter comment submitter Ensure updates can only be performed by authorized users. """ # Update as maintainer person = create_person(user=self.user) resp = self._test_update(person=person) self.assertEqual(1, CoverComment.objects.all().count()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertTrue(resp.data['addressed']) # Update as cover letter submitter person = create_person(name='cover-submitter', user=create_user()) cover = create_cover(submitter=person) resp = self._test_update(person=person, cover=cover) self.assertEqual(2, CoverComment.objects.all().count()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertTrue(resp.data['addressed']) # Update as cover letter comment submitter person = create_person(name='comment-submitter', user=create_user()) cover = create_cover() resp = self._test_update(person=person, cover=cover, submitter=person) self.assertEqual(3, CoverComment.objects.all().count()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertTrue(resp.data['addressed'])
def test_email_complete(self): people = [create_person(email='*****@*****.**'), create_person(email='*****@*****.**')] response = self.client.get(reverse('api-submitters'), {'q': 'test2'}) self.assertEqual(response.status_code, 200) data = json.loads(response.content.decode()) self.assertEqual(len(data), 1) self.assertEqual(data[0]['email'], people[1].email)
def test_param_limit(self): for i in range(10): create_person() response = self.client.get(reverse('api-submitters'), {'q': 'test', 'l': 5}) self.assertEqual(response.status_code, 200) data = json.loads(response.content.decode()) self.assertEqual(len(data), 5)
def test_email_complete(self): people = [ create_person(email='*****@*****.**'), create_person(email='*****@*****.**') ] response = self.client.get(reverse('api-submitters'), {'q': 'test2'}) self.assertEqual(response.status_code, 200) data = json.loads(response.content.decode()) self.assertEqual(len(data), 1) self.assertEqual(data[0]['email'], people[1].email)
def test_param_limit(self): for i in range(10): create_person() response = self.client.get(reverse('api-submitters'), { 'q': 'test', 'l': 5 }) self.assertEqual(response.status_code, 200) data = json.loads(response.content.decode()) self.assertEqual(len(data), 5)
def test_update_not_authorized(self): """Update an existing patch comment when not signed in and not authorized. To be authorized users must meet at least one of the following: - project maintainer, patch submitter, patch delegate, or patch comment submitter Ensure updates can only be performed by authorized users. """ person = create_person(user=self.user) resp = self._test_update(person=person, authenticate=False) self.assertEqual(status.HTTP_403_FORBIDDEN, resp.status_code) person = create_person() # normal user without edit permissions resp = self._test_update(person=person) # signed-in self.assertEqual(status.HTTP_403_FORBIDDEN, resp.status_code)
def test_detail_anonymous(self): """Show person as anonymous user.""" person = create_person() # anonymous user resp = self.client.get(self.api_url(person.id)) self.assertEqual(status.HTTP_403_FORBIDDEN, resp.status_code)
def setUp(self): self.project = create_project() for name, email, date in self.patchmeta: person = create_person(name=name, email=email) create_patch(submitter=person, project=self.project, date=date)
def test_secondary_email_optin_form(self): person = create_person(email=self.secondary_email, user=self.user) EmailOptout(email=person.email).save() form_re = self._form_re(reverse('mail-optin'), person.email) response = self.client.get(reverse('user-profile')) self.assertEqual(response.status_code, 200) self.assertIsNotNone(form_re.search(response.content.decode()))
def test_valid_relations(self): test_submitter = utils.create_person() utils.create_patches(8, submitter=test_submitter) patch_ids = models.Patch.objects.filter( submitter=test_submitter).values_list('id', flat=True) with tempfile.NamedTemporaryFile(delete=False, mode='w+') as f1: for i in range(0, len(patch_ids), 3): # we write out the patch IDs this way so that we can # have a mix of 3-patch and 2-patch lines without special # casing the format string. f1.write('%s\n' % ' '.join(map(str, patch_ids[i:(i + 3)]))) out = StringIO() call_command('replacerelations', f1.name, stdout=out) self.assertEqual(models.PatchRelation.objects.count(), 3) os.unlink(f1.name) patch_ids_with_missing = list(patch_ids) + [ i for i in range(max(patch_ids), max(patch_ids) + 3) ] with tempfile.NamedTemporaryFile(delete=False, mode='w+') as f2: for i in range(0, len(patch_ids_with_missing), 3): f2.write('%s\n' % ' '.join(map(str, patch_ids_with_missing[i:(i + 3)]))) call_command('replacerelations', f2.name, stdout=out) self.assertEqual(models.PatchRelation.objects.count(), 3) os.unlink(f2.name)
def _create_series(self): project_obj = create_project(linkname='myproject') person_obj = create_person(email='*****@*****.**') series_obj = create_series(project=project_obj, submitter=person_obj) create_cover(series=series_obj) create_patch(series=series_obj) return series_obj
def _create_patch(self, **kwargs): person_obj = create_person(email='*****@*****.**') project_obj = create_project(linkname='myproject') state_obj = create_state(name='Under Review') patch_obj = create_patch(state=state_obj, project=project_obj, submitter=person_obj, **kwargs) return patch_obj
def test_detail_unlinked(self): """Show unlinked person as authenticted user.""" person = create_person() user = create_user(link_person=False) self.client.force_authenticate(user=user) resp = self.client.get(self.api_url(person.id)) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertSerialized(person, resp.data, has_user=False)
def setUp(self): project = create_project() self.person = create_person() self.patch = create_patch(project=project, submitter=self.person, content='comment 1 text\nAcked-by: 1\n') self.comment = create_comment(submission=self.patch, submitter=self.person, content='comment 2 text\nAcked-by: 2\n')
def test_list_authenticated(self): """List people as an authenticated user.""" person = create_person() user = create_user(link_person=False) self.client.force_authenticate(user=user) resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(1, len(resp.data)) self.assertSerialized(person, resp.data[0])
def test_patchwork_from_header(self): """Validate inclusion of generated 'X-Patchwork-From' header.""" email = '*****@*****.**' from_header = 'From: Jon Doe <%s>\n' % email person = create_person(name='Jonathon Doe', email=email) patch = create_patch(submitter=person, headers=from_header) response = self.client.get(reverse('patch-mbox', args=[patch.id])) self.assertContains(response, from_header) self.assertContains(response, 'X-Patchwork-Submitter: %s <%s>' % ( person.name, email))
def test_unlinked_user(self): person = create_person() user = create_user() self.client.force_authenticate(user=user) resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(2, len(resp.data)) self.assertEqual(person.name, resp.data[0]['name']) self.assertIsNone(resp.data[0]['user_url'])
def setUp(self): project = create_project() self.person = create_person() self.patch = create_patch( project=project, submitter=self.person, content='comment 1 text\nAcked-by: 1\n') self.comment = create_comment( submission=self.patch, submitter=self.person, content='comment 2 text\nAcked-by: 2\n')
def setUp(self): self.project = create_project() self.person = create_person() self.patch = create_patch( project=self.project, submitter=self.person, diff='', content='comment 1 text\nAcked-by: 1\n---\nupdate\n') self.comment = create_patch_comment( patch=self.patch, submitter=self.person, content='comment 2 text\nAcked-by: 2\n')
def test_anonymous_create(self): """Ensure anonymous "POST" operations are rejected.""" patch = { 'project': create_project().id, 'submitter': create_person().id, 'msgid': make_msgid(), 'name': 'test-create-patch', 'diff': 'patch diff', } resp = self.client.post(self.api_url(), patch) self.assertEqual(status.HTTP_403_FORBIDDEN, resp.status_code)
def test_list(self): """Validate we can list a patch.""" resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(0, len(resp.data)) person_obj = create_person(email='*****@*****.**') state_obj = create_state(name='Under Review') project_obj = create_project(linkname='myproject') patch_obj = create_patch(state=state_obj, project=project_obj, submitter=person_obj) # anonymous user resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(1, len(resp.data)) patch_rsp = resp.data[0] self.assertSerialized(patch_obj, patch_rsp) self.assertNotIn('headers', patch_rsp) self.assertNotIn('content', patch_rsp) self.assertNotIn('diff', patch_rsp) # authenticated user user = create_user() self.client.force_authenticate(user=user) resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(1, len(resp.data)) patch_rsp = resp.data[0] self.assertSerialized(patch_obj, patch_rsp) # test filtering by state resp = self.client.get(self.api_url(), {'state': 'under-review'}) self.assertEqual([patch_obj.id], [x['id'] for x in resp.data]) resp = self.client.get(self.api_url(), {'state': 'missing-state'}) self.assertEqual(0, len(resp.data)) # test filtering by project resp = self.client.get(self.api_url(), {'project': 'myproject'}) self.assertEqual([patch_obj.id], [x['id'] for x in resp.data]) resp = self.client.get(self.api_url(), {'project': 'invalidproject'}) self.assertEqual(0, len(resp.data)) # test filtering by submitter, both ID and email resp = self.client.get(self.api_url(), {'submitter': person_obj.id}) self.assertEqual([patch_obj.id], [x['id'] for x in resp.data]) resp = self.client.get(self.api_url(), {'submitter': '*****@*****.**'}) self.assertEqual([patch_obj.id], [x['id'] for x in resp.data]) resp = self.client.get(self.api_url(), {'submitter': '*****@*****.**'}) self.assertEqual(0, len(resp.data))
def test_patchwork_submitter_header(self): """Validate inclusion of generated 'X-Patchwork-Submitter' header.""" email = '*****@*****.**' from_header = f'From: Jon Doe <{email}>\n' person = create_person(name='Jonathon Doe', email=email) submitter_header = f'X-Patchwork-Submitter: {person.name} <{email}>' patch = create_patch(submitter=person, headers=from_header) mbox = utils.patch_to_mbox(patch) self.assertIn(from_header, mbox) self.assertIn(submitter_header, mbox)
def test_list(self): """Validate we can list a patch.""" resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(0, len(resp.data)) person_obj = create_person(email='*****@*****.**') state_obj = create_state(name='Under Review') project_obj = create_project(linkname='myproject') patch_obj = create_patch(state=state_obj, project=project_obj, submitter=person_obj) # anonymous user resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(1, len(resp.data)) patch_rsp = resp.data[0] self.assertSerialized(patch_obj, patch_rsp) self.assertNotIn('headers', patch_rsp) self.assertNotIn('content', patch_rsp) self.assertNotIn('diff', patch_rsp) # authenticated user user = create_user() self.client.force_authenticate(user=user) resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(1, len(resp.data)) patch_rsp = resp.data[0] self.assertSerialized(patch_obj, patch_rsp) # test filtering by state resp = self.client.get(self.api_url(), {'state': 'under-review'}) self.assertEqual([patch_obj.id], [x['id'] for x in resp.data]) resp = self.client.get(self.api_url(), {'state': 'missing-state'}) self.assertEqual(0, len(resp.data)) # test filtering by project resp = self.client.get(self.api_url(), {'project': 'myproject'}) self.assertEqual([patch_obj.id], [x['id'] for x in resp.data]) resp = self.client.get(self.api_url(), {'project': 'invalidproject'}) self.assertEqual(0, len(resp.data)) # test filtering by submitter, both ID and email resp = self.client.get(self.api_url(), {'submitter': person_obj.id}) self.assertEqual([patch_obj.id], [x['id'] for x in resp.data]) resp = self.client.get(self.api_url(), { 'submitter': '*****@*****.**'}) self.assertEqual([patch_obj.id], [x['id'] for x in resp.data]) resp = self.client.get(self.api_url(), { 'submitter': '*****@*****.**'}) self.assertEqual(0, len(resp.data))
def test_from_header(self): """Validate non-ascii 'From' header. Test that a person with characters outside ASCII in his name do produce correct From header. As RFC 2822 state we must retain the <*****@*****.**> format for the mail while the name part may be coded in some ways. """ person = create_person(name=u'©ool guŷ') patch = create_patch(submitter=person) from_email = f'<{person.email}>' mbox = utils.patch_to_mbox(patch) self.assertIn(from_email, mbox)
def test_from_header(self): """Validate non-ascii 'From' header. Test that a person with characters outside ASCII in his name do produce correct From header. As RFC 2822 state we must retain the <*****@*****.**> format for the mail while the name part may be coded in some ways. """ person = create_person(name=u'©ool guŷ') patch = create_patch(submitter=person) from_email = '<' + person.email + '>' response = self.client.get(reverse('patch-mbox', args=[patch.id])) self.assertContains(response, from_email)
def test_update_authorized(self): """Update an existing patch comment as an authorized user. To be authorized users must meet at least one of the following: - project maintainer, patch submitter, patch delegate, or patch comment submitter Ensure updates can only be performed by authorized users. """ # Update as maintainer person = create_person(user=self.user) resp = self._test_update(person=person) self.assertEqual(1, PatchComment.objects.all().count()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertTrue(resp.data['addressed']) # Update as patch submitter person = create_person(name='patch-submitter', user=create_user()) patch = create_patch(submitter=person) resp = self._test_update(person=person, patch=patch) self.assertEqual(2, PatchComment.objects.all().count()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertTrue(resp.data['addressed']) # Update as patch delegate person = create_person(name='patch-delegate', user=create_user()) patch = create_patch(delegate=person.user) resp = self._test_update(person=person, patch=patch) self.assertEqual(3, PatchComment.objects.all().count()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertTrue(resp.data['addressed']) # Update as patch comment submitter person = create_person(name='comment-submitter', user=create_user()) patch = create_patch() resp = self._test_update(person=person, patch=patch, submitter=person) self.assertEqual(4, PatchComment.objects.all().count()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertTrue(resp.data['addressed'])
def test_list_bug_335(self): """Ensure we retrieve the embedded cover letter project in O(1).""" project_obj = create_project(linkname='myproject') person_obj = create_person(email='*****@*****.**') for i in range(10): series_obj = create_series( project=project_obj, submitter=person_obj, ) create_cover(series=series_obj) create_patch(series=series_obj) with self.assertNumQueries(6): self.client.get(self.api_url())
def test_update_invalid_addressed(self): """Update an existing patch comment using invalid values. Ensure we handle invalid patch comment addressed values. """ person = create_person(name='patch-submitter', user=create_user()) patch = create_patch(submitter=person) resp = self._test_update(person=person, patch=patch, addressed='not-valid', validate_request=False) self.assertEqual(status.HTTP_400_BAD_REQUEST, resp.status_code) self.assertFalse( getattr(PatchComment.objects.all().first(), 'addressed'))
def test_list(self): """This API requires authenticated users.""" person = create_person() # anonymous user resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_403_FORBIDDEN, resp.status_code) # authenticated user user = create_user(link_person=False) self.client.force_authenticate(user=user) resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(1, len(resp.data)) self.assertSerialized(person, resp.data[0])
def test_dmarc_from_header(self): """Validate 'From' header is rewritten correctly when DMARC-munged. Test that when an email with a DMARC-munged From header is processed, the From header will be unmunged and the munged address will be saved as 'X-Patchwork-Original-From'. """ orig_from_header = 'Person via List <*****@*****.**>' rewritten_from_header = 'Person <*****@*****.**>' project = create_project(listemail='*****@*****.**') person = create_person(name='Person', email='*****@*****.**') patch = create_patch(project=project, headers='From: ' + orig_from_header, submitter=person) mbox = utils.patch_to_mbox(patch) mail = email.message_from_string(mbox) self.assertEqual(mail['From'], rewritten_from_header) self.assertEqual(mail['X-Patchwork-Original-From'], orig_from_header)
def test_list(self): """Validate we can list series.""" resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(0, len(resp.data)) project_obj = create_project(linkname='myproject') person_obj = create_person(email='*****@*****.**') cover_obj = create_cover() series_obj = create_series(project=project_obj, submitter=person_obj) series_obj.add_cover_letter(cover_obj) create_series_patch(series=series_obj) # anonymous users resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(1, len(resp.data)) series_rsp = resp.data[0] self.assertSerialized(series_obj, series_rsp) # authenticated user user = create_user() self.client.force_authenticate(user=user) resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(1, len(resp.data)) series_rsp = resp.data[0] self.assertSerialized(series_obj, series_rsp) # test filtering by project resp = self.client.get(self.api_url(), {'project': 'myproject'}) self.assertEqual([series_obj.id], [x['id'] for x in resp.data]) resp = self.client.get(self.api_url(), {'project': 'invalidproject'}) self.assertEqual(0, len(resp.data)) # test filtering by owner, both ID and email resp = self.client.get(self.api_url(), {'submitter': person_obj.id}) self.assertEqual([series_obj.id], [x['id'] for x in resp.data]) resp = self.client.get(self.api_url(), {'submitter': '*****@*****.**'}) self.assertEqual([series_obj.id], [x['id'] for x in resp.data]) resp = self.client.get(self.api_url(), {'submitter': '*****@*****.**'}) self.assertEqual(0, len(resp.data))
def test_create(self): """Ensure creations are rejected.""" submitter = create_person() project = create_project() user = create_maintainer(project) user.is_superuser = True user.save() self.client.force_authenticate(user=user) patch = { 'project': project.id, 'submitter': submitter.id, 'msgid': make_msgid(), 'name': 'test-create-patch', 'diff': 'patch diff', } resp = self.client.post(self.api_url(), patch) self.assertEqual(status.HTTP_403_FORBIDDEN, resp.status_code)
def test_list(self): """Validate we can list series.""" resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(0, len(resp.data)) project_obj = create_project(linkname='myproject') person_obj = create_person(email='*****@*****.**') series_obj = create_series(project=project_obj, submitter=person_obj) create_cover(series=series_obj) create_patch(series=series_obj) # anonymous users resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(1, len(resp.data)) series_rsp = resp.data[0] self.assertSerialized(series_obj, series_rsp) # authenticated user user = create_user() self.client.force_authenticate(user=user) resp = self.client.get(self.api_url()) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertEqual(1, len(resp.data)) series_rsp = resp.data[0] self.assertSerialized(series_obj, series_rsp) # test filtering by project resp = self.client.get(self.api_url(), {'project': 'myproject'}) self.assertEqual([series_obj.id], [x['id'] for x in resp.data]) resp = self.client.get(self.api_url(), {'project': 'invalidproject'}) self.assertEqual(0, len(resp.data)) # test filtering by owner, both ID and email resp = self.client.get(self.api_url(), {'submitter': person_obj.id}) self.assertEqual([series_obj.id], [x['id'] for x in resp.data]) resp = self.client.get(self.api_url(), { 'submitter': '*****@*****.**'}) self.assertEqual([series_obj.id], [x['id'] for x in resp.data]) resp = self.client.get(self.api_url(), { 'submitter': '*****@*****.**'}) self.assertEqual(0, len(resp.data))
def test_create(self): """Ensure creations are rejected.""" project = create_project() patch = { 'project': project, 'submitter': create_person().id, 'msgid': make_msgid(), 'name': 'test-create-patch', 'diff': 'patch diff', } # anonymous user resp = self.client.post(self.api_url(), patch) self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, resp.status_code) # superuser user = create_maintainer(project) user.is_superuser = True user.save() self.client.force_authenticate(user=user) resp = self.client.post(self.api_url(), patch) self.assertEqual(status.HTTP_405_METHOD_NOT_ALLOWED, resp.status_code)
def test_detail(self): person = create_person() # anonymous user resp = self.client.get(self.api_url(person.id)) self.assertEqual(status.HTTP_403_FORBIDDEN, resp.status_code) # authenticated, unlinked user user = create_user(link_person=False) self.client.force_authenticate(user=user) resp = self.client.get(self.api_url(person.id)) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertSerialized(person, resp.data, has_user=False) # authenticated, linked user user = create_user(link_person=True) person = user.person_set.all().first() self.client.force_authenticate(user=user) resp = self.client.get(self.api_url(person.id)) self.assertEqual(status.HTTP_200_OK, resp.status_code) self.assertSerialized(person, resp.data, has_user=True)
def setUp(self): author = create_person(name=u'P\xe4tch Author') patch_content = read_patch('0002-utf-8.patch', encoding='utf-8') self.patch = create_patch(submitter=author, diff=patch_content)
def setUp(self): self.project = create_project() self.person = create_person()