Combine env_forms and environment_obj
This commit is contained in:
parent
4f954117c8
commit
67516b3b55
@ -13,60 +13,46 @@ from atst.utils.flash import formatted_flash as flash
|
|||||||
|
|
||||||
|
|
||||||
def get_environments_obj_for_app(application):
|
def get_environments_obj_for_app(application):
|
||||||
environments_obj = {}
|
environments_obj = []
|
||||||
for env in application.environments:
|
for env in application.environments:
|
||||||
environments_obj[env.name] = {
|
env_data = {
|
||||||
"edit_form": EditEnvironmentForm(obj=env),
|
|
||||||
"id": env.id,
|
"id": env.id,
|
||||||
|
"name": env.name,
|
||||||
|
"edit_form": EditEnvironmentForm(obj=env),
|
||||||
|
"members_form": EnvironmentRolesForm(data=data_for_env_members_form(env)),
|
||||||
|
"members": sort_env_users_by_role(env),
|
||||||
}
|
}
|
||||||
|
environments_obj.append(env_data)
|
||||||
environments_obj[env.name]["members"] = sort_env_users_by_role(env)
|
|
||||||
|
|
||||||
return environments_obj
|
return environments_obj
|
||||||
# {env_name: {edit_form, env_id, members: {csp_role: [], csp_role: []}}}
|
|
||||||
|
|
||||||
|
|
||||||
def sort_env_users_by_role(env):
|
def sort_env_users_by_role(env):
|
||||||
users_dict = {}
|
users_dict = {"no_access": []}
|
||||||
for role in CSPRole:
|
for role in CSPRole:
|
||||||
users_dict[role.value] = []
|
users_dict[role.value] = []
|
||||||
|
|
||||||
for r in env.roles:
|
for user in env.application.users:
|
||||||
users_dict[r.displayname].append(
|
if user in env.users:
|
||||||
{"name": r.user.full_name, "user_id": r.user_id}
|
role = EnvironmentRoles.get(user.id, env.id)
|
||||||
)
|
users_dict[role.displayname].append(
|
||||||
|
{"name": user.full_name, "user_id": user.id}
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
users_dict["no_access"].append({"name": user.full_name, "user_id": user.id})
|
||||||
|
|
||||||
return users_dict
|
return users_dict
|
||||||
# {csp_role: [{user info}, {user info}], csp_role: [...]}
|
|
||||||
|
|
||||||
|
|
||||||
def serialize_env_member_forms(application):
|
def data_for_env_members_form(environment):
|
||||||
environments_list = []
|
data = {"env_id": environment.id, "team_roles": []}
|
||||||
|
for user in environment.users:
|
||||||
for env in application.environments:
|
env_role = EnvironmentRoles.get(user.id, environment.id)
|
||||||
env_info = {"env_name": env.name, "no_access": []}
|
data["team_roles"].append(
|
||||||
env_team_list = []
|
{"name": user.full_name, "user_id": user.id, "role": env_role.displayname}
|
||||||
|
|
||||||
for user in application.users:
|
|
||||||
if user in env.users:
|
|
||||||
env_role = EnvironmentRoles.get(user.id, env.id)
|
|
||||||
env_team_list.append(
|
|
||||||
{
|
|
||||||
"name": user.full_name,
|
|
||||||
"user_id": user.id,
|
|
||||||
"role": env_role.displayname,
|
|
||||||
}
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
env_info["no_access"].append(
|
|
||||||
{"name": user.full_name, "user_id": user.id}
|
|
||||||
)
|
|
||||||
|
|
||||||
env_info["form"] = EnvironmentRolesForm(
|
|
||||||
data={"env_id": env.id, "team_roles": env_team_list}
|
|
||||||
)
|
)
|
||||||
environments_list.append(env_info)
|
|
||||||
return environments_list
|
return data
|
||||||
|
|
||||||
|
|
||||||
@applications_bp.route("/applications/<application_id>/settings")
|
@applications_bp.route("/applications/<application_id>/settings")
|
||||||
@ -81,7 +67,6 @@ def settings(application_id):
|
|||||||
application=application,
|
application=application,
|
||||||
form=form,
|
form=form,
|
||||||
environments_obj=get_environments_obj_for_app(application=application),
|
environments_obj=get_environments_obj_for_app(application=application),
|
||||||
env_forms=serialize_env_member_forms(application=application),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -129,7 +114,6 @@ def update(application_id):
|
|||||||
application=application,
|
application=application,
|
||||||
form=form,
|
form=form,
|
||||||
environments_obj=get_environments_obj_for_app(application=application),
|
environments_obj=get_environments_obj_for_app(application=application),
|
||||||
env_forms=serialize_env_member_forms(application=application),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -157,7 +141,6 @@ def update_env_roles(environment_id):
|
|||||||
name=application.name, description=application.description
|
name=application.name, description=application.description
|
||||||
),
|
),
|
||||||
environments_obj=get_environments_obj_for_app(application=application),
|
environments_obj=get_environments_obj_for_app(application=application),
|
||||||
env_forms=env_roles_form,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -3,8 +3,12 @@
|
|||||||
{% from "components/text_input.html" import TextInput %}
|
{% from "components/text_input.html" import TextInput %}
|
||||||
{% from "components/save_button.html" import SaveButton %}
|
{% from "components/save_button.html" import SaveButton %}
|
||||||
|
|
||||||
{% macro RolePanel(users=[], role='Unassigned(No Access)') %}
|
{% macro RolePanel(users=[], role='no_access') %}
|
||||||
{% set unassigned = role == 'Unassigned(No Access)' %}
|
{% if role == 'no_access' %}
|
||||||
|
{% set role = 'Unassigned (No Access)' %}
|
||||||
|
{% set unassigned = True %}
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
<div class='environment-role'>
|
<div class='environment-role'>
|
||||||
<h4>{{ role }}</h4>
|
<h4>{{ role }}</h4>
|
||||||
<ul class='environment-role__users'>
|
<ul class='environment-role__users'>
|
||||||
@ -40,16 +44,16 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<ul class="accordion-table__items">
|
<ul class="accordion-table__items">
|
||||||
{% for env in env_forms %}
|
{% for env in environments_obj %}
|
||||||
{% set member_count = env['form'].data['team_roles'] | length %}
|
{% set member_count = env['members_form'].data['team_roles'] | length %}
|
||||||
{% set unassigned = env['no_access'] %}
|
{% set members_by_role = env['members'] %}
|
||||||
{% set members_by_role = environments_obj[env['env_name']]['members'] %}
|
{% set unassigned = members_by_role['no_access'] %}
|
||||||
|
|
||||||
<toggler inline-template>
|
<toggler inline-template>
|
||||||
<li class="accordion-table__item">
|
<li class="accordion-table__item">
|
||||||
<div class="accordion-table__item-content">
|
<div class="accordion-table__item-content">
|
||||||
<span>
|
<span>
|
||||||
{{ env['env_name'] }}
|
{{ env['name'] }}
|
||||||
</span>
|
</span>
|
||||||
<span class="icon-link">
|
<span class="icon-link">
|
||||||
{% set edit_environment_button %}
|
{% set edit_environment_button %}
|
||||||
@ -84,20 +88,16 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% call ToggleSection(section_name="members") %}
|
{% call ToggleSection(section_name="members") %}
|
||||||
<div>
|
{% for role, members in members_by_role.items() %}
|
||||||
{{ RolePanel(users=unassigned) }}
|
{{ RolePanel(users=members, role=role) }}
|
||||||
|
{% endfor %}
|
||||||
{% for role, members in members_by_role.items() %}
|
|
||||||
{{ RolePanel(users=members, role=role) }}
|
|
||||||
{% endfor %}
|
|
||||||
</div>
|
|
||||||
{% endcall %}
|
{% endcall %}
|
||||||
|
|
||||||
{% call ToggleSection(section_name="edit") %}
|
{% call ToggleSection(section_name="edit") %}
|
||||||
<ul>
|
<ul>
|
||||||
<li class="accordion-table__item__expanded">
|
<li class="accordion-table__item__expanded">
|
||||||
{% set edit_form = environments_obj[env['env_name']]['edit_form'] %}
|
{% set edit_form = env['edit_form'] %}
|
||||||
<form action="{{ url_for('applications.update_environment', environment_id=environments_obj[env['env_name']]['id']) }}" method="post">
|
<form action="{{ url_for('applications.update_environment', environment_id=env['id']) }}" method="post">
|
||||||
{{ edit_form.csrf_token }}
|
{{ edit_form.csrf_token }}
|
||||||
{{ TextInput(edit_form.name) }}
|
{{ TextInput(edit_form.name) }}
|
||||||
{{
|
{{
|
||||||
|
@ -16,21 +16,21 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<ul class="accordion-table__items">
|
<ul class="accordion-table__items">
|
||||||
{% for name, members_list in environments_obj.items() %}
|
{% for env in environments_obj %}
|
||||||
<toggler inline-template>
|
<toggler inline-template>
|
||||||
<li class="accordion-table__item">
|
<li class="accordion-table__item">
|
||||||
<div class="accordion-table__item-content">
|
<div class="accordion-table__item-content">
|
||||||
<span>
|
<span>
|
||||||
{{ name }}
|
{{ env['name'] }}
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
<span class="icon-link icon-link--large accordion-table__item__toggler">
|
<span class="icon-link icon-link--large accordion-table__item__toggler">
|
||||||
{% set open_members_button %}
|
{% set open_members_button %}
|
||||||
{{ "common.members" | translate }} ({{ members_list | length }}) {{ Icon('caret_down') }}
|
{{ "common.members" | translate }} ({{ env['members'] | length }}) {{ Icon('caret_down') }}
|
||||||
{% endset %}
|
{% endset %}
|
||||||
|
|
||||||
{% set close_members_button %}
|
{% set close_members_button %}
|
||||||
{{ "common.members" | translate }} ({{ members_list | length }}) {{ Icon('caret_up') }}
|
{{ "common.members" | translate }} ({{ env['members'] | length }}) {{ Icon('caret_up') }}
|
||||||
{% endset %}
|
{% endset %}
|
||||||
|
|
||||||
{{
|
{{
|
||||||
@ -45,7 +45,7 @@
|
|||||||
|
|
||||||
{% call ToggleSection(section_name="members") %}
|
{% call ToggleSection(section_name="members") %}
|
||||||
<ul>
|
<ul>
|
||||||
{% for member in members_list %}
|
{% for member in env['members'] %}
|
||||||
<li class="accordion-table__item__expanded">
|
<li class="accordion-table__item__expanded">
|
||||||
<div class="accordion-table__item__expanded_first">{{ member.name }}</div>
|
<div class="accordion-table__item__expanded_first">{{ member.name }}</div>
|
||||||
</li>
|
</li>
|
||||||
|
@ -18,6 +18,7 @@ from atst.domain.portfolios import Portfolios
|
|||||||
from atst.models.environment_role import CSPRole
|
from atst.models.environment_role import CSPRole
|
||||||
from atst.models.portfolio_role import Status as PortfolioRoleStatus
|
from atst.models.portfolio_role import Status as PortfolioRoleStatus
|
||||||
from atst.forms.application import EditEnvironmentForm
|
from atst.forms.application import EditEnvironmentForm
|
||||||
|
from atst.forms.app_settings import EnvironmentRolesForm
|
||||||
|
|
||||||
from tests.utils import captured_templates
|
from tests.utils import captured_templates
|
||||||
|
|
||||||
@ -70,15 +71,16 @@ def test_edit_application_environments_obj(app, client, user_session):
|
|||||||
"A new application for me and my friends",
|
"A new application for me and my friends",
|
||||||
{"env"},
|
{"env"},
|
||||||
)
|
)
|
||||||
user1 = UserFactory.create()
|
|
||||||
user2 = UserFactory.create()
|
|
||||||
env = application.environments[0]
|
env = application.environments[0]
|
||||||
|
app_role = ApplicationRoleFactory.create(application=application)
|
||||||
env_role1 = EnvironmentRoleFactory.create(
|
env_role1 = EnvironmentRoleFactory.create(
|
||||||
environment=env, user=user1, role=CSPRole.BASIC_ACCESS.value
|
environment=env, role=CSPRole.BASIC_ACCESS.value
|
||||||
)
|
)
|
||||||
|
ApplicationRoleFactory.create(application=application, user=env_role1.user)
|
||||||
env_role2 = EnvironmentRoleFactory.create(
|
env_role2 = EnvironmentRoleFactory.create(
|
||||||
environment=env, user=user2, role=CSPRole.NETWORK_ADMIN.value
|
environment=env, role=CSPRole.NETWORK_ADMIN.value
|
||||||
)
|
)
|
||||||
|
ApplicationRoleFactory.create(application=application, user=env_role2.user)
|
||||||
|
|
||||||
user_session(portfolio.owner)
|
user_session(portfolio.owner)
|
||||||
|
|
||||||
@ -90,10 +92,15 @@ def test_edit_application_environments_obj(app, client, user_session):
|
|||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
_, context = templates[0]
|
_, context = templates[0]
|
||||||
|
|
||||||
env_obj = context["environments_obj"][env.name]
|
env_obj = context["environments_obj"][0]
|
||||||
|
assert env_obj["name"] == env.name
|
||||||
assert env_obj["id"] == env.id
|
assert env_obj["id"] == env.id
|
||||||
assert isinstance(env_obj["edit_form"], EditEnvironmentForm)
|
assert isinstance(env_obj["edit_form"], EditEnvironmentForm)
|
||||||
|
assert isinstance(env_obj["members_form"], EnvironmentRolesForm)
|
||||||
assert env_obj["members"] == {
|
assert env_obj["members"] == {
|
||||||
|
"no_access": [
|
||||||
|
{"name": app_role.user.full_name, "user_id": app_role.user_id}
|
||||||
|
],
|
||||||
CSPRole.BASIC_ACCESS.value: [
|
CSPRole.BASIC_ACCESS.value: [
|
||||||
{"name": env_role1.user.full_name, "user_id": env_role1.user_id}
|
{"name": env_role1.user.full_name, "user_id": env_role1.user_id}
|
||||||
],
|
],
|
||||||
@ -105,46 +112,6 @@ def test_edit_application_environments_obj(app, client, user_session):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def test_edit_app_serialize_env_member_form_data(app, client, user_session):
|
|
||||||
env = EnvironmentFactory.create()
|
|
||||||
application = env.application
|
|
||||||
|
|
||||||
_app_role = ApplicationRoleFactory.create(application=application)
|
|
||||||
env_role = EnvironmentRoleFactory.create(environment=env, user=_app_role.user)
|
|
||||||
|
|
||||||
app_role = ApplicationRoleFactory.create(application=application)
|
|
||||||
|
|
||||||
user_session(application.portfolio.owner)
|
|
||||||
|
|
||||||
with captured_templates(app) as templates:
|
|
||||||
response = app.test_client().get(
|
|
||||||
url_for("applications.settings", application_id=application.id)
|
|
||||||
)
|
|
||||||
assert response.status_code == 200
|
|
||||||
_, context = templates[0]
|
|
||||||
|
|
||||||
serialized_data = {
|
|
||||||
"env_name": env.name,
|
|
||||||
"no_access": [
|
|
||||||
{"name": app_role.user.full_name, "user_id": app_role.user_id}
|
|
||||||
],
|
|
||||||
"form": {
|
|
||||||
"env_id": env.id,
|
|
||||||
"team_roles": [
|
|
||||||
{
|
|
||||||
"name": env_role.user.full_name,
|
|
||||||
"user_id": env_role.user_id,
|
|
||||||
"role": env_role.displayname,
|
|
||||||
}
|
|
||||||
],
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
assert context["env_forms"][0]["env_name"] == serialized_data["env_name"]
|
|
||||||
assert context["env_forms"][0]["form"].data == serialized_data["form"]
|
|
||||||
assert context["env_forms"][0]["no_access"] == serialized_data["no_access"]
|
|
||||||
|
|
||||||
|
|
||||||
def test_user_with_permission_can_update_application(client, user_session):
|
def test_user_with_permission_can_update_application(client, user_session):
|
||||||
owner = UserFactory.create()
|
owner = UserFactory.create()
|
||||||
portfolio = PortfolioFactory.create(
|
portfolio = PortfolioFactory.create(
|
||||||
|
@ -16,6 +16,7 @@ from tests.factories import (
|
|||||||
ApplicationFactory,
|
ApplicationFactory,
|
||||||
ApplicationRoleFactory,
|
ApplicationRoleFactory,
|
||||||
EnvironmentFactory,
|
EnvironmentFactory,
|
||||||
|
EnvironmentRoleFactory,
|
||||||
InvitationFactory,
|
InvitationFactory,
|
||||||
PortfolioFactory,
|
PortfolioFactory,
|
||||||
PortfolioRoleFactory,
|
PortfolioRoleFactory,
|
||||||
@ -262,6 +263,11 @@ def test_application_settings_access(get_url_assert_status):
|
|||||||
applications=[{"name": "Mos Eisley", "description": "Where Han shot first"}],
|
applications=[{"name": "Mos Eisley", "description": "Where Han shot first"}],
|
||||||
)
|
)
|
||||||
app = portfolio.applications[0]
|
app = portfolio.applications[0]
|
||||||
|
env = EnvironmentFactory.create(application=app)
|
||||||
|
env_role = EnvironmentRoleFactory.create(
|
||||||
|
environment=env, role=CSPRole.NETWORK_ADMIN.value
|
||||||
|
)
|
||||||
|
ApplicationRoleFactory.create(application=app, user=env_role.user)
|
||||||
|
|
||||||
url = url_for("applications.settings", application_id=app.id)
|
url = url_for("applications.settings", application_id=app.id)
|
||||||
get_url_assert_status(ccpo, url, 200)
|
get_url_assert_status(ccpo, url, 200)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user