def test_new_barrier_ids_current_user(self):
        """
        Changes made by the current user should not be included in new_barrier_ids
        """
        barrier1 = BarrierFactory(priority="LOW")
        barrier2 = BarrierFactory(priority="MEDIUM")
        barrier3 = BarrierFactory(priority="HIGH")

        user = create_test_user(sso_user_id=self.sso_user_data_1["user_id"])

        saved_search = SavedSearch.objects.create(
            user=user, name="Medium", filters={"priority": ["MEDIUM"]})
        saved_search.mark_as_seen()

        assert saved_search.new_barrier_ids == []

        # Barriers created by current user should be ignored
        api_client = self.create_api_client(user=user)
        report = ReportFactory(priority="MEDIUM", created_by=user)
        submit_url = reverse("submit-report", kwargs={"pk": report.id})
        response = api_client.put(submit_url)
        assert status.HTTP_200_OK == response.status_code

        saved_search = SavedSearch.objects.get(pk=saved_search.pk)
        assert report.pk not in saved_search.new_barrier_ids
        assert saved_search.new_barrier_ids == []

        # Barriers changed by current user should be ignored
        barrier1.priority = BarrierPriority.objects.get(code="MEDIUM")
        barrier1.modified_by = user
        barrier1.save()

        saved_search = SavedSearch.objects.get(pk=saved_search.pk)
        assert barrier1.pk not in saved_search.new_barrier_ids
        assert saved_search.new_barrier_ids == []
    def test_mark_as_seen(self):
        """
        Calling mark_as_seen should reset updated and new barrier ids
        """
        barrier1 = BarrierFactory(priority="LOW")
        barrier2 = BarrierFactory(priority="MEDIUM")

        user = create_test_user(sso_user_id=self.sso_user_data_1["user_id"])

        saved_search = SavedSearch.objects.create(
            user=user, name="Medium", filters={"priority": ["MEDIUM"]})
        saved_search.mark_as_seen()
        assert saved_search.new_barrier_ids == []
        assert saved_search.updated_barrier_ids == []

        barrier1.priority = BarrierPriority.objects.get(code="MEDIUM")
        barrier1.save()

        barrier2.summary = "New summary"
        barrier2.save()

        saved_search = SavedSearch.objects.get(pk=saved_search.pk)
        assert saved_search.new_barrier_ids == [barrier1.pk]
        assert barrier1 in saved_search.new_barriers_since_notified
        assert barrier1 not in saved_search.updated_barriers_since_notified
        assert barrier2 not in saved_search.new_barriers_since_notified
        assert barrier2 in saved_search.updated_barriers_since_notified

        saved_search = SavedSearch.objects.get(pk=saved_search.pk)
        saved_search.mark_as_seen()
        assert saved_search.new_barrier_ids == []
        assert saved_search.updated_barrier_ids == []
    def test_updated_barriers_since_notified_other_user(self):
        """
        Changes made by the other users should be included in updated_barriers_since_notified
        """
        barrier1 = BarrierFactory(priority="LOW")
        barrier2 = BarrierFactory(priority="MEDIUM")
        barrier3 = BarrierFactory(priority="HIGH")

        user = create_test_user(sso_user_id=self.sso_user_data_1["user_id"])

        saved_search = SavedSearch.objects.create(
            user=user, name="Medium", filters={"priority": ["MEDIUM"]})
        saved_search.mark_as_notified()

        assert saved_search.updated_barriers_since_notified.exists() is False

        barrier1.summary = "New summary"
        barrier1.save()

        barrier2.summary = "New summary"
        barrier2.save()

        saved_search = SavedSearch.objects.get(pk=saved_search.pk)
        assert barrier2 in saved_search.updated_barriers_since_notified
        assert saved_search.updated_barriers_since_notified.count() == 1
    def test_new_barrier_ids_other_user(self):
        """
        Changes made by other users should be included in new_barrier_ids
        """
        barrier1 = BarrierFactory(priority="LOW")
        barrier2 = BarrierFactory(priority="MEDIUM")
        barrier3 = BarrierFactory(priority="HIGH")

        user = create_test_user(sso_user_id=self.sso_user_data_1["user_id"])

        saved_search = SavedSearch.objects.create(
            user=user, name="Medium", filters={"priority": ["MEDIUM"]})
        saved_search.mark_as_seen()

        assert saved_search.new_barrier_ids == []

        # Newly created barriers should be in the list
        barrier4 = BarrierFactory(priority="MEDIUM")
        barrier5 = BarrierFactory(priority="UNKNOWN")
        saved_search = SavedSearch.objects.get(pk=saved_search.pk)
        assert saved_search.new_barrier_ids == [barrier4.id]

        # Existing barriers should be in the list
        barrier1.priority = BarrierPriority.objects.get(code="MEDIUM")
        barrier1.save()

        saved_search = SavedSearch.objects.get(pk=saved_search.pk)
        assert barrier1.pk in saved_search.new_barrier_ids
    def test_new_barriers_since_notified_other_user(self):
        """
        Changes made by other users should be included in new_barriers_since_notified
        """
        barrier1 = BarrierFactory(priority="LOW")
        barrier2 = BarrierFactory(priority="MEDIUM")
        barrier3 = BarrierFactory(priority="HIGH")

        user = create_test_user(sso_user_id=self.sso_user_data_1["user_id"])
        user2 = create_test_user(sso_user_id=self.sso_user_data_2["user_id"])

        saved_search = SavedSearch.objects.create(
            user=user, name="Medium", filters={"priority": ["MEDIUM"]})
        saved_search.mark_as_notified()

        assert saved_search.new_barriers_since_notified.exists() is False

        # Newly created barriers should be in the list
        barrier4 = BarrierFactory(priority="MEDIUM")
        barrier5 = BarrierFactory(priority="UNKNOWN")
        saved_search = SavedSearch.objects.get(pk=saved_search.pk)
        assert barrier4 in saved_search.new_barriers_since_notified
        assert saved_search.new_barriers_since_notified.count() == 1

        # Existing barriers should be in the list
        barrier1.priority = BarrierPriority.objects.get(code="MEDIUM")
        barrier1.modified_by = user2
        barrier1.save()

        saved_search = SavedSearch.objects.get(pk=saved_search.pk)
        assert barrier1 in saved_search.new_barriers_since_notified
    def test_status_date_is_null(self):
        expected_status_date = None
        barrier = BarrierFactory()
        barrier.status_date = None
        barrier.save()

        serializer = BarrierCsvExportSerializer(barrier)
        assert expected_status_date == serializer.data["status_date"]
    def test_resolved_date(self):
        expected_resolved_date = "02/2022"
        barrier = BarrierFactory()
        barrier.status_date = datetime.datetime(2022, 2, 17)
        barrier.status = 4
        barrier.save()

        serializer = BarrierCsvExportSerializer(barrier)
        assert expected_resolved_date == serializer.data["resolved_date"]
