Skip to content

Commit

Permalink
Fix: handle geometry in get one export api method (#215)
Browse files Browse the repository at this point in the history
* fix: handle geometry in get_one_export_api method

* tests: output response content on assertion error

* test: adjust test to geojson format

---------

Co-authored-by: Élie Bouttier <[email protected]>
  • Loading branch information
2 people authored and jacquesfize committed Oct 4, 2024
1 parent 200c12f commit 8a25b22
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
3 changes: 1 addition & 2 deletions backend/gn_module_export/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ def get_one_export_api(id_export):
filters["orderby"] = export.view_pk_column

query = export.get_view_query(limit=limit, offset=offset, filters=filters)

data = query.return_query()
data = query.as_geofeature() if query.geometry_field else query.return_query()

export_license = export.licence
data["license"] = dict()
Expand Down
45 changes: 24 additions & 21 deletions backend/gn_module_export/tests/test_blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_private_export_without_token_or_login(self, users, exports):
id_export=exports["private_user_associated"].id,
)
)
assert response.status_code == 403
assert response.status_code == 403, response.data

def test_private_export_with_fake_token(self, users, exports):
# with fake token
Expand All @@ -32,7 +32,7 @@ def test_private_export_with_fake_token(self, users, exports):
token="fake_token",
)
)
assert response.status_code == 403
assert response.status_code == 403, response.data

def test_private_export_with_good_token(self, users, exports):
# With good token
Expand All @@ -43,7 +43,7 @@ def test_private_export_with_good_token(self, users, exports):
token=exports["private_user_associated"].cor_roles_exports[0].token,
)
)
assert response.status_code == 200
assert response.status_code == 200, response.data

@pytest.mark.skipif(
os.environ.get("CI") == "true",
Expand All @@ -66,8 +66,8 @@ def test_private_export_with_token_in_header_Authorization_Bearer(self, users, e
),
headers=headers,
)
assert response.status_code == 200, response.data
assert response.request.headers["api-key"] == token
assert response.status_code == 200

def test_private_export_with_token_in_header_Authorization(self, users, exports):
# Compatibility with Swagger not adding "Bearer" prefix to authorization header value
Expand All @@ -86,8 +86,8 @@ def test_private_export_with_token_in_header_Authorization(self, users, exports)
),
headers=headers,
)
assert response.status_code == 200, response.data
assert response.request.headers["api-key"] == token
assert response.status_code == 200

def test_private_export_with_allowed_id_role(self, users, exports):
# with an allowed user and without token
Expand All @@ -98,7 +98,7 @@ def test_private_export_with_allowed_id_role(self, users, exports):
id_export=exports["private_user_associated"].id,
)
)
assert response.status_code == 200
assert response.status_code == 200, response.data

def test_private_admin_export(self, exports, users):
set_logged_user_cookie(self.client, users["admin_user"])
Expand All @@ -108,7 +108,7 @@ def test_private_admin_export(self, exports, users):
id_export=exports["private_user_associated"].id,
)
)
assert response.status_code == 200
assert response.status_code == 200, response.data

def test_private_export_with_allowed_group(self, group_and_user, exports):
# log with a user which is in a group associated with "private_group_associated" export
Expand All @@ -119,7 +119,7 @@ def test_private_export_with_allowed_group(self, group_and_user, exports):
id_export=exports["private_group_associated"].id,
)
)
assert response.status_code == 200
assert response.status_code == 200, response.data

def test_private_export_with_scope_3(self, users, exports):
set_logged_user_cookie(self.client, users["admin_user"])
Expand All @@ -129,7 +129,7 @@ def test_private_export_with_scope_3(self, users, exports):
id_export=exports["private_user_associated"].id,
)
)
assert response.status_code == 200
assert response.status_code == 200, response.data

def test_private_export_with_unautorized_user(self, users, exports):
# with an not allowed user and without token
Expand All @@ -140,7 +140,7 @@ def test_private_export_with_unautorized_user(self, users, exports):
id_export=exports["private_user_associated"].id,
)
)
assert response.status_code == 403
assert response.status_code == 403, response.data

