@@ -404,6 +404,51 @@ def test_create_app_with_additional_groups(
404
404
assert test_app_group .is_owner is False
405
405
406
406
407
+ def test_create_app_with_name_collision (
408
+ client : FlaskClient ,
409
+ db : SQLAlchemy ,
410
+ mocker : MockerFixture ,
411
+ faker : Faker , # type: ignore[type-arg]
412
+ app_group : AppGroup ,
413
+ ) -> None :
414
+ app = AppFactory .create ()
415
+ app .name = "Test-Staging"
416
+ db .session .add (app )
417
+ db .session .commit ()
418
+
419
+ app_group .app_id = app .id
420
+ app_group .name = "App-Test-Staging-Group"
421
+ db .session .add (app_group )
422
+ db .session .commit ()
423
+
424
+ create_group_spy = mocker .patch .object (
425
+ okta , "create_group" , return_value = Group ({"id" : cast (FakerWithPyStr , faker ).pystr ()})
426
+ )
427
+ add_user_to_group_spy = mocker .patch .object (okta , "async_add_user_to_group" )
428
+ add_owner_to_group_spy = mocker .patch .object (okta , "async_add_owner_to_group" )
429
+
430
+ data = {"name" : "Test" }
431
+
432
+ apps_url = url_for ("api-apps.apps" )
433
+ rep = client .post (apps_url , json = data )
434
+ assert rep .status_code == 201
435
+ assert create_group_spy .call_count == 1
436
+ assert add_user_to_group_spy .call_count == 1
437
+ assert add_owner_to_group_spy .call_count == 1
438
+
439
+ data = rep .get_json ()
440
+ assert db .session .get (App , data ["id" ]) is not None
441
+ assert data ["name" ] == "Test"
442
+
443
+ # Make sure new app doesn't end up with additional app groups from name collision
444
+ app_groups = AppGroup .query .filter (AppGroup .app_id == data ["id" ]).all ()
445
+ assert len (app_groups ) == 1
446
+
447
+ # Make sure original app still has its app group
448
+ app_groups = AppGroup .query .filter (AppGroup .app_id == app .id ).all ()
449
+ assert len (app_groups ) == 1
450
+
451
+
407
452
def test_get_all_app (client : FlaskClient , db : SQLAlchemy ) -> None :
408
453
apps_url = url_for ("api-apps.apps" )
409
454
apps = AppFactory .create_batch (10 )
0 commit comments