Skip to content

Commit

Permalink
Feat: add test and remove security suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
Rotheem committed Jan 2, 2025
1 parent cefe0a9 commit 8f78a25
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
4 changes: 1 addition & 3 deletions app/core/schools/endpoints_schools.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ async def update_school(
status_code=400,
detail=f"A school with the name {school.name} already exist",
)

await cruds_schools.update_school(
db=db,
school_id=school_id,
Expand All @@ -159,11 +158,10 @@ async def update_school(
school_update.email_regex is not None
and school_update.email_regex != school.email_regex
):
safe_email_regex = re.escape(school_update.email_regex)
await cruds_users.remove_users_from_school(db, school_id=school_id)
users = await cruds_users.get_users(db, schools_ids=[SchoolType.no_school])
for db_user in users:
if re.match(safe_email_regex, db_user.email):
if re.match(school_update.email_regex, db_user.email):

Check failure

Code scanning / CodeQL

Regular expression injection High

This regular expression depends on a
user-provided value
and is executed by
re.match
.
await cruds_users.update_user(
db,
db_user.id,
Expand Down
35 changes: 35 additions & 0 deletions tests/test_schools.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ def test_read_school(client: TestClient) -> None:
assert data["name"] == "ENS"


def test_create_school_with_used_name(client: TestClient) -> None:
token = create_api_access_token(admin_user)

response = client.post(
"/schools/",
json={
"name": "ENS",
"email_regex": r"^.*@ens.fr$",
},
headers={"Authorization": f"Bearer {token}"},
)
assert response.status_code == 400


def test_create_school(client: TestClient) -> None:
token = create_api_access_token(admin_user)

Expand All @@ -91,6 +105,17 @@ def test_create_school(client: TestClient) -> None:
assert data["school_id"] == school.json()["id"]


def test_update_school_with_used_name(client: TestClient) -> None:
token = create_api_access_token(admin_user)

response = client.patch(
f"/schools/{id_test_ens}",
json={"name": "centrale_lyon"},
headers={"Authorization": f"Bearer {token}"},
)
assert response.status_code == 400


def test_update_school(client: TestClient) -> None:
token = create_api_access_token(admin_user)

Expand Down Expand Up @@ -169,6 +194,16 @@ def test_create_user_corresponding_to_school(
assert user_detail.json()["school_id"] == school_id


def test_delete_base_school(client: TestClient) -> None:
token = create_api_access_token(admin_user)

response = client.delete(
f"/schools/{SchoolType.centrale_lyon.value}",
headers={"Authorization": f"Bearer {token}"},
)
assert response.status_code == 400


def test_delete_school(client: TestClient) -> None:
token = create_api_access_token(admin_user)

Expand Down

0 comments on commit 8f78a25

Please sign in to comment.