8
8
@pytest .fixture
9
9
def admin_user (session : Session , test_user : User , test_organization ):
10
10
"""Create an admin user with CREATE_ROLE permission"""
11
- admin_role = Role (
11
+ admin_role : Role = Role (
12
12
name = "Admin" ,
13
13
organization_id = test_organization .id
14
14
)
15
- create_role_permission = session .exec (
15
+
16
+ create_role_permission : Permission | None = session .exec (
16
17
select (Permission ).where (Permission .name == ValidPermissions .CREATE_ROLE )
17
18
).first ()
19
+
20
+ if create_role_permission is None :
21
+ raise ValueError ("Error during test setup: CREATE_ROLE permission not found" )
22
+
18
23
admin_role .permissions .append (create_role_permission )
19
24
session .add (admin_role )
20
-
25
+
21
26
test_user .roles .append (admin_role )
22
27
session .commit ()
28
+
23
29
return test_user
24
30
25
31
@@ -34,17 +40,17 @@ def test_create_role_success(auth_client, admin_user, test_organization, session
34
40
},
35
41
follow_redirects = False
36
42
)
37
-
43
+
38
44
assert response .status_code == 303
39
-
45
+
40
46
# Verify role was created in database
41
47
created_role = session .exec (
42
48
select (Role ).where (
43
49
Role .name == "Test Role" ,
44
50
Role .organization_id == test_organization .id
45
51
)
46
52
).first ()
47
-
53
+
48
54
assert created_role is not None
49
55
assert created_role .name == "Test Role"
50
56
assert len (created_role .permissions ) == 1
@@ -62,7 +68,7 @@ def test_create_role_unauthorized(auth_client, test_user, test_organization):
62
68
},
63
69
follow_redirects = False
64
70
)
65
-
71
+
66
72
assert response .status_code == 403
67
73
68
74
@@ -75,7 +81,7 @@ def test_create_duplicate_role(auth_client, admin_user, test_organization, sessi
75
81
)
76
82
session .add (existing_role )
77
83
session .commit ()
78
-
84
+
79
85
# Attempt to create role with same name
80
86
response = auth_client .post (
81
87
"/roles/create" ,
@@ -86,7 +92,7 @@ def test_create_duplicate_role(auth_client, admin_user, test_organization, sessi
86
92
},
87
93
follow_redirects = False
88
94
)
89
-
95
+
90
96
assert response .status_code == 400
91
97
92
98
@@ -101,5 +107,5 @@ def test_create_role_unauthenticated(unauth_client, test_organization):
101
107
},
102
108
follow_redirects = False
103
109
)
104
-
110
+
105
111
assert response .status_code == 303
0 commit comments