Skip to content

Commit 0679810

Browse files
Basic tests of create_role endpoint
1 parent 56b4b3c commit 0679810

File tree

1 file changed

+102
-57
lines changed

1 file changed

+102
-57
lines changed

tests/test_role.py

Lines changed: 102 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,105 @@
11
# test_role.py
22

33
import pytest
4-
from utils.models import Role
5-
6-
def test_role_creation():
7-
"""Test basic role creation"""
8-
role = Role("Admin", ["read", "write"])
9-
assert role.name == "Admin"
10-
assert role.permissions == ["read", "write"]
11-
12-
def test_role_add_permission():
13-
"""Test adding a permission to role"""
14-
role = Role("Admin", ["read"])
15-
role.add_permission("write")
16-
assert "write" in role.permissions
17-
assert len(role.permissions) == 2
18-
19-
def test_role_remove_permission():
20-
"""Test removing a permission from role"""
21-
role = Role("Admin", ["read", "write"])
22-
role.remove_permission("write")
23-
assert "write" not in role.permissions
24-
assert len(role.permissions) == 1
25-
26-
def test_role_has_permission():
27-
"""Test checking if role has specific permission"""
28-
role = Role("Admin", ["read", "write"])
29-
assert role.has_permission("read") is True
30-
assert role.has_permission("delete") is False
31-
32-
def test_role_add_existing_permission():
33-
"""Test adding a permission that already exists"""
34-
role = Role("Admin", ["read"])
35-
role.add_permission("read")
36-
assert len(role.permissions) == 1
37-
38-
def test_role_remove_nonexistent_permission():
39-
"""Test removing a permission that doesn't exist"""
40-
role = Role("Admin", ["read"])
41-
role.remove_permission("write")
42-
assert len(role.permissions) == 1
43-
assert role.permissions == ["read"]
44-
45-
# Additional tests for role.py
46-
47-
def test_role_invalid_name():
48-
"""Test role creation with invalid name"""
49-
with pytest.raises(ValueError):
50-
Role("", ["read"])
51-
52-
def test_role_none_permissions():
53-
"""Test role creation with None permissions"""
54-
with pytest.raises(ValueError):
55-
Role("Admin", None)
56-
57-
def test_role_empty_permissions():
58-
"""Test role creation with empty permissions list"""
59-
role = Role("Admin", [])
60-
assert len(role.permissions) == 0
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(
12+
name="Admin",
13+
organization_id=test_organization.id
14+
)
15+
create_role_permission = session.exec(
16+
select(Permission).where(Permission.name == ValidPermissions.CREATE_ROLE)
17+
).first()
18+
admin_role.permissions.append(create_role_permission)
19+
session.add(admin_role)
20+
21+
test_user.roles.append(admin_role)
22+
session.commit()
23+
return test_user
24+
25+
26+
def test_create_role_success(auth_client, admin_user, test_organization, session: Session):
27+
"""Test successful role creation"""
28+
response = auth_client.post(
29+
"/roles/create",
30+
data={
31+
"name": "Test Role",
32+
"organization_id": test_organization.id,
33+
"permissions": [ValidPermissions.EDIT_ROLE.value]
34+
},
35+
follow_redirects=False
36+
)
37+
38+
assert response.status_code == 303
39+
40+
# Verify role was created in database
41+
created_role = session.exec(
42+
select(Role).where(
43+
Role.name == "Test Role",
44+
Role.organization_id == test_organization.id
45+
)
46+
).first()
47+
48+
assert created_role is not None
49+
assert created_role.name == "Test Role"
50+
assert len(created_role.permissions) == 1
51+
assert created_role.permissions[0].name == ValidPermissions.EDIT_ROLE
52+
53+
54+
def test_create_role_unauthorized(auth_client, test_user, test_organization):
55+
"""Test role creation without proper permissions"""
56+
response = auth_client.post(
57+
"/roles/create",
58+
data={
59+
"name": "Test Role",
60+
"organization_id": test_organization.id,
61+
"permissions": [ValidPermissions.EDIT_ROLE.value]
62+
},
63+
follow_redirects=False
64+
)
65+
66+
assert response.status_code == 403
67+
68+
69+
def test_create_duplicate_role(auth_client, admin_user, test_organization, session: Session):
70+
"""Test creating a role with a name that already exists in the organization"""
71+
# Create initial role
72+
existing_role = Role(
73+
name="Existing Role",
74+
organization_id=test_organization.id
75+
)
76+
session.add(existing_role)
77+
session.commit()
78+
79+
# Attempt to create role with same name
80+
response = auth_client.post(
81+
"/roles/create",
82+
data={
83+
"name": "Existing Role",
84+
"organization_id": test_organization.id,
85+
"permissions": [ValidPermissions.EDIT_ROLE.value]
86+
},
87+
follow_redirects=False
88+
)
89+
90+
assert response.status_code == 400
91+
92+
93+
def test_create_role_unauthenticated(unauth_client, test_organization):
94+
"""Test role creation without authentication"""
95+
response = unauth_client.post(
96+
"/roles/create",
97+
data={
98+
"name": "Test Role",
99+
"organization_id": test_organization.id,
100+
"permissions": [ValidPermissions.EDIT_ROLE.value]
101+
},
102+
follow_redirects=False
103+
)
104+
105+
assert response.status_code == 303

0 commit comments

Comments
 (0)