Skip to content

Commit 52ed410

Browse files
Merge pull request #75 from Promptly-Technologies-LLC/21-extend-the-test-suite
21 extend the test suite
2 parents 377162e + bc6c699 commit 52ed410

File tree

3 files changed

+550
-13
lines changed

3 files changed

+550
-13
lines changed

routers/role.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,6 @@ class RoleUpdate(BaseModel):
8585
organization_id: int
8686
permissions: List[ValidPermissions]
8787

88-
@field_validator("id")
89-
@classmethod
90-
def validate_role_exists(cls, id: int, info):
91-
session = info.context.get("session")
92-
if session:
93-
role = session.get(Role, id)
94-
if not role or not role.id:
95-
raise RoleNotFoundError()
96-
return id
97-
9888
@classmethod
9989
async def as_form(
10090
cls,

tests/test_organization.py

Lines changed: 205 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
# test_organization.py
2-
3-
from utils.models import Organization, Role
1+
from utils.models import Organization, Role, Permission, ValidPermissions
42
from sqlmodel import select
53

64
def test_create_organization_success(auth_client, session, test_user):
@@ -66,3 +64,207 @@ def test_create_organization_unauthenticated(unauth_client):
6664
)
6765

6866
assert response.status_code == 303 # Unauthorized
67+
68+
def test_update_organization_success(auth_client, session, test_organization, test_user):
69+
"""Test successful organization update"""
70+
# Set up test user as owner with edit permission
71+
owner_role = Role(name="Owner", organization_id=test_organization.id)
72+
owner_role.permissions = [
73+
Permission(name=ValidPermissions.EDIT_ORGANIZATION)
74+
]
75+
owner_role.users.append(test_user)
76+
session.add(owner_role)
77+
session.commit()
78+
79+
new_name = "Updated Organization Name"
80+
response = auth_client.post(
81+
f"/organizations/update/{test_organization.id}",
82+
data={"id": test_organization.id, "name": new_name},
83+
follow_redirects=False
84+
)
85+
86+
assert response.status_code == 303 # Redirect status code
87+
assert "/profile" in response.headers["location"]
88+
89+
# Verify database update
90+
updated_org = session.get(Organization, test_organization.id)
91+
assert updated_org.name == new_name
92+
93+
def test_update_organization_unauthorized(auth_client, session, test_organization, test_user):
94+
"""Test organization update without proper permissions"""
95+
# Add user to organization but without edit permission
96+
basic_role = Role(name="Basic", organization_id=test_organization.id)
97+
basic_role.users.append(test_user)
98+
session.add(basic_role)
99+
session.commit()
100+
101+
response = auth_client.post(
102+
f"/organizations/update/{test_organization.id}",
103+
data={
104+
"id": test_organization.id,
105+
"name": "Unauthorized Update"
106+
},
107+
follow_redirects=False
108+
)
109+
110+
assert response.status_code == 403
111+
assert "permission" in response.text.lower()
112+
113+
def test_update_organization_duplicate_name(auth_client, session, test_organization, test_user):
114+
"""Test organization update with duplicate name"""
115+
# Create another organization with the target name
116+
existing_org = Organization(name="Existing Org")
117+
session.add(existing_org)
118+
119+
# Set up permissions
120+
owner_role = Role(name="Owner", organization_id=test_organization.id)
121+
owner_role.permissions = [
122+
Permission(name=ValidPermissions.EDIT_ORGANIZATION)
123+
]
124+
owner_role.users.append(test_user)
125+
session.add(owner_role)
126+
session.commit()
127+
128+
response = auth_client.post(
129+
f"/organizations/update/{test_organization.id}",
130+
data={
131+
"id": test_organization.id,
132+
"name": "Existing Org"
133+
},
134+
follow_redirects=False
135+
)
136+
137+
assert response.status_code == 400
138+
assert "organization name already taken" in response.text.lower()
139+
140+
def test_update_organization_empty_name(auth_client, session, test_organization, test_user):
141+
"""Test organization update with empty name"""
142+
# Set up permissions
143+
owner_role = Role(name="Owner", organization_id=test_organization.id)
144+
owner_role.permissions = [
145+
Permission(name=ValidPermissions.EDIT_ORGANIZATION)
146+
]
147+
owner_role.users.append(test_user)
148+
session.add(owner_role)
149+
session.commit()
150+
151+
response = auth_client.post(
152+
f"/organizations/update/{test_organization.id}",
153+
data={
154+
"id": test_organization.id,
155+
"name": " "
156+
},
157+
follow_redirects=False
158+
)
159+
160+
assert response.status_code == 400
161+
assert "organization name cannot be empty" in response.text.lower()
162+
163+
def test_update_organization_unauthenticated(unauth_client, test_organization):
164+
"""Test organization update without authentication"""
165+
response = unauth_client.post(
166+
f"/organizations/update/{test_organization.id}",
167+
data={
168+
"id": test_organization.id,
169+
"name": "Unauthorized Update"
170+
},
171+
follow_redirects=False
172+
)
173+
174+
assert response.status_code == 303 # Redirect to login
175+
176+
def test_delete_organization_success(auth_client, session, test_organization, test_user):
177+
"""Test successful organization deletion"""
178+
# Set up test user as owner with delete permission
179+
owner_role = Role(name="Owner", organization_id=test_organization.id)
180+
owner_role.permissions = [
181+
Permission(name=ValidPermissions.DELETE_ORGANIZATION)
182+
]
183+
owner_role.users.append(test_user)
184+
session.add(owner_role)
185+
session.commit()
186+
187+
response = auth_client.post(
188+
f"/organizations/delete/{test_organization.id}",
189+
follow_redirects=False
190+
)
191+
192+
assert response.status_code == 303 # Redirect status code
193+
assert "/profile" in response.headers["location"]
194+
195+
# Verify organization was deleted
196+
deleted_org = session.get(Organization, test_organization.id)
197+
assert deleted_org is None
198+
199+
def test_delete_organization_unauthorized(auth_client, session, test_organization, test_user):
200+
"""Test organization deletion without proper permissions"""
201+
# Add user to organization but without delete permission
202+
basic_role = Role(name="Owner", organization_id=test_organization.id)
203+
basic_role.users.append(test_user)
204+
session.add(basic_role)
205+
session.commit()
206+
207+
response = auth_client.post(
208+
f"/organizations/delete/{test_organization.id}",
209+
follow_redirects=False
210+
)
211+
212+
assert response.status_code == 403
213+
assert "permission" in response.text.lower()
214+
215+
# Verify organization still exists
216+
org = session.get(Organization, test_organization.id)
217+
assert org is not None
218+
219+
def test_delete_organization_not_member(auth_client, session, test_organization, test_user):
220+
"""Test organization deletion by non-member"""
221+
response = auth_client.post(
222+
f"/organizations/delete/{test_organization.id}",
223+
follow_redirects=False
224+
)
225+
226+
assert response.status_code == 403
227+
assert "permission" in response.text.lower()
228+
229+
# Verify organization still exists
230+
org = session.get(Organization, test_organization.id)
231+
assert org is not None
232+
233+
def test_delete_organization_unauthenticated(unauth_client, test_organization):
234+
"""Test organization deletion without authentication"""
235+
response = unauth_client.post(
236+
f"/organizations/delete/{test_organization.id}",
237+
follow_redirects=False
238+
)
239+
240+
assert response.status_code == 303 # Redirect to login
241+
242+
def test_delete_organization_cascade(auth_client, session, test_organization, test_user):
243+
"""Test that deleting organization cascades to roles"""
244+
# Set up test user as owner with delete permission
245+
owner_role = Role(name="Owner", organization_id=test_organization.id)
246+
owner_role.permissions = [
247+
Permission(name=ValidPermissions.DELETE_ORGANIZATION)
248+
]
249+
owner_role.users.append(test_user)
250+
251+
# Add another role to verify cascade
252+
member_role = Role(name="Member", organization_id=test_organization.id)
253+
254+
session.add(owner_role)
255+
session.add(member_role)
256+
session.commit()
257+
258+
response = auth_client.post(
259+
f"/organizations/delete/{test_organization.id}",
260+
follow_redirects=False
261+
)
262+
263+
assert response.status_code == 303
264+
265+
# Verify roles were also deleted
266+
roles = session.exec(
267+
select(Role)
268+
.where(Role.organization_id == test_organization.id)
269+
).all()
270+
assert len(roles) == 0

0 commit comments

Comments
 (0)