class TestBarrierTradeDirection(APITestMixin, TestCase):
    def setUp(self):
        super().setUp()
        self.barrier = BarrierFactory()
        self.url = reverse("get-barrier", kwargs={"pk": self.barrier.id})

    def test_get_barrier_without_trade_direction(self):
        """
        By default all existing barriers start with trade_direction not begin set.
        """
        self.barrier.trade_direction = None
        self.barrier.save()

        assert 1 == Barrier.objects.count()
        assert self.barrier.trade_direction is None

        response = self.api_client.get(self.url)

        assert status.HTTP_200_OK == response.status_code
        assert response.data["trade_direction"] is None

    def test_get_barrier_with_trade_direction(self):
        response = self.api_client.get(self.url)

        assert status.HTTP_200_OK == response.status_code
        assert 1 == response.data["trade_direction"]["id"]

    def test_set_trade_direction_to_none(self):
        """
        Trade direction cannot be set to None.
        """
        payload = {"trade_direction": None}
        response = self.api_client.patch(self.url, format="json", data=payload)

        assert status.HTTP_400_BAD_REQUEST == response.status_code
        assert 1 == self.barrier.trade_direction

    def test_patch_trade_direction(self):
        payload = {"trade_direction": 2}
        response = self.api_client.patch(self.url, format="json", data=payload)

        assert status.HTTP_200_OK == response.status_code
        assert 2 == response.data["trade_direction"]["id"]

    def test_patch_trade_direction_with_invalid_values(self):
        invalid_values = [0, 14, "123", "Wibble", [], {"a": 6}, "null"]

        for value in invalid_values:
            with self.subTest(value=value):
                payload = {"trade_direction": 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}"
Example #9
0
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()
Example #10
0
    def test_caused_by_trading_bloc_clears_on_non_trading_bloc_country(self):
        barrier = BarrierFactory(
            country="82756b9a-5d95-e211-a939-e4115bead28a",
            caused_by_trading_bloc=True,
        )
        assert barrier.country == "82756b9a-5d95-e211-a939-e4115bead28a"
        assert barrier.caused_by_trading_bloc is True

        barrier.country = "b05f66a0-5d95-e211-a939-e4115bead28a"
        barrier.save()

        assert barrier.country == "b05f66a0-5d95-e211-a939-e4115bead28a"
        assert barrier.caused_by_trading_bloc is None
Example #11
0
    def test_trade_direction_filter(self):
        barrier1 = BarrierFactory()
        barrier1.trade_direction = None
        barrier1.save()
        barrier2 = BarrierFactory(trade_direction=1)
        barrier3 = BarrierFactory(trade_direction=2)

        assert 3 == Barrier.objects.count()

        url = f'{reverse("list-barriers")}?trade_direction=1,2'

        response = self.api_client.get(url)
        assert status.HTTP_200_OK == response.status_code
        assert 2 == response.data["count"]
        barrier_ids = [b["id"] for b in response.data["results"]]
        assert {str(barrier2.id), str(barrier3.id)} == set(barrier_ids)
