def test_project_list_two(self): # create instances ProjectFactory.create_batch(2, visibility=VisibilityCharChoices.PUBLIC) # submit list request response = self.client.get("/api/v2/project/") # check response self.assert_200(response) self.assertMatchSnapshot(json.loads(response.content))
def test_project_csv_api(self): _country = country.CountryFactory() district1 = district.DistrictFactory(country=_country) district2 = district.DistrictFactory(country=_country) ProjectFactory.create_batch( 10, project_districts=[district1, district2], secondary_sectors=[SectorTags.WASH, SectorTags.PGI], visibility=VisibilityCharChoices.PUBLIC) url = '/api/v2/project/?format=csv' resp = self.client.get(url) self.assert_200(resp) self.assertMatchSnapshot(resp.content.decode('utf-8'))
def test_project_current_status(self): Project.objects.all().delete() project = ProjectFactory.create( start_date=datetime.date(2012, 11, 12), end_date=datetime.date(2012, 12, 13), status=Statuses.PLANNED.value, ) self.authenticate() patcher = mock.patch('django.utils.timezone.now') mock_timezone_now = patcher.start() for now, current_status in [ (datetime.date(2011, 11, 11), Statuses.PLANNED), (datetime.date(2012, 11, 12), Statuses.ONGOING), (datetime.date(2012, 11, 15), Statuses.ONGOING), (datetime.date(2012, 12, 13), Statuses.ONGOING), (datetime.date(2012, 12, 14), Statuses.COMPLETED), ]: mock_timezone_now.return_value.date.return_value = now management.call_command('update_project_status') response = self.client.get(f'/api/v2/project/{project.id}/') self.assert_200(response) self.assertEqual(response.data['status_display'], str(current_status)) patcher.stop()
def test_project_update(self): # create instance new_project = ProjectFactory.create( visibility=VisibilityCharChoices.PUBLIC) # authenticate self.authenticate() new_project = pydash.omit( new_project, [ "_state", "modified_at", "user", "event", "dtype", "regional_project" ], ) # update project name new_project_name = "Mock Project for Update API Test" new_country = country.CountryFactory() new_district = district.DistrictFactory(country=new_country) new_project["name"] = new_project_name new_project["reporting_ns"] = new_country.id new_project["project_country"] = new_country.id new_project["event"] = new_project["event_id"] new_project["project_districts"] = [new_district.id] # submit update request response = self.client.put(f"/api/v2/project/{new_project['id']}/", new_project, format='json') # check response self.assert_200(response) self.assertMatchSnapshot(json.loads(response.content)) self.assertTrue(Project.objects.get(name=new_project_name))
def test_project_read(self): # create instance new_project = ProjectFactory.create( visibility=VisibilityCharChoices.PUBLIC) # submit read request response = self.client.get(f"/api/v2/project/{new_project.pk}/") # check response self.assert_200(response) self.assertMatchSnapshot(json.loads(response.content))
def test_modified_by_field(self): district = District.objects.create() project = ProjectFactory.create( start_date=datetime.date(2012, 11, 12), end_date=datetime.date(2012, 12, 13), status=Statuses.PLANNED.value, ) data = { 'name': 'CreateMePls', 'project_districts': [district.id], 'programme_type': ProgrammeTypes.BILATERAL.value, 'primary_sector': Sectors.WASH.value, 'secondary_sectors': [Sectors.CEA.value, Sectors.PGI.value], 'operation_type': OperationTypes.EMERGENCY_OPERATION.value, 'start_date': '2012-11-12', 'end_date': '2013-11-13', 'budget_amount': 7000, 'target_total': 100, 'status': Statuses.PLANNED.value, } self.authenticate(self.user) response = self.client.patch(f'/api/v2/project/{project.id}/', data) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['modified_by'], self.user.id) # let another user get the project self.authenticate(self.ifrc_user) response = self.client.get(f'/api/v2/project/{project.id}/') self.assertEqual(response.status_code, 200) self.assertEqual(response.data['modified_by'], self.user.id) # let this user update the project data = { 'name': 'CreateMeNot', 'project_districts': [district.id], 'programme_type': ProgrammeTypes.BILATERAL.value, 'primary_sector': Sectors.WASH.value, 'secondary_sectors': [Sectors.CEA.value, Sectors.PGI.value], 'operation_type': OperationTypes.EMERGENCY_OPERATION.value, 'start_date': '2012-10-15', 'end_date': '2013-12-13', 'budget_amount': 7000, 'target_total': 100, 'status': Statuses.PLANNED.value, } response = self.client.patch(f'/api/v2/project/{project.id}/', data) self.assertEqual(response.status_code, 200) self.assertEqual(response.data['modified_by'], self.ifrc_user.id)
def test_project_delete(self): # create instance new_project = ProjectFactory.create( visibility=VisibilityCharChoices.PUBLIC) # authenticate self.authenticate() # submit delete request response = self.client.delete("/api/v2/project/{}/".format( new_project.pk)) # check response self.assert_204(response) self.assertMatchSnapshot(response.content) self.assertFalse(Project.objects.count())
def test_project_create(self): # authenticate new_user = UserFactory.create() self.authenticate(new_user) # create project new_project_name = "Mock Project for Create API Test" new_project = ProjectFactory.stub( name=new_project_name, visibility=VisibilityCharChoices.PUBLIC, user=new_user, ) new_country = country.CountryFactory() new_district = district.DistrictFactory(country=new_country) new_project = pydash.omit( new_project, [ "user", "reporting_ns", "project_country", "event", "dtype", "regional_project", ], ) new_project["reporting_ns"] = new_country.id new_project["project_country"] = new_country.id new_project["project_districts"] = [new_district.id] # submit create request response = self.client.post("/api/v2/project/", new_project, format='json') # check response self.assert_201(response) self.assertMatchSnapshot(json.loads(response.content)) self.assertTrue(Project.objects.get(name=new_project_name))
def test_global_project_api(self): country_1 = country.CountryFactory() country_2 = country.CountryFactory() ns_1 = country.CountryFactory() ns_2 = country.CountryFactory() c1_district1 = district.DistrictFactory(country=country_1) c1_district2 = district.DistrictFactory(country=country_1) c2_district1 = district.DistrictFactory(country=country_2) c2_district2 = district.DistrictFactory(country=country_2) [ ProjectFactory.create_batch( 2, project_districts=project_districts, secondary_sectors=secondary_sectors, visibility=VisibilityCharChoices.PUBLIC, ) for project_districts, secondary_sectors in [ ([c1_district1, c1_district2], [SectorTags.WASH, SectorTags.PGI]), ([c1_district1, c1_district2], [SectorTags.WASH, SectorTags.MIGRATION]), ([c2_district1, c2_district2], [SectorTags.LIVELIHOODS_AND_BASIC_NEEDS, SectorTags.PGI]), ([c2_district1, c2_district2], [SectorTags.INTERNAL_DISPLACEMENT, SectorTags.RECOVERY]), ] for ns in [ns_1, ns_2] ] url = '/api/v2/global-project/overview/' resp = self.client.get(url) self.assert_200(resp) self.assertMatchSnapshot(resp.json()) url = '/api/v2/global-project/ns-ongoing-projects-stats/' resp = self.client.get(url) self.assert_200(resp) self.assertMatchSnapshot(resp.json())