Skip to content

Commit fea83dd

Browse files
Merge pull request #65 from Promptly-Technologies-LLC/21-extend-the-test-suite
21 extend the test suite
2 parents c618c6a + 8a3ff74 commit fea83dd

File tree

3 files changed

+183
-1
lines changed

3 files changed

+183
-1
lines changed

main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,10 @@ async def read_organization(
255255
params: dict = Depends(common_authenticated_parameters)
256256
):
257257
# Get the organization only if the user is a member of it
258-
org: Organization = params["user"].organizations.get(org_id)
258+
org = next(
259+
(org for org in params["user"].organizations if org.id == org_id),
260+
None
261+
)
259262
if not org:
260263
raise organization.OrganizationNotFoundError()
261264

tests/test_organization.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# test_organization.py
2+
3+
from utils.models import Organization, Role
4+
from sqlmodel import select
5+
6+
def test_create_organization_success(auth_client, session, test_user):
7+
"""Test successful organization creation"""
8+
response = auth_client.post(
9+
"/organizations/create",
10+
data={"name": "New Test Organization"},
11+
follow_redirects=False
12+
)
13+
14+
# Check response
15+
assert response.status_code == 303 # Redirect status code
16+
assert "/organizations/" in response.headers["location"]
17+
18+
# Verify database state
19+
org = session.exec(
20+
select(Organization)
21+
.where(Organization.name == "New Test Organization")
22+
).first()
23+
24+
assert org is not None
25+
assert org.name == "New Test Organization"
26+
27+
# Verify default roles were created
28+
roles = session.exec(
29+
select(Role)
30+
.where(Role.organization_id == org.id)
31+
).all()
32+
33+
assert len(roles) > 0
34+
assert any(role.name == "Owner" for role in roles)
35+
36+
# Verify test_user was assigned as owner
37+
owner_role = next(role for role in roles if role.name == "Owner")
38+
assert test_user in owner_role.users
39+
40+
def test_create_organization_empty_name(auth_client):
41+
"""Test organization creation with empty name"""
42+
response = auth_client.post(
43+
"/organizations/create",
44+
data={"name": " "} # Empty or whitespace name
45+
)
46+
47+
assert response.status_code == 400
48+
assert "Organization name cannot be empty" in response.text
49+
50+
def test_create_organization_duplicate_name(auth_client, test_organization):
51+
"""Test organization creation with duplicate name"""
52+
response = auth_client.post(
53+
"/organizations/create",
54+
data={"name": test_organization.name}
55+
)
56+
57+
assert response.status_code == 400
58+
assert "Organization name already taken" in response.text
59+
60+
def test_create_organization_unauthenticated(unauth_client):
61+
"""Test organization creation without authentication"""
62+
response = unauth_client.post(
63+
"/organizations/create",
64+
data={"name": "Unauthorized Org"},
65+
follow_redirects=False
66+
)
67+
68+
assert response.status_code == 303 # Unauthorized

tests/test_role.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# test_role.py
2+
3+
import pytest
4+
from utils.models import Role, Permission, ValidPermissions, User
5+
from sqlmodel import Session, select
6+
7+
8+
@pytest.fixture
9+
def admin_user(session: Session, test_user: User, test_organization):
10+
"""Create an admin user with CREATE_ROLE permission"""
11+
admin_role: Role = Role(
12+
name="Admin",
13+
organization_id=test_organization.id
14+
)
15+
16+
create_role_permission: Permission | None = session.exec(
17+
select(Permission).where(Permission.name == ValidPermissions.CREATE_ROLE)
18+
).first()
19+
20+
if create_role_permission is None:
21+
raise ValueError("Error during test setup: CREATE_ROLE permission not found")
22+
23+
admin_role.permissions.append(create_role_permission)
24+
session.add(admin_role)
25+
26+
test_user.roles.append(admin_role)
27+
session.commit()
28+
29+
return test_user
30+
31+
32+
def test_create_role_success(auth_client, admin_user, test_organization, session: Session):
33+
"""Test successful role creation"""
34+
response = auth_client.post(
35+
"/roles/create",
36+
data={
37+
"name": "Test Role",
38+
"organization_id": test_organization.id,
39+
"permissions": [ValidPermissions.EDIT_ROLE.value]
40+
},
41+
follow_redirects=False
42+
)
43+
44+
assert response.status_code == 303
45+
46+
# Verify role was created in database
47+
created_role = session.exec(
48+
select(Role).where(
49+
Role.name == "Test Role",
50+
Role.organization_id == test_organization.id
51+
)
52+
).first()
53+
54+
assert created_role is not None
55+
assert created_role.name == "Test Role"
56+
assert len(created_role.permissions) == 1
57+
assert created_role.permissions[0].name == ValidPermissions.EDIT_ROLE
58+
59+
60+
def test_create_role_unauthorized(auth_client, test_user, test_organization):
61+
"""Test role creation without proper permissions"""
62+
response = auth_client.post(
63+
"/roles/create",
64+
data={
65+
"name": "Test Role",
66+
"organization_id": test_organization.id,
67+
"permissions": [ValidPermissions.EDIT_ROLE.value]
68+
},
69+
follow_redirects=False
70+
)
71+
72+
assert response.status_code == 403
73+
74+
75+
def test_create_duplicate_role(auth_client, admin_user, test_organization, session: Session):
76+
"""Test creating a role with a name that already exists in the organization"""
77+
# Create initial role
78+
existing_role = Role(
79+
name="Existing Role",
80+
organization_id=test_organization.id
81+
)
82+
session.add(existing_role)
83+
session.commit()
84+
85+
# Attempt to create role with same name
86+
response = auth_client.post(
87+
"/roles/create",
88+
data={
89+
"name": "Existing Role",
90+
"organization_id": test_organization.id,
91+
"permissions": [ValidPermissions.EDIT_ROLE.value]
92+
},
93+
follow_redirects=False
94+
)
95+
96+
assert response.status_code == 400
97+
98+
99+
def test_create_role_unauthenticated(unauth_client, test_organization):
100+
"""Test role creation without authentication"""
101+
response = unauth_client.post(
102+
"/roles/create",
103+
data={
104+
"name": "Test Role",
105+
"organization_id": test_organization.id,
106+
"permissions": [ValidPermissions.EDIT_ROLE.value]
107+
},
108+
follow_redirects=False
109+
)
110+
111+
assert response.status_code == 303

0 commit comments

Comments
 (0)