Skip to content

Commit af1939f

Browse files
Refactor ARO User Endpoints (#628)
# Purpose: Refactor ARO User endpoints Closes #627 . Explain the purpose of the PR here if it doesn't match the linked issue. Be sure to add a comment in the linked issue explaining the changes. Refactor the ARO User endpoints to use the new data wrapper # New Changes Explain new changes below in short bullet points. - Delete the update user endpoint - Refactor all user endpoints to use abstract data wrapper - Create get by id endpoint (with unit tests) - No changes were needed for the existing unit tests # Testing Explain tests that you ran to verify code functionality. - [x ] I have unit-tested this PR. Otherwise, explain why it cannot be unit-tested. - [ ] I have tested this PR on a board if the code will run on a board (Only required for firmware developers). - [ ] I have tested this PR by running the ARO website (Only required if the code will impact the ARO website). - [ ] I have tested this PR by running the MCC website (Only required if the code will impact the MCC website). - [x ] I have included screenshots of the tests performed below. <img width="894" height="120" alt="image" src="https://github.com/user-attachments/assets/3fd76b06-5f5b-40d0-917f-a6013174077b" /> # Outstanding Changes If there are non-critical changes (i.e. additional features) that can be made to this feature in the future, indicate them here. --------- Co-authored-by: Edison Wang <ew040607@gmail.com>
1 parent 7a6cbc1 commit af1939f

File tree

2 files changed

+57
-89
lines changed

2 files changed

+57
-89
lines changed
Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
from uuid import UUID
22

3-
from fastapi import APIRouter, HTTPException
3+
from fastapi import APIRouter
44

55
from gs.backend.api.v1.aro.models.requests import UserRequest
66
from gs.backend.api.v1.aro.models.responses import AllUsersResponse, UserResponse
7-
from gs.backend.data.data_wrappers.aro_wrapper.aro_user_data_wrapper import (
8-
add_user,
9-
delete_user_by_id,
10-
update_user_by_id,
11-
)
12-
from gs.backend.data.data_wrappers.aro_wrapper.aro_user_data_wrapper import get_all_users as get_all_db_users
7+
from gs.backend.data.data_wrappers.wrappers import AROUsersWrapper
138

149
aro_user_router = APIRouter(tags=["ARO", "User Information"])
1510

@@ -21,10 +16,22 @@ async def get_all_users() -> AllUsersResponse:
2116
2217
:return: all users
2318
"""
24-
users = get_all_db_users()
19+
users = AROUsersWrapper().get_all()
2520
return AllUsersResponse(data=users)
2621

2722

23+
@aro_user_router.get("/get_user/{userid}", response_model=UserResponse)
24+
def get_user(userid: str) -> UserResponse:
25+
"""
26+
Gets a user by ID
27+
28+
:param userid: The unique identifier of the user
29+
:return: the user
30+
"""
31+
user = AROUsersWrapper().get_by_id(UUID(userid))
32+
return UserResponse(data=user)
33+
34+
2835
@aro_user_router.post("/create_user", response_model=UserResponse)
2936
def create_user(payload: UserRequest) -> UserResponse:
3037
"""
@@ -33,48 +40,25 @@ def create_user(payload: UserRequest) -> UserResponse:
3340
:return: returns the user created
3441
"""
3542

36-
user = add_user(
37-
call_sign=payload.call_sign,
38-
email=payload.email,
39-
f_name=payload.first_name,
40-
l_name=payload.last_name,
41-
phone_number=payload.phone_number,
43+
user = AROUsersWrapper().create(
44+
data={
45+
"call_sign": payload.call_sign,
46+
"email": payload.email,
47+
"first_name": payload.first_name,
48+
"last_name": payload.last_name,
49+
"phone_number": payload.phone_number,
50+
}
4251
)
4352

4453
return UserResponse(data=user)
4554

4655

47-
@aro_user_router.put("/update_user/{userid}", response_model=UserResponse)
48-
def update_user(userid: str, payload: UserRequest) -> UserResponse:
49-
"""
50-
Modifies the user’s info based on the payload
51-
:param userid: The unique identifier of the user to be updated
52-
:param payload: The data used to update a user
53-
:return: returns the user updated
54-
"""
55-
try:
56-
user = update_user_by_id(
57-
userid=UUID(userid),
58-
call_sign=payload.call_sign,
59-
email=payload.email,
60-
f_name=payload.first_name,
61-
l_name=payload.last_name,
62-
phone_number=payload.phone_number,
63-
)
64-
return UserResponse(data=user)
65-
except ValueError as e:
66-
raise HTTPException(status_code=404, detail=str(e)) from e
67-
68-
69-
@aro_user_router.delete("/delete_user/{userid}", response_model=AllUsersResponse)
70-
def delete_user(userid: str) -> AllUsersResponse:
56+
@aro_user_router.delete("/delete_user/{userid}", response_model=UserResponse)
57+
def delete_user(userid: str) -> UserResponse:
7158
"""
7259
Deletes a user based on the user ID
7360
:param userid: The unique identifier of the user to be deleted
74-
:return: returns the user deleted
61+
:return: returns the deleted user
7562
"""
76-
try:
77-
users = delete_user_by_id(UUID(userid))
78-
return AllUsersResponse(data=users)
79-
except ValueError as e:
80-
raise HTTPException(status_code=404, detail=str(e)) from e
63+
deleted_user = AROUsersWrapper().delete_by_id(UUID(userid))
64+
return UserResponse(data=deleted_user)

python_test/test_aro_user_api.py

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -68,49 +68,37 @@ def test_user2_creation(client, user2_data):
6868
return user
6969

7070

71-
# Test updating user1 (depends on test_user1_creation)
72-
@pytest.fixture
73-
def test_user1_update(client, test_user1_creation):
71+
# Test getting user1 by ID
72+
def test_get_user1_by_id(client, test_user1_creation):
7473
user_id = test_user1_creation["id"]
75-
update_data = {
76-
"call_sign": "123456",
77-
"email": "bob2@test.com",
78-
"first_name": "Rob",
79-
"last_name": "Smith",
80-
"phone_number": "234567890",
81-
}
82-
res = client.put(
83-
f"/api/v1/aro/user/update_user/{user_id}", json=update_data, headers={"Content-Type": "application/json"}
84-
)
85-
74+
res = client.get(f"/api/v1/aro/user/get_user/{user_id}")
8675
assert res.status_code == 200
87-
updated_user = res.json()["data"]
88-
assert updated_user["email"] == update_data["email"]
89-
assert updated_user["call_sign"] == update_data["call_sign"]
90-
assert updated_user["first_name"] == update_data["first_name"]
91-
assert updated_user["last_name"] == update_data["last_name"]
92-
assert updated_user["phone_number"] == update_data["phone_number"]
76+
assert res.json()["data"]["id"] == user_id
9377

94-
return updated_user
9578

79+
# Test getting user2 by ID
80+
def test_get_user2_by_id(client, test_user2_creation):
81+
user_id = test_user2_creation["id"]
82+
res = client.get(f"/api/v1/aro/user/get_user/{user_id}")
83+
assert res.status_code == 200
84+
assert res.json()["data"]["id"] == user_id
9685

97-
# Test getting all users (after creating user1 and user2, and updating user1 to ensure creation and update work oncorrect user objects)
98-
@pytest.fixture
99-
def test_get_users(client, test_user1_update, test_user2_creation):
100-
res = client.get("/api/v1/aro/user/get_all_users")
10186

87+
# Test getting all users
88+
def test_get_all_users(client, test_user1_creation, test_user2_creation):
89+
res = client.get("/api/v1/aro/user/get_all_users")
10290
assert res.status_code == 200
10391
all_users = res.json()["data"]
10492
assert len(all_users) == 2
10593

10694
# Check user1
107-
user1_id = test_user1_update["id"]
95+
user1_id = test_user1_creation["id"]
10896
user1_from_response = next(user for user in all_users if user["id"] == user1_id)
109-
assert user1_from_response["call_sign"] == test_user1_update["call_sign"]
110-
assert user1_from_response["email"] == test_user1_update["email"]
111-
assert user1_from_response["first_name"] == test_user1_update["first_name"]
112-
assert user1_from_response["last_name"] == test_user1_update["last_name"]
113-
assert user1_from_response["phone_number"] == test_user1_update["phone_number"]
97+
assert user1_from_response["call_sign"] == test_user1_creation["call_sign"]
98+
assert user1_from_response["email"] == test_user1_creation["email"]
99+
assert user1_from_response["first_name"] == test_user1_creation["first_name"]
100+
assert user1_from_response["last_name"] == test_user1_creation["last_name"]
101+
assert user1_from_response["phone_number"] == test_user1_creation["phone_number"]
114102

115103
# Check user2
116104
user2_id = test_user2_creation["id"]
@@ -122,20 +110,16 @@ def test_get_users(client, test_user1_update, test_user2_creation):
122110
assert user2_from_response["phone_number"] == test_user2_creation["phone_number"]
123111

124112

125-
# Test deleting user1 (after test_get_users to ensure both users exist)
126-
def test_user1_deletion(client, test_user1_update, test_user2_creation, test_get_users):
127-
user_id = test_user1_update["id"]
113+
# Test deleting user1
114+
def test_user1_deletion(client, test_user1_creation, test_user2_creation):
115+
user_id = test_user1_creation["id"]
128116
res = client.delete(f"/api/v1/aro/user/delete_user/{user_id}", headers={"Content-Type": "application/json"})
129117

130118
assert res.status_code == 200
131-
all_users = res.json()["data"]
132-
assert len(all_users) == 1
133-
assert all_users[0]["id"] != user_id # Ensure user1 is deleted
134-
assert all_users[0]["id"] == test_user2_creation["id"]
135-
assert all_users[0]["email"] == test_user2_creation["email"]
136-
assert all_users[0]["call_sign"] == test_user2_creation["call_sign"]
137-
assert all_users[0]["first_name"] == test_user2_creation["first_name"]
138-
assert all_users[0]["last_name"] == test_user2_creation["last_name"]
139-
assert all_users[0]["phone_number"] == test_user2_creation["phone_number"]
140-
141-
return all_users
119+
deleted_user = res.json()["data"]
120+
assert deleted_user["id"] == user_id
121+
assert deleted_user["email"] == test_user1_creation["email"]
122+
assert deleted_user["call_sign"] == test_user1_creation["call_sign"]
123+
assert deleted_user["first_name"] == test_user1_creation["first_name"]
124+
assert deleted_user["last_name"] == test_user1_creation["last_name"]
125+
assert deleted_user["phone_number"] == test_user1_creation["phone_number"]

0 commit comments

Comments
 (0)