Skip to content

Commit 253cf78

Browse files
committed
completed user login and existing user fixture
1 parent b304e09 commit 253cf78

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

test/conftest.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from app.database import get_db
1010
from app.database import Base
1111
from app.oauth2 import create_access_token
12-
from app import models
12+
from app import models, utils
1313
from alembic import command
1414

1515

@@ -41,5 +41,36 @@ def override_get_db():
4141
yield session
4242
finally:
4343
session.close()
44+
4445
app.dependency_overrides[get_db] = override_get_db
4546
yield TestClient(app)
47+
48+
49+
@pytest.fixture
50+
def test_user_exist(session):
51+
"""
52+
Create a test user directly in the test database session.
53+
This is used to verify login test
54+
"""
55+
user = models.Users(
56+
email="gbolahan@gmail.com",
57+
password=utils.hash("password123")
58+
)
59+
session.add(user)
60+
session.commit()
61+
session.refresh(user)
62+
63+
return {"id": user.id, "email": user.email, "password": "password123"}
64+
65+
66+
@pytest.fixture
67+
def test_user_exist_not(client):
68+
user_data = {"email": "ola@gmail.com",
69+
"password": "123"}
70+
res = client.post("/users/", json=user_data)
71+
72+
assert res.status_code == 201
73+
74+
new_user = res.json()
75+
new_user['password'] = user_data['password']
76+
return new_user

test/test_users.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,21 @@
66

77
def test_create_user(client):
88
res = client.post(
9-
"/users/", json={"email": "hello123@gmail.com", "password": "password123", "id": 1})
9+
"/users/", json={"email": "omogbolahan@gmail.com", "password": "password123", "id": 1})
1010

1111
print(res.json())
1212
new_user = schemas.UserResponse(**res.json())
13-
assert new_user.email == "hello123@gmail.com"
14-
assert res.status_code == 201
13+
assert new_user.email == "omogbolahan@gmail.com"
14+
assert res.status_code == 201
15+
16+
17+
def test_login_user(test_user_exist, client):
18+
res = client.post(
19+
"/login", data={"username": test_user_exist['email'], "password": test_user_exist['password']})
20+
login_res = schemas.Token(**res.json())
21+
payload = jwt.decode(login_res.access_token,
22+
settings.jwt_secret_key, algorithms=[settings.jwt_algorithm])
23+
id = payload.get("user_id")
24+
assert id == test_user_exist['id']
25+
assert login_res.token_type == "bearer"
26+
assert res.status_code == 200

0 commit comments

Comments
 (0)