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
16 changes: 16 additions & 0 deletions lib/workos/user_management.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1172,6 +1172,22 @@ def send_invitation(email:, organization_id: nil, expires_in_days: nil, inviter_
WorkOS::Invitation.new(response.body)
end

# Accepts an existing Invitation.
#
# @param [String] id The unique ID of the Invitation.
#
# @return WorkOS::Invitation
def accept_invitation(id:)
request = post_request(
path: "/user_management/invitations/#{id}/accept",
auth: true,
)

response = execute_request(request: request)

WorkOS::Invitation.new(response.body)
end

# Revokes an existing Invitation.
#
# @param [String] id The unique ID of the Invitation.
Expand Down
75 changes: 75 additions & 0 deletions spec/lib/workos/user_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1728,6 +1728,81 @@
end
end

describe '.accept_invitation' do
context 'with a valid id' do
it 'accepts invitation' do
expect(described_class).to receive(:post_request) do |options|
expect(options[:path]).to eq('/user_management/invitations/invitation_123/accept')
expect(options[:auth]).to be true

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

response_body = {
id: 'invitation_123',
email: 'test@workos.com',
state: 'accepted',
}.to_json

expect(described_class).to receive(:execute_request).and_return(
double('response', body: response_body),
)

invitation = described_class.accept_invitation(
id: 'invitation_123',
)

expect(invitation.id).to eq('invitation_123')
expect(invitation.email).to eq('test@workos.com')
expect(invitation.state).to eq('accepted')
end
end

context 'with an invalid id' do
it 'returns an error' do
expect(described_class).to receive(:post_request) do |options|
expect(options[:path]).to eq('/user_management/invitations/invalid_id/accept')
expect(options[:auth]).to be true

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

expect(described_class).to receive(:execute_request).and_raise(
WorkOS::NotFoundError.new(message: 'Invitation not found'),
)

expect do
described_class.accept_invitation(id: 'invalid_id')
end.to raise_error(
WorkOS::NotFoundError,
/Invitation not found/,
)
end
end

context 'when invitation has already been accepted' do
it 'returns an error' do
expect(described_class).to receive(:post_request) do |options|
expect(options[:path]).to eq('/user_management/invitations/invitation_123/accept')
expect(options[:auth]).to be true

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

expect(described_class).to receive(:execute_request).and_raise(
WorkOS::InvalidRequestError.new(message: 'Invite has already been accepted'),
)

expect do
described_class.accept_invitation(id: 'invitation_123')
end.to raise_error(
WorkOS::InvalidRequestError,
/Invite has already been accepted/,
)
end
end
end
Comment on lines +1731 to +1804
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike the comparable revoke_invitation and resend_invitation specs, there is no test context for failure scenarios (e.g. invitation not found, invitation already accepted). Both of those methods include error-case contexts ('with an invalid payload' for revoke_invitation at line 1776, and 'with an invalid id' / 'when invitation has expired' / etc. for resend_invitation starting at line 1806) that exercise the error path via VCR cassette.

Even if the VCR cassette is blocked for now, a context 'with an invalid id' block using the same mock-based approach would be straightforward to add and improve coverage consistency.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Comment on lines +1759 to +1804
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
end
end
end
context 'with an invalid id' do
it 'returns an error' do
expect(described_class).to receive(:post_request).and_return(double('request'))
expect(described_class).to receive(:execute_request).and_raise(
WorkOS::NotFoundError.new(message: 'Invitation not found'),
)
expect do
described_class.accept_invitation(id: 'invalid_id')
end.to raise_error(
WorkOS::NotFoundError,
/Invitation not found/,
)
end
end
context 'when invitation has already been accepted' do
it 'returns an error' do
expect(described_class).to receive(:post_request).and_return(double('request'))
expect(described_class).to receive(:execute_request).and_raise(
WorkOS::InvalidRequestError.new(message: 'Invite has already been accepted'),
)
expect do
described_class.accept_invitation(id: 'invitation_123')
end.to raise_error(
WorkOS::InvalidRequestError,
/Invite has already been accepted/,
)
end
end
end


describe '.revoke_invitation' do
context 'with valid payload' do
it 'revokes invitation' do
Expand Down