CORS/app/test/test_main.py

26 lines
1.1 KiB
Python

#curl -i -X POST http://localhost:8000/token -H "Content-Type: application/x-www-form-urlencoded" -d "username=johndoe&password=secret"
# curl -X 'GET' \
# 'http://localhost:8000/users/me/' \
# -H 'accept: application/json' \
# -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJqb2huZG9lIiwiZXhwIjoxNjMxNDQ4MjQ1fQ.DrM92jgRiry0uXBXn-61rRehATW4zDhHUWoGR6lv6Us'
from fastapi.testclient import TestClient
from datetime import timedelta
from app.main import app, create_access_token
client = TestClient(app)
def test_login():
response = client.post("/token", headers={"Content-Type": "application/x-www-form-urlencoded"}, data="username=johndoe&password=secret")
assert response.status_code == 200
def test_token():
access_token_expires = timedelta(minutes=1)
access_token = create_access_token(
data={"sub": "johndoe"}, expires_delta=access_token_expires
)
response = client.get("/users/me", headers={"accept": "application/json", "Authorization": f"Bearer {access_token}"})
assert response.status_code == 200