Skip to content

Commit 34d5719

Browse files
committed
Add tests for group creation
1 parent 82a1eb8 commit 34d5719

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

pydatalab/src/pydatalab/models/people.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class Person(Entry):
137137
contact_email: Optional[EmailStr]
138138
"""In the case of multiple *verified* email identities, this email will be used as the primary contact."""
139139

140-
groups: Optional[List[Group]]
140+
groups: List[Group] = Field(default_factory=list)
141141
"""A list of groups that this person belongs to."""
142142

143143
managers: Optional[List[PyObjectId]]

pydatalab/tests/server/test_users.py

+57-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def test_get_current_user(client):
1313
assert (resp_json := resp.json)
1414
assert resp_json["immutable_id"] == 24 * "1"
1515
assert resp_json["role"] == "user"
16+
assert resp_json["groups"] == []
1617

1718

1819
def test_get_current_user_admin(admin_client):
@@ -41,7 +42,7 @@ def test_role_update_by_user(client, real_mongo_client, user_id):
4142
assert user["role"] == "manager"
4243

4344

44-
def test_user_update(client, real_mongo_client, user_id, admin_user_id):
45+
def test_user_update(client, unauthenticated_client, real_mongo_client, user_id, admin_user_id):
4546
endpoint = f"/users/{str(user_id)}"
4647
# Test display name update
4748
user_request = {"display_name": "Test Person II"}
@@ -105,6 +106,16 @@ def test_user_update(client, real_mongo_client, user_id, admin_user_id):
105106
user = real_mongo_client.get_database().users.find_one({"_id": admin_user_id})
106107
assert user["display_name"] == "Test Admin"
107108

109+
# Test that differing user auth can/cannot search for users
110+
endpoint = "/search-users/"
111+
resp = client.get(endpoint + "?query='Test Person'")
112+
assert resp.status_code == 200
113+
assert len(resp.json["users"]) == 4
114+
115+
# Test that differing user auth can/cannot search for users
116+
resp = unauthenticated_client.get(endpoint + "?query='Test Person'")
117+
assert resp.status_code == 401
118+
108119

109120
def test_user_update_admin(admin_client, real_mongo_client, user_id):
110121
endpoint = f"/users/{str(user_id)}"
@@ -114,3 +125,48 @@ def test_user_update_admin(admin_client, real_mongo_client, user_id):
114125
assert resp.status_code == 200
115126
user = real_mongo_client.get_database().users.find_one({"_id": user_id})
116127
assert user["display_name"] == "Test Person"
128+
129+
130+
def test_create_group(admin_client, client, unauthenticated_client, real_mongo_client):
131+
from bson import ObjectId
132+
133+
good_group = {
134+
"display_name": "My New Group",
135+
"group_id": "my-new-group",
136+
"description": "A group for testing",
137+
"group_admins": [],
138+
}
139+
140+
# Group ID cannot be None
141+
bad_group = good_group.copy()
142+
bad_group["group_id"] = None
143+
resp = admin_client.put("/groups", json=bad_group)
144+
assert resp.status_code == 400
145+
146+
# Successfully create group
147+
resp = admin_client.put("/groups", json=good_group)
148+
assert resp.status_code == 200
149+
group_immutable_id = ObjectId(resp.json["group_immutable_id"])
150+
assert real_mongo_client.get_database().groups.find_one({"_id": group_immutable_id})
151+
152+
# Group ID must be unique
153+
resp = admin_client.put("/groups", json=good_group)
154+
assert resp.status_code == 400
155+
156+
# Request must come from admin
157+
# Make ID unique so that this would otherwise pass
158+
good_group["group_id"] = "my-new-group-2"
159+
resp = unauthenticated_client.put("/groups", json=good_group)
160+
assert resp.status_code == 401
161+
assert (
162+
real_mongo_client.get_database().groups.find_one({"group_id": good_group["group_id"]})
163+
is None
164+
)
165+
166+
# Request must come from admin
167+
resp = client.put("/groups", json=good_group)
168+
assert resp.status_code == 403
169+
assert (
170+
real_mongo_client.get_database().groups.find_one({"group_id": good_group["group_id"]})
171+
is None
172+
)

0 commit comments

Comments
 (0)