-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_user.py
200 lines (147 loc) · 5.25 KB
/
test_user.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import pytest
from icecream import ic
from fastapi.testclient import TestClient
from sqlmodel import Session, SQLModel, create_engine
from sqlmodel.pool import StaticPool
from main import app
from encrypt import hash_password, check_password
from model import UserBase, User
from database import get_db
@pytest.fixture(name="session")
def session_fixture():
engine = create_engine(
"sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool
)
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
yield session
@pytest.fixture(name="client")
def client_fixture(session: Session):
def get_session_override():
return session
app.dependency_overrides[get_db] = get_session_override
client = TestClient(app)
yield client
app.dependency_overrides.clear()
def test_create_user(client: TestClient):
response = client.post(
"/users/",
json={
"username": "Admin",
"email": "email@email.com",
"fullname": "Adam Adamsson",
},
)
data = response.json()
assert response.status_code == 200
assert data["username"] == "Admin"
assert data["email"] == "email@email.com"
def test_create_user_incomplete(client: TestClient):
# No secret_name
response = client.post("/users/", json={"name": "Deadpond"})
assert response.status_code == 422
def test_create_user_invalid(client: TestClient):
# secret_name has an invalid type
response = client.post(
"/users/",
json={
"username": "deadpond",
"email": {"message": "Not okey!"},
},
)
assert response.status_code == 422
def test_list_users(session: Session, client: TestClient):
user_1 = UserBase(username="deadpond", email="dive@email.com")
user_2 = UserBase(username="rustyman", email="rusty@email.com")
session.add(user_1)
session.add(user_2)
session.commit()
response = client.get("/users/")
data = response.json()["users"]
assert response.status_code == 200
ic(data)
assert len(data) == 2
assert data[0]["username"] == user_1.username
def test_read_user(session: Session, client: TestClient):
user_1 = UserBase(username="deadpond", email="dive@email.com")
session.add(user_1)
session.commit()
response = client.get(f"/users/{user_1.id}")
data = response.json()
assert response.status_code == 200
assert data["username"] == user_1.username
assert data["email"] == user_1.email
def test_update_user(session: Session, client: TestClient):
user_1 = UserBase(username="Deadpond", email="dive@email.com")
session.add(user_1)
session.commit()
response = client.patch(
f"/users/{user_1.id}", json={"username": "deadpuddle", "email": "aaaa@bee.com"}
)
data = response.json()
assert response.status_code == 200
assert data["username"] == "deadpuddle"
def test_update_user_missing_field(session: Session, client: TestClient):
user_1 = UserBase(username="Deadpond", email="dive@email.com")
session.add(user_1)
session.commit()
response = client.patch(f"/users/{user_1.id}", json={"username": "deadpuddle"})
missing_field = response.json()
assert response.status_code == 422
ic(missing_field)
def test_delete_user(session: Session, client: TestClient):
user_1 = UserBase(username="Deadpond", email="dive@email.com")
session.add(user_1)
session.commit()
response = client.delete(f"/users/{user_1.id}")
user_in_db = session.get(UserBase, user_1.id)
ic()
ic(user_in_db)
assert response.status_code == 200
assert user_in_db is None
def test_encrypt_password():
hash = hash_password("User pw")
assert check_password("User pw", hashed_password=hash)
assert not check_password("other pw", hashed_password=hash)
def test_user_with_password(session: Session, client: TestClient):
secret_pw = "aPassowrd"
user_1 = User(username="Deadpond", email="dive@email.com", password=secret_pw)
session.add(user_1)
session.commit()
session.refresh(user_1)
user_2 = session.get(User, user_1.id)
assert user_1.username == user_2.username
assert user_1.password == secret_pw
user_2.password = hash_password(user_1.password)
assert check_password(secret_pw, user_2.password) is True
def test_set_password_and_login(session: Session, client: TestClient):
secret_pw = "aPassowrd"
user = User(username="Deadpond", email="dive@email.com")
session.add(user)
session.commit()
session.refresh(user)
response = client.post(
"/user/set_password",
json={
"id": user.id,
"username": "Deadpond",
"email": "aaaa@bee.com",
"password": secret_pw,
},
)
json_response = response.json()
assert response.status_code == 200
assert len(json_response["password"]) > 20
# Verify login for the same user
response = client.post(
"/user/login",
json={
"id": user.id,
"username": "Deadpond",
"email": "aaaa@bee.com",
"password": secret_pw,
},
)
json_response = response.json()
assert response.status_code == 200
assert len(json_response["password"]) > 20