|
1 |
| -import pytest |
| 1 | +from datetime import timedelta, datetime, UTC |
| 2 | +from typing import Optional |
2 | 3 | from sqlmodel import select, Session
|
3 | 4 | from utils.models import (
|
4 | 5 | Permission,
|
|
8 | 9 | ValidPermissions,
|
9 | 10 | User,
|
10 | 11 | UserRoleLink,
|
11 |
| - PasswordResetToken, |
| 12 | + PasswordResetToken |
12 | 13 | )
|
13 |
| -from datetime import timedelta, datetime, UTC |
| 14 | +from .conftest import SetupError |
14 | 15 |
|
15 | 16 |
|
16 | 17 | def test_permissions_persist_after_role_deletion(session: Session):
|
@@ -102,10 +103,9 @@ def test_organization_users_property(session: Session, test_user: User, test_org
|
102 | 103 | session.refresh(test_organization)
|
103 | 104 |
|
104 | 105 | # Test the users property
|
105 |
| - users_list = test_organization.users |
| 106 | + users_list: list[User] = test_organization.users |
106 | 107 | assert len(users_list) == 1
|
107 |
| - # users_list is a list of lists due to the property implementation |
108 |
| - assert test_user in users_list[0] |
| 108 | + assert test_user in users_list |
109 | 109 |
|
110 | 110 |
|
111 | 111 | def test_cascade_delete_organization(session: Session, test_user: User, test_organization: Organization):
|
@@ -185,3 +185,46 @@ def test_password_reset_token_is_expired(session: Session, test_user: User):
|
185 | 185 | # Verify expiration states
|
186 | 186 | assert expired_token.is_expired()
|
187 | 187 | assert not valid_token.is_expired()
|
| 188 | + |
| 189 | + |
| 190 | +def test_user_has_permission(session: Session, test_user: User, test_organization: Organization): |
| 191 | + """ |
| 192 | + Test that User.has_permission method correctly checks if a user has a specific |
| 193 | + permission for a given organization. |
| 194 | + """ |
| 195 | + # Create a role with specific permissions in the test organization |
| 196 | + role = Role(name="Test Role", organization_id=test_organization.id) |
| 197 | + session.add(role) |
| 198 | + session.commit() |
| 199 | + session.refresh(role) |
| 200 | + |
| 201 | + # Assign permissions to the role |
| 202 | + delete_org_permission: Optional[Permission] = session.exec( |
| 203 | + select(Permission).where(Permission.name == |
| 204 | + ValidPermissions.DELETE_ORGANIZATION) |
| 205 | + ).first() |
| 206 | + edit_org_permission: Optional[Permission] = session.exec( |
| 207 | + select(Permission).where(Permission.name == |
| 208 | + ValidPermissions.EDIT_ORGANIZATION) |
| 209 | + ).first() |
| 210 | + |
| 211 | + if delete_org_permission is not None and edit_org_permission is not None: |
| 212 | + role.permissions.append(delete_org_permission) |
| 213 | + role.permissions.append(edit_org_permission) |
| 214 | + else: |
| 215 | + raise SetupError( |
| 216 | + "Test setup failed; permission not found in database") |
| 217 | + session.commit() |
| 218 | + |
| 219 | + # Link the user to the role |
| 220 | + test_user.roles.append(role) |
| 221 | + session.commit() |
| 222 | + session.refresh(test_user) |
| 223 | + |
| 224 | + # Test the has_permission method |
| 225 | + assert test_user.has_permission( |
| 226 | + ValidPermissions.DELETE_ORGANIZATION, test_organization) is True |
| 227 | + assert test_user.has_permission( |
| 228 | + ValidPermissions.EDIT_ORGANIZATION, test_organization) is True |
| 229 | + assert test_user.has_permission( |
| 230 | + ValidPermissions.INVITE_USER, test_organization) is False |
0 commit comments