Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test user apis #77

Merged
merged 2 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ GEM

PLATFORMS
arm64-darwin-23
arm64-darwin-24
x86_64-linux

DEPENDENCIES
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,13 @@ descope_client.create_test_user(
user_tenants: client.associated_tenants_to_hash_array(associated_tenants)
)

# Search all test users, optionally according to tenant and/or role filter
# results can be paginated using the limit and page parameters
users_resp = descope_client.search_all_test_users()
users = users_resp["users"]
users.each do |user|
# Do something

# Now test user got created, and this user will be available until you delete it,
# you can use any management operation for test user CRUD.
# You can also delete all test users.
Expand Down
3 changes: 3 additions & 0 deletions lib/descope/api/v1/management/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Common

# user
USER_CREATE_PATH = '/v1/mgmt/user/create'
TEST_USER_CREATE_PATH = '/v1/mgmt/user/create/test'
USER_CREATE_BATCH_PATH = '/v1/mgmt/user/create/batch'
USER_UPDATE_PATH = '/v1/mgmt/user/update'
USER_DELETE_PATH = '/v1/mgmt/user/delete'
Expand All @@ -37,6 +38,8 @@ module Common
USER_SET_TEMPORARY_PASSWORD_PATH = '/v1/mgmt/user/password/set/temporary'
USER_SET_ACTIVE_PASSWORD_PATH = '/v1/mgmt/user/password/set/active'
USER_SET_PASSWORD_PATH = '/v1/mgmt/user/password/set'
USER_SEARCH_PATH = "/v2/mgmt/user/search"
TEST_USERS_SEARCH_PATH = "/v2/mgmt/user/search/test"
USER_EXPIRE_PASSWORD_PATH = '/v1/mgmt/user/password/expire'
USER_ADD_TENANT_PATH = '/v1/mgmt/user/update/tenant/add'
USER_REMOVE_TENANT_PATH = '/v1/mgmt/user/update/tenant/remove'
Expand Down
86 changes: 79 additions & 7 deletions lib/descope/api/v1/management/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,71 @@ def generate_embedded_link(login_id: nil, custom_claims: nil)
post(USER_GENERATE_EMBEDDED_LINK_PATH, request_params)
end

# Search for all test users.
#
# @param tenant_ids [Array<String>] Optional list of tenant IDs to filter by
# @param role_names [Array<String>] Optional list of role names to filter by
# @param limit [Integer] Optional limit of the number of users returned. Leave empty for default.
# @param page [Integer] Optional pagination control. Pages start at 0 and must be non-negative.
# @param custom_attributes [Hash] Optional search for an attribute with a given value
# @param statuses [Array<String>] Optional list of statuses to search for ("enabled", "disabled", "invited")
# @param emails [Array<String>] Optional list of emails to search for
# @param phones [Array<String>] Optional list of phones to search for
# @param sso_app_ids [Array<String>] Optional list of SSO application IDs to filter by
# @param text [String] Optional string, allows free text search among all user's attributes.
# @param login_ids [Array<String>] Optional list of login ids
# @param sort [Array<Hash>] Optional array, allows to sort by fields.
#
# @return [Hash] Return hash in the format {"users": []}
#
# The "users" key contains a list of all the found users and their information
#
# @raise [AuthException] Raised if the search operation fails
#
def search_all_test_users(
tenant_ids: [],
role_names: [],
limit: 0,
page: 0,
custom_attributes: {},
statuses: [],
emails: [],
phones: [],
sso_app_ids: [],
sort: [],
text: nil,
login_ids: []
)
tenant_ids ||= []
role_names ||= []

if limit < 0 || page < 0
raise Descope::ArgumentException.new(
'limit or page must be non-negative', code: 400
)
end

body = {
tenantIds: tenant_ids,
roleNames: role_names,
limit:,
page:,
testUsersOnly: true,
withTestUser: true
}

