Skip to content

Commit c6df6ea

Browse files
Debug some test expectations and template rendering
1 parent bd7be60 commit c6df6ea

File tree

5 files changed

+49
-22
lines changed

5 files changed

+49
-22
lines changed

routers/organization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ async def read_organization(
5252
select(Organization)
5353
.where(Organization.id == org_id)
5454
.options(
55-
selectinload(Organization.roles).selectinload(Role.users),
55+
selectinload(Organization.roles).selectinload(Role.users).selectinload(User.account),
5656
selectinload(Organization.roles).selectinload(Role.users).selectinload(User.roles),
5757
selectinload(Organization.roles).selectinload(Role.permissions)
5858
)

templates/organization/modals/roles_card.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
{% endif %}
1010
</div>
1111
<div class="card-body">
12-
{% set custom_roles_exist = false %}
12+
{% set ns = namespace(custom_roles_exist=false) %}
1313
{% for role in organization.roles %}
1414
{% if role.name not in ["Owner", "Administrator", "Member"] %}
15-
{% set custom_roles_exist = true %}
15+
{% set ns.custom_roles_exist = true %}
1616
{% endif %}
1717
{% endfor %}
1818

1919
{% if organization.roles %}
20-
{% if custom_roles_exist %}
20+
{% if ns.custom_roles_exist %}
2121
<div class="table-responsive">
2222
<table class="table table-hover">
2323
<thead>
@@ -50,7 +50,7 @@
5050
</button>
5151
{% endif %}
5252

53-
{% if ValidPermissions.DELETE_ROLE in user_permissions and role.name != "Owner" %}
53+
{% if ValidPermissions.DELETE_ROLE in user_permissions and role.name not in ["Owner", "Administrator", "Member"] %}
5454
<form method="POST" action="{{ url_for('delete_role') }}" class="d-inline">
5555
<input type="hidden" name="id" value="{{ role.id }}">
5656
<input type="hidden" name="organization_id" value="{{ organization.id }}">

tests/conftest.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def test_organization(session: Session) -> Organization:
133133
"""Create a test organization with default roles and permissions"""
134134
organization = Organization(name="Test Organization")
135135
session.add(organization)
136-
session.flush() # Flush to get the organization ID
136+
session.flush()
137137

138138
if organization.id is None:
139139
pytest.fail("Failed to get organization ID after flush")
@@ -142,9 +142,6 @@ def test_organization(session: Session) -> Organization:
142142
# This function handles the commit internally
143143
create_default_roles(session, organization.id, check_first=False)
144144

145-
# Refresh the organization object to load the newly created roles/permissions
146-
session.refresh(organization)
147-
148145
return organization
149146

150147

@@ -366,4 +363,4 @@ def second_test_organization(session: Session) -> Organization:
366363
organization = Organization(name="Second Test Organization")
367364
session.add(organization)
368365
session.commit()
369-
return organization
366+
return organization

tests/routers/test_organization.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,9 +362,9 @@ def test_read_organization_as_admin(auth_client_admin, test_organization):
362362
assert "Invite Member" in response.text
363363
assert "Create Role" in response.text
364364
assert "Edit Role" in response.text
365-
366-
# Admin shouldn't have these permissions
367-
assert "Delete Organization" not in response.text
365+
366+
# Admin shouldn't have the permission to trigger the delete modal
367+
assert 'data-bs-target="#deleteOrganizationModal"' not in response.text
368368

369369

370370
def test_read_organization_as_member(auth_client_member, test_organization):

tests/routers/test_role.py

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from tests.conftest import SetupError
55
from utils.models import Role, Permission, ValidPermissions, User
66
from sqlmodel import Session, select
7+
import re
78

89

910
@pytest.fixture
@@ -521,31 +522,60 @@ def test_organization_page_role_edit_access(auth_client_owner, auth_client_admin
521522
assert "Edit Role" not in member_response.text
522523

523524

524-
def test_organization_page_role_delete_access(auth_client_owner, auth_client_admin, auth_client_member, test_organization):
525+
def test_organization_page_role_delete_access(auth_client_owner, auth_client_admin, auth_client_member, test_organization, session: Session):
525526
"""Test that role deletion UI elements are only shown to users with DELETE_ROLE permission"""
526-
# Owner should see role deletion controls
527+
# Create a custom, deletable role for the test
528+
custom_role = Role(name="Custom Role To Delete", organization_id=test_organization.id)
529+
session.add(custom_role)
530+
session.commit()
531+
session.refresh(custom_role)
532+
533+
# Confirm that the custom role is accessible from organization object
534+
assert custom_role in test_organization.roles
535+
536+
# Owner should see the delete role form action because a custom role exists and they have permission
527537
owner_response = auth_client_owner.get(
528538
f"/organizations/{test_organization.id}",
529539
follow_redirects=False
530540
)
531541
assert owner_response.status_code == 200
532-
assert "Delete Role" in owner_response.text
533-
534-
# Admin should not see role deletion controls (wasn't given DELETE_ROLE)
542+
expected_custom_delete_form = f'<form method="POST" action="http://testserver/roles/delete" class="d-inline">\\s*<input type="hidden" name="id" value="{custom_role.id}">\\s*<input type="hidden" name="organization_id" value="{test_organization.id}">\\s*<button type="submit" class="btn btn-sm btn-outline-danger"\\s*>\\s*Delete Role\\s*</button>\\s*</form>'
543+
assert re.search(expected_custom_delete_form, owner_response.text) is not None
544+
545+
# Admin should see the delete role form action
535546
admin_response = auth_client_admin.get(
536547
f"/organizations/{test_organization.id}",
537548
follow_redirects=False
538549
)
539550
assert admin_response.status_code == 200
540-
assert "Delete Role" not in admin_response.text
541-
542-
# Member should not see role deletion controls
551+
assert f'<input type="hidden" name="id" value="{custom_role.id}">' in admin_response.text
552+
assert 'action="http://testserver/roles/delete"' in admin_response.text
553+
554+
# Member should *not* see the delete role form action
543555
member_response = auth_client_member.get(
544556
f"/organizations/{test_organization.id}",
545557
follow_redirects=False
546558
)
547559
assert member_response.status_code == 200
548-
assert "Delete Role" not in member_response.text
560+
assert f'<input type="hidden" name="id" value="{custom_role.id}">' not in member_response.text
561+
assert 'action="http://testserver/roles/delete"' not in member_response.text
562+
563+
# Built-in roles should not have delete forms for anyone
564+
# Check that the delete form is NOT present for the built-in "Owner" role (hardcoded ID 1 in fixtures)
565+
expected_owner_delete_form = f'<form method="POST" action="http://testserver/roles/delete" class="d-inline">\\s*<input type="hidden" name="id" value="1">' # Check only for the form targeting owner role ID
566+
assert expected_owner_delete_form not in owner_response.text
567+
assert expected_owner_delete_form not in admin_response.text
568+
assert expected_owner_delete_form not in member_response.text
569+
# Check that the delete form is NOT present for built-in Administrator role
570+
expected_admin_delete_form = f'<form method="POST" action="http://testserver/roles/delete" class="d-inline">\\s*<input type="hidden" name="id" value="2">' # Check only for the form targeting admin role ID
571+
assert expected_admin_delete_form not in owner_response.text
572+
assert expected_admin_delete_form not in admin_response.text
573+
assert expected_admin_delete_form not in member_response.text
574+
# Check that the delete form is NOT present for built-in Member role
575+
expected_member_delete_form = f'<form method="POST" action="http://testserver/roles/delete" class="d-inline">\\s*<input type="hidden" name="id" value="3">' # Check only for the form targeting member role ID
576+
assert expected_member_delete_form not in owner_response.text
577+
assert expected_member_delete_form not in admin_response.text
578+
assert expected_member_delete_form not in member_response.text
549579

550580

551581
def test_create_role_form_modal(auth_client_owner, test_organization):

0 commit comments

Comments
 (0)