def test_delete_two_patch_relation_nobody(self): relation = create_relation() patch = create_patches(2, project=self.project, related=relation)[0] self.assertEqual(PatchRelation.objects.count(), 1) resp = self.client.patch(self.api_url(item=patch.pk), {'related': []}) self.assertEqual(resp.status_code, status.HTTP_403_FORBIDDEN) self.assertEqual(PatchRelation.objects.count(), 1)
def test_forbid_moving_patch_between_relations(self): """Test the break-before-make logic""" relation_a = create_relation() create_patches(2, project=self.project, related=relation_a) relation_b = create_relation() create_patches(2, project=self.project, related=relation_b) patch_a = relation_a.patches.first() patch_b = relation_b.patches.first() self.client.force_authenticate(user=self.maintainer) resp = self.client.patch(self.api_url(item=patch_a.pk), {'related': [patch_b.pk]}) self.assertEqual(resp.status_code, status.HTTP_409_CONFLICT) resp = self.client.patch(self.api_url(item=patch_b.pk), {'related': [patch_a.pk]}) self.assertEqual(resp.status_code, status.HTTP_409_CONFLICT)
def test_delete_from_three_patch_relation(self): relation = create_relation() patch = create_patches(3, project=self.project, related=relation)[0] self.assertEqual(PatchRelation.objects.count(), 1) self.client.force_authenticate(user=self.maintainer) resp = self.client.patch(self.api_url(item=patch.pk), {'related': []}) self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertIsNone(Patch.objects.get(id=patch.pk).related) self.assertEqual(PatchRelation.objects.count(), 1) self.assertEqual(PatchRelation.objects.first().patches.count(), 2)
def test_remove_one_patch_from_relation_good(self): relation = create_relation(3, project=self.project) target_patch = relation.patches.all()[0] # maintainer self.client.force_authenticate(user=self.maintainer) resp = self.client.patch( self.api_url(item=target_patch.pk), {'related': []} ) self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertIsNone(Patch.objects.get(id=target_patch.id).related) self.assertEqual(relation.patches.count(), 2)
def test_delete_two_patch_relation_maintainer(self): relation = create_relation() patch = create_patches(2, project=self.project, related=relation)[0] self.assertEqual(PatchRelation.objects.count(), 1) self.client.force_authenticate(user=self.maintainer) resp = self.client.patch(self.api_url(item=patch.pk), {'related': []}) self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(PatchRelation.objects.count(), 0) self.assertEqual( Patch.objects.filter(related__isnull=False).exists(), False)
def test_extend_relation_through_new(self): relation = create_relation(project=self.project) existing_patch_a = relation.patches.first() new_patch = create_patch(project=self.project) self.client.force_authenticate(user=self.maintainer) resp = self.client.patch( self.api_url(item=new_patch.pk), {'related': [existing_patch_a.pk]} ) self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(relation, Patch.objects.get(pk=new_patch.pk).related) self.assertEqual(relation.patches.count(), 3)
def test_remove_one_patch_from_relation_bad(self): relation = create_relation() patches = create_patches(3, project=self.project, related=relation) keep_patch_a = patches[1] keep_patch_b = patches[1] # this should do nothing - it is interpreted as # _adding_ keep_patch_b again which is a no-op. # maintainer self.client.force_authenticate(user=self.maintainer) resp = self.client.patch(self.api_url(item=keep_patch_a.pk), {'related': [keep_patch_b.pk]}) self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(relation.patches.count(), 3)
def test_extend_relation_through_old(self): relation = create_relation() existing_patch_a = create_patches(2, project=self.project, related=relation)[0] new_patch = create_patch(project=self.project) # maintainer self.client.force_authenticate(user=self.maintainer) resp = self.client.patch(self.api_url(item=existing_patch_a.pk), {'related': [new_patch.pk]}) self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertEqual(relation, Patch.objects.get(id=new_patch.id).related) self.assertEqual(relation.patches.count(), 3)
def test_patch_relations_changed(self): # purposefully setting series to None to minimize additional events relation = utils.create_relation() patches = utils.create_patches(3, series=None) # mark the first two patches as related; the second patch should be the # one that the event is raised for patches[0].related = relation patches[0].save() patches[1].related = relation patches[1].save() events = _get_events(patch=patches[1]) self.assertEqual(events.count(), 2) self.assertEqual(events[1].category, Event.CATEGORY_PATCH_RELATION_CHANGED) self.assertEqual(events[1].project, patches[1].project) self.assertIsNone(events[1].previous_relation) self.assertIsNone(events[1].current_relation) # add the third patch patches[2].related = relation patches[2].save() events = _get_events(patch=patches[2]) self.assertEqual(events.count(), 2) self.assertEqual(events[1].category, Event.CATEGORY_PATCH_RELATION_CHANGED) self.assertEqual(events[1].project, patches[1].project) self.assertIsNone(events[1].previous_relation) self.assertIsNone(events[1].current_relation) # drop the third patch patches[2].related = None patches[2].save() events = _get_events(patch=patches[2]) self.assertEqual(events.count(), 3) self.assertEqual(events[2].category, Event.CATEGORY_PATCH_RELATION_CHANGED) self.assertEqual(events[2].project, patches[1].project) self.assertIsNone(events[2].previous_relation) self.assertIsNone(events[2].current_relation)
def test_list_two_patch_relation(self): relation = create_relation() patches = create_patches(2, project=self.project, related=relation) # nobody resp = self.client.get(self.api_url(item=patches[0].pk)) self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertIn('related', resp.data) self.assertEqual(len(resp.data['related']), 1) self.assertEqual(resp.data['related'][0]['id'], patches[1].pk) resp = self.client.get(self.api_url(item=patches[1].pk)) self.assertEqual(resp.status_code, status.HTTP_200_OK) self.assertIn('related', resp.data) self.assertEqual(len(resp.data['related']), 1) self.assertEqual(resp.data['related'][0]['id'], patches[0].pk)