|
| 1 | +from functools import lru_cache, wraps |
| 2 | +from typing import Any, Callable, TypeVar |
| 3 | +from unittest import TestCase |
| 4 | + |
| 5 | +from office365.directory.applications.roles.collection import AppRoleCollection |
| 6 | +from office365.graph_client import GraphClient |
| 7 | +from office365.runtime.types.collections import StringCollection |
| 8 | +from tests import test_client_id |
| 9 | + |
| 10 | +T = TypeVar("T", bound=Callable[..., Any]) |
| 11 | + |
| 12 | + |
| 13 | +@lru_cache(maxsize=1) |
| 14 | +def _get_cached_permissions(client, client_id): |
| 15 | + # type: (GraphClient, str) -> AppRoleCollection |
| 16 | + """Get and cache application permissions for a client""" |
| 17 | + resource = client.service_principals.get_by_name("Microsoft Graph") |
| 18 | + result = resource.get_application_permissions(client_id).execute_query() |
| 19 | + return result.value |
| 20 | + |
| 21 | + |
| 22 | +def requires_app_permission(app_role): |
| 23 | + # type: (str) -> Callable[[T], T] |
| 24 | + def decorator(test_method): |
| 25 | + # type: (T) -> T |
| 26 | + @wraps(test_method) |
| 27 | + def wrapper(self, *args, **kwargs): |
| 28 | + # type: (TestCase, *Any, **Any) -> Any |
| 29 | + client = getattr(self, "client", None) |
| 30 | + if not client: |
| 31 | + self.skipTest("No client available for permission check") |
| 32 | + |
| 33 | + try: |
| 34 | + permissions = _get_cached_permissions(client, test_client_id) |
| 35 | + |
| 36 | + if not any(role.value == app_role for role in permissions): |
| 37 | + self.skipTest(f"Required app permission '{app_role}' not granted") |
| 38 | + |
| 39 | + return test_method(self, *args, **kwargs) |
| 40 | + |
| 41 | + except Exception as e: |
| 42 | + self.skipTest(f"Permission check failed: {str(e)}") |
| 43 | + |
| 44 | + return wrapper |
| 45 | + |
| 46 | + return decorator |
| 47 | + |
| 48 | + |
| 49 | +@lru_cache(maxsize=1) |
| 50 | +def _get_cached_delegated_permissions(client, client_id): |
| 51 | + # type: (GraphClient, str) -> StringCollection |
| 52 | + """Get and cache delegated permissions for a client""" |
| 53 | + resource = client.service_principals.get_by_name("Microsoft Graph") |
| 54 | + result = resource.get_delegated_permissions(client_id).execute_query() |
| 55 | + return result.value |
| 56 | + |
| 57 | + |
| 58 | +def requires_delegated_permission(*scopes): |
| 59 | + # type: (*str) -> Callable[[T], T] |
| 60 | + """Decorator to verify delegated permissions before test execution""" |
| 61 | + |
| 62 | + def decorator(test_method): |
| 63 | + # type: (T) -> T |
| 64 | + @wraps(test_method) |
| 65 | + def wrapper(self, *args, **kwargs): |
| 66 | + # type: (TestCase, *Any, **Any) -> Any |
| 67 | + client = getattr(self, "client", None) |
| 68 | + if not client: |
| 69 | + self.skipTest("No client available for permission check") |
| 70 | + |
| 71 | + try: |
| 72 | + # Get permissions from cache or API |
| 73 | + granted_scopes = _get_cached_delegated_permissions(client, test_client_id) |
| 74 | + |
| 75 | + if not any(scope in granted_scopes for scope in scopes): |
| 76 | + self.skipTest( |
| 77 | + f"Required delegated permission '{', '.join(scopes)}' not granted" |
| 78 | + ) |
| 79 | + |
| 80 | + return test_method(self, *args, **kwargs) |
| 81 | + |
| 82 | + except Exception as e: |
| 83 | + self.skipTest(f"Permission check failed: {str(e)}") |
| 84 | + |
| 85 | + return wrapper # type: ignore[return-value] |
| 86 | + |
| 87 | + return decorator |
0 commit comments