Spaces:
Runtime error
Runtime error
Commit ·
bb4b25f
1
Parent(s): d454ab0
Added Test to test Login Route
Browse files- tests/database.py +2 -3
- tests/test_users.py +22 -7
tests/database.py
CHANGED
|
@@ -12,7 +12,7 @@ SQLALCHEMY_DATABASE_URL = f"postgresql://{setting.database_username}:{setting.da
|
|
| 12 |
engine = create_engine(SQLALCHEMY_DATABASE_URL)
|
| 13 |
TestingSessionMaker = sessionmaker(autoflush=False, autocommit=False, bind=engine)
|
| 14 |
|
| 15 |
-
@pytest.fixture(
|
| 16 |
def session():
|
| 17 |
Base.metadata.drop_all(bind=engine)
|
| 18 |
Base.metadata.create_all(bind=engine)
|
|
@@ -22,13 +22,12 @@ def session():
|
|
| 22 |
finally:
|
| 23 |
db.close()
|
| 24 |
|
| 25 |
-
@pytest.fixture(
|
| 26 |
def client(session):
|
| 27 |
def override_get_db():
|
| 28 |
try:
|
| 29 |
yield session
|
| 30 |
finally:
|
| 31 |
session.close()
|
| 32 |
-
|
| 33 |
app.dependency_overrides[get_db] = override_get_db
|
| 34 |
yield TestClient(app)
|
|
|
|
| 12 |
engine = create_engine(SQLALCHEMY_DATABASE_URL)
|
| 13 |
TestingSessionMaker = sessionmaker(autoflush=False, autocommit=False, bind=engine)
|
| 14 |
|
| 15 |
+
@pytest.fixture()
|
| 16 |
def session():
|
| 17 |
Base.metadata.drop_all(bind=engine)
|
| 18 |
Base.metadata.create_all(bind=engine)
|
|
|
|
| 22 |
finally:
|
| 23 |
db.close()
|
| 24 |
|
| 25 |
+
@pytest.fixture()
|
| 26 |
def client(session):
|
| 27 |
def override_get_db():
|
| 28 |
try:
|
| 29 |
yield session
|
| 30 |
finally:
|
| 31 |
session.close()
|
|
|
|
| 32 |
app.dependency_overrides[get_db] = override_get_db
|
| 33 |
yield TestClient(app)
|
tests/test_users.py
CHANGED
|
@@ -1,10 +1,20 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
| 2 |
from tests.database import client, session
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def test_create_user(client):
|
| 10 |
response = client.post("/users/", json={"email": "abdullah@gmail.com", "password": "abdullah1234"})
|
|
@@ -12,6 +22,11 @@ def test_create_user(client):
|
|
| 12 |
assert response.status_code == 201
|
| 13 |
assert new_user.email == "abdullah@gmail.com"
|
| 14 |
|
| 15 |
-
def test_login_user(client):
|
| 16 |
-
response = client.post("/login", data={"username": "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
assert response.status_code==200
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
from app.config import setting
|
| 3 |
+
from app.schemas import user, token
|
| 4 |
+
from jose import jwt
|
| 5 |
from tests.database import client, session
|
| 6 |
|
| 7 |
+
@pytest.fixture
|
| 8 |
+
def create_test_user(client):
|
| 9 |
+
user_data={
|
| 10 |
+
"email": "abdullah@gmail.com",
|
| 11 |
+
"password": "abdullah1234"
|
| 12 |
+
}
|
| 13 |
+
response = client.post("/users/",json=user_data)
|
| 14 |
+
assert response.status_code == 201
|
| 15 |
+
new_user=response.json()
|
| 16 |
+
new_user["password"]="abdullah1234"
|
| 17 |
+
return new_user
|
| 18 |
|
| 19 |
def test_create_user(client):
|
| 20 |
response = client.post("/users/", json={"email": "abdullah@gmail.com", "password": "abdullah1234"})
|
|
|
|
| 22 |
assert response.status_code == 201
|
| 23 |
assert new_user.email == "abdullah@gmail.com"
|
| 24 |
|
| 25 |
+
def test_login_user(client,create_test_user):
|
| 26 |
+
response = client.post("/login", data={"username": create_test_user["email"], "password": create_test_user["password"]})
|
| 27 |
+
login_response=token.Token(**response.json())
|
| 28 |
+
payload = jwt.decode(login_response.access_token, setting.secret_key, algorithms=[setting.algorithm])
|
| 29 |
+
id=payload.get("id")
|
| 30 |
+
assert id == create_test_user["id"]
|
| 31 |
+
assert login_response.token_type == "bearer"
|
| 32 |
assert response.status_code==200
|