From e4e76df2edf8e0bb7964b85eececfc738cd912b4 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Mon, 13 Sep 2021 19:41:03 +1200 Subject: [PATCH] Add tests --- app/test/test_main.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/app/test/test_main.py b/app/test/test_main.py index 394ffe6..cde0784 100644 --- a/app/test/test_main.py +++ b/app/test/test_main.py @@ -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 - assert response.json() == {"msg": "Hello World"} + + +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 \ No newline at end of file