Skip to content

Commit b37dad7

Browse files
authored
Fix incomplete Portal intent documentation and add validation (#310)
Update PHPDoc to include missing intent values (certificate_renewal, domain_verification) and add runtime validation for $intent parameter to provide better error messages before API calls. Includes test coverage for all intent values and invalid intent handling. Fixes #295
1 parent 2714471 commit b37dad7

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

lib/Portal.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,33 @@ class Portal
1313
* Generate a Portal Link scoped to an Organization.
1414
*
1515
* @param string $organization An Organization identifier.
16-
* @param string $intent The intent of the Admin Portal. Possible values are ["audit_logs", "dsync", "log_streams", "sso",].
16+
* @param string $intent The intent of the Admin Portal. Possible values are ["audit_logs", "certificate_renewal", "domain_verification", "dsync", "log_streams", "sso"].
1717
* @param null|string $returnUrl The URL to which WorkOS should send users when they click on
1818
* the link to return to your website. (Optional).
1919
* @param null|string $successUrl The URL to which WorkOS will redirect users to
2020
* upon successfully setting up Single Sign On or Directory Sync. (Optional).
2121
*
22+
* @throws Exception\UnexpectedValueException
2223
* @throws Exception\WorkOSException
2324
*
2425
* @return Resource\PortalLink
2526
*/
2627
public function generateLink($organization, $intent, ?string $returnUrl = null, ?string $successUrl = null)
2728
{
29+
$validIntents = [
30+
'audit_logs',
31+
'certificate_renewal',
32+
'domain_verification',
33+
'dsync',
34+
'log_streams',
35+
'sso'
36+
];
37+
38+
if (!in_array($intent, $validIntents)) {
39+
$msg = "Invalid intent. Valid values are: " . implode(", ", $validIntents);
40+
throw new Exception\UnexpectedValueException($msg);
41+
}
42+
2843
$generateLinkPath = "portal/generate_link";
2944
$params = [
3045
"organization" => $organization,

tests/WorkOS/PortalTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,70 @@ public function testGenerateLinkLogStreams()
135135
$this->assertSame($expectation, $response->link);
136136
}
137137

138+
public function testGenerateLinkCertificateRenewal()
139+
{
140+
$generateLinkPath = "portal/generate_link";
141+
142+
$result = $this->generatePortalLinkFixture();
143+
144+
$params = [
145+
"organization" => "org_01EHZNVPK3SFK441A1RGBFSHRT",
146+
"intent" => "certificate_renewal",
147+
"return_url" => null,
148+
"success_url" => null
149+
];
150+
151+
$this->mockRequest(
152+
Client::METHOD_POST,
153+
$generateLinkPath,
154+
null,
155+
$params,
156+
true,
157+
$result
158+
);
159+
160+
$expectation = "https://id.workos.com/portal/launch?secret=secret";
161+
162+
$response = $this->ap->generateLink("org_01EHZNVPK3SFK441A1RGBFSHRT", "certificate_renewal");
163+
$this->assertSame($expectation, $response->link);
164+
}
165+
166+
public function testGenerateLinkDomainVerification()
167+
{
168+
$generateLinkPath = "portal/generate_link";
169+
170+
$result = $this->generatePortalLinkFixture();
171+
172+
$params = [
173+
"organization" => "org_01EHZNVPK3SFK441A1RGBFSHRT",
174+
"intent" => "domain_verification",
175+
"return_url" => null,
176+
"success_url" => null
177+
];
178+
179+
$this->mockRequest(
180+
Client::METHOD_POST,
181+
$generateLinkPath,
182+
null,
183+
$params,
184+
true,
185+
$result
186+
);
187+
188+
$expectation = "https://id.workos.com/portal/launch?secret=secret";
189+
190+
$response = $this->ap->generateLink("org_01EHZNVPK3SFK441A1RGBFSHRT", "domain_verification");
191+
$this->assertSame($expectation, $response->link);
192+
}
193+
194+
public function testGenerateLinkWithInvalidIntent()
195+
{
196+
$this->expectException(Exception\UnexpectedValueException::class);
197+
$this->expectExceptionMessage("Invalid intent. Valid values are:");
198+
199+
$this->ap->generateLink("org_01EHZNVPK3SFK441A1RGBFSHRT", "invalid_intent");
200+
}
201+
138202
// Fixtures
139203

140204
private function generatePortalLinkFixture()

0 commit comments

Comments
 (0)