Skip to content

Commit 3fb4f5a

Browse files
authored
Bug fix and new test (#246)
1 parent 94ff7d8 commit 3fb4f5a

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

api/operations/create_app.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,8 @@ def execute(self) -> App:
168168
)
169169
existing_app_group_ids_to_update = []
170170
for existing_app_group in other_existing_app_groups:
171-
if type(existing_app_group) is AppGroup:
172-
existing_app_group.app_id = app_id
173-
existing_app_group.is_owner = False
174-
else:
171+
if type(existing_app_group) is not AppGroup:
175172
existing_app_group_ids_to_update.append(existing_app_group.id)
176-
db.session.commit()
177173

178174
for existing_app_group_id in existing_app_group_ids_to_update:
179175
ModifyGroupType(

tests/test_app.py

+45
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,51 @@ def test_create_app_with_additional_groups(
404404
assert test_app_group.is_owner is False
405405

406406

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+
407452
def test_get_all_app(client: FlaskClient, db: SQLAlchemy) -> None:
408453
apps_url = url_for("api-apps.apps")
409454
apps = AppFactory.create_batch(10)

0 commit comments

Comments
 (0)