class TestHibernateEndpoint(APITestMixin, TestCase): def setUp(self): self.barrier = BarrierFactory() self.url = reverse("hibernate-barrier", kwargs={"pk": self.barrier.id}) @freeze_time("2020-02-22") def test_hibernate_barrier_endpoint_sets_status_to_dormant(self): """ Barrier status should be set to DORMANT when it gets hibernated. Also status date should be updated. """ expected_status_date = "2020-02-22" dormant = 5 assert 1 == self.barrier.status response = self.api_client.put(self.url) assert status.HTTP_200_OK == response.status_code self.barrier.refresh_from_db() assert dormant == self.barrier.status assert expected_status_date == self.barrier.status_date.strftime( '%Y-%m-%d') def test_update_barrier_through_hibernate_barrier_endpoint(self): """ Users should be able to update status summary while hibernating a barrier. """ status_summary = "some status summary" payload = {"status_summary": status_summary} response = self.api_client.put(self.url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code self.barrier.refresh_from_db() assert status_summary == self.barrier.status_summary
class TestMassTransportMigration(TestMigrations): """ This test case is inteded to test the logic in the migration itself rather than the mapping. Using a sector called Mass Transport as that sector has been split to 3 new ones. If the migration works with this then it will work with simpler cases too. """ app = "metadata" migrate_from = "0013_barriertag" migrate_to = "0014_sectors" def setUpBeforeMigration(self, apps): self.mass_transport_uuid_str = "b5959812-6095-e211-a939-e4115bead28a" self.barrier = BarrierFactory(sectors=[self.mass_transport_uuid_str]) self.expected_sectors = [ uuid.UUID("9738cecc-5f95-e211-a939-e4115bead28a"), # Airports uuid.UUID("aa22c9d2-5f95-e211-a939-e4115bead28a"), # Railways uuid.UUID("aa38cecc-5f95-e211-a939-e4115bead28a"), # Maritime ] assert 1 == len(self.barrier.sectors) assert [self.mass_transport_uuid_str] == self.barrier.sectors assert 4 == self.barrier.history.count() # create a history items self.barrier.status = BarrierStatus.OPEN_IN_PROGRESS self.barrier.save() self.barrier.refresh_from_db() assert 5 == self.barrier.history.count() @pytest.mark.skip(reason="Migration has already been run") def test_sectors_migrated_in_barrier(self): self.barrier.refresh_from_db() assert set(self.expected_sectors) == set(self.barrier.sectors) @pytest.mark.skip(reason="Migration has already been run") def test_sectors_migration_removed_old_sector_id_in_history_items(self): history_items = self.barrier.history.filter( sectors__contains=[self.mass_transport_uuid_str]) assert 0 == history_items.count() @pytest.mark.skip(reason="Migration has already been run") def test_sectors_migration_does_not_create_a_new_history_item(self): assert 5 == self.barrier.history.count() @pytest.mark.skip(reason="Migration has already been run") def test_sectors_migration_adds_dest_sector_ids_to_history_items(self): for sector in self.expected_sectors: with self.subTest(sector=sector): history_items = self.barrier.history.filter( sectors__contains=[sector]) assert 5 == history_items.count()
def test_patch_barrier_with_nonexisting_tags(self): barrier = BarrierFactory() assert not barrier.tags.exists(), "Expected no tags to start with." url = reverse("get-barrier", kwargs={"pk": barrier.id}) payload = {"tags": [123321]} response = self.api_client.patch(url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code barrier.refresh_from_db() assert not barrier.tags.exists(), "Expected no tags to be assigned."
def test_patch_barrier_with_valid_tags(self): barrier = BarrierFactory() tag_title = "wobble" tag = BarrierTagFactory(title=tag_title) assert not barrier.tags.exists(), "Expected no tags to start with." url = reverse("get-barrier", kwargs={"pk": barrier.id}) payload = {"tags": [tag.id]} response = self.api_client.patch(url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code barrier.refresh_from_db() assert 1 == barrier.tags.count() assert tag.id == barrier.tags.first().id
def test_barrier_completion_percentage_changed_full(self): barrier = BarrierFactory( country="a05f66a0-5d95-e211-a939-e4115bead28a", summary="This... Is... A SUMMARY!", source="Ketchup", sectors=["75debee7-a182-410e-bde0-3098e4f7b822"], ) category = CategoryFactory() barrier.categories.add(category) barrier.commodities.set((CommodityFactory(code="010410"), )) barrier.save() barrier_completion_percentage_changed(sender=Barrier, instance=barrier) barrier.refresh_from_db() assert barrier.completion_percent == 100
def test_patch_barrier_with_invalid_tags_returns_400(self): barrier = BarrierFactory() url = reverse("get-barrier", kwargs={"pk": barrier.id}) assert not barrier.tags.exists(), "Expected no tags to start with." invalid_payloads = [123, "wobble", {"also": "invalid"}] for case in invalid_payloads: with self.subTest(case=case): payload = {"tags": case} response = self.api_client.patch(url, format="json", data=payload) assert status.HTTP_400_BAD_REQUEST == response.status_code barrier.refresh_from_db() assert not barrier.tags.exists( ), "Expected no tags to be added."
class TestBarrierPublicEligibility(APITestMixin, TestCase): def setUp(self): self.barrier = BarrierFactory() self.url = reverse("get-barrier", kwargs={"pk": self.barrier.id}) def test_get_barrier_without_public_eligibility(self): """ By default all existing barriers start with public_eligibility not begin set. """ assert 1 == Barrier.objects.count() assert self.barrier.public_eligibility is None response = self.api_client.get(self.url) assert status.HTTP_200_OK == response.status_code assert response.data["public_eligibility"] is None def test_patch_public_eligibility_with_valid_values(self): valid_values = [True, False] for value in valid_values: with self.subTest(value=value): payload = {"public_eligibility": value} response = self.api_client.patch(self.url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code, \ f"Expected 200 when value is {value}" assert value == response.data["public_eligibility"], \ f'Expected {value} in "public_eligibility" field.' def test_patch_public_eligibility_with_invalid_values(self): invalid_values = [None, "", 123, {"1": "test"}, [1, 2, 3]] for value in invalid_values: with self.subTest(value=value): payload = {"public_eligibility": value} response = self.api_client.patch(self.url, format="json", data=payload) assert status.HTTP_400_BAD_REQUEST == response.status_code, \ f"Expected 400 when value is {value}" def test_patch_public_eligibility_summary(self): summary = "Wibble wobble" payload = {"public_eligibility_summary": summary} response = self.api_client.patch(self.url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code, \ f"Expected 200 when public_eligibility_summary is {summary}" assert summary == response.data["public_eligibility_summary"] def test_patch_public_eligibility_resets_public_eligibility_summary(self): self.barrier.public_eligibility = False self.barrier.public_eligibility_summary = "Wibble wobble" self.barrier.save() self.barrier.refresh_from_db() assert not self.barrier.public_eligibility assert self.barrier.public_eligibility_summary payload = {"public_eligibility": True} response = self.api_client.patch(self.url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code assert response.data["public_eligibility"] assert not response.data["public_eligibility_summary"] def test_patch_public_eligibility_with_permissions(self): pass def test_patch_public_eligibility_without_permissions(self): pass
class TestBarrierDetails(APITestMixin, APITestCase): def setUp(self): self.barrier = BarrierFactory() self.url = reverse("get-barrier", kwargs={"pk": self.barrier.id}) def test_get_barrier_details(self): response = self.api_client.get(self.url) assert status.HTTP_200_OK == response.status_code assert str(self.barrier.id) == response.data["id"] def test_barrier_detail_by_code(self): url = reverse("barrier_detail_code", kwargs={"code": self.barrier.code}) response = self.api_client.get(url) assert response.status_code == status.HTTP_200_OK assert str(self.barrier.id) == response.data["id"] def test_patch_barrier_status_to_resolved_in_full(self): assert 1 == self.barrier.status status_date = "2018-09-10" status_summary = "some status summary" resolved_in_full = 4 url = reverse("resolve-in-full", kwargs={"pk": self.barrier.id}) payload = { "status_date": status_date, "status_summary": status_summary, } response = self.api_client.put(url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code self.barrier.refresh_from_db() assert resolved_in_full == self.barrier.status assert status_date == self.barrier.status_date.strftime('%Y-%m-%d') assert status_summary == self.barrier.status_summary @freeze_time("2020-02-22") def test_unknown_barrier_endpoint_sets_status_to_unknown(self): assert 1 == self.barrier.status status_summary = "some status summary" unknown = 7 url = reverse("unknown-barrier", kwargs={"pk": self.barrier.id}) payload = { "status_summary": status_summary, } response = self.api_client.put(url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code self.barrier.refresh_from_db() assert unknown == self.barrier.status assert "2020-02-22" == self.barrier.status_date.strftime('%Y-%m-%d') assert status_summary == self.barrier.status_summary @freeze_time("2020-02-22") def test_open_in_progress_endpoint_sets_status_to_open_in_progress(self): assert 1 == self.barrier.status status_summary = "some status summary" open_in_progress = 2 url = reverse("open-in-progress", kwargs={"pk": self.barrier.id}) payload = { "status_summary": status_summary, } response = self.api_client.put(url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code self.barrier.refresh_from_db() assert open_in_progress == self.barrier.status assert "2020-02-22" == self.barrier.status_date.strftime('%Y-%m-%d') assert status_summary == self.barrier.status_summary def test_patch_title(self): title = "Just a new title" payload = {"title": title} response = self.api_client.patch(self.url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code assert str(self.barrier.id) == response.data["id"] assert title == response.data["title"] def test_patch_barrier_country(self): payload = {"country": "82756b9a-5d95-e211-a939-e4115bead28a"} response = self.api_client.patch(self.url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code assert str(self.barrier.id) == response.data["id"] assert "82756b9a-5d95-e211-a939-e4115bead28a" == response.data[ "country"]["id"] def test_patch_barrier_trading_bloc(self): payload = {"trading_bloc": "TB00016"} response = self.api_client.patch(self.url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code assert str(self.barrier.id) == response.data["id"] assert "TB00016" == response.data["trading_bloc"]["code"] def test_patch_barrier_caused_by_trading_bloc(self): payload = { "country": "82756b9a-5d95-e211-a939-e4115bead28a", "caused_by_trading_bloc": True, } response = self.api_client.patch(self.url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code assert str(self.barrier.id) == response.data["id"] assert response.data["caused_by_trading_bloc"] is True def test_patch_barrier_term(self): assert 1 == self.barrier.term test_parameters = [ { "term": None, "status_code": status.HTTP_200_OK, "expected_term": None }, { "term": 2, "status_code": status.HTTP_200_OK, "expected_term": 2 }, { "term": 1, "status_code": status.HTTP_200_OK, "expected_term": 1 }, { "term": 0, "status_code": status.HTTP_400_BAD_REQUEST, "expected_term": 1 }, { "term": "ahoy!", "status_code": status.HTTP_400_BAD_REQUEST, "expected_term": 1 }, { "term": "987", "status_code": status.HTTP_400_BAD_REQUEST, "expected_term": 1 }, ] for tp in test_parameters: with self.subTest(tp=tp): payload = { "term": tp["term"], } response = self.api_client.patch(self.url, format="json", data=payload) self.barrier.refresh_from_db() assert tp[ "status_code"] == response.status_code, f"Test params: {tp}" assert tp[ "expected_term"] == self.barrier.term, f"Test params: {tp}" def test_patch_barrier_to_affect_all_sectors(self): assert not self.barrier.all_sectors assert 1 == len(self.barrier.sectors) payload = {"all_sectors": True} response = self.api_client.patch(self.url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code assert str(self.barrier.id) == response.data["id"] assert response.data["all_sectors"] is True assert self.barrier.sectors == [ sector["id"] for sector in response.data["sectors"] ] def test_patch_barrier_priority(self): unknown_priority = BarrierPriority.objects.get(code="UNKNOWN") assert unknown_priority == self.barrier.priority priorities = ["HIGH", "MEDIUM", "LOW", "UNKNOWN"] for priority in priorities: with self.subTest(priority=priority): payload = { "priority": priority, "priority_summary": "wibble wobble" } response = self.api_client.patch(self.url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code assert str(self.barrier.id) == response.data["id"] assert priority == response.data["priority"]["code"] def test_add_barrier_categories(self): categories = Category.objects.all() category1 = categories[0] category2 = categories[1] self.barrier.categories.add(category1) self.barrier.refresh_from_db() assert 1 == self.barrier.categories.count() payload = {"categories": (category1.id, category2.id)} response = self.api_client.patch(self.url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code assert {category1.id, category2.id} == set( [category["id"] for category in response.data["categories"]]) def test_replace_barrier_categories(self): categories = Category.objects.all() category1 = categories[0] category2 = categories[1] category3 = categories[2] self.barrier.categories.add(category1) self.barrier.refresh_from_db() assert 1 == self.barrier.categories.count() payload = {"categories": (category2.id, category3.id)} response = self.api_client.patch(self.url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code assert {category2.id, category3.id} == set( [category["id"] for category in response.data["categories"]]) def test_flush_barrier_categories(self): categories = Category.objects.all() category1 = categories[0] self.barrier.categories.add(category1) self.barrier.refresh_from_db() assert 1 == self.barrier.categories.count() payload = {"categories": ()} response = self.api_client.patch(self.url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code assert not response.data["categories"] def test_update_barrier_adds_user_as_contributor(self): """ Users who edit a barrier should be added as a Contributor automatically. """ assert not get_team_members(self.barrier) payload = {"title": "Wibble wobble"} response = self.api_client.patch(self.url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code team_members = get_team_members(self.barrier) assert 1 == team_members.count() assert "Contributor" == team_members.first().role def test_add_barrier_government_organisations(self): assert 0 == self.barrier.organisations.count() payload = {"government_organisations": ("1", "2")} response = self.api_client.patch(self.url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code assert 2 == len(response.data["government_organisations"]) assert {1, 2} == set( [org.id for org in self.barrier.government_organisations]) def test_replace_barrier_government_organisations(self): org1 = Organisation.objects.get(id=1) org2 = Organisation.objects.get(id=2) org3 = Organisation.objects.get(id=3) self.barrier.organisations.add(org1, org2) assert 2 == self.barrier.government_organisations.count() payload = {"government_organisations": ("3", )} response = self.api_client.patch(self.url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code assert 1 == len(response.data["government_organisations"]) assert org3.id == response.data["government_organisations"][0]["id"] assert 1 == self.barrier.government_organisations.count() assert org3 == self.barrier.government_organisations.first() def test_flush_barrier_government_organisations(self): org1 = Organisation.objects.get(id=1) self.barrier.organisations.add(org1) self.barrier.refresh_from_db() assert 1 == self.barrier.government_organisations.count() payload = {"government_organisations": ()} response = self.api_client.patch(self.url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code assert not response.data["government_organisations"] assert 0 == self.barrier.government_organisations.count() def test_flushing_government_organisations_leaves_other_organisations_intact( self): org1 = OrganisationFactory(name="Wibble", organisation_type=0) org2 = Organisation.objects.get(id=1) self.barrier.organisations.add(org1, org2) self.barrier.refresh_from_db() assert 2 == self.barrier.organisations.count() assert 1 == self.barrier.government_organisations.count() payload = {"government_organisations": ()} response = self.api_client.patch(self.url, format="json", data=payload) assert status.HTTP_200_OK == response.status_code assert not response.data["government_organisations"] assert 0 == self.barrier.government_organisations.count() assert 1 == self.barrier.organisations.count() assert org1 == self.barrier.organisations.first()
class BarrierModifiedByTestCase(APITestMixin, APITestCase): """ Updating models related to a barrier should update the barrier's modified_by value """ def setUp(self): super().setUp() self.barrier = BarrierFactory() self.user1 = create_test_user( sso_user_id=self.sso_user_data_1["user_id"]) self.user2 = create_test_user( sso_user_id=self.sso_user_data_2["user_id"]) self.api_client1 = self.create_api_client(user=self.user1) self.api_client2 = self.create_api_client(user=self.user2) def test_note_changes_barrier_modified_by(self): assert self.barrier.modified_by != self.user1 # Create a note create_url = reverse("list-interactions", kwargs={"pk": self.barrier.id}) response = self.api_client1.post(create_url, json={"text": "note"}) assert response.status_code == status.HTTP_201_CREATED interaction_id = response.data.get("id") self.barrier.refresh_from_db() assert self.barrier.modified_by == self.user1 # Update the note update_url = reverse("get-interaction", kwargs={"pk": interaction_id}) response = self.api_client2.patch(update_url, json={"text": "updated note"}) assert response.status_code == status.HTTP_200_OK self.barrier.refresh_from_db() assert self.barrier.modified_by == self.user2 def test_assessment_changes_barrier_modified_by(self): assert self.barrier.modified_by != self.user1 # Create an assessment url = reverse("economic-assessment-list") response = self.api_client1.post(url, data={ "barrier_id": self.barrier.id, "rating": "HIGH" }) assert response.status_code == status.HTTP_201_CREATED assessment_id = response.data.get("id") self.barrier.refresh_from_db() assert self.barrier.modified_by == self.user1 # Update the assessment url = reverse("economic-assessment-detail", kwargs={"pk": assessment_id}) response = self.api_client2.patch(url, data={"rating": "MEDIUMLOW"}) assert response.status_code == status.HTTP_200_OK self.barrier.refresh_from_db() assert self.barrier.modified_by == self.user2 def test_team_member_changes_barrier_modified_by(self): assert self.barrier.modified_by != self.user1 # Create a new team member create_url = reverse("list-members", kwargs={"pk": self.barrier.id}) response = self.api_client1.post( create_url, format="json", data={ "user": { "profile": { "sso_user_id": self.user1.profile.sso_user_id } }, "role": "Contributor", }, ) assert response.status_code == status.HTTP_201_CREATED team_member_id = response.data.get("id") self.barrier.refresh_from_db() assert self.barrier.modified_by == self.user1 # Update the team member - only allowed to change the owner role TeamMember.objects.filter(pk=team_member_id).update(role="Owner") update_url = reverse("get-member", kwargs={"pk": team_member_id}) response = self.api_client2.patch( update_url, format="json", data={"user": self.user2.profile.sso_user_id}) assert response.status_code == status.HTTP_200_OK self.barrier.refresh_from_db() assert self.barrier.modified_by == self.user2 def test_wto_changes_barrier_modified_by(self): assert self.barrier.modified_by != self.user1 # Update WTO information url = reverse("get-barrier", kwargs={"pk": self.barrier.id}) response = self.api_client1.patch( url, json={ "wto_profile": { "case_number": {"ABC123"} }, }, ) assert response.status_code == status.HTTP_200_OK self.barrier.refresh_from_db() assert self.barrier.modified_by == self.user1
def test_datawarehouse_action_plans_values(self): owner = create_test_user() barrier = BarrierFactory(status_date=date.today()) ActionPlan.objects.filter(barrier=barrier).update( barrier=barrier, owner=owner, current_status="Progress update here", current_status_last_updated=date(2021, 8, 1), strategic_context="Strategic context text", strategic_context_last_updated=date(2021, 7, 1), ) action_plan = barrier.action_plan barrier.refresh_from_db() data_with_empty_action_plan = DataWorkspaceSerializer(barrier).data assert data_with_empty_action_plan["action_plan_added"] is False assert (data_with_empty_action_plan["action_plan"]["progress_update"] == "Progress update here") assert (data_with_empty_action_plan["action_plan"] ["progress_update_updated_on"] == "2021-08-01") assert (data_with_empty_action_plan["action_plan"]["strategic_context"] == "Strategic context text") assert (data_with_empty_action_plan["action_plan"] ["strategic_context_updated_on"] == "2021-07-01") milestone1 = ActionPlanMilestoneFactory(action_plan=action_plan) milestone2 = ActionPlanMilestoneFactory(action_plan=action_plan) task1 = ActionPlanTaskFactory( milestone=milestone1, action_type="SCOPING_AND_RESEARCH", action_type_category="Dialogue", status="IN_PROGRESS", ) task2 = ActionPlanTaskFactory( milestone=milestone1, action_type="LOBBYING", action_type_category="Lobbying by officials", status="IN_PROGRESS", ) task3 = ActionPlanTaskFactory( milestone=milestone2, action_type="BILATERAL_ENGAGEMENT", action_type_category="Creating and maintaining trade agreements", status="IN_PROGRESS", ) task4 = ActionPlanTaskFactory( milestone=milestone2, action_type="WHITEHALL_FUNDING_STREAMS", action_type_category="Prosperity fund", status="IN_PROGRESS", ) data_with_action_plan = DataWorkspaceSerializer(barrier).data assert data_with_action_plan["action_plan_added"] is True assert (data_with_action_plan["action_plan"] ["action_plan_percent_complete"] == "0.0%") assert data_with_action_plan["action_plan"][ "action_plan_owner"] == owner.email assert data_with_action_plan["action_plan"][ "all_intervention_types"] == ( "Dialogue - Scoping/Research,Lobbying by " "officials - Lobbying,Creating and maintaining " "trade agreements - Bilateral " "engagement,Prosperity fund - Whitehall funding " "streams") assert data_with_action_plan["action_plan"][ "number_of_objectives"] == 2 assert (data_with_action_plan["action_plan"] ["number_of_objectives_complete"] == 0) assert data_with_action_plan["action_plan"][ "number_of_interventions"] == 4 assert (data_with_action_plan["action_plan"] ["number_of_interventions_complete"] == 0) task4.status = "COMPLETED" task4.save() data_with_action_plan = DataWorkspaceSerializer(barrier).data assert data_with_action_plan["action_plan"][ "number_of_objectives"] == 2 assert (data_with_action_plan["action_plan"] ["number_of_objectives_complete"] == 0) assert data_with_action_plan["action_plan"][ "number_of_interventions"] == 4 assert (data_with_action_plan["action_plan"] ["number_of_interventions_complete"] == 1) assert (data_with_action_plan["action_plan"] ["action_plan_percent_complete"] == "25.0%") task3.status = "COMPLETED" task3.save() data_with_action_plan = DataWorkspaceSerializer(barrier).data assert data_with_action_plan["action_plan"][ "number_of_objectives"] == 2 assert (data_with_action_plan["action_plan"] ["number_of_objectives_complete"] == 1) assert data_with_action_plan["action_plan"][ "number_of_interventions"] == 4 assert (data_with_action_plan["action_plan"] ["number_of_interventions_complete"] == 2) assert (data_with_action_plan["action_plan"] ["action_plan_percent_complete"] == "50.0%") task1.status = "COMPLETED" task1.save() task2.status = "COMPLETED" task2.save() data_with_action_plan = DataWorkspaceSerializer(barrier).data assert data_with_action_plan["action_plan"][ "number_of_objectives"] == 2 assert (data_with_action_plan["action_plan"] ["number_of_objectives_complete"] == 2) assert data_with_action_plan["action_plan"][ "number_of_interventions"] == 4 assert (data_with_action_plan["action_plan"] ["number_of_interventions_complete"] == 4) assert (data_with_action_plan["action_plan"] ["action_plan_percent_complete"] == "100.0%")