CORS/app/test/test_main.py

26 lines
1.1 KiB
Python
Raw Normal View History

2021-09-13 07:41:03 +00:00
#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'
2021-09-12 08:45:44 +00:00
from fastapi.testclient import TestClient
2021-09-13 07:41:03 +00:00
from datetime import timedelta
2021-09-12 08:45:44 +00:00
2021-09-13 07:41:03 +00:00
from app.main import app, create_access_token
2021-09-12 08:45:44 +00:00
2021-09-13 07:41:03 +00:00
client = TestClient(app)
2021-09-12 08:45:44 +00:00
2021-09-13 07:41:03 +00:00
def test_login():
response = client.post("/token", headers={"Content-Type": "application/x-www-form-urlencoded"}, data="username=johndoe&password=secret")
2021-09-12 08:45:44 +00:00
assert response.status_code == 200
2021-09-13 07:41:03 +00:00
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