def test__device_type__target_model__invalid(target_model, factory):
    client = get_api_client(user=get_user(admin=True))
    related_model = factory()
    device_type = related_model.device_type
    data = {
        "target_model": target_model.value,
    }

    response = client.patch(
        reverse("v1:trafficcontroldevicetype-detail",
                kwargs={"pk": device_type.id}),
        data,
        format="json",
    )

    device_type.refresh_from_db()
    assert response.status_code == status.HTTP_400_BAD_REQUEST
    assert response.json() == {
        "target_model": [
            (f"Some traffic control devices related to this device type instance "
             f"will become invalid if target_model value is changed to "
             f"{target_model.value}. target_model can not be changed until this "
             f"is resolved."),
        ]
    }
    assert not device_type.target_model
Exemple #2
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
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
def test__owner_api__list(is_admin):
    Owner.objects.all().delete()
    user = get_user(admin=is_admin)
    client = get_api_client(user)
    get_owner(name_fi="owner 1")
    get_owner(name_fi="owner 2")

    response = client.get(reverse("v1:owner-list"))

    assert response.status_code == status.HTTP_200_OK
    assert response.data["count"] == 2
def test__device_type__response_code_attribute():
    client = get_api_client()
    dt = get_traffic_control_device_type()

    response = client.get(
        reverse("v1:trafficcontroldevicetype-detail", kwargs={"pk": dt.pk}))
    response_data = response.json()

    assert "code" in response_data, (
        "Removing `code` attribute from the TrafficControlDeviceType API will break "
        "the admin traffic sign icon frontend functionality in "
        "AdminTrafficSignIconSelectWidget!")
def test__owner_api__retrieve(is_admin):
    Owner.objects.all().delete()
    user = get_user(admin=is_admin)
    client = get_api_client(user)
    owner_1 = get_owner(name_fi="owner 1")
    get_owner(name_fi="owner 2")

    response = client.get(reverse("v1:owner-detail", kwargs={"pk":
                                                             owner_1.pk}))

    assert response.status_code == status.HTTP_200_OK
    assert response.data["id"] == str(owner_1.pk)
    assert response.data["name_fi"] == owner_1.name_fi
def test__owner_api__destroy(is_admin):
    Owner.objects.all().delete()
    user = get_user(admin=is_admin)
    client = get_api_client(user)
    owner = get_owner(name_fi="foo", name_en="bar")

    response = client.delete(
        reverse("v1:owner-detail", kwargs={"pk": owner.pk}))

    if is_admin:
        assert response.status_code == status.HTTP_204_NO_CONTENT
        assert Owner.objects.count() == 0
    else:
        assert response.status_code == status.HTTP_403_FORBIDDEN
        assert Owner.objects.count() == 1
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
def test__device_type__target_model__valid(target_model, factory):
    client = get_api_client(user=get_user(admin=True))
    related_model = factory()
    device_type = related_model.device_type
    data = {
        "target_model": target_model.value,
    }

    response = client.patch(
        reverse("v1:trafficcontroldevicetype-detail",
                kwargs={"pk": device_type.id}),
        data,
        format="json",
    )

    device_type.refresh_from_db()
    assert response.status_code == status.HTTP_200_OK
    assert device_type.target_model == target_model
Exemple #11
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 test__owner_api__create(is_admin):
    Owner.objects.all().delete()
    user = get_user(admin=is_admin)
    client = get_api_client(user)
    data = {
        "name_fi": "Omistajan nimi",
        "name_en": "Owner name",
    }

    response = client.post(reverse("v1:owner-list"), data=data)

    if is_admin:
        owner = Owner.objects.first()
        assert response.status_code == status.HTTP_201_CREATED
        assert Owner.objects.count() == 1
        assert owner.name_fi == data["name_fi"]
        assert owner.name_en == data["name_en"]
    else:
        assert response.status_code == status.HTTP_403_FORBIDDEN
        assert Owner.objects.count() == 0
def test__owner_api__partial_update(is_admin):
    Owner.objects.all().delete()
    user = get_user(admin=is_admin)
    client = get_api_client(user)
    owner = get_owner(name_fi="foo", name_en="bar")
    data = {
        "name_fi": "Omistajan nimi",
    }

    response = client.patch(reverse("v1:owner-detail", kwargs={"pk":
                                                               owner.pk}),
                            data=data)
    owner.refresh_from_db()
    assert Owner.objects.count() == 1

    if is_admin:
        assert response.status_code == status.HTTP_200_OK
        assert owner.name_fi == data["name_fi"]
        assert owner.name_en == "bar"
    else:
        assert response.status_code == status.HTTP_403_FORBIDDEN
        assert owner.name_fi == "foo"
        assert owner.name_en == "bar"