diff --git a/atst/routes/applications/settings.py b/atst/routes/applications/settings.py index f695e4b0..a22707a1 100644 --- a/atst/routes/applications/settings.py +++ b/atst/routes/applications/settings.py @@ -22,19 +22,22 @@ from atst.jobs import send_mail def get_environments_obj_for_app(application): - environments_obj = [] - for env in application.environments: - env_data = { - "id": env.id, - "name": env.name, - "pending": env.is_pending, - "edit_form": EditEnvironmentForm(obj=env), - "member_count": len(env.roles), - "members": [env_role.application_role.user_name for env_role in env.roles], - } - environments_obj.append(env_data) - - return environments_obj + return sorted( + [ + { + "id": env.id, + "name": env.name, + "pending": env.is_pending, + "edit_form": EditEnvironmentForm(obj=env), + "member_count": len(env.roles), + "members": [ + env_role.application_role.user_name for env_role in env.roles + ], + } + for env in application.environments + ], + key=lambda env: env["name"], + ) def filter_perm_sets_data(member): @@ -54,16 +57,17 @@ def filter_perm_sets_data(member): def filter_env_roles_data(roles): - env_role_data = [ - { - "environment_id": str(role.environment.id), - "environment_name": role.environment.name, - "role": role.role, - } - for role in roles - ] - - return env_role_data + return sorted( + [ + { + "environment_id": str(role.environment.id), + "environment_name": role.environment.name, + "role": role.role, + } + for role in roles + ], + key=lambda env: env["environment_name"], + ) def filter_env_roles_form_data(member, environments): diff --git a/tests/routes/applications/test_settings.py b/tests/routes/applications/test_settings.py index fbb8f941..a9336b72 100644 --- a/tests/routes/applications/test_settings.py +++ b/tests/routes/applications/test_settings.py @@ -16,7 +16,11 @@ from atst.models.permissions import Permissions from atst.forms.application import EditEnvironmentForm from atst.forms.application_member import UpdateMemberForm from atst.forms.data import ENV_ROLE_NO_ACCESS as NO_ACCESS -from atst.routes.applications.settings import filter_env_roles_form_data +from atst.routes.applications.settings import ( + filter_env_roles_form_data, + filter_env_roles_data, + get_environments_obj_for_app, +) from tests.utils import captured_templates @@ -132,6 +136,19 @@ def test_edit_application_environments_obj(app, client, user_session): assert isinstance(context["audit_events"], Paginator) +def test_get_environments_obj_for_app(app, client, user_session): + application = ApplicationFactory.create( + environments=[{"name": "Naboo"}, {"name": "Endor"}, {"name": "Hoth"}] + ) + environments_obj = get_environments_obj_for_app(application) + + assert [environment["name"] for environment in environments_obj] == [ + "Endor", + "Hoth", + "Naboo", + ] + + def test_get_members_data(app, client, user_session): user = UserFactory.create() application = ApplicationFactory.create( @@ -620,3 +637,21 @@ def test_filter_environment_roles(): assert invite.is_revoked assert app_role.status == ApplicationRoleStatus.PENDING assert app_role.latest_invitation.email == "an_email@example.com" + + +def test_filter_env_roles_data(): + env_a = EnvironmentFactory.create(name="a") + env_b = EnvironmentFactory.create(name="b") + env_c = EnvironmentFactory.create(name="c") + + env_role_a = EnvironmentRoleFactory.create(environment=env_a) + env_role_b = EnvironmentRoleFactory.create(environment=env_b) + env_role_c = EnvironmentRoleFactory.create(environment=env_c) + + env_role_data = filter_env_roles_data([env_role_b, env_role_c, env_role_a]) + + # test that the environments are sorted in alphabetical order by name. Since + # we're just testing if the names are sorted, in this case we don't need to + # ensure that the environment roles and environments are associated with the + # same application. + assert [env["environment_name"] for env in env_role_data] == ["a", "b", "c"]