body[:statuses] = statuses unless statuses.nil? || statuses.empty?
body[:emails] = emails unless emails.nil? || emails.empty?
body[:phones] = phones unless phones.nil? || phones.empty?
body[:customAttributes] = custom_attributes unless custom_attributes.nil? || custom_attributes.empty?
body[:ssoAppsIds] = sso_app_ids unless sso_app_ids.nil? || sso_app_ids.empty?
body[:loginIds] = login_ids unless login_ids.nil? || login_ids.empty?
body[:sort] = sort unless sort.nil? || sort.empty?
body[:text] = text unless text.nil? || text.empty?

post(Common::TEST_USERS_SEARCH_PATH, body)
end


private

Expand Down Expand Up @@ -536,6 +601,9 @@ def user_create(
role_names ||= []
user_tenants ||= []
path = Common::USER_CREATE_PATH

path = Common::TEST_USER_CREATE_PATH if test

request_params = user_compose_create_body(
login_id:,
email:,
Expand Down Expand Up @@ -609,11 +677,11 @@ def user_compose_create_body(
sso_app_ids:
)
body[:invite] = invite
body[:verifiedEmail] = verified_email unless verified_email.nil? || !verified_email.empty?
body[:verifiedPhone] = verified_phone unless verified_phone.nil? || !verified_phone.empty?
body[:inviteUrl] = invite_url unless invite_url.nil? || !invite_url.empty?
body[:sendMail] = send_mail unless send_mail.nil? || !send_mail.empty?
body[:sendSMS] = send_sms unless send_sms.nil? || !send_sms.empty?
body[:verifiedEmail] = verified_email unless verified_email.nil? || verified_email.empty?
guyp-descope marked this conversation as resolved.
Show resolved Hide resolved
body[:verifiedPhone] = verified_phone unless verified_phone.nil? || verified_phone.empty?
body[:inviteUrl] = invite_url unless invite_url.nil? || invite_url.empty?
body[:sendMail] = send_mail unless send_mail.nil? || send_mail.empty?
body[:sendSMS] = send_sms unless send_sms.nil? || send_sms.empty?

body
end
Expand Down Expand Up @@ -660,12 +728,16 @@ def user_compose_update_body(
body[:phone] = phone unless phone.nil? || phone.empty?
body[:name] = name unless name.nil? || name.empty?
body[:roleNames] = role_names unless role_names.nil? || role_names.empty?
body[:userTenants] = associated_tenants_to_hash_array(user_tenants) unless user_tenants.nil? || user_tenants.empty?
unless user_tenants.nil? || user_tenants.empty?
body[:userTenants] = associated_tenants_to_hash_array(user_tenants)
end
body[:test] = test unless test.nil?
body[:invite] = invite unless invite.nil?
body[:picture] = picture unless picture.nil? || picture.empty?
body[:customAttributes] = custom_attributes unless custom_attributes.nil? || custom_attributes.empty?
body[:additionalIdentifiers] = additional_identifiers unless additional_identifiers.nil? || additional_identifiers.empty?
unless additional_identifiers.nil? || additional_identifiers.empty?
body[:additionalIdentifiers] = additional_identifiers
end
body[:ssoAppIds] = sso_app_ids unless sso_app_ids.nil? || sso_app_ids.empty?
body[:verifiedEmail] = verified_email unless verified_email.nil? || !verified_email.to_s.empty?
body[:givenName] = given_name unless given_name.nil?
Expand Down
12 changes: 11 additions & 1 deletion spec/integration/lib.descope/api/v1/management/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,22 @@

it 'should create a test user' do
@client.delete_all_test_users
# ensure no roles exist with that name
role_name = 'some-new-role'
all_roles = @client.load_all_roles
all_roles['roles'].each do |role|
@client.delete_role(name: role['name']) if role['name'] == role_name
end
sleep 5

user_args = build(:user)
test_user = @client.create_test_user(**user_args)['user']
test_users = @client.search_all_users(test_users_only: true)['users']
@client.create_role(name: role_name)
@client.user_add_roles(login_id: test_user['loginIds'][0], role_names: [role_name])
test_users = @client.search_all_test_users(role_names: [role_name])['users']
expect(test_users.length).to be >= 1
expect(test_users[0]['loginIds'][0]).to eq(test_user['loginIds'][0])
expect(test_users[0]['roleNames']).to eq([role_name])
end

it 'should update user status' do
Expand Down
122 changes: 80 additions & 42 deletions spec/lib.descope/api/v1/management/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,69 @@
@instance = dummy_instance
end

context '.create_user' do
context '.create_user_and_test_user' do
it 'is expected to respond to a user create method' do
expect(@instance).to respond_to(:create_user)
expect(@instance).to respond_to(:create_test_user)
end

user_tenants_args = [
{
tenant_id: 'tenant1'
},
{
tenant_id: 'tenant2',
role_names: %w[role1 role2]
}
]

params = {
loginId: '[email protected]',
email: '[email protected]',
phone: '+1-212-669-2542',
name: 'name',
givenName: 'name',
familyName: 'Ruby SDK',
userTenants: associated_tenants_to_hash_array(user_tenants_args),
test: false,
picture: 'https://www.example.com/picture.png',
customAttributes: { 'attr1' => 'value1', 'attr2' => 'value2' },
additionalIdentifiers: %w[id-1 id-2],
password: 's3cr3t',
ssoAppIds: %w[app1 app2],
invite: false
}

args = {
login_id: '[email protected]',
email: '[email protected]',
phone: '+1-212-669-2542',
name: 'name',
given_name: 'name',
family_name: 'Ruby SDK',
user_tenants: user_tenants_args,
picture: 'https://www.example.com/picture.png',
custom_attributes: { 'attr1' => 'value1', 'attr2' => 'value2' },
additional_identifiers: %w[id-1 id-2],
password: 's3cr3t',
sso_app_ids: %w[app1 app2]
}

it 'is expected to create a user with user data' do
user_tenants_args = [
{
tenant_id: 'tenant1'
},
{
tenant_id: 'tenant2',
role_names: %w[role1 role2]
}
]
expect(@instance).to receive(:post).with(
USER_CREATE_PATH, {
loginId: '[email protected]',
email: '[email protected]',
phone: '+1-212-669-2542',
name: 'name',
givenName: 'name',
familyName: 'Ruby SDK',
userTenants: associated_tenants_to_hash_array(user_tenants_args),
test: false,
picture: 'https://www.example.com/picture.png',
customAttributes: { 'attr1' => 'value1', 'attr2' => 'value2' },
additionalIdentifiers: %w[id-1 id-2],
password: 's3cr3t',
ssoAppIds: %w[app1 app2],
invite: false
}
)
expect(@instance).to receive(:post).with(USER_CREATE_PATH, params)

expect do
@instance.create_user(
login_id: '[email protected]',
email: '[email protected]',
phone: '+1-212-669-2542',
name: 'name',
given_name: 'name',
family_name: 'Ruby SDK',
user_tenants: user_tenants_args,
picture: 'https://www.example.com/picture.png',
custom_attributes: { 'attr1' => 'value1', 'attr2' => 'value2' },
additional_identifiers: %w[id-1 id-2],
password: 's3cr3t',
sso_app_ids: %w[app1 app2]
)
@instance.create_user(**args)
end.not_to raise_error
end

it 'is expected to create a test user with user data' do
params[:test] = true
expect(@instance).to receive(:post).with(TEST_USER_CREATE_PATH, params)

expect do
args[:test] = true
@instance.create_test_user(**args)
end.not_to raise_error
end
end
Expand Down Expand Up @@ -732,4 +746,28 @@
end.not_to raise_error
end
end

context '.search_all_test_users' do
it 'is expected to respond to a search_all_test_users method' do
expect(@instance).to respond_to(:search_all_test_users)

expect(@instance).to receive(:post).with(
TEST_USERS_SEARCH_PATH, {
tenantIds: %w[t1 t2],
roleNames: %w[r1 r2],
limit: 0,
page: 0,
testUsersOnly: true,
withTestUser: true
}
)

expect do
@instance.search_all_test_users(
tenant_ids: %w[t1 t2],
role_names: %w[r1 r2]
)
end.not_to raise_error
end
end
end