Skip to content

Commit b5b4da1

Browse files
feat(user-management): add invitation accept endpoint (#448)
1 parent 914d824 commit b5b4da1

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

lib/workos/user_management.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,22 @@ def send_invitation(email:, organization_id: nil, expires_in_days: nil, inviter_
11721172
WorkOS::Invitation.new(response.body)
11731173
end
11741174

1175+
# Accepts an existing Invitation.
1176+
#
1177+
# @param [String] id The unique ID of the Invitation.
1178+
#
1179+
# @return WorkOS::Invitation
1180+
def accept_invitation(id:)
1181+
request = post_request(
1182+
path: "/user_management/invitations/#{id}/accept",
1183+
auth: true,
1184+
)
1185+
1186+
response = execute_request(request: request)
1187+
1188+
WorkOS::Invitation.new(response.body)
1189+
end
1190+
11751191
# Revokes an existing Invitation.
11761192
#
11771193
# @param [String] id The unique ID of the Invitation.

spec/lib/workos/user_management_spec.rb

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,6 +1728,81 @@
17281728
end
17291729
end
17301730

1731+
describe '.accept_invitation' do
1732+
context 'with a valid id' do
1733+
it 'accepts invitation' do
1734+
expect(described_class).to receive(:post_request) do |options|
1735+
expect(options[:path]).to eq('/user_management/invitations/invitation_123/accept')
1736+
expect(options[:auth]).to be true
1737+
1738+
double('request')
1739+
end.and_return(double('request'))
1740+
1741+
response_body = {
1742+
id: 'invitation_123',
1743+
email: 'test@workos.com',
1744+
state: 'accepted',
1745+
}.to_json
1746+
1747+
expect(described_class).to receive(:execute_request).and_return(
1748+
double('response', body: response_body),
1749+
)
1750+
1751+
invitation = described_class.accept_invitation(
1752+
id: 'invitation_123',
1753+
)
1754+
1755+
expect(invitation.id).to eq('invitation_123')
1756+
expect(invitation.email).to eq('test@workos.com')
1757+
expect(invitation.state).to eq('accepted')
1758+
end
1759+
end
1760+
1761+
context 'with an invalid id' do
1762+
it 'returns an error' do
1763+
expect(described_class).to receive(:post_request) do |options|
1764+
expect(options[:path]).to eq('/user_management/invitations/invalid_id/accept')
1765+
expect(options[:auth]).to be true
1766+
1767+
double('request')
1768+
end.and_return(double('request'))
1769+
1770+
expect(described_class).to receive(:execute_request).and_raise(
1771+
WorkOS::NotFoundError.new(message: 'Invitation not found'),
1772+
)
1773+
1774+
expect do
1775+
described_class.accept_invitation(id: 'invalid_id')
1776+
end.to raise_error(
1777+
WorkOS::NotFoundError,
1778+
/Invitation not found/,
1779+
)
1780+
end
1781+
end
1782+
1783+
context 'when invitation has already been accepted' do
1784+
it 'returns an error' do
1785+
expect(described_class).to receive(:post_request) do |options|
1786+
expect(options[:path]).to eq('/user_management/invitations/invitation_123/accept')
1787+
expect(options[:auth]).to be true
1788+
1789+
double('request')
1790+
end.and_return(double('request'))
1791+
1792+
expect(described_class).to receive(:execute_request).and_raise(
1793+
WorkOS::InvalidRequestError.new(message: 'Invite has already been accepted'),
1794+
)
1795+
1796+
expect do
1797+
described_class.accept_invitation(id: 'invitation_123')
1798+
end.to raise_error(
1799+
WorkOS::InvalidRequestError,
1800+
/Invite has already been accepted/,
1801+
)
1802+
end
1803+
end
1804+
end
1805+
17311806
describe '.revoke_invitation' do
17321807
context 'with valid payload' do
17331808
it 'revokes invitation' do

0 commit comments

Comments
 (0)