class JobTestCase(TestCase): def setUp(self): self.org = Organization() self.org.save() self.org2 = Organization() self.org2.save() self.project = Project(organization=self.org) self.project.save() self.workstream = Workstream(project=self.project) self.workstream.save() self.status1 = JobStatus(organization=self.org) self.status1.save() self.status2 = JobStatus(organization=self.org2) self.status2.save() self.job = Job(workstream=self.workstream, status=self.status1) self.job.save() def tearDown(self): self.org.delete() self.org2.delete() def test__job_workstream__matches(self): self.assertEqual(self.workstream, self.job.workstream) def test__save__throws_when_org_doesnt_match_workstream(self): job = Job(workstream=self.workstream, status=self.status2) self.assertRaises(PermissionDenied, lambda: job.save())
class MembershipTestCase(TestCase): def setUp(self): self.organization = Organization() self.organization.save() self.actor = Actor() self.actor.save() self.membership = Membership() self.membership.organization = self.organization self.membership.actor = self.actor self.membership.save() def tearDown(self): self.actor.delete() self.organization.delete() def test__actor__returns_actor(self): self.assertEquals(self.membership.actor_id, self.actor.id) self.assertEquals(self.membership.actor.id, self.actor.id) self.assertEquals(self.organization.actors.get().id, self.actor.id) def test__organization__returns_organization(self): self.assertEquals(self.membership.organization_id, self.organization.id) self.assertEquals(self.membership.organization.id, self.organization.id) self.assertEquals(self.actor.organizations.get().id, self.organization.id)
class ProjectTestCase(TestCase): def setUp(self): self.org = Organization() self.org.save() self.project = Project(organization=self.org) self.project.save() def tearDown(self): self.org.delete() def test__project_organization__matches(self): self.assertEqual(self.project.organization, self.org)
class JobStatusTestCase(TestCase): def setUp(self): self.org = Organization() self.org.save() self.status = JobStatus(organization=self.org) self.status.save() def tearDown(self): self.org.delete() def test__status_organization__matches(self): self.assertEqual(self.status.organization, self.org)
class WorkstreamTestCase(TestCase): def setUp(self): self.org = Organization() self.org.save() self.project = Project(organization=self.org) self.project.save() self.workstream = Workstream(project=self.project) self.workstream.save() def tearDown(self): self.org.delete() def test__workstream_project__matches(self): self.assertEqual(self.project, self.workstream.project)
class ActorsAPITestCase(APITestCase): def setUp(self): self.bran_user = User.objects.create_user(username="******", email="*****@*****.**", password="******") self.ned_user = User.objects.create_user(username="******", email="*****@*****.**", password="******") self.sansa_user = User.objects.create_user(username="******", email="*****@*****.**", password="******") self.jamie_user = User.objects.create_user(username="******", email="*****@*****.**", password="******") self.bran_actor = Actor(user_id=self.bran_user.id, name="bran") self.bran_actor.save() self.ned_actor = Actor(user_id=self.ned_user.id, name="ned") self.ned_actor.save() self.sansa_actor = Actor(user_id=self.sansa_user.id, name="sansa") self.sansa_actor.save() self.jamie_actor = Actor(user_id=self.jamie_user.id, name="jamie") self.jamie_actor.save() self.starks = Organization(name="starks") self.starks.save() self.lannisters = Organization(name="lannisters") self.lannisters.save() Membership(organization=self.starks, actor=self.bran_actor).save() Membership(organization=self.starks, actor=self.ned_actor).save() Membership(organization=self.starks, actor=self.sansa_actor).save() Membership(organization=self.lannisters, actor=self.sansa_actor).save() Membership(organization=self.lannisters, actor=self.jamie_actor).save() def tearDown(self): self.bran_actor.delete() self.sansa_actor.delete() self.jamie_actor.delete() self.ned_actor.delete() self.starks.delete() self.lannisters.delete() self.bran_user.delete() self.sansa_user.delete() self.jamie_user.delete() self.ned_user.delete() def test__list_actors__lists_all_actors_from_organizations_actor_belongs( self): self.client.login(username="******", password='******') response = self.client.get(reverse('actors-list')) self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals(response.data, [{ 'id': self.bran_actor.id, 'name': self.bran_actor.name, 'organizations': [self.starks.id] }, { 'id': self.ned_actor.id, 'name': self.ned_actor.name, 'organizations': [self.starks.id] }, { 'id': self.sansa_actor.id, 'name': self.sansa_actor.name, 'organizations': [self.starks.id] }]) def test__list_actors__filters_on_name(self): self.client.login(username="******", password='******') url = reverse('actors-list') + '?name=sansa' response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals(response.data, [{ 'id': self.sansa_actor.id, 'name': self.sansa_actor.name, 'organizations': [self.starks.id] }]) def test__list_actors__filters_on_organization(self): self.client.login(username="******", password='******') url = reverse('actors-list') + '?organizations=' + str(self.starks.id) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals( response.data, [{ 'id': self.bran_actor.id, 'name': self.bran_actor.name, 'organizations': [self.starks.id] }, { 'id': self.ned_actor.id, 'name': self.ned_actor.name, 'organizations': [self.starks.id] }, { 'id': self.sansa_actor.id, 'name': self.sansa_actor.name, 'organizations': [ self.starks.id, self.lannisters.id, ] }]) def test__get_actor__returns_actor(self): self.client.login(username="******", password='******') url = reverse('actors-detail', kwargs={'pk': self.sansa_actor.id}) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals( response.data, { 'id': self.sansa_actor.id, 'name': self.sansa_actor.name, 'organizations': [self.starks.id] }) def test__get_actor__returns_404_when_user_unknown(self): self.client.login(username="******", password='******') url = reverse('actors-detail', kwargs={'pk': self.jamie_actor.id}) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_404_NOT_FOUND) def test__create_actor__returns_405_not_allowed(self): self.client.login(username="******", password='******') url = reverse('actors-list') response = self.client.post(url, {}) self.assertEquals(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) def test__patch_actor__returns_405_not_allowed(self): self.client.login(username="******", password='******') url = reverse('actors-detail', kwargs={'pk': self.bran_actor.id}) response = self.client.patch(url, {}) self.assertEquals(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) def test__put_actor__returns_405_not_allowed(self): self.client.login(username="******", password='******') url = reverse('actors-detail', kwargs={'pk': self.bran_actor.id}) response = self.client.put(url, {}) self.assertEquals(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) def test__delete_actor__returns_405_not_allowed(self): self.client.login(username="******", password='******') url = reverse('actors-detail', kwargs={'pk': self.bran_actor.id}) response = self.client.delete(url) self.assertEquals(response.status_code, status.HTTP_405_METHOD_NOT_ALLOWED) # TODO: known bug with django filter-fields (since they have to be model only?) def test__list_actors__returns_404_when_user_unknowingly_part_of_other_org( self): pass
class ProjectsAPITestCase(APITestCase): projects_list_name = 'projects-list' projects_detail_name = 'projects-detail' def setUp(self): self.bran_user = User.objects.create_user(username="******", email="*****@*****.**", password="******") self.sansa_user = User.objects.create_user(username="******", email="*****@*****.**", password="******") self.jamie_user = User.objects.create_user(username="******", email="*****@*****.**", password="******") self.bran_actor = Actor(user_id=self.bran_user.id, name="bran") self.bran_actor.save() self.sansa_actor = Actor(user_id=self.sansa_user.id, name="sansa") self.sansa_actor.save() self.jamie_actor = Actor(user_id=self.jamie_user.id, name="jamie") self.jamie_actor.save() self.starks = Organization(name="starks") self.starks.save() self.lannisters = Organization(name="lannisters") self.lannisters.save() Membership(organization=self.starks, actor=self.bran_actor).save() Membership(organization=self.starks, actor=self.sansa_actor).save() Membership(organization=self.lannisters, actor=self.sansa_actor).save() Membership(organization=self.lannisters, actor=self.jamie_actor).save() self.winterfell = Project(organization=self.starks, name="winterfell") self.winterfell.save() self.kingslanding = Project(organization=self.lannisters, name="king's landing") self.kingslanding.save() self.casterlyrock = Project(organization=self.lannisters, name="casterly rock") self.casterlyrock.save() def tearDown(self): self.bran_actor.delete() self.sansa_actor.delete() self.jamie_actor.delete() self.starks.delete() self.lannisters.delete() self.bran_user.delete() self.sansa_user.delete() self.jamie_user.delete() def test__list_projects__lists_all_projects_from_organizations_actor_belongs( self): self.client.login(username="******", password='******') response = self.client.get(reverse(self.projects_list_name)) self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals( response.data, [{ 'id': self.winterfell.id, 'name': self.winterfell.name, 'organization': self.starks.id, 'created': format_iso8601(self.winterfell.created) }]) def test__list_projects__filters_on_name(self): self.client.login(username="******", password='******') url = reverse(self.projects_list_name) + '?' + urlencode( {'name': 'king\'s landing'}) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals( response.data, [{ 'id': self.kingslanding.id, 'name': self.kingslanding.name, 'organization': self.lannisters.id, 'created': format_iso8601(self.kingslanding.created) }]) def test__list_projects__filters_on_organization(self): self.client.login(username="******", password='******') url = reverse(self.projects_list_name) + '?' + urlencode( {'organization': self.lannisters.id}) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals( response.data, [{ 'id': self.kingslanding.id, 'name': self.kingslanding.name, 'organization': self.lannisters.id, 'created': format_iso8601(self.kingslanding.created) }, { 'id': self.casterlyrock.id, 'name': self.casterlyrock.name, 'organization': self.lannisters.id, 'created': format_iso8601(self.casterlyrock.created) }]) def test__get_project__returns_project(self): self.client.login(username="******", password='******') url = reverse(self.projects_detail_name, kwargs={'pk': self.winterfell.id}) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals( response.data, { 'id': self.winterfell.id, 'name': self.winterfell.name, 'organization': self.starks.id, 'created': format_iso8601(self.winterfell.created) }) def test__get_project__returns_404_when_project_unknown(self): self.client.login(username="******", password='******') url = reverse(self.projects_detail_name, kwargs={'pk': self.casterlyrock.id}) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_404_NOT_FOUND) def test__create_project__returns_project(self): self.client.login(username="******", password='******') url = reverse(self.projects_list_name) name = 'greywater' post_data = {'organization': self.starks.id, 'name': name} response = self.client.post(url, post_data) self.assertEquals(response.status_code, status.HTTP_201_CREATED) self.assertEquals(response.data['name'], name) self.assertEquals(response.data['organization'], self.starks.id) def test__create_project__forbids_adding_to_other_org(self): self.client.login(username="******", password='******') url = reverse(self.projects_list_name) name = 'greywater' post_data = {'organization': self.lannisters.id, 'name': name} response = self.client.post(url, post_data) self.assertEquals(response.status_code, status.HTTP_400_BAD_REQUEST) def test__patch_project__updates_name(self): self.client.login(username="******", password='******') url = reverse(self.projects_detail_name, kwargs={'pk': self.winterfell.id}) response = self.client.patch(url, {'name': 'greywater'}) self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals( response.data, { 'id': self.winterfell.id, 'name': 'greywater', 'organization': self.starks.id, 'created': format_iso8601(self.winterfell.created) }) def test__patch_project__doesnt_allow_updates_to_org(self): self.client.login(username="******", password='******') url = reverse(self.projects_detail_name, kwargs={'pk': self.winterfell.id}) response = self.client.patch(url, {'organization': self.lannisters.id}) self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals( Project.objects.get(name=self.winterfell.name).organization.id, self.starks.id) def test__patch_project__forbids_adding_to_other_org(self): self.client.login(username="******", password='******') url = reverse(self.projects_detail_name, kwargs={'pk': self.winterfell.id}) response = self.client.patch(url, { 'organization': self.lannisters.id, 'name': 'new name' }) self.assertEquals(response.status_code, status.HTTP_400_BAD_REQUEST) def test__patch_project__returns_404_when_project_unknown(self): self.client.login(username="******", password='******') url = reverse(self.projects_detail_name, kwargs={'pk': self.casterlyrock.id}) response = self.client.patch(url, {}) self.assertEquals(response.status_code, status.HTTP_404_NOT_FOUND) def test__put_project__updates_name(self): self.client.login(username="******", password='******') url = reverse(self.projects_detail_name, kwargs={'pk': self.winterfell.id}) data = {'name': 'greywater'} response = self.client.put(url, data) self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals( response.data, { 'id': self.winterfell.id, 'name': 'greywater', 'organization': self.starks.id, 'created': format_iso8601(self.winterfell.created) }) def test__put_project__forbids_adding_to_other_org(self): self.client.login(username="******", password='******') url = reverse(self.projects_detail_name, kwargs={'pk': self.winterfell.id}) data = {'organization': self.lannisters.id, 'name': 'anything'} response = self.client.put(url, data) self.assertEquals(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEquals( Project.objects.get(name=self.winterfell.name).organization.id, self.starks.id) def test__put_project__returns_404_when_project_unknown(self): self.client.login(username="******", password='******') url = reverse(self.projects_detail_name, kwargs={'pk': self.casterlyrock.id}) response = self.client.put(url, {}) self.assertEquals(response.status_code, status.HTTP_404_NOT_FOUND) def test__delete_project__returns_404_when_project_unknown(self): self.client.login(username="******", password='******') url = reverse(self.projects_detail_name, kwargs={'pk': self.casterlyrock.id}) response = self.client.delete(url) self.assertEquals(response.status_code, status.HTTP_404_NOT_FOUND) def test__delete_project__deletes_project(self): self.client.login(username="******", password='******') url = reverse(self.projects_detail_name, kwargs={'pk': self.winterfell.id}) response = self.client.delete(url) self.assertEquals(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEquals( Project.objects.filter(pk=self.winterfell.id).count(), 0)
class OrganizationsAPITestCase(APITestCase): def setUp(self): self.user = self.create_user() self.organization1 = Organization(name="tk1") self.organization1.save() self.organization2 = Organization(name="tk2") self.organization2.save() self.organization3 = Organization(name="tk3") self.organization3.save() self.actor = Actor(user_id=self.user.id) self.actor.save() self.membership1 = Membership(organization=self.organization1, actor=self.actor) self.membership1.save() self.membership2 = Membership(organization=self.organization2, actor=self.actor) self.membership2.save() def create_user(self): user = User.objects.create_user(username="******", email="*****@*****.**", password="******") self.client.login(username='******', password='******') return user def tearDown(self): self.actor.delete() self.organization1.delete() self.organization2.delete() self.user.delete() def test__list_organizations__lists_all_organizations_user_belongs(self): response = self.client.get(reverse('organizations-list')) self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals(response.data, [{ 'id': self.organization1.id, 'name': self.organization1.name }, { 'id': self.organization2.id, 'name': self.organization2.name }]) def test__list_organizations__filters_on_name(self): url = reverse('organizations-list') + '?name=tk2' response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals(response.data, [{ 'id': self.organization2.id, 'name': self.organization2.name }]) def test__get_organization__returns_org_if_user_belongs(self): url = reverse('organizations-detail', kwargs={'pk': self.organization1.id}) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals(response.data, { 'id': self.organization1.id, 'name': self.organization1.name }) def test__get_organization__returns_404_if_user_doesnt_belong(self): url = reverse('organizations-detail', kwargs={'pk': self.organization3.id}) response = self.client.get(url) self.assertEquals(response.status_code, status.HTTP_404_NOT_FOUND) def test__create_organization__creates_organization(self): url = reverse('organizations-list') response = self.client.post(url, {'name': 'new org'}) self.assertEquals(response.status_code, status.HTTP_201_CREATED) self.assertEquals(response.data['name'], 'new org') def test__patch_organization__updates_name(self): url = reverse('organizations-detail', kwargs={'pk': self.organization1.id}) response = self.client.patch(url, {'name': 'name1'}) self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals(response.data, { 'name': 'name1', 'id': self.organization1.id }) def test__patch_organization__returns_404_if_user_doesnt_belong(self): url = reverse('organizations-detail', kwargs={'pk': self.organization3.id}) response = self.client.patch(url, {'name': 'name1'}) self.assertEquals(response.status_code, status.HTTP_404_NOT_FOUND) def test__put_organization__updates_name(self): url = reverse('organizations-detail', kwargs={'pk': self.organization1.id}) response = self.client.put(url, {'name': 'name1'}) self.assertEquals(response.status_code, status.HTTP_200_OK) self.assertEquals(response.data, { 'name': 'name1', 'id': self.organization1.id }) def test__put_organization__returns_404_if_user_doesnt_belong(self): url = reverse('organizations-detail', kwargs={'pk': self.organization3.id}) response = self.client.put(url, {'name': 'name1'}) self.assertEquals(response.status_code, status.HTTP_404_NOT_FOUND) def test__delete_organization__deletes_org(self): url = reverse('organizations-detail', kwargs={'pk': self.organization1.id}) response = self.client.delete(url) self.assertEquals(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEquals( Organization.objects.filter(pk=self.organization1.id).count(), 0) def test__delete_organization__returns_404_if_user_doesnt_belong(self): url = reverse('organizations-detail', kwargs={'pk': self.organization3.id}) response = self.client.delete(url, {'name': 'name1'}) self.assertEquals(response.status_code, status.HTTP_404_NOT_FOUND)