|
4 | 4 | from tests.conftest import SetupError
|
5 | 5 | from utils.models import Role, Permission, ValidPermissions, User
|
6 | 6 | from sqlmodel import Session, select
|
| 7 | +import re |
7 | 8 |
|
8 | 9 |
|
9 | 10 | @pytest.fixture
|
@@ -521,31 +522,60 @@ def test_organization_page_role_edit_access(auth_client_owner, auth_client_admin
|
521 | 522 | assert "Edit Role" not in member_response.text
|
522 | 523 |
|
523 | 524 |
|
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): |
525 | 526 | """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 |
527 | 537 | owner_response = auth_client_owner.get(
|
528 | 538 | f"/organizations/{test_organization.id}",
|
529 | 539 | follow_redirects=False
|
530 | 540 | )
|
531 | 541 | 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 |
535 | 546 | admin_response = auth_client_admin.get(
|
536 | 547 | f"/organizations/{test_organization.id}",
|
537 | 548 | follow_redirects=False
|
538 | 549 | )
|
539 | 550 | 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 |
543 | 555 | member_response = auth_client_member.get(
|
544 | 556 | f"/organizations/{test_organization.id}",
|
545 | 557 | follow_redirects=False
|
546 | 558 | )
|
547 | 559 | 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 |
549 | 579 |
|
550 | 580 |
|
551 | 581 | def test_create_role_form_modal(auth_client_owner, test_organization):
|
|
0 commit comments