diff options
| author | Claude Sonnet 4.6 | 2026-06-15 17:39:00 +0000 |
|---|---|---|
| committer | Frederick Muriuki Muriithi | 2026-06-15 13:01:19 -0500 |
| commit | b8d3cc1919f48272d6a88228c77a00101c96349e (patch) | |
| tree | ea8cb560f36eaa79097cdf4d92619a179b82f542 | |
| parent | 4265f8d9d788f3403160a0e270a5f6bc8ed61076 (diff) | |
| download | gn-integration-tests-b8d3cc1919f48272d6a88228c77a00101c96349e.tar.gz | |
tests: add TestUserProfileWithToken and TestUserProfileWithoutToken
Test that GET /auth/user/ returns 200 with user fields when a valid Bearer token is present, and 401 when no token or a garbage token is supplied.
| -rw-r--r-- | tests/test_gn_auth_auth_flow.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/tests/test_gn_auth_auth_flow.py b/tests/test_gn_auth_auth_flow.py index 6019443..cb164cd 100644 --- a/tests/test_gn_auth_auth_flow.py +++ b/tests/test_gn_auth_auth_flow.py @@ -123,3 +123,71 @@ class TestTokenGrantRejection: assert resp.status_code == 401, ( f"Expected 401 for unknown email, got {resp.status_code}: {resp.text}" ) + + +# --------------------------------------------------------------------------- +# GET /auth/user/ — protected endpoint +# --------------------------------------------------------------------------- + +class TestUserProfileWithToken: + """GET /auth/user/ with a valid Bearer token returns the user's profile.""" + + def test_returns_200_with_valid_token( + self, gn_auth_url, http, access_token): + resp = http.get( + f"{gn_auth_url}/auth/user/", + headers={"Authorization": f"Bearer {access_token}"}, + timeout=30, + ) + assert resp.status_code == 200, ( + f"Expected 200 from /auth/user/ with token, " + f"got {resp.status_code}: {resp.text}" + ) + + def test_response_contains_user_fields( + self, gn_auth_url, http, access_token): + resp = http.get( + f"{gn_auth_url}/auth/user/", + headers={"Authorization": f"Bearer {access_token}"}, + timeout=30, + ) + data = resp.json() + for field in ("user_id", "email", "name"): + assert field in data, ( + f"Missing field '{field}' in user profile response: {data}" + ) + + def test_response_email_matches_credentials( + self, gn_auth_url, http, access_token, oauth2_credentials): + expected_email, *_ = oauth2_credentials + resp = http.get( + f"{gn_auth_url}/auth/user/", + headers={"Authorization": f"Bearer {access_token}"}, + timeout=30, + ) + assert resp.json().get("email") == expected_email, ( + f"Profile email {resp.json().get('email')!r} does not match " + f"credentials email {expected_email!r}" + ) + + +class TestUserProfileWithoutToken: + """GET /auth/user/ without a token must be rejected.""" + + def test_returns_401_without_token(self, gn_auth_url, http): + resp = http.get(f"{gn_auth_url}/auth/user/", timeout=30) + assert resp.status_code == 401, ( + f"Expected 401 from /auth/user/ without token, " + f"got {resp.status_code}: {resp.text}" + ) + + def test_returns_401_with_invalid_token(self, gn_auth_url, http): + resp = http.get( + f"{gn_auth_url}/auth/user/", + headers={"Authorization": "Bearer this-is-not-a-valid-token"}, + timeout=30, + ) + assert resp.status_code == 401, ( + f"Expected 401 from /auth/user/ with garbage token, " + f"got {resp.status_code}: {resp.text}" + ) |
