コード例 #1
0
    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
コード例 #2
0
    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 == []
コード例 #3
0
    def test_barrier_status_date_filter_multiple_status(self):
        barrier_open_pending = BarrierFactory(
            status=1, estimated_resolution_date="2020-06-06")
        barrier_open_in_progress = BarrierFactory(
            status=2, estimated_resolution_date="2020-06-06")
        barrier_resolved_in_part = BarrierFactory(status=3,
                                                  status_date="2020-02-02")
        barrier_resolved_in_full = BarrierFactory(status=4,
                                                  status_date="2020-02-02")

        saved_barrier_list = [
            str(barrier_open_pending.id),
            str(barrier_open_in_progress.id),
            str(barrier_resolved_in_part.id),
            str(barrier_resolved_in_full.id),
        ]

        url = (f'{reverse("list-barriers")}?'
               "status=1,2,3,4&"
               "status_date_open_pending_action=2020-01-01,2021-06-30&"
               "status_date_open_in_progress=2020-01-01,2021-06-30&"
               "status_date_resolved_in_part=2020-01-01,2021-06-30&"
               "status_date_resolved_in_full=2020-01-01,2021-06-30&")

        response = self.api_client.get(url)

        assert response.status_code == status.HTTP_200_OK

        barrier_ids = []
        for result in response.data["results"]:
            barrier_ids.append(result["id"])

        assert barrier_ids == saved_barrier_list
コード例 #4
0
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
コード例 #5
0
    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"]
コード例 #6
0
    def test_list_barriers_get_multiple_barriers(self):
        count = 2
        BarrierFactory.create_batch(count)

        url = reverse("list-barriers")
        response = self.api_client.get(url)
        assert response.status_code == status.HTTP_200_OK
        assert count == response.data["count"]
コード例 #7
0
 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)
コード例 #8
0
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}"
コード例 #9
0
    def test_list_barriers_count(self):
        count = 2
        BarrierFactory.create_batch(count)

        assert count == Barrier.objects.count()

        dataset_url = reverse("dataset:barrier-list")
        response = self.api_client.get(dataset_url)
        assert status.HTTP_200_OK == response.status_code
        assert count == len(response.data["results"])
コード例 #10
0
    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
コード例 #11
0
    def test_filter_barrier_tags__no_match(self):
        tag1 = BarrierTagFactory()
        BarrierFactory(tags=(tag1, ))
        BarrierFactory()

        url = f'{reverse("list-barriers")}?tags=123,321'

        response = self.api_client.get(url)

        assert status.HTTP_200_OK == response.status_code
        assert 0 == response.data["count"]
コード例 #12
0
    def test_filter_barrier_tags__single_tag(self):
        tag = BarrierTagFactory()
        barrier = BarrierFactory(tags=(tag, ))
        BarrierFactory()

        url = f'{reverse("list-barriers")}?tags={tag.id}'

        response = self.api_client.get(url)

        assert status.HTTP_200_OK == response.status_code
        assert 1 == response.data["count"]
        assert str(barrier.id) == response.data["results"][0]["id"]
コード例 #13
0
    def test_list_barriers_order_by(self):
        test_parameters = [
            "reported_on",
            "-reported_on",
            "modified_on",
            "-modified_on",
            "status",
            "-status",
            "priority",
            "-priority",
            "country",
            "-country",
        ]
        priorities = BarrierPriorityFactory.create_batch(3)
        bahamas = "a25f66a0-5d95-e211-a939-e4115bead28a"
        barrier1 = BarrierFactory(
            reported_on=datetime(2020, 1, 1, tzinfo=UTC),
            modified_on=datetime(2020, 1, 2, tzinfo=UTC),
            status=1,
            priority=priorities[0],
            country=bahamas,
        )
        bhutan = "ab5f66a0-5d95-e211-a939-e4115bead28a"
        barrier2 = BarrierFactory(
            reported_on=datetime(2020, 2, 2, tzinfo=UTC),
            modified_on=datetime(2020, 2, 3, tzinfo=UTC),
            status=2,
            priority=priorities[1],
            country=bhutan,
        )
        spain = "86756b9a-5d95-e211-a939-e4115bead28a"
        barrier3 = BarrierFactory(
            reported_on=datetime(2020, 3, 3, tzinfo=UTC),
            modified_on=datetime(2020, 3, 4, tzinfo=UTC),
            status=7,
            priority=priorities[2],
            country=spain,
        )

        assert 3 == Barrier.objects.count()

        for order_by in test_parameters:
            with self.subTest(order_by=order_by):
                url = f'{reverse("list-barriers")}?ordering={order_by}'
                response = self.api_client.get(url)

                assert response.status_code == status.HTTP_200_OK
                barriers = Barrier.objects.all().order_by(order_by)
                assert barriers.count() == response.data["count"]
                response_list = [b["id"] for b in response.data["results"]]
                db_list = [str(b.id) for b in barriers]
                assert db_list == response_list
