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
23 changes: 23 additions & 0 deletions lib/UserManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,29 @@ public function resendInvitation($invitationId)
return Resource\Invitation::constructFromResponse($response);
}

/**
* Accept an Invitation
*
* @param string $invitationId ID of the Invitation
* @return Resource\Invitation
*
* @throws Exception\WorkOSException
*/
public function acceptInvitation($invitationId)
{
$path = "user_management/invitations/{$invitationId}/accept";

$response = Client::request(
Client::METHOD_POST,
$path,
null,
null,
true
);

return Resource\Invitation::constructFromResponse($response);
}

/**
* Generates an OAuth 2.0 authorization URL used to initiate the SSO flow with WorkOS.
*
Expand Down
44 changes: 44 additions & 0 deletions tests/WorkOS/UserManagementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,50 @@ public function testResendInvitationAccepted()
$this->userManagement->resendInvitation($invitationId);
}

public function testAcceptInvitation()
{
$invitationId = "invitation_01E4ZCR3C56J083X43JQXF3JK5";
$path = "user_management/invitations/{$invitationId}/accept";

$result = $this->invitationResponseFixture();

$this->mockRequest(
Client::METHOD_POST,
$path,
null,
null,
true,
$result
);

$response = $this->userManagement->acceptInvitation($invitationId);

$expected = $this->invitationFixture();

$this->assertSame($response->toArray(), $expected);
}

public function testAcceptInvitation404()
{
$invitationId = "invitation_01E4ZCR3C56J083X43JQXF3JK5";
$path = "user_management/invitations/{$invitationId}/accept";

$this->expectException(Exception\NotFoundException::class);

$this->mockRequest(
Client::METHOD_POST,
$path,
null,
null,
true,
null,
null,
404
);

$this->userManagement->acceptInvitation($invitationId);
}
Comment on lines +1704 to +1723
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing 400-level error test cases

The comparable resendInvitation tests cover several 400 error scenarios (testResendInvitationExpired, testResendInvitationRevoked, testResendInvitationAccepted), but acceptInvitation only tests 404. In practice, the accept endpoint can also return a 400 when the invitation is in an invalid state (e.g. already accepted, revoked, or expired). Consider adding at least one BadRequestException test, mirroring the pattern used for the resend tests:

public function testAcceptInvitationAlreadyAccepted()
{
    $invitationId = "invitation_01E4ZCR3C56J083X43JQXF3JK5";
    $path = "user_management/invitations/{$invitationId}/accept";

    $this->expectException(Exception\BadRequestException::class);

    $this->mockRequest(
        Client::METHOD_POST,
        $path,
        null,
        null,
        true,
        null,
        null,
        400
    );

    $this->userManagement->acceptInvitation($invitationId);
}


public function testGetJwksUrl()
{
$clientId = "12345";
Expand Down