-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add Directory Sync support * rubocop * fix fixture
- Loading branch information
Showing
14 changed files
with
895 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
# frozen_string_literal: true | ||
# typed: true | ||
|
||
module WorkOS | ||
# The Directory Sync module provides convenience methods for working with the | ||
# WorkOS Directory Sync platform. You'll need a valid API key and to have | ||
# created a Directory Sync connection on your WorkOS dashboard. | ||
# | ||
# @see https://docs.workos.com/directory-sync/overview | ||
module DirectorySync | ||
class << self | ||
extend T::Sig | ||
include Base | ||
include Client | ||
|
||
# Retrieve directories. | ||
# | ||
# @param [Hash] options An options hash | ||
# @option options [String] domain The domain of the directory to be | ||
# retrieved. | ||
# @option options [String] search A search term for direcory names. | ||
# | ||
# @return [Hash] | ||
sig do | ||
params( | ||
options: T::Hash[Symbol, String], | ||
).returns(T::Array[T::Hash[String, T.nilable(String)]]) | ||
end | ||
def list_directories(options = {}) | ||
response = execute_request( | ||
request: get_request( | ||
path: '/directories', | ||
auth: true, | ||
params: options, | ||
), | ||
) | ||
|
||
JSON.parse(response.body)['data'] | ||
end | ||
|
||
# Retrieve directory groups. | ||
# | ||
# @param [Hash] options An options hash | ||
# @option options [String] directory The ID of the directory whose | ||
# directory groups will be retrieved. | ||
# @option options [String] user The ID of the directory user whose | ||
# directory groups will be retrieved. | ||
# | ||
# @return [Hash] | ||
sig do | ||
params( | ||
options: T::Hash[Symbol, String], | ||
).returns(T::Array[T::Hash[String, T.nilable(String)]]) | ||
end | ||
def list_groups(options = {}) | ||
response = execute_request( | ||
request: get_request( | ||
path: '/directory_groups', | ||
auth: true, | ||
params: options, | ||
), | ||
) | ||
|
||
JSON.parse(response.body)['data'] | ||
end | ||
|
||
# Retrieve directory users. | ||
# | ||
# @param [Hash] options An options hash | ||
# @option options [String] directory The ID of the directory whose | ||
# directory users will be retrieved. | ||
# @option options [String] user The ID of the directory group whose | ||
# directory users will be retrieved. | ||
# | ||
# @return [Hash] | ||
sig do | ||
params( | ||
options: T::Hash[Symbol, String], | ||
).returns(T::Array[T::Hash[String, T.untyped]]) | ||
end | ||
def list_users(options = {}) | ||
response = execute_request( | ||
request: get_request( | ||
path: '/directory_users', | ||
auth: true, | ||
params: options, | ||
), | ||
) | ||
|
||
JSON.parse(response.body)['data'] | ||
end | ||
|
||
# Retrieve the directory group with the given ID. | ||
# | ||
# @param [String] id The ID of the directory group. | ||
# | ||
# @return Hash | ||
sig { params(id: String).returns(T::Hash[String, T.untyped]) } | ||
def get_group(id) | ||
response = execute_request( | ||
request: get_request( | ||
path: "/directory_groups/#{id}", | ||
auth: true, | ||
), | ||
) | ||
|
||
JSON.parse(response.body) | ||
end | ||
|
||
# Retrieve the directory user with the given ID. | ||
# | ||
# @param [String] id The ID of the directory user. | ||
# | ||
# @return Hash | ||
sig { params(id: String).returns(T::Hash[String, T.untyped]) } | ||
def get_user(id) | ||
response = execute_request( | ||
request: get_request( | ||
path: "/directory_users/#{id}", | ||
auth: true, | ||
), | ||
) | ||
|
||
JSON.parse(response.body) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
# frozen_string_literal: true | ||
# typed: false | ||
|
||
describe WorkOS::DirectorySync do | ||
before(:all) do | ||
WorkOS.key = 'key' | ||
end | ||
|
||
after(:all) do | ||
WorkOS.key = nil | ||
end | ||
|
||
describe '.list_directories' do | ||
context 'with no options' do | ||
it 'returns directories' do | ||
VCR.use_cassette('directory_sync/list_directories') do | ||
directories = WorkOS::DirectorySync.list_directories | ||
expect(directories.size).to eq(1) | ||
end | ||
end | ||
end | ||
|
||
context 'with domain option' do | ||
it 'returns directories' do | ||
VCR.use_cassette('directory_sync/list_directories_with_domain_param') do | ||
directories = WorkOS::DirectorySync.list_directories( | ||
domain: 'foo-corp.com', | ||
) | ||
|
||
expect(directories.first['domain']).to eq('foo-corp.com') | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe '.list_groups' do | ||
context 'with no options' do | ||
it 'returns groups' do | ||
VCR.use_cassette('directory_sync/list_groups') do | ||
expect do | ||
WorkOS::DirectorySync.list_groups | ||
end.to raise_error( | ||
WorkOS::InvalidRequestError, | ||
/Status 422, Validation failed/, | ||
) | ||
end | ||
end | ||
end | ||
|
||
context 'with directory option' do | ||
it 'returns groups' do | ||
VCR.use_cassette('directory_sync/list_groups_with_directory_param') do | ||
groups = WorkOS::DirectorySync.list_groups( | ||
directory: 'directory_edp_01E64QQVQTCB0DECJ9CFNXEWDW', | ||
) | ||
|
||
expect(groups.size).to eq(2) | ||
expect(groups.first['name']).to eq('Walrus') | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe '.list_users' do | ||
context 'with no options' do | ||
it 'returns users' do | ||
VCR.use_cassette('directory_sync/list_users') do | ||
expect do | ||
WorkOS::DirectorySync.list_users | ||
end.to raise_error( | ||
WorkOS::InvalidRequestError, | ||
/Status 422, Validation failed/, | ||
) | ||
end | ||
end | ||
end | ||
|
||
context 'with directory option' do | ||
it 'returns users' do | ||
VCR.use_cassette('directory_sync/list_users_with_directory_param') do | ||
users = WorkOS::DirectorySync.list_users( | ||
directory: 'directory_edp_01E64QQVQTCB0DECJ9CFNXEWDW', | ||
) | ||
|
||
expect(users.size).to eq(1) | ||
expect(users.first['last_name']).to eq('Tran') | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe '.get_group' do | ||
context 'with valid id' do | ||
it 'returns a group' do | ||
VCR.use_cassette('directory_sync/get_group') do | ||
group = WorkOS::DirectorySync.get_group( | ||
'directory_grp_01E64QTDNS0EGJ0FMCVY9BWGZT', | ||
) | ||
|
||
expect(group['name']).to eq('Walrus') | ||
end | ||
end | ||
end | ||
|
||
context 'with invalid id' do | ||
it 'raises an error' do | ||
VCR.use_cassette('directory_sync/get_group_with_invalid_id') do | ||
expect do | ||
WorkOS::DirectorySync.get_group('invalid') | ||
end.to raise_error(WorkOS::APIError) | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe '.get_user' do | ||
context 'with valid id' do | ||
it 'returns a user' do | ||
VCR.use_cassette('directory_sync/get_user') do | ||
user = WorkOS::DirectorySync.get_user( | ||
'directory_usr_01E64QS50EAY48S0XJ1AA4WX4D', | ||
) | ||
|
||
expect(user['first_name']).to eq('Mark') | ||
end | ||
end | ||
end | ||
|
||
context 'with invalid id' do | ||
it 'raises an error' do | ||
VCR.use_cassette('directory_sync/get_user_with_invalid_id') do | ||
expect do | ||
WorkOS::DirectorySync.get_user('invalid') | ||
end.to raise_error(WorkOS::APIError) | ||
end | ||
end | ||
end | ||
end | ||
end |
62 changes: 62 additions & 0 deletions
62
spec/support/fixtures/vcr_cassettes/directory_sync/get_group.yml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.