コード例 #14
0
    def test_list_barriers_status_filter__multivalues(self):
        BarrierFactory(term=1)
        BarrierFactory(term=2)
        BarrierFactory(term=4)

        assert 3 == Barrier.objects.count()

        url = f'{reverse("list-barriers")}?status=2,4'
        response = self.api_client.get(url)

        assert response.status_code == status.HTTP_200_OK
        barriers = Barrier.objects.filter(status__in=[2, 4])
        assert response.data["count"] == barriers.count()
コード例 #15
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
コード例 #16
0
    def test_has_wto_raised_date_filter(self):
        barrier1 = BarrierFactory(wto_profile__raised_date="2020-01-31")
        barrier2 = BarrierFactory(wto_profile__raised_date=None)
        barrier3 = BarrierFactory(wto_profile=None)

        assert 3 == Barrier.objects.count()

        url = f'{reverse("list-barriers")}?wto=has_raised_date'
        response = self.api_client.get(url)

        assert status.HTTP_200_OK == response.status_code
        assert 1 == response.data["count"]
        assert str(barrier1.id) == response.data["results"][0]["id"]
コード例 #17
0
    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."
コード例 #18
0
    def test_has_wto_case_number_filter(self):
        barrier1 = BarrierFactory(wto_profile__case_number="CASE123")
        barrier2 = BarrierFactory(wto_profile__case_number="")
        barrier3 = BarrierFactory(wto_profile=None)

        assert 3 == Barrier.objects.count()

        url = f'{reverse("list-barriers")}?wto=has_case_number'
        response = self.api_client.get(url)

        assert status.HTTP_200_OK == response.status_code
        assert 1 == response.data["count"]
        assert str(barrier1.id) == response.data["results"][0]["id"]
コード例 #19
0
    def test_list_barriers_status_filter(self):
        prob_status = 2
        BarrierFactory.create_batch(2, term=1)
        BarrierFactory(term=prob_status)

        assert 3 == Barrier.objects.count()

        url = f'{reverse("list-barriers")}?status={prob_status}'
        response = self.api_client.get(url)

        assert response.status_code == status.HTTP_200_OK
        barriers = Barrier.objects.filter(status=prob_status)
        assert response.data["count"] == barriers.count()
コード例 #20
0
    def test_wto_should_be_notified_filter(self):
        barrier1 = BarrierFactory(wto_profile__wto_should_be_notified=True)
        barrier2 = BarrierFactory(wto_profile__wto_should_be_notified=False)
        barrier3 = BarrierFactory(wto_profile=None)

        assert 3 == Barrier.objects.count()

        url = f'{reverse("list-barriers")}?wto=wto_should_be_notified'
        response = self.api_client.get(url)

        assert status.HTTP_200_OK == response.status_code
        assert 1 == response.data["count"]
        assert str(barrier1.id) == response.data["results"][0]["id"]
コード例 #21
0
    def test_list_barriers_country_filter(self):
        country_id = "a05f66a0-5d95-e211-a939-e4115bead28a"
        BarrierFactory.create_batch(2, country=country_id)
        BarrierFactory()

        assert 3 == Barrier.objects.count()

        url = f'{reverse("list-barriers")}?location={country_id}'
        response = self.api_client.get(url)

        assert response.status_code == status.HTTP_200_OK
        barriers = Barrier.objects.filter(country=country_id)
        assert barriers.count() == response.data["count"]
コード例 #22
0
    def test_barriers(self):
        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"]})

        assert barrier1 not in saved_search.barriers
        assert barrier2 in saved_search.barriers
        assert barrier3 not in saved_search.barriers
コード例 #23
0
    def test_list_barriers_text_filter_based_on_title(self):
        barrier1 = BarrierFactory(title="Wibble blockade")
        _barrier2 = BarrierFactory(title="Wobble blockade")
        barrier3 = BarrierFactory(title="Look wibble in the middle")

        assert 3 == Barrier.objects.count()

        url = f'{reverse("list-barriers")}?text=wibble'

        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(barrier1.id), str(barrier3.id)} == set(barrier_ids)
