diff --git a/lib/workos/user_management.rb b/lib/workos/user_management.rb index d3ae3cd1..09ab6c63 100644 --- a/lib/workos/user_management.rb +++ b/lib/workos/user_management.rb @@ -206,7 +206,7 @@ def create_user( email_verified: email_verified, password_hash: password_hash, password_hash_type: password_hash_type, - }, + }.compact, auth: true, ) @@ -251,7 +251,7 @@ def update_user( password: password, password_hash: password_hash, password_hash_type: password_hash_type, - }, + }.compact, auth: true, ) @@ -643,7 +643,7 @@ def create_magic_auth(email:, invitation_token: nil) body: { email: email, invitation_token: invitation_token, - }, + }.compact, auth: true, ), ) @@ -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, ), ) @@ -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, ) @@ -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, ), ) diff --git a/spec/lib/workos/user_management_spec.rb b/spec/lib/workos/user_management_spec.rb index cb2324a2..8ba0f023 100644 --- a/spec/lib/workos/user_management_spec.rb +++ b/spec/lib/workos/user_management_spec.rb @@ -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: 'test@example.com', 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": "test@example.com"}'), + ) + + described_class.create_user( + email: 'test@example.com', + first_name: 'John', + ) + end + context 'with an invalid payload' do it 'returns an error' do VCR.use_cassette 'user_management/create_user_invalid' do @@ -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 @@ -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 @@ -1444,6 +1510,27 @@ expect(invitation.email).to eq('test@workos.com') 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: 'test@workos.com', 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: 'test@workos.com', + organization_id: 'org_123', + ) + end end context 'with an invalid payload' do