From 7a6065b23754fa883484523d0be79b37087da7a6 Mon Sep 17 00:00:00 2001 From: Nick Nisi Date: Mon, 7 Jul 2025 11:18:38 -0500 Subject: [PATCH 1/4] add compact to body --- lib/workos/user_management.rb | 2 +- spec/lib/workos/user_management_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/workos/user_management.rb b/lib/workos/user_management.rb index d3ae3cd1..15626404 100644 --- a/lib/workos/user_management.rb +++ b/lib/workos/user_management.rb @@ -251,7 +251,7 @@ def update_user( password: password, password_hash: password_hash, password_hash_type: password_hash_type, - }, + }.compact, auth: true, ) diff --git a/spec/lib/workos/user_management_spec.rb b/spec/lib/workos/user_management_spec.rb index cb2324a2..74fed17a 100644 --- a/spec/lib/workos/user_management_spec.rb +++ b/spec/lib/workos/user_management_spec.rb @@ -382,6 +382,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 From 4efb61b834988b031c3b032f8cd1e0360c9b4da3 Mon Sep 17 00:00:00 2001 From: Nick Nisi Date: Mon, 7 Jul 2025 11:23:30 -0500 Subject: [PATCH 2/4] update other methods --- lib/workos/user_management.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/workos/user_management.rb b/lib/workos/user_management.rb index 15626404..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, ) @@ -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, ), ) From f0338b557c3525999d6bc0008b9ddbf24c1fff2a Mon Sep 17 00:00:00 2001 From: Nick Nisi Date: Mon, 7 Jul 2025 11:27:52 -0500 Subject: [PATCH 3/4] update tests --- spec/lib/workos/user_management_spec.rb | 62 +++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/spec/lib/workos/user_management_spec.rb b/spec/lib/workos/user_management_spec.rb index 74fed17a..62de7509 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 @@ -802,6 +822,27 @@ 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 @@ -1468,6 +1509,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 From 13fddab2c95c94b05cf16a040cd1bf2d0a2e0a7b Mon Sep 17 00:00:00 2001 From: Nick Nisi Date: Mon, 7 Jul 2025 11:36:20 -0500 Subject: [PATCH 4/4] formatting --- spec/lib/workos/user_management_spec.rb | 39 +++++++++++++------------ 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/spec/lib/workos/user_management_spec.rb b/spec/lib/workos/user_management_spec.rb index 62de7509..8ba0f023 100644 --- a/spec/lib/workos/user_management_spec.rb +++ b/spec/lib/workos/user_management_spec.rb @@ -344,17 +344,17 @@ 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"}') + double('response', body: '{"id": "test_user", "email": "test@example.com"}'), ) - + described_class.create_user( email: 'test@example.com', - first_name: 'John' + first_name: 'John', ) end @@ -411,18 +411,18 @@ 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}') + double('response', body: '{"id": "test_user", "email_verified": true}'), ) - + described_class.update_user( id: 'user_01H7TVSKS45SDHN5V9XPSM6H44', - email_verified: true + email_verified: true, ) end @@ -829,18 +829,19 @@ 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"}}') + 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' + totp_issuer: 'Test App', ) end end @@ -1517,17 +1518,17 @@ 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"}') + double('response', body: '{"id": "test_invitation"}'), ) described_class.send_invitation( email: 'test@workos.com', - organization_id: 'org_123' + organization_id: 'org_123', ) end end