Пример #1
0
def test__api_operational_area_permission__partial_update(
        model, location, success):
    operational_area = get_operational_area()
    user = get_user()
    perms = Permission.objects.filter(codename__contains=model.lower())
    user.operational_areas.add(operational_area)
    user.user_permissions.add(*perms)
    instance = model_factory_map[model](location=location)

    data = {
        "location": location.ewkt,
    }
    api_client = get_api_client(user=user)
    response = api_client.patch(
        reverse(f"v1:{model.lower()}-detail", kwargs={"pk": instance.pk}),
        data,
        format="json",
    )

    instance.refresh_from_db()
    if success:
        assert response.status_code == status.HTTP_200_OK
        assert instance.updated_by == user
    else:
        assert response.status_code == status.HTTP_403_FORBIDDEN
        assert instance.updated_by != user
def test__api_operational_area_permission__create(model, location, success):
    operational_area = get_operational_area()
    user = get_user()
    perms = Permission.objects.filter(codename__contains=model.lower())
    user.operational_areas.add(operational_area)
    user.user_permissions.add(*perms)
    device_type = get_traffic_control_device_type()

    location = location.ewkt if location else None

    if model == "Plan":
        data = {
            "name": "Test plan",
            "plan_number": "2020_1",
            "location": location,
            "planner": user.pk,
            "decision_maker": user.pk,
            "plans": {
                "barrier": [],
                "mount": [],
                "road_marking": [],
                "signpost": [],
                "traffic_light": [],
                "traffic_sign": [],
                "additional_sign": [],
            },
        }
    else:
        data = {
            "location": location,
            "device_type": device_type.pk,
            "decision_date": "2020-07-01",
            "lifecycle": Lifecycle.ACTIVE.value,
            "owner": get_owner().pk,
        }

        if model in ["BarrierPlan", "BarrierReal"]:
            data["road_name"] = "testroad"
        elif model in ["RoadMarkingPlan", "RoadMarkingReal"]:
            data["source_id"] = 1
            data["source_name"] = "test source"

    api_client = get_api_client(user=user)
    response = api_client.post(reverse(f"v1:{model.lower()}-list"),
                               data=data,
                               format="json")

    ModelClass = getattr(models, model)  # noqa: N806
    if success:
        assert response.status_code == status.HTTP_201_CREATED
        assert ModelClass.objects.count() == 1
    elif not location:
        assert response.status_code == status.HTTP_400_BAD_REQUEST
        assert response.json() == {
            "location": [_("This field may not be null.")]
        }
    else:
        assert response.status_code == status.HTTP_403_FORBIDDEN
        assert ModelClass.objects.count() == 0
Пример #3
0
def test__superuser_operational_area(location):
    user = get_user(admin=True)
    oa = get_operational_area(area=area)
    user.operational_areas.add(oa)

    in_area = user.location_is_in_operational_area(location)

    assert in_area
Пример #4
0
def test__user_operational_area__contains_location(location, expected):
    user = get_user()
    oa = get_operational_area(area=area)
    user.operational_areas.add(oa)

    in_area = user.location_is_in_operational_area(location)

    assert in_area == expected
Пример #5
0
def test__user_operational_area__bypass_operational_area(location):
    user = get_user(admin=True)
    user.bypass_operational_area = True
    user.save(update_fields=["bypass_operational_area"])
    oa = get_operational_area(area=area)
    user.operational_areas.add(oa)

    in_area = user.location_is_in_operational_area(location)

    assert in_area
def test__api_operational_area_permission__update(model, location, success):
    operational_area = get_operational_area()
    user = get_user()
    perms = Permission.objects.filter(codename__contains=model.lower())
    user.operational_areas.add(operational_area)
    user.user_permissions.add(*perms)
    device_type = get_traffic_control_device_type()
    instance = model_factory_map[model](location=location)

    if model == "Plan":
        data = {
            "name": "Test plan",
            "plan_number": "2020_1",
            "location": location.ewkt,
            "planner": user.pk,
            "decision_maker": user.pk,
            "plans": {
                "barrier": [],
                "mount": [],
                "road_marking": [],
                "signpost": [],
                "traffic_light": [],
                "traffic_sign": [],
                "additional_sign": [],
            },
        }
    else:
        data = {
            "location": location.ewkt,
            "device_type": device_type.pk,
            "decision_date": "2020-07-01",
            "lifecycle": Lifecycle.ACTIVE.value,
            "owner": get_owner().pk,
        }

        if model in ["BarrierPlan", "BarrierReal"]:
            data["road_name"] = "testroad"
        elif model in ["RoadMarkingPlan", "RoadMarkingReal"]:
            data["source_id"] = 1
            data["source_name"] = "test source"

    api_client = get_api_client(user=user)
    response = api_client.put(
        reverse(f"v1:{model.lower()}-detail", kwargs={"pk": instance.pk}),
        data,
        format="json",
    )

    instance.refresh_from_db()
    if success:
        assert response.status_code == status.HTTP_200_OK
        assert instance.updated_by == user
    else:
        assert response.status_code == status.HTTP_403_FORBIDDEN
        assert instance.updated_by != user
Пример #7
0
def test__user_group_operational_area__contains_location(location, expected):
    user = get_user()
    group = Group.objects.create(name="test group")
    user.groups.add(group)
    oa = get_operational_area(area)
    group_oa = GroupOperationalArea.objects.create(group=group)
    group_oa.areas.add(oa)

    in_area = user.location_is_in_operational_area(location)

    assert in_area == expected
def test__api_operational_area_permission__create__geojson(location, success):
    operational_area = get_operational_area()
    user = get_user()
    perms = Permission.objects.filter(codename__contains="barrierplan")
    user.operational_areas.add(operational_area)
    user.user_permissions.add(*perms)
    device_type = get_traffic_control_device_type()

    if location:
        location = json.loads(location.geojson)
        location.update({
            "crs": {
                "type": "name",
                "properties": {
                    "name": f"EPSG:{settings.SRID}"
                }
            }
        })

    data = {
        "location": location,
        "device_type": device_type.pk,
        "decision_date": "2020-07-01",
        "lifecycle": Lifecycle.ACTIVE.value,
        "owner": get_owner().pk,
        "road_name": "testroad",
    }

    api_client = get_api_client(user=user)
    response = api_client.post(
        f"{reverse('v1:barrierplan-list')}?geo_format=geojson",
        data=data,
        format="json")

    if success:
        assert response.status_code == status.HTTP_201_CREATED
        assert BarrierPlan.objects.count() == 1
    elif not location:
        assert response.status_code == status.HTTP_400_BAD_REQUEST
        assert response.json() == {
            "location": [_("This field may not be null.")]
        }
    else:
        assert response.status_code == status.HTTP_403_FORBIDDEN
        assert BarrierPlan.objects.count() == 0
Пример #9
0
def test__api_operational_area_permission__delete(model, location, success):
    operational_area = get_operational_area()
    user = get_user()
    perms = Permission.objects.filter(codename__contains=model.lower())
    user.operational_areas.add(operational_area)
    user.user_permissions.add(*perms)
    instance = model_factory_map[model](location=location)

    api_client = get_api_client(user=user)
    response = api_client.delete(
        reverse(f"v1:{model.lower()}-detail", kwargs={"pk": instance.pk}))

    instance.refresh_from_db()
    if success:
        assert response.status_code == status.HTTP_204_NO_CONTENT
        assert not instance.is_active
    else:
        assert response.status_code == status.HTTP_403_FORBIDDEN
        assert instance.is_active
 def setUp(self):
     self.operational_area = get_operational_area()
     self.user = get_user()
     self.admin = get_user(admin=True)