|
| 1 | +# test_role.py |
| 2 | + |
| 3 | +import pytest |
| 4 | +from utils.models import Role, Permission, ValidPermissions, User |
| 5 | +from sqlmodel import Session, select |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture |
| 9 | +def admin_user(session: Session, test_user: User, test_organization): |
| 10 | + """Create an admin user with CREATE_ROLE permission""" |
| 11 | + admin_role: Role = Role( |
| 12 | + name="Admin", |
| 13 | + organization_id=test_organization.id |
| 14 | + ) |
| 15 | + |
| 16 | + create_role_permission: Permission | None = session.exec( |
| 17 | + select(Permission).where(Permission.name == ValidPermissions.CREATE_ROLE) |
| 18 | + ).first() |
| 19 | + |
| 20 | + if create_role_permission is None: |
| 21 | + raise ValueError("Error during test setup: CREATE_ROLE permission not found") |
| 22 | + |
| 23 | + admin_role.permissions.append(create_role_permission) |
| 24 | + session.add(admin_role) |
| 25 | + |
| 26 | + test_user.roles.append(admin_role) |
| 27 | + session.commit() |
| 28 | + |
| 29 | + return test_user |
| 30 | + |
| 31 | + |
| 32 | +def test_create_role_success(auth_client, admin_user, test_organization, session: Session): |
| 33 | + """Test successful role creation""" |
| 34 | + response = auth_client.post( |
| 35 | + "/roles/create", |
| 36 | + data={ |
| 37 | + "name": "Test Role", |
| 38 | + "organization_id": test_organization.id, |
| 39 | + "permissions": [ValidPermissions.EDIT_ROLE.value] |
| 40 | + }, |
| 41 | + follow_redirects=False |
| 42 | + ) |
| 43 | + |
| 44 | + assert response.status_code == 303 |
| 45 | + |
| 46 | + # Verify role was created in database |
| 47 | + created_role = session.exec( |
| 48 | + select(Role).where( |
| 49 | + Role.name == "Test Role", |
| 50 | + Role.organization_id == test_organization.id |
| 51 | + ) |
| 52 | + ).first() |
| 53 | + |
| 54 | + assert created_role is not None |
| 55 | + assert created_role.name == "Test Role" |
| 56 | + assert len(created_role.permissions) == 1 |
| 57 | + assert created_role.permissions[0].name == ValidPermissions.EDIT_ROLE |
| 58 | + |
| 59 | + |
| 60 | +def test_create_role_unauthorized(auth_client, test_user, test_organization): |
| 61 | + """Test role creation without proper permissions""" |
| 62 | + response = auth_client.post( |
| 63 | + "/roles/create", |
| 64 | + data={ |
| 65 | + "name": "Test Role", |
| 66 | + "organization_id": test_organization.id, |
| 67 | + "permissions": [ValidPermissions.EDIT_ROLE.value] |
| 68 | + }, |
| 69 | + follow_redirects=False |
| 70 | + ) |
| 71 | + |
| 72 | + assert response.status_code == 403 |
| 73 | + |
| 74 | + |
| 75 | +def test_create_duplicate_role(auth_client, admin_user, test_organization, session: Session): |
| 76 | + """Test creating a role with a name that already exists in the organization""" |
| 77 | + # Create initial role |
| 78 | + existing_role = Role( |
| 79 | + name="Existing Role", |
| 80 | + organization_id=test_organization.id |
| 81 | + ) |
| 82 | + session.add(existing_role) |
| 83 | + session.commit() |
| 84 | + |
| 85 | + # Attempt to create role with same name |
| 86 | + response = auth_client.post( |
| 87 | + "/roles/create", |
| 88 | + data={ |
| 89 | + "name": "Existing Role", |
| 90 | + "organization_id": test_organization.id, |
| 91 | + "permissions": [ValidPermissions.EDIT_ROLE.value] |
| 92 | + }, |
| 93 | + follow_redirects=False |
| 94 | + ) |
| 95 | + |
| 96 | + assert response.status_code == 400 |
| 97 | + |
| 98 | + |
| 99 | +def test_create_role_unauthenticated(unauth_client, test_organization): |
| 100 | + """Test role creation without authentication""" |
| 101 | + response = unauth_client.post( |
| 102 | + "/roles/create", |
| 103 | + data={ |
| 104 | + "name": "Test Role", |
| 105 | + "organization_id": test_organization.id, |
| 106 | + "permissions": [ValidPermissions.EDIT_ROLE.value] |
| 107 | + }, |
| 108 | + follow_redirects=False |
| 109 | + ) |
| 110 | + |
| 111 | + assert response.status_code == 303 |
0 commit comments