コード例 #24
0
    def test_list_barriers_sector_filter(self):
        sector1 = "75debee7-a182-410e-bde0-3098e4f7b822"
        sector2 = "af959812-6095-e211-a939-e4115bead28a"
        BarrierFactory(sectors=[sector1])
        barrier = BarrierFactory(sectors=[sector2])

        assert 2 == Barrier.objects.count()

        url = f'{reverse("list-barriers")}?sector={sector2}'
        response = self.api_client.get(url)

        assert status.HTTP_200_OK == response.status_code
        assert 1 == response.data["count"]
        assert str(barrier.id) == response.data["results"][0]["id"]
コード例 #25
0
    def test_list_barriers_category_filter(self):
        BarrierFactory()
        cat1 = CategoryFactory()
        barrier = BarrierFactory()
        barrier.categories.add(cat1)

        assert 2 == Barrier.objects.count()

        url = f'{reverse("list-barriers")}?category={cat1.id}'
        response = self.api_client.get(url)

        assert status.HTTP_200_OK == response.status_code
        assert 1 == response.data["count"]
        assert str(barrier.id) == response.data["results"][0]["id"]
コード例 #26
0
    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 == []
コード例 #27
0
    def test_list_barriers_country_filter__no_find(self):
        spain = "86756b9a-5d95-e211-a939-e4115bead28a"
        germany = "83756b9a-5d95-e211-a939-e4115bead28a"
        BarrierFactory(country=spain)
        BarrierFactory()

        assert 2 == Barrier.objects.count()
        assert 0 == Barrier.objects.filter(country=germany).count()

        url = f'{reverse("list-barriers")}?location={germany}'
        response = self.api_client.get(url)

        assert response.status_code == status.HTTP_200_OK
        assert 0 == response.data["count"]
コード例 #28
0
    def test_include_archived_barriers(self):
        user = create_test_user()
        barrier = BarrierFactory()

        assert 1 == Barrier.objects.count()

        response = self.api_client.get(self.url)
        assert status.HTTP_200_OK == response.status_code
        assert 1 == response.data["count"]

        barrier.archive(user=user)
        response = self.api_client.get(self.url)
        assert status.HTTP_200_OK == response.status_code
        assert 1 == response.data["count"]
コード例 #29
0
    def test_list_barriers_text_filter_based_on_public_id(self):
        barrier1 = BarrierFactory(public_barrier___title="Public Title")
        barrier2 = BarrierFactory(public_barrier___title="Public Title")
        barrier3 = BarrierFactory()

        public_id = f"PID-{barrier2.public_barrier.id}"

        assert 3 == Barrier.objects.count()

        from logging import getLogger

        logger = getLogger(__name__)

        search_queryset = Barrier.objects.annotate(
            search=SearchVector("summary"),
        ).filter(
            Q(code=public_id)
            | Q(search=public_id)
            | Q(title__icontains=public_id)
            | Q(public_barrier__id__iexact=public_id.lstrip("PID-").upper()))

        # assert 1 == search_queryset.count()
        search_queryset_count = search_queryset.count()
        logger.info(f"search_queryset_count: {search_queryset_count}")
        search_queryset_public_ids = [
            f"search_queryset: {barrier.title}: {barrier.public_barrier.id}"
            for barrier in search_queryset
        ]
        for public_id_string in search_queryset_public_ids:
            logger.info(public_id_string)

        public_ids = [
            f"Barrier.objects.all: {barrier.title}: {barrier.public_barrier.id}"
            for barrier in Barrier.objects.all()
        ]
        for public_id_string in public_ids:
            logger.info(public_id_string)

        url = f'{reverse("list-barriers")}?text={public_id}'
        logger.info(f"URL: {url}")
        response = self.api_client.get(url)

        assert status.HTTP_200_OK == response.status_code
        for name, value in response.data.items():
            logger.info(f"response.data {name}: {value}")

        assert 1 == response.data["count"]
        barrier_ids = [b["id"] for b in response.data["results"]]
        assert {str(barrier2.id)} == set(barrier_ids)
コード例 #30
0
    def test_organisation_filter(self):
        org1 = Organisation.objects.get(id=1)
        barrier1 = BarrierFactory()
        barrier1.organisations.add(org1)
        barrier2 = BarrierFactory()

        assert 2 == Barrier.objects.count()
        assert 1 == Barrier.objects.filter(organisations__in=[org1]).count()

        url = f'{reverse("list-barriers")}?organisation={org1.id}'
        response = self.api_client.get(url)

        assert status.HTTP_200_OK == response.status_code
        assert 1 == response.data["count"]
        assert str(barrier1.id) == response.data["results"][0]["id"]