Skip to content
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
12 changes: 6 additions & 6 deletions lib/workos/user_management.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def create_user(
email_verified: email_verified,
password_hash: password_hash,
password_hash_type: password_hash_type,
},
}.compact,
auth: true,
)

Expand Down Expand Up @@ -251,7 +251,7 @@ def update_user(
password: password,
password_hash: password_hash,
password_hash_type: password_hash_type,
},
}.compact,
auth: true,
)

Expand Down Expand Up @@ -643,7 +643,7 @@ def create_magic_auth(email:, invitation_token: nil)
body: {
email: email,
invitation_token: invitation_token,
},
}.compact,
auth: true,
),
)
Expand Down Expand Up @@ -697,7 +697,7 @@ def enroll_auth_factor(user_id:, type:, totp_issuer: nil, totp_user: nil, totp_s
totp_issuer: totp_issuer,
totp_user: totp_user,
totp_secret: totp_secret,
},
}.compact,
auth: true,
),
)
Expand Down Expand Up @@ -928,7 +928,7 @@ def create_organization_membership(user_id:, organization_id:, role_slug: nil)
user_id: user_id,
organization_id: organization_id,
role_slug: role_slug,
},
}.compact,
auth: true,
)

Expand Down Expand Up @@ -1093,7 +1093,7 @@ def send_invitation(email:, organization_id: nil, expires_in_days: nil, inviter_
expires_in_days: expires_in_days,
inviter_user_id: inviter_user_id,
role_slug: role_slug,
},
}.compact,
auth: true,
),
)
Expand Down
87 changes: 87 additions & 0 deletions spec/lib/workos/user_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,26 @@
end
end

it 'only sends non-nil values in request body' do
expect(described_class).to receive(:post_request) do |options|
body = options[:body]
expect(body).to eq({ email: '[email protected]', first_name: 'John' })
expect(body).not_to have_key(:last_name)
expect(body).not_to have_key(:email_verified)

double('request')
end.and_return(double('request'))

expect(described_class).to receive(:execute_request).and_return(
double('response', body: '{"id": "test_user", "email": "[email protected]"}'),
)

described_class.create_user(
email: '[email protected]',
first_name: 'John',
)
end

context 'with an invalid payload' do
it 'returns an error' do
VCR.use_cassette 'user_management/create_user_invalid' do
Expand Down Expand Up @@ -382,6 +402,30 @@
end
end

it 'only sends non-nil values in request body' do
# Mock the request to inspect what's being sent
expect(described_class).to receive(:put_request) do |options|
# Verify that the body only contains non-nil values
body = options[:body]
expect(body).to eq({ email_verified: true })
expect(body).not_to have_key(:first_name)
expect(body).not_to have_key(:last_name)
expect(body).not_to have_key(:email)

# Return a mock request object
double('request')
end.and_return(double('request'))

expect(described_class).to receive(:execute_request).and_return(
double('response', body: '{"id": "test_user", "email_verified": true}'),
)

described_class.update_user(
id: 'user_01H7TVSKS45SDHN5V9XPSM6H44',
email_verified: true,
)
end

context 'with an invalid payload' do
it 'returns an error' do
VCR.use_cassette 'user_management/update_user/invalid' do
Expand Down Expand Up @@ -778,6 +822,28 @@
expect(authentication_response.authentication_challenge.id).to eq('auth_challenge_01H96FETXGTW1QMBSBT2T36PW0')
end
end

it 'only sends non-nil values in request body' do
expect(described_class).to receive(:post_request) do |options|
body = options[:body]
expect(body).to eq({ type: 'totp', totp_issuer: 'Test App' })
expect(body).not_to have_key(:totp_user)
expect(body).not_to have_key(:totp_secret)

double('request')
end.and_return(double('request'))

expect(described_class).to receive(:execute_request).and_return(
double('response',
body: '{"authentication_factor": {"id": "test"}, "authentication_challenge": {"id": "test"}}',),
)

described_class.enroll_auth_factor(
user_id: 'user_123',
type: 'totp',
totp_issuer: 'Test App',
)
end
end

context 'with an incorrect user id' do
Expand Down Expand Up @@ -1444,6 +1510,27 @@
expect(invitation.email).to eq('[email protected]')
end
end

it 'only sends non-nil values in request body' do
expect(described_class).to receive(:post_request) do |options|
body = options[:body]
expect(body).to eq({ email: '[email protected]', organization_id: 'org_123' })
expect(body).not_to have_key(:expires_in_days)
expect(body).not_to have_key(:inviter_user_id)
expect(body).not_to have_key(:role_slug)

double('request')
end.and_return(double('request'))

expect(described_class).to receive(:execute_request).and_return(
double('response', body: '{"id": "test_invitation"}'),
)

described_class.send_invitation(
email: '[email protected]',
organization_id: 'org_123',
)
end
end

context 'with an invalid payload' do
Expand Down