Skip to content

Commit c425b4c

Browse files
committed
add tests for POST term & insert site_user
1 parent 020490d commit c425b4c

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

src/auth/crud.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ async def get_site_user(db_session: AsyncSession, session_id: str) -> None | Sit
101101
)
102102

103103

104+
async def site_user_exists(db_session: AsyncSession, computing_id: str) -> bool:
105+
user = await db_session.scalar(
106+
sqlalchemy
107+
.select(SiteUser)
108+
.where(SiteUser.computing_id == computing_id)
109+
)
110+
return user is not None
111+
112+
104113
# update the optional user info for a given site user (e.g., display name, profile picture, ...)
105114
async def update_site_user(
106115
db_session: AsyncSession,

src/officers/crud.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import logging
2+
from datetime import datetime
23

34
import sqlalchemy
45
from fastapi import HTTPException
56

7+
import auth.crud
8+
import auth.tables
69
import database
710
import utils
811
from data import semesters
@@ -157,6 +160,14 @@ async def create_new_officer_info(
157160
new_officer_info: OfficerInfo
158161
) -> bool:
159162
"""Return False if the officer already exists & don't do anything."""
163+
if not await auth.crud.site_user_exists(db_session, new_officer_info.computing_id):
164+
# if computing_id has not been created as a site_user yet, add them
165+
db_session.add(auth.tables.SiteUser(
166+
computing_id=new_officer_info.computing_id,
167+
first_logged_in=datetime.now(),
168+
last_logged_in=datetime.now()
169+
))
170+
160171
existing_officer_info = await db_session.scalar(
161172
sqlalchemy
162173
.select(OfficerInfo)

src/officers/urls.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from fastapi import APIRouter, Body, HTTPException, Request
44
from fastapi.responses import JSONResponse, PlainTextResponse
5+
from sqlalchemy.exc import IntegrityError
56

67
import auth.crud
78
import database
@@ -166,6 +167,8 @@ async def new_officer_term(
166167
await WebsiteAdmin.has_permission_or_raise(db_session, session_computing_id)
167168

168169
for officer_info in officer_info_list:
170+
# if user with officer_info.computing_id has never logged into the website before,
171+
# a site_user tuple will be created for them.
169172
await officers.crud.create_new_officer_info(db_session, OfficerInfo(
170173
computing_id = officer_info.computing_id,
171174
# TODO (#71): use sfu api to get legal name from officer_info.computing_id

tests/integration/test_officers.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio # NOTE: don't comment this out; it's required
2+
import json
23

34
import pytest
45
from httpx import ASGITransport, AsyncClient
@@ -120,11 +121,21 @@ async def test__endpoints(client, database_setup):
120121
response = await client.get(f"/officers/info/{load_test_db.SYSADMIN_COMPUTING_ID}")
121122
assert response.status_code == 401
122123

123-
# TODO: this
124-
response = await client.post("officers/term/abc11")
124+
response = await client.post("officers/term", content=json.dumps([{
125+
"computing_id": "ehbc12",
126+
"position": OfficerPosition.DIRECTOR_OF_MULTIMEDIA,
127+
"start_date": "2025-12-29"
128+
}]))
129+
assert response.status_code == 401
130+
131+
response = await client.post("officers/term", content=json.dumps([{
132+
"computing_id": "ehbc12",
133+
"position": "balargho",
134+
"start_date": "2025-12-29"
135+
}]))
136+
assert response.status_code == 400
125137

126138
# TODO: add tests for the POST & PATCH commands
127-
# TODO: ensure that the database is being reset every time!
128139

129140
@pytest.mark.anyio
130141
async def test__endpoints_admin(client, database_setup):
@@ -169,4 +180,20 @@ async def test__endpoints_admin(client, database_setup):
169180
response = await client.get("/officers/info/balargho")
170181
assert response.status_code == 404
171182

183+
response = await client.get("/officers/terms/ehbc12?include_future_terms=true")
184+
assert response.status_code == 200
185+
assert response.json() == []
186+
187+
response = await client.post("officers/term", content=json.dumps([{
188+
"computing_id": "ehbc12",
189+
"position": OfficerPosition.DIRECTOR_OF_MULTIMEDIA,
190+
"start_date": "2025-12-29"
191+
}]))
192+
assert response.status_code == 200
193+
194+
response = await client.get("/officers/terms/ehbc12?include_future_terms=true")
195+
assert response.status_code == 200
196+
assert response.json() != []
197+
assert len(response.json()) == 1
198+
172199
# TODO: ensure that all endpoints are tested at least once

0 commit comments

Comments
 (0)