def test_get_one_export_api_public(self, exports):
response = self.client.get(
Expand All @@ -149,7 +149,7 @@ def test_get_one_export_api_public(self, exports):
id_export=exports["public"].id,
)
)
assert response.status_code == 200
assert response.status_code == 200, response.data

def test_export_schema(self, exports, users):
schema = {
Expand All @@ -176,7 +176,7 @@ def test_export_schema(self, exports, users):
id_export=exports["private_user_associated"].id,
)
)
assert response.status_code == 200
assert response.status_code == 200, response.data
validate_json(instance=response.json, schema=schema)

def test_export_orderby(self, synthese_data, export_synthese_sinp, users):
Expand All @@ -187,13 +187,15 @@ def test_export_orderby(self, synthese_data, export_synthese_sinp, users):
id_export=export_synthese_sinp.id,
)
)
assert response.status_code == 200
assert response.status_code == 200, response.data

unordered_id_synthese = [i["id_synthese"] for i in response.json["items"]]
unordered_id_synthese = [
i["properties"]["id_synthese"] for i in response.json["items"]["features"]
]
ordered_id_synthese = sorted(unordered_id_synthese)
assert ordered_id_synthese == unordered_id_synthese

unordered_cd_noms = [i["cd_nom"] for i in response.json["items"]]
unordered_cd_noms = [i["properties"]["cd_nom"] for i in response.json["items"]["features"]]
ordered_cd_noms = sorted(unordered_cd_noms)
assert ordered_cd_noms != unordered_cd_noms
assert set(ordered_cd_noms).issubset(set(unordered_cd_noms))
Expand All @@ -203,19 +205,20 @@ def test_export_orderby(self, synthese_data, export_synthese_sinp, users):
"exports.get_one_export_api", id_export=export_synthese_sinp.id, orderby="cd_nom"
)
)
return_cd_noms = [i["cd_nom"] for i in response.json["items"]]
assert response.status_code == 200, response.data
return_cd_noms = [i["properties"]["cd_nom"] for i in response.json["items"]["features"]]
assert ordered_cd_noms == return_cd_noms

def test_unknown_export(self, exports, users):
set_logged_user_cookie(self.client, users["admin_user"])
response = self.client.get(url_for("exports.get_one_export_api", id_export=1000000))
assert response.status_code == 404
assert response.status_code == 404, response.data

def test_get_all_exports_with_scope_3(self, exports, users):
fixtures_export_id = set([export.id for _, export in exports.items()])
set_logged_user_cookie(self.client, users["admin_user"])
response = self.client.get(url_for("exports.get_exports"))
assert response.status_code == 200
assert response.status_code == 200, response.data
response_id_export = set([exp["id"] for exp in response.json])
assert fixtures_export_id.issubset(response_id_export)

Expand All @@ -226,7 +229,7 @@ def test_get_all_exports_with_scope_1_2_no_group(self, exports, users):
allowed_fixture_ids.add(exp.id)
set_logged_user_cookie(self.client, users["self_user"])
response = self.client.get(url_for("exports.get_exports"))
assert response.status_code == 200
assert response.status_code == 200, response.data
response_id_export = set([exp["id"] for exp in response.json])
assert allowed_fixture_ids.issubset(response_id_export)

Expand All @@ -237,7 +240,7 @@ def test_get_all_exports_with_scope_1_2_group(self, exports, users, group_and_us
allowed_fixture_ids.add(exp.id)
set_logged_user_cookie(self.client, group_and_user["user"])
response = self.client.get(url_for("exports.get_exports"))
assert response.status_code == 200
assert response.status_code == 200, response.data
response_id_export = set([exp["id"] for exp in response.json])
assert allowed_fixture_ids.issubset(response_id_export)

Expand All @@ -249,5 +252,5 @@ def test_get_all_exports_with_empty_response(self, users, group_and_user):
db.session.delete(export)
set_logged_user_cookie(self.client, group_and_user["user"])
response = self.client.get(url_for("exports.get_exports"))
assert response.status_code == 200
assert response.status_code == 200, response.data
assert response.json == []

0 comments on commit 8a25b22

Please sign in to comment.