Repository navigation
Expand file tree
/
Copy pathtest_auth.py
More file actions
67 lines (55 loc) · 2.68 KB
/
Copy pathtest_auth.py
File metadata and controls
67 lines (55 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
from datetime import datetime, timedelta, timezone
import jwt
import pytest
from fastapi.testclient import TestClient
os.environ["JWT_SECRET_KEY"] = "testing-only-secret-key-never-use-in-production-1234567890"
from auth_app import ALGORITHM, SECRET_KEY, app, issue_token, users
def test_login_and_protected_route():
with TestClient(app) as client:
response = client.post("/token", data={"username": "alice", "password": "training-password-123"})
assert response.status_code == 200
assert response.json()["token_type"] == "bearer"
token = response.json()["access_token"]
me = client.get("/users/me", headers={"Authorization": f"Bearer {token}"})
assert me.status_code == 200
assert me.json() == {"username": "alice"}
@pytest.mark.parametrize("username,password", [
("alice", "wrong-password"), ("unknown", "training-password-123"),
])
def test_invalid_credentials(username, password):
with TestClient(app) as client:
response = client.post("/token", data={"username": username, "password": password})
assert response.status_code == 401
assert "access_token" not in response.json()
def test_missing_token():
with TestClient(app) as client:
response = client.get("/users/me")
assert response.status_code == 401
assert response.headers["WWW-Authenticate"] == "Bearer"
@pytest.mark.parametrize("kind", ["malformed", "expired", "wrong-key", "wrong-algorithm", "no-exp", "no-sub", "unknown-user"])
def test_invalid_tokens(kind):
claims = {"sub": "alice", "exp": datetime.now(timezone.utc) + timedelta(minutes=1)}
key, algorithm = SECRET_KEY, ALGORITHM
if kind == "expired":
claims["exp"] = datetime.now(timezone.utc) - timedelta(minutes=1)
elif kind == "no-exp":
del claims["exp"]
elif kind == "no-sub":
del claims["sub"]
elif kind == "unknown-user":
claims["sub"] = "unknown"
elif kind == "wrong-key":
key = "different-test-key-1234567890123456789012345678901234567890"
elif kind == "wrong-algorithm":
algorithm = "HS384"
token = "not.a.valid-token" if kind == "malformed" else jwt.encode(claims, key, algorithm=algorithm)
with TestClient(app) as client:
response = client.get("/users/me", headers={"Authorization": f"Bearer {token}"})
assert response.status_code == 401
assert response.headers["WWW-Authenticate"] == "Bearer"
def test_disabled_user(monkeypatch):
monkeypatch.setitem(users["alice"], "disabled", True)
with TestClient(app) as client:
token = issue_token("alice")
assert client.get("/users/me", headers={"Authorization": f"Bearer {token}"}).status_code == 401