Skip to content

Commit 245a930

Browse files
Add permissions to Role (#382)
The API response for Roles now includes the permissions associated with that role. However, this has not been exposed in the ruby API. This commit fixes that problem.
1 parent 85d8249 commit 245a930

File tree

5 files changed

+202
-9
lines changed

5 files changed

+202
-9
lines changed

lib/workos/organizations.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,14 @@ def delete_organization(id:)
185185

186186
# Retrieve a list of roles for the given organization.
187187
#
188-
# @param [String] organizationId The ID of the organization to fetch roles for.
188+
# @param [String] organization_id The ID of the organization to fetch roles for.
189+
#
190+
# @example
191+
# WorkOS::Organizations.list_organization_roles(organization_id: 'org_01EHZNVPK3SFK441A1RGBFSHRT')
192+
# => #<WorkOS::Types::ListStruct data=[#<WorkOS::Role id="role_123" name="Admin" slug="admin"
193+
# permissions=["admin:all"] ...>] ...>
194+
#
195+
# @return [WorkOS::Types::ListStruct] - Collection of Role objects, each including permissions array
189196
def list_organization_roles(organization_id:)
190197
response = execute_request(
191198
request: get_request(

lib/workos/role.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module WorkOS
77
class Role
88
include HashProvider
99

10-
attr_accessor :id, :name, :slug, :description, :type, :created_at, :updated_at
10+
attr_accessor :id, :name, :slug, :description, :permissions, :type, :created_at, :updated_at
1111

1212
def initialize(json)
1313
hash = JSON.parse(json, symbolize_names: true)
@@ -16,6 +16,7 @@ def initialize(json)
1616
@name = hash[:name]
1717
@slug = hash[:slug]
1818
@description = hash[:description]
19+
@permissions = hash[:permissions] || []
1920
@type = hash[:type]
2021
@created_at = hash[:created_at]
2122
@updated_at = hash[:updated_at]
@@ -27,6 +28,7 @@ def to_json(*)
2728
name: name,
2829
slug: slug,
2930
description: description,
31+
permissions: permissions,
3032
type: type,
3133
created_at: created_at,
3234
updated_at: updated_at,

spec/lib/workos/organizations_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,48 @@
354354
expect(roles.list_metadata).to eq(expected_metadata)
355355
end
356356
end
357+
358+
it 'returns properly initialized Role objects with all attributes' do
359+
VCR.use_cassette 'organization/list_organization_roles' do
360+
roles = described_class.list_organization_roles(organization_id: 'org_01JEXP6Z3X7HE4CB6WQSH9ZAFE')
361+
362+
first_role = roles.data.first
363+
expect(first_role).to be_a(WorkOS::Role)
364+
expect(first_role.id).to eq('role_01HS1C7GRJE08PBR3M6Y0ZYGDZ')
365+
expect(first_role.name).to eq('Admin')
366+
expect(first_role.slug).to eq('admin')
367+
expect(first_role.description).to eq('Write access to every resource available')
368+
expect(first_role.permissions).to eq(['admin:all', 'read:users', 'write:users', 'manage:roles'])
369+
expect(first_role.type).to eq('EnvironmentRole')
370+
expect(first_role.created_at).to eq('2024-03-15T15:38:29.521Z')
371+
expect(first_role.updated_at).to eq('2024-11-14T17:08:00.556Z')
372+
end
373+
end
374+
375+
it 'handles roles with empty permissions arrays' do
376+
VCR.use_cassette 'organization/list_organization_roles' do
377+
roles = described_class.list_organization_roles(organization_id: 'org_01JEXP6Z3X7HE4CB6WQSH9ZAFE')
378+
379+
platform_manager_role = roles.data.find { |role| role.slug == 'org-platform-manager' }
380+
expect(platform_manager_role).to be_a(WorkOS::Role)
381+
expect(platform_manager_role.permissions).to eq([])
382+
end
383+
end
384+
385+
it 'properly serializes Role objects including permissions' do
386+
VCR.use_cassette 'organization/list_organization_roles' do
387+
roles = described_class.list_organization_roles(organization_id: 'org_01JEXP6Z3X7HE4CB6WQSH9ZAFE')
388+
389+
billing_role = roles.data.find { |role| role.slug == 'billing' }
390+
serialized = billing_role.to_json
391+
392+
expect(serialized[:id]).to eq('role_01JA8GJZRDSZEB9289DQXJ3N9Z')
393+
expect(serialized[:name]).to eq('Billing Manager')
394+
expect(serialized[:slug]).to eq('billing')
395+
expect(serialized[:permissions]).to eq(['read:billing', 'write:billing'])
396+
expect(serialized[:type]).to eq('EnvironmentRole')
397+
end
398+
end
357399
end
358400
end
359401
end

spec/lib/workos/role_spec.rb

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# frozen_string_literal: true
2+
3+
describe WorkOS::Role do
4+
describe '.initialize' do
5+
context 'with full role data including permissions' do
6+
it 'initializes all attributes correctly' do
7+
role_json = {
8+
id: 'role_01FAEAJCJ3P1Z6WP5Y9VQPN2XY',
9+
name: 'Admin',
10+
slug: 'admin',
11+
description: 'Administrator role with full access',
12+
permissions: ['read:users', 'write:users', 'admin:all'],
13+
type: 'system',
14+
created_at: '2022-05-13T17:45:31.732Z',
15+
updated_at: '2022-07-13T17:45:42.618Z',
16+
}.to_json
17+
18+
role = described_class.new(role_json)
19+
20+
expect(role.id).to eq('role_01FAEAJCJ3P1Z6WP5Y9VQPN2XY')
21+
expect(role.name).to eq('Admin')
22+
expect(role.slug).to eq('admin')
23+
expect(role.description).to eq('Administrator role with full access')
24+
expect(role.permissions).to eq(['read:users', 'write:users', 'admin:all'])
25+
expect(role.type).to eq('system')
26+
expect(role.created_at).to eq('2022-05-13T17:45:31.732Z')
27+
expect(role.updated_at).to eq('2022-07-13T17:45:42.618Z')
28+
end
29+
end
30+
31+
context 'with role data without permissions' do
32+
it 'initializes permissions as empty array' do
33+
role_json = {
34+
id: 'role_01FAEAJCJ3P1Z6WP5Y9VQPN2XY',
35+
name: 'User',
36+
slug: 'user',
37+
description: 'Basic user role',
38+
type: 'custom',
39+
created_at: '2022-05-13T17:45:31.732Z',
40+
updated_at: '2022-07-13T17:45:42.618Z',
41+
}.to_json
42+
43+
role = described_class.new(role_json)
44+
45+
expect(role.id).to eq('role_01FAEAJCJ3P1Z6WP5Y9VQPN2XY')
46+
expect(role.name).to eq('User')
47+
expect(role.slug).to eq('user')
48+
expect(role.description).to eq('Basic user role')
49+
expect(role.permissions).to eq([])
50+
expect(role.type).to eq('custom')
51+
expect(role.created_at).to eq('2022-05-13T17:45:31.732Z')
52+
expect(role.updated_at).to eq('2022-07-13T17:45:42.618Z')
53+
end
54+
end
55+
56+
context 'with role data with null permissions' do
57+
it 'initializes permissions as empty array' do
58+
role_json = {
59+
id: 'role_01FAEAJCJ3P1Z6WP5Y9VQPN2XY',
60+
name: 'User',
61+
slug: 'user',
62+
description: 'Basic user role',
63+
permissions: nil,
64+
type: 'custom',
65+
created_at: '2022-05-13T17:45:31.732Z',
66+
updated_at: '2022-07-13T17:45:42.618Z',
67+
}.to_json
68+
69+
role = described_class.new(role_json)
70+
71+
expect(role.permissions).to eq([])
72+
end
73+
end
74+
75+
context 'with role data with empty permissions array' do
76+
it 'preserves empty permissions array' do
77+
role_json = {
78+
id: 'role_01FAEAJCJ3P1Z6WP5Y9VQPN2XY',
79+
name: 'User',
80+
slug: 'user',
81+
description: 'Basic user role',
82+
permissions: [],
83+
type: 'custom',
84+
created_at: '2022-05-13T17:45:31.732Z',
85+
updated_at: '2022-07-13T17:45:42.618Z',
86+
}.to_json
87+
88+
role = described_class.new(role_json)
89+
90+
expect(role.permissions).to eq([])
91+
end
92+
end
93+
end
94+
95+
describe '.to_json' do
96+
context 'with role that has permissions' do
97+
it 'includes permissions in serialized output' do
98+
role_json = {
99+
id: 'role_01FAEAJCJ3P1Z6WP5Y9VQPN2XY',
100+
name: 'Admin',
101+
slug: 'admin',
102+
description: 'Administrator role',
103+
permissions: ['read:all', 'write:all'],
104+
type: 'system',
105+
created_at: '2022-05-13T17:45:31.732Z',
106+
updated_at: '2022-07-13T17:45:42.618Z',
107+
}.to_json
108+
109+
role = described_class.new(role_json)
110+
serialized = role.to_json
111+
112+
expect(serialized[:id]).to eq('role_01FAEAJCJ3P1Z6WP5Y9VQPN2XY')
113+
expect(serialized[:name]).to eq('Admin')
114+
expect(serialized[:slug]).to eq('admin')
115+
expect(serialized[:description]).to eq('Administrator role')
116+
expect(serialized[:permissions]).to eq(['read:all', 'write:all'])
117+
expect(serialized[:type]).to eq('system')
118+
expect(serialized[:created_at]).to eq('2022-05-13T17:45:31.732Z')
119+
expect(serialized[:updated_at]).to eq('2022-07-13T17:45:42.618Z')
120+
end
121+
end
122+
123+
context 'with role that has no permissions' do
124+
it 'includes empty permissions array in serialized output' do
125+
role_json = {
126+
id: 'role_01FAEAJCJ3P1Z6WP5Y9VQPN2XY',
127+
name: 'User',
128+
slug: 'user',
129+
description: 'Basic user role',
130+
type: 'custom',
131+
created_at: '2022-05-13T17:45:31.732Z',
132+
updated_at: '2022-07-13T17:45:42.618Z',
133+
}.to_json
134+
135+
role = described_class.new(role_json)
136+
serialized = role.to_json
137+
138+
expect(serialized[:permissions]).to eq([])
139+
end
140+
end
141+
end
142+
end

spec/support/fixtures/vcr_cassettes/organization/list_organization_roles.yml

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)