Example #12
0
    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
Example #13
0
    def test_should_notify(self):
        barrier1 = BarrierFactory(priority="LOW")
        barrier2 = BarrierFactory(priority="MEDIUM")

        user = create_test_user(sso_user_id=self.sso_user_data_1["user_id"])

        saved_search = SavedSearch.objects.create(
            user=user,
            name="Medium",
            filters={"priority": ["MEDIUM"]},
        )
        saved_search.mark_as_notified()

        assert saved_search.should_notify() is False

        saved_search.notify_about_additions = True
        saved_search.save()
        assert saved_search.should_notify() is False

        # An addition to the saved search
        barrier1.priority = BarrierPriority.objects.get(code="MEDIUM")
        barrier1.save()

        saved_search = SavedSearch.objects.get(pk=saved_search.pk)
        assert saved_search.should_notify() is True

        saved_search.notify_about_additions = False
        saved_search.notify_about_updates = True
        saved_search.save()
        assert saved_search.should_notify() is False

        # An update to a barrier in search search
        barrier2.summary = "New summary"
        barrier2.save()
        saved_search = SavedSearch.objects.get(pk=saved_search.pk)
        assert saved_search.should_notify() is True

        saved_search.notify_about_additions = True
        saved_search.mark_as_notified()
        saved_search = SavedSearch.objects.get(pk=saved_search.pk)
        assert saved_search.should_notify() is False
Example #14
0
    def test_updated_barrier_ids_current_user(self):
        """
        Changes made by the current user should not be included in updated_barrier_ids
        """
        barrier1 = BarrierFactory(priority="LOW")
        barrier2 = BarrierFactory(priority="MEDIUM")
        barrier3 = BarrierFactory(priority="HIGH")

        user = create_test_user(sso_user_id=self.sso_user_data_1["user_id"])

        saved_search = SavedSearch.objects.create(
            user=user, name="Medium", filters={"priority": ["MEDIUM"]})
        saved_search.mark_as_seen()

        assert saved_search.updated_barrier_ids == []

        barrier2.summary = "New summary"
        barrier2.modified_by = user
        barrier2.save()

        saved_search = SavedSearch.objects.get(pk=saved_search.pk)
        assert barrier2.pk not in saved_search.updated_barrier_ids
        assert saved_search.updated_barrier_ids == []
Example #15
0
    def test_changed_filter(self):
        url = f'{reverse("list-barriers")}?public_view=changed'
        barrier1 = BarrierFactory(
            public_barrier___public_view_status=PublicBarrierStatus.PUBLISHED,
            public_barrier__last_published_on="2020-08-01",
            priority="LOW",
            source="COMPANY",
        )
        barrier2 = BarrierFactory(
            public_barrier___public_view_status=PublicBarrierStatus.PUBLISHED,
            public_barrier__last_published_on="2020-08-01",
        )
        barrier3 = BarrierFactory(
            public_barrier___public_view_status=PublicBarrierStatus.PUBLISHED,
            public_barrier__last_published_on="2020-08-01",
        )
        CachedHistoryItem.objects.all().delete()

        # No barriers should have 'changed' since being published
        response = self.api_client.get(url)
        assert status.HTTP_200_OK == response.status_code
        assert 0 == response.data["count"]

        # Change some fields that do not affect the public barrier
        barrier1.source = "TRADE"
        barrier1.priority = BarrierPriority.objects.get(code="MEDIUM")
        barrier1.save()

        # No barriers should have 'changed' since being published
        response = self.api_client.get(url)
        assert status.HTTP_200_OK == response.status_code
        assert 0 == response.data["count"]

        # Change a field that does affect the public barrier
        barrier1.summary = "New summary"
        barrier1.save()

        # barrier1 should now be in the search results for changed barriers
        response = self.api_client.get(url)
        assert status.HTTP_200_OK == response.status_code
        assert 1 == response.data["count"]
        barrier_ids = set(
            [result["id"] for result in response.data["results"]])
        assert set([str(barrier1.id)]) == barrier_ids

        # Change a field that does affect the public barrier
        barrier2.sectors = ["9f38cecc-5f95-e211-a939-e4115bead28a"]
        barrier2.save()

        # barrier2 should now also be in the search results for changed barriers
        response = self.api_client.get(url)
        assert status.HTTP_200_OK == response.status_code
        assert 2 == response.data["count"]
        barrier_ids = set(
            [result["id"] for result in response.data["results"]])
        assert set([str(barrier1.id), str(barrier2.id)]) == barrier_ids
Example #16
0
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