Skip to Content
Octopus Energy Packages

OAuth redirects

Run an OAuth sign-in through your application’s start and callback routes

Test the application’s OAuth start and callback routes instead of seeding session cookies. A local authorization service handles the redirect; the fixture supplies the token-exchange response.

Before you start

Complete the Blueprint Auth tutorial. Your app must have OAuth start and callback handlers, with unauthenticated dashboard visits redirected to the start handler.

This example uses /api/auth/oauth/callback and the tutorial’s viewer query. Adjust the callback path to your application. For refresh without a redirect, use the user-session guide.

Configure the local auth endpoint

Export a fixed port above testAuthConfig in lib/playwright/auth-config.ts:

lib/playwright/auth-config.ts (addition)
export const authServerPort = 4100;

Replace testAuthConfig.krakenConfig.authEndpoint with this local endpoint:

lib/playwright/auth-config.ts (authEndpoint replacement)
authEndpoint: `http://127.0.0.1:${authServerPort}/`,

Keep the tutorial’s webServer.env mappings. They configure the app’s auth endpoint and trusted issuer from the updated value.

Start the authorization service

Replace your global setup with composed signing-key and authorization-service setup. Return close so Playwright stops the service after the run:

lib/playwright/auth-setup.ts
import { startMockAuthorizationServer } from "@krakentech/blueprint-auth/testing";
import { composeGlobalSetup } from "@octopus-energy/playwright/setup";
import { createSigningKeysSetup } from "@octopus-energy/playwright/blueprint-auth";
import { authServerPort } from "./auth-config";

export default composeGlobalSetup(createSigningKeysSetup(), async () => {
  const { close } = await startMockAuthorizationServer({
    port: authServerPort,
  });
  return close;
});

Exercise the callback

Register the exchange before opening the protected page. Match redirectUri exactly to the application’s callback URL, including any trailing slash:

tests/playwright/oauth.spec.ts
import { HttpResponse } from "msw";
import { expect, test } from "../../lib/playwright/test";

test("signs in through the OAuth callback", async ({ page, baseURL, msw, blueprintAuth }) => {
  await blueprintAuth.mock.oauth.login({
    redirectUri: new URL("/api/auth/oauth/callback", baseURL).href,
    session: { subject: "alice" },
  });
  msw.use(
    msw.graphql
      .link("https://api.kraken.test/v1/graphql/")
      .query("viewer", () => HttpResponse.json({ data: { viewer: { preferredName: "Alice" } } })),
  );

  await page.goto("/dashboard");
  await expect(page).toHaveURL("/dashboard");
  await expect(page.getByRole("heading", { name: "Alice", exact: true })).toBeVisible();
});

The app redirects to the local service, processes the callback, and sets the session cookies. The test verifies that this flow reaches the authenticated dashboard.

Test a rejected exchange

In a separate test, replace the successful exchange registration with an error outcome:

await blueprintAuth.mock.oauth.login({
  redirectUri: new URL("/api/auth/oauth/callback", baseURL).href,
  errorCode: "invalid_grant",
});

Open the protected page as above, then assert your app’s callback error page instead of the dashboard. Keep the authorization service running so the test still exercises the redirect and callback.

FAQ

On this page