Skip to content

Commit

Permalink
chore: Un peu d'amour dans le backend (dépendences, simplifications d…
Browse files Browse the repository at this point in the history
…e syntaxe) (#1435)

* chore: remove unneeded formatted strings in tests

* chore: upgrade dependencies to get rid of warnings

see pytest-dev/pytest-asyncio#460

* chore: add a UUID Json encoder

* chore: simplify jwt_token type

* chore: move dev dependencies to dev group

* chore: upgrade poetry to lastest version

* chore: try to use a .poetry_version file
  • Loading branch information
GUL authored Jan 18, 2023
1 parent b751b56 commit de0a354
Show file tree
Hide file tree
Showing 20 changed files with 1,112 additions and 1,076 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
pull_request:

env:
POETRY_VERSION: 1.2.0
POETRY_VERSION: 1.3.2
PYTHON_VERSION: 3.10.6

concurrency:
Expand Down
2 changes: 1 addition & 1 deletion backend/.buildpacks
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
https://github.com/jonathanperret/scalingo-python-poetry-buildpack#6e192a664097ea777d32dfe436d98cf9da2e5e03
https://github.com/jonathanperret/scalingo-python-poetry-buildpack#8c941d3f9e2bca45691482d936a0dc2950f688eb
https://github.com/Scalingo/python-buildpack
1 change: 1 addition & 0 deletions backend/.poetry_version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.3.2
14 changes: 14 additions & 0 deletions backend/cdb/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# JSONEncoder monkey patch to serialize UUID
from json import JSONEncoder
from uuid import UUID

old_default = JSONEncoder.default


def new_default(self, o):
if isinstance(o, UUID):
return str(o)
return old_default(self, o)


JSONEncoder.default = new_default
6 changes: 3 additions & 3 deletions backend/cdb/api/db/crud/orientation_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ async def get_orientation_info(
orientation_info_response = await gql_session.execute(
get_orientation_info_gql(),
variable_values={
"notebook_id": str(notebook_id),
"structure_id": str(structure_id),
"new_referent_account_id": str(new_referent_account_id),
"notebook_id": notebook_id,
"structure_id": structure_id,
"new_referent_account_id": new_referent_account_id,
"with_new_referent": with_new_referent,
},
)
Expand Down
2 changes: 1 addition & 1 deletion backend/cdb/api/db/models/orientation_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class OrientationInfo(BaseModel):
@property
def former_referent_account_id(self) -> UUID | None:
return (
self.former_referents[0]["account"]["id"]
UUID(self.former_referents[0]["account"]["id"])
if len(self.former_referents) > 0
else None
)
Expand Down
2 changes: 1 addition & 1 deletion backend/cdb/api/v1/routers/orientation_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def gql_variables(self):
async def deny_orientation_request(
data: DenyBeneficiaryOrientationInput,
background_tasks: BackgroundTasks,
jwt_token: str | None = Header(default=None),
jwt_token: str = Header(default=None),
):
"""
Deny an orientation request
Expand Down
13 changes: 8 additions & 5 deletions backend/cdb/api/v1/routers/orientations.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,17 @@ async def change_beneficiary_orientation(

try:
raise_if_orientation_request_not_match_notebook(
str(data.orientation_request_id),
str(
data.orientation_request_id,
UUID(
orientation_info.beneficiary.get("orientation_request", [{}])[
0
].get("id")
)
if len(orientation_info.beneficiary.get("orientation_request")) > 0
else "",
if len(
orientation_info.beneficiary.get("orientation_request", [{}])
)
> 0
else None,
)
except Exception as exception:
logging.error(
Expand Down Expand Up @@ -209,7 +212,7 @@ async def change_beneficiary_orientation(


def raise_if_orientation_request_not_match_notebook(
orientation_request_id: str, beneficiary_request_id: str
orientation_request_id: UUID, beneficiary_request_id: UUID | None
) -> None:

if orientation_request_id != beneficiary_request_id:
Expand Down
1,941 changes: 985 additions & 956 deletions backend/poetry.lock

Large diffs are not rendered by default.

11 changes: 4 additions & 7 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ pydantic = {extras = ["email"], version = "^1.9.1"}
fastapi-asyncpg = "^1.0.1"
python-dateutil = "^2.8.2"
Jinja2 = "^3.1.2"
pytest-mock = "^3.7.0"
chardet = "^5.0.0"
httpx = "^0.23.0"
respx = "^0.20.0"
Expand All @@ -37,15 +36,13 @@ phonenumbers = "^8.12.56"
gql = {extras = ["aiohttp"], version = "^3.4.0"}
structlog = "^22.3.0"
structlog-sentry = "^2.0.0"
syrupy = "^3.0.5"

[tool.poetry.dev-dependencies]
[tool.poetry.group.dev.dependencies]
pytest = "^7.1.1"
pytest-asyncio = "^0.19.0"
pytest-watch = "^4.2.0"
pytest-asyncio = "0.20.3"
pytest-mock = "^3.7.0"
pytest-testmon = "^1.3.7"

[tool.poetry.group.dev.dependencies]
pytest-watch = "^4.2.0"
syrupy = "^3.0.5"

[build-system]
Expand Down
30 changes: 15 additions & 15 deletions backend/tests/api/test_admin_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ async def test_jwt_token_verification(
"lastname": "Bé",
"phone_numbers": "0601020304",
"position": "responsable",
"deployment_id": str(deployment_id_cd93),
"deployment_id": deployment_id_cd93,
},
"structure_id": str(structure_id_pe_livry),
"structure_id": structure_id_pe_livry,
},
headers={"jwt-token": f"{get_professional_jwt}"},
headers={"jwt-token": get_professional_jwt},
)

json = response.json()
Expand Down Expand Up @@ -57,11 +57,11 @@ async def test_insert_admin_structure_with_structure_in_db(
"lastname": "Bé",
"phone_numbers": "0601020304",
"position": "responsable",
"deployment_id": str(deployment_id_cd93),
"deployment_id": deployment_id_cd93,
},
"structure_id": str(structure_id_pe_livry),
"structure_id": structure_id_pe_livry,
},
headers={"jwt-token": f"{get_admin_structure_jwt}"},
headers={"jwt-token": get_admin_structure_jwt},
)

assert response.status_code == 200
Expand Down Expand Up @@ -112,11 +112,11 @@ async def test_insert_existing_admin_structure_in_structure_in_db(
"lastname": "timaitre",
"phone_numbers": "0601020304",
"position": "responsable",
"deployment_id": str(deployment_id_cd93),
"deployment_id": deployment_id_cd93,
},
"structure_id": str(structure_id_pe_livry),
"structure_id": structure_id_pe_livry,
},
headers={"jwt-token": f"{get_admin_structure_jwt}"},
headers={"jwt-token": get_admin_structure_jwt},
)

assert response.status_code == 200
Expand Down Expand Up @@ -159,11 +159,11 @@ async def test_insert_existing_admin_structure_in_existing_structure(
"lastname": "timaitre",
"phone_numbers": "0601020304",
"position": "responsable",
"deployment_id": str(deployment_id_cd93),
"deployment_id": deployment_id_cd93,
},
"structure_id": str(structure_id_pe_livry),
"structure_id": structure_id_pe_livry,
},
headers={"jwt-token": f"{get_admin_structure_jwt}"},
headers={"jwt-token": get_admin_structure_jwt},
)

assert response.status_code == 200
Expand Down Expand Up @@ -200,11 +200,11 @@ async def test_insert_deleted_admin_structure_in_structure_in_db(
"lastname": "timaitre",
"phone_numbers": "0601020304",
"position": "responsable",
"deployment_id": str(deployment_id_cd93),
"deployment_id": deployment_id_cd93,
},
"structure_id": str(structure_id_pe_livry),
"structure_id": structure_id_pe_livry,
},
headers={"jwt-token": f"{get_admin_structure_jwt}"},
headers={"jwt-token": get_admin_structure_jwt},
)

assert response.status_code == 200
Expand Down
12 changes: 6 additions & 6 deletions backend/tests/api/test_csv2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ async def test_parse_csv(
response = test_client.post(
"/v1/convert-file/beneficiaries",
files={"upload_file": ("filename", file, "text/csv")},
headers={"jwt-token": f"{get_manager_jwt}"},
headers={"jwt-token": get_manager_jwt},
)

assert response.status_code == 200
Expand All @@ -25,7 +25,7 @@ async def test_parse_csv_with_all_date_formats(
response = test_client.post(
"/v1/convert-file/beneficiaries",
files={"upload_file": ("filename", file, "text/csv")},
headers={"jwt-token": f"{get_manager_jwt}"},
headers={"jwt-token": get_manager_jwt},
)

assert response.json()[0]["data"]["Identifiant dans le SI*"] == "1234"
Expand All @@ -42,7 +42,7 @@ async def test_parse_csv_errors(
response = test_client.post(
"/v1/convert-file/beneficiaries",
files={"upload_file": ("filename", file, "text/csv")},
headers={"jwt-token": f"{get_manager_jwt}"},
headers={"jwt-token": get_manager_jwt},
)

assert response.json()[0]["errors"][0]["key"] == "Date de naissance*"
Expand All @@ -66,7 +66,7 @@ async def test_structure_parse_csv(
response = test_client.post(
"/v1/convert-file/structures",
files={"upload_file": ("filename", file, "text/csv")},
headers={"jwt-token": f"{get_manager_jwt}"},
headers={"jwt-token": get_manager_jwt},
)

assert response.status_code == 200
Expand All @@ -81,7 +81,7 @@ async def test_structure_parse_csv_with_error(
response = test_client.post(
"/v1/convert-file/structures",
files={"upload_file": ("filename", file, "text/csv")},
headers={"jwt-token": f"{get_manager_jwt}"},
headers={"jwt-token": get_manager_jwt},
)
data = response.json()

Expand Down Expand Up @@ -143,7 +143,7 @@ async def test_structure_parse_csv_with_missing_column_should_not_fail(
response = test_client.post(
"/v1/convert-file/structures",
files={"upload_file": ("filename", file, "text/csv")},
headers={"jwt-token": f"{get_manager_jwt}"},
headers={"jwt-token": get_manager_jwt},
)
data = response.json()

Expand Down
4 changes: 2 additions & 2 deletions backend/tests/api/test_import_beneficiaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async def import_beneficiaries(
):
return client.post(
"/v1/beneficiaries/bulk",
headers={"jwt-token": f"{token}"},
headers={"jwt-token": token},
data=json.dumps(
{
"need_orientation": True,
Expand All @@ -36,7 +36,7 @@ async def test_import_beneficiaries_must_be_done_by_a_manager(
):
response = test_client.post(
"/v1/beneficiaries/bulk",
headers={"jwt-token": f"{get_professional_jwt}"},
headers={"jwt-token": get_professional_jwt},
data=json.dumps(
{
"beneficiaries": [],
Expand Down
6 changes: 3 additions & 3 deletions backend/tests/api/test_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async def test_jwt_token_verification(
response = test_client.post(
ENDPOINT_PATH,
json={"email": sender_email, "firstname": "lionel", "lastname": "Bé"},
headers={"jwt-token": f"{get_admin_structure_jwt}"},
headers={"jwt-token": get_admin_structure_jwt},
)

json = response.json()
Expand All @@ -44,9 +44,9 @@ async def test_insert_admin_in_db(
"email": sender_email,
"firstname": "lionel",
"lastname": "Bé",
"deployment_id": str(deployment_id_cd93),
"deployment_id": deployment_id_cd93,
},
headers={"jwt-token": f"{get_admin_cdb_jwt}"},
headers={"jwt-token": get_admin_cdb_jwt},
)
admins = await get_managers_from_deployment(db_connection, deployment_id_cd93)

Expand Down
14 changes: 7 additions & 7 deletions backend/tests/api/test_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async def test_verify_no_token(
notebook_sophie_tifour: Notebook,
):
response = test_client.post(
f"/v1/notebooks/{str(notebook_sophie_tifour.id)}/members",
f"/v1/notebooks/{notebook_sophie_tifour.id}/members",
)
assert response.status_code == 401
json = response.json()
Expand All @@ -32,8 +32,8 @@ async def test_orientation_manager_not_allowed_to_add_notebook_member(
notebook_sophie_tifour: Notebook,
):
response = test_client.post(
f"/v1/notebooks/{str(notebook_sophie_tifour.id)}/members",
headers={"jwt-token": f"{giulia_diaby_jwt}"},
f"/v1/notebooks/{notebook_sophie_tifour.id}/members",
headers={"jwt-token": giulia_diaby_jwt},
)
assert response.status_code == 403
json = response.json()
Expand Down Expand Up @@ -68,9 +68,9 @@ async def test_add_notebook_member_as_no_referent(
db_connection: Connection,
):
response = test_client.post(
f"/v1/notebooks/{str(notebook_sophie_tifour.id)}/members",
f"/v1/notebooks/{notebook_sophie_tifour.id}/members",
json={"member_type": "no_referent"},
headers={"jwt-token": f"{get_professional_paul_camara_jwt}"},
headers={"jwt-token": get_professional_paul_camara_jwt},
)
assert response.status_code == 204
members = await get_notebook_members_by_notebook_id(
Expand Down Expand Up @@ -98,9 +98,9 @@ async def test_add_notebook_member_as_referent(
db_connection: Connection,
):
response = test_client.post(
f"/v1/notebooks/{str(notebook_sophie_tifour.id)}/members",
f"/v1/notebooks/{notebook_sophie_tifour.id}/members",
json={"member_type": "referent"},
headers={"jwt-token": f"{get_professional_paul_camara_jwt}"},
headers={"jwt-token": get_professional_paul_camara_jwt},
)
assert response.status_code == 204
members = await get_notebook_members_by_notebook_id(
Expand Down
4 changes: 2 additions & 2 deletions backend/tests/api/test_orientation_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ async def test_deny_orientation_request_email(
response = test_client.post(
DENY_ORIENTATION_ENDPOINT_PATH,
json={
"orientation_request_id": str(orientation_request_jennings_dee.id),
"orientation_request_id": orientation_request_jennings_dee.id,
},
headers={"jwt-token": f"{giulia_diaby_jwt}"},
headers={"jwt-token": giulia_diaby_jwt},
)
print(response.json())
assert response.status_code == 200
Expand Down
Loading

0 comments on commit de0a354

Please sign in to comment.