(email: string)
| 206 | // consent page (pick the account's form) -> callback back into the app. |
| 207 | // Returns the callback response plus the cookies it set. |
| 208 | const signInThroughIdp = async (email: string) => { |
| 209 | const start = await handler( |
| 210 | new Request(`${BASE}/api/auth/sign-in/oauth2`, { |
| 211 | method: "POST", |
| 212 | headers: { "content-type": "application/json" }, |
| 213 | body: JSON.stringify({ providerId: "okta", callbackURL: "/" }), |
| 214 | }), |
| 215 | ); |
| 216 | expect(start.status).toBe(200); |
| 217 | const { url } = (await start.json()) as { url: string }; |
| 218 | expect(url.startsWith(idp.url)).toBe(true); |
| 219 | const stateCookies = start.headers |
| 220 | .getSetCookie() |
| 221 | .map((cookie) => cookie.split(";")[0]!) |
| 222 | .join("; "); |
| 223 | |
| 224 | const consentHtml = await fetch(url).then((r) => r.text()); |
| 225 | const form = [...consentHtml.matchAll(/<form[\s\S]*?<\/form>/gi)] |
| 226 | .map((m) => m[0]) |
| 227 | .find((f) => f.includes(email)); |
| 228 | expect(form).toBeDefined(); |
| 229 | const action = decodeHtml(form!.match(/\baction=["']([^"']+)["']/i)![1]!); |
| 230 | const consent = await fetch(action, { |
| 231 | method: "POST", |
| 232 | headers: { "content-type": "application/x-www-form-urlencoded" }, |
| 233 | body: new URLSearchParams(formFields(form!)), |
| 234 | redirect: "manual", |
| 235 | }); |
| 236 | expect(consent.status).toBe(302); |
| 237 | const callbackUrl = consent.headers.get("location")!; |
| 238 | expect(callbackUrl.startsWith(CALLBACK)).toBe(true); |
| 239 | |
| 240 | const callback = await handler(new Request(callbackUrl, { headers: { cookie: stateCookies } })); |
| 241 | const cookies = callback.headers |
| 242 | .getSetCookie() |
| 243 | .map((cookie) => cookie.split(";")[0]!) |
| 244 | .join("; "); |
| 245 | return { callback, cookies }; |
| 246 | }; |
| 247 | |
| 248 | test("an allowlisted-domain IdP account signs in and joins the org as a member", async () => { |
| 249 | const { callback, cookies } = await signInThroughIdp("alice@example.com"); |
no test coverage detected