test-api¶
Integration tests for the API blueprint.
Covers token issuance and the two protected resource endpoints
(/api/users and /api/rooms). All requests that require Basic
Auth use the pre-encoded credentials for the test_user fixture
(testuser:password123).
Note
The Base64 string dGVzdHVzZXI6cGFzc3dvcmQxMjM= decodes to
testuser:password123 and must stay in sync with the credentials
set in the test_user() fixture. If those
credentials change, re-encode with:
python -c "import base64; print(base64.b64encode(b'testuser:password123').decode())"
- tests.test_api.test_get_rooms_with_token(client, test_user, test_room)¶
Room list endpoint returns 200 and includes seeded rooms for a valid token.
Obtains a bearer token via Basic Auth, then issues a
GET /api/roomsrequest with that token. Asserts200 OK, confirming both that the endpoint is accessible to authenticated users and that thetest_room()fixture room is visible in the response scope.- Parameters:
client (flask.testing.FlaskClient) – The test client provided by
client().test_user (app.models.User) – The room owner whose credentials are used to obtain the token, provided by
test_user().test_room (app.models.Room) – A seeded room owned by test_user, provided by
test_room(). Declared as a dependency to ensure the room exists in the database before the request is made.
- tests.test_api.test_get_token(client, test_user)¶
Token endpoint issues a bearer token for valid Basic Auth credentials.
Sends a
POSTto/api/tokenswith a pre-encoded Basic Auth header and asserts that the response is200 OKand the JSON body contains atokenkey.- Parameters:
client (flask.testing.FlaskClient) – The test client provided by
client().test_user (app.models.User) – The seeded user whose credentials are encoded in the Authorization header, provided by
test_user().
- tests.test_api.test_get_users_with_token(client, test_user)¶
User list endpoint returns 200 for a valid bearer token.
Performs a two-step flow: first obtains a token via
POST /api/tokensusing Basic Auth, then uses that token as a Bearer credential in aGET /api/usersrequest. Asserts that the authenticated request succeeds with200 OK.- Parameters:
client (flask.testing.FlaskClient) – The test client provided by
client().test_user (app.models.User) – The seeded user whose credentials are used to obtain the token, provided by
test_user().
- tests.test_api.test_get_users_without_token(client)¶
User list endpoint rejects unauthenticated requests with 401.
Sends a
GETto/api/userswith no Authorization header and asserts that the API returns401 Unauthorized, confirming that the endpoint is not publicly accessible.- Parameters:
client (flask.testing.FlaskClient) – The test client provided by
client().