@@ -116,17 +116,37 @@ def test_update_organization_success(
116
116
auth_client : TestClient , session : Session , test_organization : Organization , test_user : User
117
117
):
118
118
"""Test successful organization update"""
119
- # Set up test user as owner with edit permission
119
+ # Ensure test_user has the EDIT_ORGANIZATION permission via the Owner role (already created by fixture)
120
120
if test_organization .id is None :
121
121
raise SetupError ("Test organization ID is None" )
122
+
123
+ owner_role = session .exec (
124
+ select (Role ).where (
125
+ Role .organization_id == test_organization .id ,
126
+ Role .name == "Owner"
127
+ )
128
+ ).first ()
129
+
130
+ if owner_role is None :
131
+ raise SetupError ("Owner role not found for test organization." )
122
132
123
- owner_role = Role (name = "Owner" , organization_id = test_organization .id )
124
- owner_role .permissions = [
125
- Permission (name = ValidPermissions .EDIT_ORGANIZATION )
126
- ]
127
- owner_role .users .append (test_user )
128
- session .add (owner_role )
133
+ # Ensure the permission is present (it should be by default)
134
+ edit_permission = session .exec (
135
+ select (Permission ).where (Permission .name == ValidPermissions .EDIT_ORGANIZATION )
136
+ ).first ()
137
+ if edit_permission is None :
138
+ raise SetupError ("EDIT_ORGANIZATION permission not found." )
139
+
140
+ if edit_permission not in owner_role .permissions :
141
+ owner_role .permissions .append (edit_permission ) # Add just in case
142
+
143
+ # Ensure the user is assigned to the role (it should be by default for the creating user)
144
+ if test_user not in owner_role .users :
145
+ owner_role .users .append (test_user )
146
+
129
147
session .commit ()
148
+ session .refresh (owner_role )
149
+ session .refresh (test_user )
130
150
131
151
new_name = "Updated Organization Name"
132
152
response = auth_client .post (
@@ -168,19 +188,38 @@ def test_update_organization_unauthorized(auth_client, session, test_organizatio
168
188
assert "permission" in response .text .lower ()
169
189
170
190
def test_update_organization_duplicate_name (auth_client , session , test_organization , test_user ):
171
- """Test organization update with duplicate name"""
172
- # Create another organization with the target name
173
191
existing_org = Organization (name = "Existing Org" )
174
192
session .add (existing_org )
175
193
176
- # Set up permissions
177
- owner_role = Role (name = "Owner" , organization_id = test_organization .id )
178
- owner_role .permissions = [
179
- Permission (name = ValidPermissions .EDIT_ORGANIZATION )
180
- ]
181
- owner_role .users .append (test_user )
182
- session .add (owner_role )
194
+ # Ensure test_user has EDIT_ORGANIZATION permission via the Owner role
195
+ if test_organization .id is None :
196
+ raise SetupError ("Test organization ID is None" )
197
+
198
+ owner_role = session .exec (
199
+ select (Role ).where (
200
+ Role .organization_id == test_organization .id ,
201
+ Role .name == "Owner"
202
+ )
203
+ ).first ()
204
+
205
+ if owner_role is None :
206
+ raise SetupError ("Owner role not found for test organization." )
207
+
208
+ edit_permission = session .exec (
209
+ select (Permission ).where (Permission .name == ValidPermissions .EDIT_ORGANIZATION )
210
+ ).first ()
211
+ if edit_permission is None :
212
+ raise SetupError ("EDIT_ORGANIZATION permission not found." )
213
+
214
+ if edit_permission not in owner_role .permissions :
215
+ owner_role .permissions .append (edit_permission )
216
+
217
+ if test_user not in owner_role .users :
218
+ owner_role .users .append (test_user )
219
+
183
220
session .commit ()
221
+ session .refresh (owner_role )
222
+ session .refresh (test_user )
184
223
185
224
response = auth_client .post (
186
225
app .url_path_for ("update_organization" , org_id = test_organization .id ),
@@ -196,14 +235,35 @@ def test_update_organization_duplicate_name(auth_client, session, test_organizat
196
235
197
236
def test_update_organization_empty_name (auth_client , session , test_organization , test_user ):
198
237
"""Test organization update with empty name"""
199
- # Set up permissions
200
- owner_role = Role (name = "Owner" , organization_id = test_organization .id )
201
- owner_role .permissions = [
202
- Permission (name = ValidPermissions .EDIT_ORGANIZATION )
203
- ]
204
- owner_role .users .append (test_user )
205
- session .add (owner_role )
238
+ # Ensure test_user has EDIT_ORGANIZATION permission via the Owner role
239
+ if test_organization .id is None :
240
+ raise SetupError ("Test organization ID is None" )
241
+
242
+ owner_role = session .exec (
243
+ select (Role ).where (
244
+ Role .organization_id == test_organization .id ,
245
+ Role .name == "Owner"
246
+ )
247
+ ).first ()
248
+
249
+ if owner_role is None :
250
+ raise SetupError ("Owner role not found for test organization." )
251
+
252
+ edit_permission = session .exec (
253
+ select (Permission ).where (Permission .name == ValidPermissions .EDIT_ORGANIZATION )
254
+ ).first ()
255
+ if edit_permission is None :
256
+ raise SetupError ("EDIT_ORGANIZATION permission not found." )
257
+
258
+ if edit_permission not in owner_role .permissions :
259
+ owner_role .permissions .append (edit_permission )
260
+
261
+ if test_user not in owner_role .users :
262
+ owner_role .users .append (test_user )
263
+
206
264
session .commit ()
265
+ session .refresh (owner_role )
266
+ session .refresh (test_user )
207
267
208
268
response = auth_client .post (
209
269
app .url_path_for ("update_organization" , org_id = test_organization .id ),
@@ -235,15 +295,35 @@ def test_delete_organization_success(auth_client, session, test_organization, te
235
295
"""Test successful organization deletion"""
236
296
# Store the organization ID for later verification
237
297
org_id = test_organization .id
298
+ if org_id is None : # Add check for None
299
+ raise SetupError ("Test organization ID is None" )
238
300
239
- # Set up test user as owner with delete permission
240
- owner_role = Role (name = "Owner" , organization_id = org_id )
241
- owner_role .permissions = [
242
- Permission (name = ValidPermissions .DELETE_ORGANIZATION )
243
- ]
244
- owner_role .users .append (test_user )
245
- session .add (owner_role )
246
- session .commit ()
301
+ # Ensure test_user has DELETE_ORGANIZATION permission via the Owner role
302
+ owner_role = session .exec (
303
+ select (Role ).where (
304
+ Role .organization_id == org_id ,
305
+ Role .name == "Owner"
306
+ )
307
+ ).first ()
308
+
309
+ if owner_role is None :
310
+ raise SetupError ("Owner role not found for test organization." )
311
+
312
+ delete_permission = session .exec (
313
+ select (Permission ).where (Permission .name == ValidPermissions .DELETE_ORGANIZATION )
314
+ ).first ()
315
+ if delete_permission is None :
316
+ raise SetupError ("DELETE_ORGANIZATION permission not found." )
317
+
318
+ if delete_permission not in owner_role .permissions :
319
+ owner_role .permissions .append (delete_permission )
320
+
321
+ if test_user not in owner_role .users :
322
+ owner_role .users .append (test_user )
323
+
324
+ session .commit () # Commit permission/user assignment changes
325
+ session .refresh (owner_role )
326
+ session .refresh (test_user )
247
327
248
328
response = auth_client .post (
249
329
app .url_path_for ("delete_organization" , org_id = org_id ),
@@ -304,20 +384,44 @@ def test_delete_organization_cascade(auth_client, session, test_organization, te
304
384
"""Test that deleting organization cascades to roles"""
305
385
# Store the organization ID for later verification
306
386
org_id = test_organization .id
387
+ if org_id is None : # Add check for None
388
+ raise SetupError ("Test organization ID is None" )
307
389
308
- # Set up test user as owner with delete permission
309
- owner_role = Role (name = "Owner" , organization_id = org_id )
310
- owner_role .permissions = [
311
- Permission (name = ValidPermissions .DELETE_ORGANIZATION )
312
- ]
313
- owner_role .users .append (test_user )
314
-
315
- # Add another role to verify cascade
316
- member_role = Role (name = "Member" , organization_id = org_id )
317
-
318
- session .add (owner_role )
319
- session .add (member_role )
320
- session .commit ()
390
+ # Ensure test_user has DELETE_ORGANIZATION permission via the Owner role
391
+ owner_role = session .exec (
392
+ select (Role ).where (
393
+ Role .organization_id == org_id ,
394
+ Role .name == "Owner"
395
+ )
396
+ ).first ()
397
+ if owner_role is None :
398
+ raise SetupError ("Owner role not found for test organization." )
399
+
400
+ delete_permission = session .exec (
401
+ select (Permission ).where (Permission .name == ValidPermissions .DELETE_ORGANIZATION )
402
+ ).first ()
403
+ if delete_permission is None :
404
+ raise SetupError ("DELETE_ORGANIZATION permission not found." )
405
+
406
+ if delete_permission not in owner_role .permissions :
407
+ owner_role .permissions .append (delete_permission )
408
+
409
+ if test_user not in owner_role .users :
410
+ owner_role .users .append (test_user )
411
+
412
+ # Verify the Member role exists (created by fixture)
413
+ member_role = session .exec (
414
+ select (Role ).where (
415
+ Role .organization_id == org_id ,
416
+ Role .name == "Member"
417
+ )
418
+ ).first ()
419
+ if member_role is None :
420
+ raise SetupError ("Member role not found for test organization. Fixture might have changed." )
421
+
422
+ session .commit () # Commit permission/user assignment changes
423
+ session .refresh (owner_role )
424
+ session .refresh (test_user )
321
425
322
426
response = auth_client .post (
323
427
app .url_path_for ("delete_organization" , org_id = org_id ),
0 commit comments