Skip to content

Commit ffb578e

Browse files
authored
Fix request body construction to only send non-nil values (#384)
1 parent 31a0e79 commit ffb578e

File tree

2 files changed

+93
-6
lines changed

2 files changed

+93
-6
lines changed

lib/workos/user_management.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ def create_user(
206206
email_verified: email_verified,
207207
password_hash: password_hash,
208208
password_hash_type: password_hash_type,
209-
},
209+
}.compact,
210210
auth: true,
211211
)
212212

@@ -251,7 +251,7 @@ def update_user(
251251
password: password,
252252
password_hash: password_hash,
253253
password_hash_type: password_hash_type,
254-
},
254+
}.compact,
255255
auth: true,
256256
)
257257

@@ -643,7 +643,7 @@ def create_magic_auth(email:, invitation_token: nil)
643643
body: {
644644
email: email,
645645
invitation_token: invitation_token,
646-
},
646+
}.compact,
647647
auth: true,
648648
),
649649
)
@@ -697,7 +697,7 @@ def enroll_auth_factor(user_id:, type:, totp_issuer: nil, totp_user: nil, totp_s
697697
totp_issuer: totp_issuer,
698698
totp_user: totp_user,
699699
totp_secret: totp_secret,
700-
},
700+
}.compact,
701701
auth: true,
702702
),
703703
)
@@ -928,7 +928,7 @@ def create_organization_membership(user_id:, organization_id:, role_slug: nil)
928928
user_id: user_id,
929929
organization_id: organization_id,
930930
role_slug: role_slug,
931-
},
931+
}.compact,
932932
auth: true,
933933
)
934934

@@ -1093,7 +1093,7 @@ def send_invitation(email:, organization_id: nil, expires_in_days: nil, inviter_
10931093
expires_in_days: expires_in_days,
10941094
inviter_user_id: inviter_user_id,
10951095
role_slug: role_slug,
1096-
},
1096+
}.compact,
10971097
auth: true,
10981098
),
10991099
)

spec/lib/workos/user_management_spec.rb

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,26 @@
338338
end
339339
end
340340

341+
it 'only sends non-nil values in request body' do
342+
expect(described_class).to receive(:post_request) do |options|
343+
body = options[:body]
344+
expect(body).to eq({ email: '[email protected]', first_name: 'John' })
345+
expect(body).not_to have_key(:last_name)
346+
expect(body).not_to have_key(:email_verified)
347+
348+
double('request')
349+
end.and_return(double('request'))
350+
351+
expect(described_class).to receive(:execute_request).and_return(
352+
double('response', body: '{"id": "test_user", "email": "[email protected]"}'),
353+
)
354+
355+
described_class.create_user(
356+
357+
first_name: 'John',
358+
)
359+
end
360+
341361
context 'with an invalid payload' do
342362
it 'returns an error' do
343363
VCR.use_cassette 'user_management/create_user_invalid' do
@@ -382,6 +402,30 @@
382402
end
383403
end
384404

405+
it 'only sends non-nil values in request body' do
406+
# Mock the request to inspect what's being sent
407+
expect(described_class).to receive(:put_request) do |options|
408+
# Verify that the body only contains non-nil values
409+
body = options[:body]
410+
expect(body).to eq({ email_verified: true })
411+
expect(body).not_to have_key(:first_name)
412+
expect(body).not_to have_key(:last_name)
413+
expect(body).not_to have_key(:email)
414+
415+
# Return a mock request object
416+
double('request')
417+
end.and_return(double('request'))
418+
419+
expect(described_class).to receive(:execute_request).and_return(
420+
double('response', body: '{"id": "test_user", "email_verified": true}'),
421+
)
422+
423+
described_class.update_user(
424+
id: 'user_01H7TVSKS45SDHN5V9XPSM6H44',
425+
email_verified: true,
426+
)
427+
end
428+
385429
context 'with an invalid payload' do
386430
it 'returns an error' do
387431
VCR.use_cassette 'user_management/update_user/invalid' do
@@ -778,6 +822,28 @@
778822
expect(authentication_response.authentication_challenge.id).to eq('auth_challenge_01H96FETXGTW1QMBSBT2T36PW0')
779823
end
780824
end
825+
826+
it 'only sends non-nil values in request body' do
827+
expect(described_class).to receive(:post_request) do |options|
828+
body = options[:body]
829+
expect(body).to eq({ type: 'totp', totp_issuer: 'Test App' })
830+
expect(body).not_to have_key(:totp_user)
831+
expect(body).not_to have_key(:totp_secret)
832+
833+
double('request')
834+
end.and_return(double('request'))
835+
836+
expect(described_class).to receive(:execute_request).and_return(
837+
double('response',
838+
body: '{"authentication_factor": {"id": "test"}, "authentication_challenge": {"id": "test"}}',),
839+
)
840+
841+
described_class.enroll_auth_factor(
842+
user_id: 'user_123',
843+
type: 'totp',
844+
totp_issuer: 'Test App',
845+
)
846+
end
781847
end
782848

783849
context 'with an incorrect user id' do
@@ -1444,6 +1510,27 @@
14441510
expect(invitation.email).to eq('[email protected]')
14451511
end
14461512
end
1513+
1514+
it 'only sends non-nil values in request body' do
1515+
expect(described_class).to receive(:post_request) do |options|
1516+
body = options[:body]
1517+
expect(body).to eq({ email: '[email protected]', organization_id: 'org_123' })
1518+
expect(body).not_to have_key(:expires_in_days)
1519+
expect(body).not_to have_key(:inviter_user_id)
1520+
expect(body).not_to have_key(:role_slug)
1521+
1522+
double('request')
1523+
end.and_return(double('request'))
1524+
1525+
expect(described_class).to receive(:execute_request).and_return(
1526+
double('response', body: '{"id": "test_invitation"}'),
1527+
)
1528+
1529+
described_class.send_invitation(
1530+
1531+
organization_id: 'org_123',
1532+
)
1533+
end
14471534
end
14481535

14491536
context 'with an invalid payload' do

0 commit comments

Comments
 (0)