Skip to content
This repository was archived by the owner on Jun 7, 2025. It is now read-only.

Commit 0e20e2d

Browse files
committed
🐛 fix: streamline user update logic and enhance test cases for user updates
1 parent de49329 commit 0e20e2d

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

app/repositories/user_repository.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,7 @@ async def update_user(self, user_id: EmailStr, user_data: schemas.UserUpdate):
122122
update_payload["remember_me"] = user_data.remember_me
123123
if user_data.scopes is not None and user_data.scopes != user_current_data.get("scopes"):
124124
update_payload["scopes"] = user_data.scopes
125-
if user_data.name is not None and user_data.name != user_current_data.get(
126-
"name"
127-
):
125+
if user_data.name is not None and user_data.name != user_current_data.get("name"):
128126
update_payload["name"] = user_data.name
129127

130128
print(f"[USER REPO] Update payload: {update_payload}")

app/routes/auth.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@
3232
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="auth/token")
3333
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
3434

35-
def init_router(app_instance):
36-
yield
37-
38-
3935
def security_check(): # pragma: no cover
4036
if SECRET_KEY_JWT == "$2b$12$zqt9Rgv1PzORjG5ghJSb6OSdYrt7f7cLc38a21DgX/DMyqt80AUCi":
4137
print(

tests/repositories/test_user_repository.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ async def test__unit_test__update_user_success(user_repository, mock_database):
169169

170170
update_data = UserUpdate(
171171
_id = "user@test.com",
172+
name="Test",
172173
password="newpass",
173174
is_initialized=True,
174175
remember_me=True,

tests/routes/test_user.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,25 @@ async def get_by_email(self, email: str):
3838
"hashed_password": "$2b$12$zqt9Rgv1PzORjG5ghJSb6OSdYrt7f7cLc38a21DgX/DMyqt80AUCi",
3939
"is_initialized": True,
4040
}
41-
if email == "hi2@hi.com":
41+
# if email == "hi2@hi.com":
42+
# return {
43+
# "_id": "user123",
44+
# "name": "Bob",
45+
# "email": email,
46+
# "scopes": AccessRoles.USER,
47+
# "hashed_password": "$2b$12$zqt9Rgv1PzORjG5ghJSb6OSdYrt7f7cLc38a21DgX/DMyqt80AUCi",
48+
# "is_initialized": False,
49+
# }
50+
# if email == "hi3@hi.com":
51+
# return {
52+
# "_id": "user123",
53+
# "name": "Bob",
54+
# "email": email,
55+
# "scopes": AccessRoles.USER,
56+
# "hashed_password": "$2b$12$zqt9Rgv1PzORjG5ghJSb6OSdYrt7f7cLc38a21DgX/DMyqt80AUCi",
57+
# "is_initialized": False,
58+
# }
59+
if email == "hi4@hi.com":
4260
return {
4361
"_id": "user123",
4462
"name": "Bob",
@@ -47,17 +65,19 @@ async def get_by_email(self, email: str):
4765
"hashed_password": "$2b$12$zqt9Rgv1PzORjG5ghJSb6OSdYrt7f7cLc38a21DgX/DMyqt80AUCi",
4866
"is_initialized": False,
4967
}
68+
5069
async def update_user(self, user_id: str, user_data: UserUpdate):
5170
MockUpdateResult = MagicMock()
5271
print(user_id, user_data)
5372
if user_id == "hi@hi.com" or user_id == "hihi@hi.com":
5473
MockUpdateResult.modified_count = 1
5574
MockUpdateResult.matched_count = 1
5675
return MockUpdateResult
57-
if user_id == "hi2@hi.com":
76+
if user_id == "hi3@hi.com":
5877
MockUpdateResult.modified_count = 0
5978
MockUpdateResult.matched_count = 1
6079
return MockUpdateResult
80+
6181
if user_id == "error@error.com":
6282
raise Exception("error")
6383
MockUpdateResult.modified_count = 0
@@ -145,7 +165,7 @@ async def test__unit_test__update_user(fake_user_repo, monkeypatch):
145165

146166
@pytest.mark.asyncio
147167
async def test__unit_test__update_user_same_data(fake_user_repo, monkeypatch):
148-
user_new_data = UserUpdate(_id="hi2@hi.com", password="newpassword", is_initialized=True, remember_me=True, scopes=AccessRoles.USER)
168+
user_new_data = UserUpdate(_id="hi3@hi.com", password="newpassword", is_initialized=True, remember_me=True, scopes=AccessRoles.USER)
149169
result = await update_user(user_new_data, current_user, fake_user_repo)
150170
assert result["message"] != None
151171

@@ -252,14 +272,12 @@ async def test__unit_test__update_password_old_password_error(fake_user_repo, mo
252272
@pytest.mark.asyncio
253273
async def test__unit_test__update_password_is_not_init_with_not_found(fake_user_repo, monkeypatch):
254274
user_data = UserUpdatePassword(password="Abc123!@#@js", current_password="test_password")
255-
current_user = {"sub": "hi2@hi.com"}
275+
current_user = {"sub": "hi4@hi.com"}
276+
256277
with pytest.raises(HTTPException) as excinfo:
257278
await update_password(user_data, current_user, fake_user_repo)
258279
assert excinfo.value.status_code == 500
259280

260-
261-
262-
263281
@pytest.mark.asyncio
264282
async def test__unit_test__reset_password(fake_user_repo, monkeypatch):
265283
user_data = UserForgotPassword(email="hi@hi.com")

0 commit comments

Comments
 (0)