Skip to content

Commit

Permalink
Managemnet: User Patch
Browse files Browse the repository at this point in the history
  • Loading branch information
ami-descope committed Aug 6, 2024
1 parent 26501c3 commit 3d0b587
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
descope (1.0.5)
descope (1.0.6)
addressable (~> 2.8)
jwt (~> 2.7)
rest-client (~> 2.1)
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,16 @@ descope_client.update_user(
user_tenants: client.associated_tenants_to_hash_array(associated_tenants)
)

# Patch all user attribute in one api call
descope_client.patch_user(
login_id: 'desmond@descope.com',
email: 'desmond@descope.com',
given_name: 'Desmond',
family_name: 'Copeland',
display_name: 'Desmond Copeland',
user_tenants: client.associated_tenants_to_hash_array(associated_tenants)
)

# Update explicit data for a user rather than overriding all fields
descope_client.update_login_id(
login_id: 'desmond@descope.com',
Expand Down
1 change: 1 addition & 0 deletions lib/descope/api/v1/management/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module Common
USER_GENERATE_MAGIC_LINK_FOR_TEST_PATH = '/v1/mgmt/tests/generate/magiclink'
USER_GENERATE_ENCHANTED_LINK_FOR_TEST_PATH = '/v1/mgmt/tests/generate/enchantedlink'
USER_GENERATE_EMBEDDED_LINK_PATH = '/v1/mgmt/user/signin/embeddedlink'
USER_PATCH_PATH = '/v1/mgmt/user/patch'

# access key
ACCESS_KEY_CREATE_PATH = '/v1/mgmt/accesskey/create'
Expand Down
45 changes: 45 additions & 0 deletions lib/descope/api/v1/management/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,51 @@ def update_custom_attribute(login_id: nil, attribute_key: nil, attribute_value:
post(Common::USER_UPDATE_CUSTOM_ATTRIBUTE_PATH, body)
end

def patch_user(
login_id: nil,
email: nil,
phone: nil,
name: nil,
given_name: nil,
middle_name: nil,
family_name: nil,
role_names: [],
user_tenants: [],
picture: nil,
custom_attributes: nil,
verified_email: nil,
verified_phone: nil,
additional_identifiers: [],
password: nil,
hashed_password: {},
sso_app_ids: []
)
validate_login_id(login_id)
role_names ||= []
user_tenants ||= []
path = Common::USER_PATCH_PATH
request_params = user_compose_update_body(
login_id:,
email:,
phone:,
name:,
given_name:,
middle_name:,
family_name:,
role_names:,
user_tenants:,
picture:,
custom_attributes:,
verified_email:,
verified_phone:,
additional_identifiers:,
password:,
hashed_password:,
sso_app_ids:
)
patch(path, request_params)
end

def update_jwt(jwt: nil, custom_claims: nil)
body = {
jwt:,
Expand Down
35 changes: 35 additions & 0 deletions spec/integration/lib.descope/api/v1/management/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,41 @@
expect(updated_user['first_name']).to eq(created_user[updated_first_name])
end

it 'should patch a user' do
user = build(:user)
role_name = 'some-new-role'

# ensure no roles exist with that name
all_roles = @client.load_all_roles
all_roles['roles'].each do |role|
@client.delete_role(name: role['name']) if role['name'] == role_name
end

@client.create_role(name: role_name)
@client.create_user(**user)['user']
updated_first_name = 'new name'
updated_given_name = 'new given name'
update_phone_number = "+1#{Faker::Number.number(digits: 10)}"
updated_role_names = [role_name]
updated_middle_name = 'new middle name'
updated_user = @client.patch_user(
**user,
name: updated_first_name,
given_name: updated_given_name,
phone: update_phone_number,
role_names: updated_role_names,
middle_name: updated_middle_name
)['user']

puts "updated_user #{updated_user}"

expect(updated_user['name']).to eq(updated_first_name)
expect(updated_user['givenName']).to eq(updated_given_name)
expect(updated_user['phone']).to eq(update_phone_number)
expect(updated_user['roleNames']).to eq(updated_role_names)
expect(updated_user['middleName']).to eq(updated_middle_name)
end

it 'should delete a user' do
user = build(:user)
created_user = @client.create_user(**user)['user']
Expand Down
28 changes: 28 additions & 0 deletions spec/lib.descope/api/v1/management/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -704,4 +704,32 @@
end.not_to raise_error
end
end

context '.patch_user' do
it 'is expected to respond to a patch user method' do
expect(@instance).to respond_to(:patch_user)
end

it 'is expected to respond to a user patch method' do
expect(@instance).to receive(:patch).with(
USER_PATCH_PATH, {
loginId: 'name@mail.com',
email: 'name@mail.com',
givenName: 'mister',
name: 'something else',
test: false,
invite: false
}
)

expect do
@instance.patch_user(
login_id: 'name@mail.com',
email: 'name@mail.com',
given_name: 'mister',
name: 'something else'
)
end.not_to raise_error
end
end
end

0 comments on commit 3d0b587

Please sign in to comment.