|
| 1 | +from typing import Union |
| 2 | + |
| 3 | +import pytest |
| 4 | +from tests.utils.syncify import syncify |
| 5 | +from workos.authorization import AsyncAuthorization, Authorization |
| 6 | +from workos.types.authorization.resource_identifier import ( |
| 7 | + ResourceIdentifierByExternalId, |
| 8 | + ResourceIdentifierById, |
| 9 | +) |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.sync_and_async(Authorization, AsyncAuthorization) |
| 13 | +class TestAuthorizationCheck: |
| 14 | + @pytest.fixture(autouse=True) |
| 15 | + def setup(self, module_instance: Union[Authorization, AsyncAuthorization]): |
| 16 | + self.http_client = module_instance._http_client |
| 17 | + self.authorization = module_instance |
| 18 | + |
| 19 | + @pytest.fixture |
| 20 | + def mock_check_authorized(self): |
| 21 | + return {"authorized": True} |
| 22 | + |
| 23 | + @pytest.fixture |
| 24 | + def mock_check_unauthorized(self): |
| 25 | + return {"authorized": False} |
| 26 | + |
| 27 | + def test_check_authorized( |
| 28 | + self, mock_check_authorized, capture_and_mock_http_client_request |
| 29 | + ): |
| 30 | + request_kwargs = capture_and_mock_http_client_request( |
| 31 | + self.http_client, mock_check_authorized, 200 |
| 32 | + ) |
| 33 | + |
| 34 | + result = syncify( |
| 35 | + self.authorization.check( |
| 36 | + "om_01ABC", |
| 37 | + permission_slug="documents:read", |
| 38 | + resource=ResourceIdentifierById(resource_id="res_01ABC"), |
| 39 | + ) |
| 40 | + ) |
| 41 | + |
| 42 | + assert result.authorized is True |
| 43 | + assert request_kwargs["method"] == "post" |
| 44 | + assert request_kwargs["url"].endswith( |
| 45 | + "/authorization/organization_memberships/om_01ABC/check" |
| 46 | + ) |
| 47 | + |
| 48 | + def test_check_unauthorized( |
| 49 | + self, mock_check_unauthorized, capture_and_mock_http_client_request |
| 50 | + ): |
| 51 | + request_kwargs = capture_and_mock_http_client_request( |
| 52 | + self.http_client, mock_check_unauthorized, 200 |
| 53 | + ) |
| 54 | + |
| 55 | + result = syncify( |
| 56 | + self.authorization.check( |
| 57 | + "om_01ABC", |
| 58 | + permission_slug="documents:write", |
| 59 | + resource=ResourceIdentifierById(resource_id="res_01ABC"), |
| 60 | + ) |
| 61 | + ) |
| 62 | + |
| 63 | + assert result.authorized is False |
| 64 | + assert request_kwargs["method"] == "post" |
| 65 | + |
| 66 | + def test_check_with_resource_id( |
| 67 | + self, mock_check_authorized, capture_and_mock_http_client_request |
| 68 | + ): |
| 69 | + request_kwargs = capture_and_mock_http_client_request( |
| 70 | + self.http_client, mock_check_authorized, 200 |
| 71 | + ) |
| 72 | + |
| 73 | + syncify( |
| 74 | + self.authorization.check( |
| 75 | + "om_01ABC", |
| 76 | + permission_slug="documents:read", |
| 77 | + resource=ResourceIdentifierById(resource_id="res_01XYZ"), |
| 78 | + ) |
| 79 | + ) |
| 80 | + |
| 81 | + assert request_kwargs["json"] == { |
| 82 | + "permission_slug": "documents:read", |
| 83 | + "resource_id": "res_01XYZ", |
| 84 | + } |
| 85 | + |
| 86 | + def test_check_with_resource_external_id( |
| 87 | + self, mock_check_authorized, capture_and_mock_http_client_request |
| 88 | + ): |
| 89 | + request_kwargs = capture_and_mock_http_client_request( |
| 90 | + self.http_client, mock_check_authorized, 200 |
| 91 | + ) |
| 92 | + |
| 93 | + syncify( |
| 94 | + self.authorization.check( |
| 95 | + "om_01ABC", |
| 96 | + permission_slug="documents:read", |
| 97 | + resource=ResourceIdentifierByExternalId( |
| 98 | + resource_external_id="ext_doc_123", |
| 99 | + resource_type_slug="document", |
| 100 | + ), |
| 101 | + ) |
| 102 | + ) |
| 103 | + |
| 104 | + assert request_kwargs["json"] == { |
| 105 | + "permission_slug": "documents:read", |
| 106 | + "resource_external_id": "ext_doc_123", |
| 107 | + "resource_type_slug": "document", |
| 108 | + } |
| 109 | + |
| 110 | + def test_check_url_construction( |
| 111 | + self, mock_check_authorized, capture_and_mock_http_client_request |
| 112 | + ): |
| 113 | + request_kwargs = capture_and_mock_http_client_request( |
| 114 | + self.http_client, mock_check_authorized, 200 |
| 115 | + ) |
| 116 | + |
| 117 | + syncify( |
| 118 | + self.authorization.check( |
| 119 | + "om_01MEMBERSHIP", |
| 120 | + permission_slug="admin:access", |
| 121 | + resource=ResourceIdentifierById(resource_id="res_01ABC"), |
| 122 | + ) |
| 123 | + ) |
| 124 | + |
| 125 | + assert request_kwargs["url"].endswith( |
| 126 | + "/authorization/organization_memberships/om_01MEMBERSHIP/check" |
| 127 | + ) |
0 commit comments