|
| 1 | +"""Tests for new authorization types: Resource, RoleAssignment, AccessEvaluation, |
| 2 | +AuthorizationOrganizationMembership.""" |
| 3 | + |
| 4 | +from workos.types.authorization import ( |
| 5 | + AccessEvaluation, |
| 6 | + AuthorizationOrganizationMembership, |
| 7 | + Resource, |
| 8 | + RoleAssignment, |
| 9 | + RoleAssignmentResource, |
| 10 | + RoleAssignmentRole, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +class TestAccessEvaluation: |
| 15 | + def test_authorized_true(self): |
| 16 | + result = AccessEvaluation(authorized=True) |
| 17 | + assert result.authorized is True |
| 18 | + |
| 19 | + def test_authorized_false(self): |
| 20 | + result = AccessEvaluation(authorized=False) |
| 21 | + assert result.authorized is False |
| 22 | + |
| 23 | + def test_from_dict(self): |
| 24 | + result = AccessEvaluation.model_validate({"authorized": True}) |
| 25 | + assert result.authorized is True |
| 26 | + |
| 27 | + |
| 28 | +class TestResource: |
| 29 | + def test_resource_deserialization(self): |
| 30 | + data = { |
| 31 | + "object": "authorization_resource", |
| 32 | + "id": "res_01ABC", |
| 33 | + "external_id": "ext_123", |
| 34 | + "name": "Test Document", |
| 35 | + "resource_type_slug": "document", |
| 36 | + "organization_id": "org_01EHT88Z8J8795GZNQ4ZP1J81T", |
| 37 | + "created_at": "2024-01-01T00:00:00Z", |
| 38 | + "updated_at": "2024-01-01T00:00:00Z", |
| 39 | + } |
| 40 | + resource = Resource.model_validate(data) |
| 41 | + |
| 42 | + assert resource.object == "authorization_resource" |
| 43 | + assert resource.id == "res_01ABC" |
| 44 | + assert resource.external_id == "ext_123" |
| 45 | + assert resource.name == "Test Document" |
| 46 | + assert resource.resource_type_slug == "document" |
| 47 | + assert resource.organization_id == "org_01EHT88Z8J8795GZNQ4ZP1J81T" |
| 48 | + assert resource.description is None |
| 49 | + assert resource.parent_resource_id is None |
| 50 | + |
| 51 | + def test_resource_with_optional_fields(self): |
| 52 | + data = { |
| 53 | + "object": "authorization_resource", |
| 54 | + "id": "res_01ABC", |
| 55 | + "external_id": "ext_123", |
| 56 | + "name": "Test Document", |
| 57 | + "description": "A test document resource", |
| 58 | + "resource_type_slug": "document", |
| 59 | + "organization_id": "org_01EHT88Z8J8795GZNQ4ZP1J81T", |
| 60 | + "parent_resource_id": "res_01PARENT", |
| 61 | + "created_at": "2024-01-01T00:00:00Z", |
| 62 | + "updated_at": "2024-01-01T00:00:00Z", |
| 63 | + } |
| 64 | + resource = Resource.model_validate(data) |
| 65 | + |
| 66 | + assert resource.description == "A test document resource" |
| 67 | + assert resource.parent_resource_id == "res_01PARENT" |
| 68 | + |
| 69 | + |
| 70 | +class TestRoleAssignment: |
| 71 | + def test_role_assignment_deserialization(self): |
| 72 | + data = { |
| 73 | + "object": "role_assignment", |
| 74 | + "id": "ra_01ABC", |
| 75 | + "role": {"slug": "admin"}, |
| 76 | + "resource": { |
| 77 | + "id": "res_01ABC", |
| 78 | + "external_id": "ext_123", |
| 79 | + "resource_type_slug": "document", |
| 80 | + }, |
| 81 | + "created_at": "2024-01-01T00:00:00Z", |
| 82 | + "updated_at": "2024-01-01T00:00:00Z", |
| 83 | + } |
| 84 | + assignment = RoleAssignment.model_validate(data) |
| 85 | + |
| 86 | + assert assignment.object == "role_assignment" |
| 87 | + assert assignment.id == "ra_01ABC" |
| 88 | + assert assignment.role.slug == "admin" |
| 89 | + assert assignment.resource.id == "res_01ABC" |
| 90 | + assert assignment.resource.external_id == "ext_123" |
| 91 | + assert assignment.resource.resource_type_slug == "document" |
| 92 | + |
| 93 | + def test_role_assignment_role(self): |
| 94 | + role = RoleAssignmentRole(slug="editor") |
| 95 | + assert role.slug == "editor" |
| 96 | + |
| 97 | + def test_role_assignment_resource(self): |
| 98 | + resource = RoleAssignmentResource( |
| 99 | + id="res_01ABC", |
| 100 | + external_id="ext_123", |
| 101 | + resource_type_slug="document", |
| 102 | + ) |
| 103 | + assert resource.id == "res_01ABC" |
| 104 | + assert resource.external_id == "ext_123" |
| 105 | + assert resource.resource_type_slug == "document" |
| 106 | + |
| 107 | + |
| 108 | +class TestAuthorizationOrganizationMembership: |
| 109 | + def test_membership_deserialization(self): |
| 110 | + data = { |
| 111 | + "object": "organization_membership", |
| 112 | + "id": "om_01ABC", |
| 113 | + "user_id": "user_01ABC", |
| 114 | + "organization_id": "org_01ABC", |
| 115 | + "organization_name": "Test Org", |
| 116 | + "status": "active", |
| 117 | + "created_at": "2024-01-01T00:00:00Z", |
| 118 | + "updated_at": "2024-01-01T00:00:00Z", |
| 119 | + } |
| 120 | + membership = AuthorizationOrganizationMembership.model_validate(data) |
| 121 | + |
| 122 | + assert membership.object == "organization_membership" |
| 123 | + assert membership.id == "om_01ABC" |
| 124 | + assert membership.user_id == "user_01ABC" |
| 125 | + assert membership.organization_id == "org_01ABC" |
| 126 | + assert membership.organization_name == "Test Org" |
| 127 | + assert membership.status == "active" |
| 128 | + assert membership.custom_attributes is None |
| 129 | + |
| 130 | + def test_membership_with_custom_attributes(self): |
| 131 | + data = { |
| 132 | + "object": "organization_membership", |
| 133 | + "id": "om_01ABC", |
| 134 | + "user_id": "user_01ABC", |
| 135 | + "organization_id": "org_01ABC", |
| 136 | + "organization_name": "Test Org", |
| 137 | + "status": "active", |
| 138 | + "custom_attributes": {"department": "Engineering"}, |
| 139 | + "created_at": "2024-01-01T00:00:00Z", |
| 140 | + "updated_at": "2024-01-01T00:00:00Z", |
| 141 | + } |
| 142 | + membership = AuthorizationOrganizationMembership.model_validate(data) |
| 143 | + |
| 144 | + assert membership.custom_attributes == {"department": "Engineering"} |
0 commit comments