-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_server.py
52 lines (39 loc) · 1.4 KB
/
test_server.py
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
import jwt
import pytest
from datetime import datetime, timedelta
from fastapi.testclient import TestClient
from src.api import app, create_jwt, JWT_SECRET_KEY, JWT_ALGORITHM
client = TestClient(app)
@pytest.fixture()
def valid_token():
return create_jwt("test_user")
@pytest.fixture()
def expired_token():
payload = {
"user_id": "test_user",
"exp": datetime.now() - timedelta(days=1)
}
return jwt.encode(payload, JWT_SECRET_KEY, algorithm=JWT_ALGORITHM)
@pytest.mark.skip(reason="Skipping the test")
def test_github_login():
response = client.get("/github-login")
assert response.status_code == 302
assert "https://github.com/login/oauth/authorize" in response.headers["location"]
@pytest.mark.skip(reason="Skipping the test")
def test_verify_token(valid_token):
headers = {
"Authorization" : f"Bearer {valid_token}"
}
response = client.get("/verify-token", headers=headers)
assert response.status_code == 200
assert response.json()["username"] == "test_user"
assert "email" in response.json()
def test_verify_token_invalid(expired_token):
headers = {
"Authorization": f"Bearer {expired_token}"
}
response = client.get("/verify-token", headers=headers)
assert response.status_code == 401
def test_get_recommendations_unauthorized():
response = client.post("/api/recommendations/")
assert response.status_code == 401