Add tests
This commit is contained in:
parent
1605527367
commit
e4e76df2ed
|
@ -1,10 +1,26 @@
|
|||
#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
|
||||
import app.main
|
||||
from datetime import timedelta
|
||||
|
||||
client = TestClient(app.main.app)
|
||||
from app.main import app, create_access_token
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
def test_read_main():
|
||||
response = client.get("/")
|
||||
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
|
||||
assert response.json() == {"msg": "Hello World"}
|
||||
|
|
Loading…
Reference in New Issue