Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

8.7 KB
Raw Blame
// Sign in, sign out and the signed-out redirects. Every test starts without the
// shared auth state and signs in by itself, so the shared session is never touched.

import {
  apiUrl,
  FRESH_CONTEXT,
  gotoHydrated,
  hasHorizontalOverflow,
  SHOTS_DIR,
  SIGN_IN_PATH,
  submitSignIn,
  toast,
  waitForHydration,
  watchIssues,
} from './auth-helpers';
import { expect, test } from './fixtures';

test.use({ storageState: FRESH_CONTEXT });
// The dev server hydrates slowly when several suites share it.
test.describe.configure({ timeout: 120_000 });

const MOBILE_VIEWPORT = { width: 390, height: 844 };
const SESSION_COOKIE = 'openpanel.session_token';
const DEV_SIGN_IN_REQUEST = '/trpc/auth.signInDev';

test('signed-out visitors are sent to /login from every protected route', async ({
  page,
  seed,
}) => {
  const issues = watchIssues(page);
  const protectedPaths = [
    '/',
    '/account',
    `/${seed.organizationId}`,
    `/${seed.organizationId}/account`,
    `/${seed.organizationId}/account/two-factor`,
    `/${seed.organizationId}/${seed.projects[0]?.id}/events?foo=1`,
  ];
  for (const path of protectedPaths) {
    await page.goto(path);
    await expect(page, `from ${path}`).toHaveURL(/\/login$/);
  }
  await expect(
    page.getByRole('heading', { name: 'Welcome back' })
  ).toBeVisible();

  // The onboarding steps send a signed-out visitor to the sign-up page instead.
  await page.goto('/onboarding/project');
  await expect(page).toHaveURL(/\/onboarding$/);
  await page.goto(`/onboarding/${seed.projects[0]?.id}/connect`);
  await expect(page).toHaveURL(/\/onboarding$/);
  await page.goto(`/onboarding/${seed.projects[0]?.id}/verify`);
  await expect(page).toHaveURL(/\/onboarding$/);

  expect(issues.unexpected()).toEqual([]);
});

test('the login form validates before it calls the API', async ({ page }) => {
  const issues = watchIssues(page);
  let signInRequests = 0;
  page.on('request', (request) => {
    if (request.url().includes(SIGN_IN_PATH)) {
      signInRequests += 1;
    }
  });
  await gotoHydrated(page, '/login');

  const fieldErrors = page.locator('[data-slot="field-error"]');
  await page.getByRole('button', { name: 'Sign in', exact: true }).click();
  await expect(fieldErrors).toHaveCount(2);
  await expect(fieldErrors.first()).toHaveText('Invalid email address');

  await page.getByLabel('Email').fill('not-an-email');
  await page.getByLabel('Password', { exact: true }).fill('short');
  await page.getByLabel('Password', { exact: true }).press('Enter');
  await expect(fieldErrors).toHaveCount(2);

  await page.getByLabel('Email').fill('[email protected]');
  await page
    .getByLabel('Password', { exact: true })
    .fill('long-enough-password');
  await page.getByLabel('Email').press('Tab');
  await expect(fieldErrors).toHaveCount(0);

  await expect(
    page.getByRole('link', { name: 'Create an account' })
  ).toHaveAttribute('href', '/onboarding');
  expect(signInRequests).toBe(0);
  await expect(page).toHaveURL(/\/login$/);
  expect(issues.unexpected()).toEqual([]);
});

test('a wrong password and an unknown email are both refused', async ({
  page,
  seed,
}) => {
  const issues = watchIssues(page);
  await gotoHydrated(page, '/login');

  const wrongPassword = await submitSignIn(page, {
    email: seed.user.email,
    password: 'definitely-not-the-password',
  });
  // better-auth's 401 goes to the form, not through the tRPC "signed out" handler.
  expect(wrongPassword.status()).toBe(401);
  await expect(toast(page, 'Invalid email or password')).toBeVisible();
  await expect(page).toHaveURL(/\/login$/);
  await page.screenshot({ path: `${SHOTS_DIR}/login-wrong-password.png` });

  const unknownEmail = await submitSignIn(page, {
    email: `nobody-${Date.now()}@example.com`,
    password: 'definitely-not-the-password',
  });
  expect(unknownEmail.status()).toBeGreaterThanOrEqual(400);
  await expect(page).toHaveURL(/\/login$/);
  expect(
    (await page.context().cookies()).map((cookie) => cookie.name)
  ).not.toContain(SESSION_COOKIE);
  expect(issues.unexpected([/api\/auth\/sign-in/])).toEqual([]);
});

test('sign in, land on the organization, then log out from the profile menu', async ({
  page,
  context,
  seed,
  request,
}) => {
  const issues = watchIssues(page);
  await gotoHydrated(page, '/login');
  await expect(page.getByText('Used last time')).toHaveCount(0);

  const response = await submitSignIn(page, seed.user);
  expect(response.status()).toBe(200);
  // One organization: `/` forwards straight to it.
  await expect(page).toHaveURL(new RegExp(`/${seed.organizationId}$`), {
    timeout: 20_000,
  });

  const cookies = await context.cookies();
  const session = cookies.find((cookie) => cookie.name === SESSION_COOKIE);
  expect(session).toMatchObject({
    httpOnly: true,
    secure: true,
    sameSite: 'Lax',
    path: '/',
  });
  const THIRTY_DAYS_IN_SECONDS = 60 * 60 * 24 * 30;
  expect(session?.expires).toBeGreaterThan(
    Date.now() / 1000 + THIRTY_DAYS_IN_SECONDS - 120
  );
  expect(
    cookies.find((cookie) => cookie.name === 'last-auth-provider')?.value
  ).toBe('email');

  // The pages for signed-out visitors forward a signed-in one to the app.
  for (const path of ['/login', '/onboarding', '/reset-password?token=x']) {
    await page.goto(path);
    await expect(page, `from ${path}`).toHaveURL(
      new RegExp(`/${seed.organizationId}$`)
    );
  }

  await waitForHydration(page);
  await page.getByRole('button', { name: 'Profile' }).click();
  await expect(page.getByRole('menuitem', { name: 'Account' })).toBeVisible();
  await page.getByRole('menuitem', { name: 'Logout' }).click();
  await expect(page).toHaveURL(/\/login$/);

  // The provider hint survives the logout, the session does not.
  await expect(page.getByText('Used last time')).toBeVisible();
  await page.screenshot({ path: `${SHOTS_DIR}/login-used-last-time.png` });
  const afterLogout = (await context.cookies()).map((cookie) => cookie.name);
  expect(afterLogout).not.toContain(SESSION_COOKIE);
  await page.goto(`/${seed.organizationId}`);
  await expect(page).toHaveURL(/\/login$/);

  // The server forgot the session too: replaying the old cookie is anonymous.
  const replay = await request.get(`${apiUrl()}/trpc/auth.session`, {
    headers: { cookie: `${SESSION_COOKIE}=${session?.value}` },
  });
  expect((await replay.json()).result.data.json.userId).toBeNull();
  expect(issues.unexpected()).toEqual([]);
});

test('the login and sign-up pages fit a phone screen', async ({ page }) => {
  await page.setViewportSize(MOBILE_VIEWPORT);
  for (const path of ['/login', '/onboarding']) {
    await gotoHydrated(page, path);
    expect(await hasHorizontalOverflow(page), path).toBe(false);
    await page.screenshot({
      path: `${SHOTS_DIR}/mobile${path.replace('/', '-')}.png`,
      fullPage: true,
    });
  }
  await page.goto('/login');
  await expect(
    page.getByRole('button', { name: 'Sign in', exact: true })
  ).toBeInViewport();
});

// Until React hydrates, a submit is native: without `method="post"` it would be
// a GET to /login?email=…&password=….
test('a submit before hydration never puts the password in the URL', async ({
  page,
  baseURL,
}) => {
  const dashboardHost = new URL(baseURL ?? '').hostname;
  // The document and its inline scripts arrive, the client bundle does not:
  // the state every visitor is in until hydration finishes.
  await page.route(
    (url) => url.hostname === dashboardHost,
    (route) =>
      route.request().resourceType() === 'script'
        ? route.abort()
        : route.continue()
  );
  await page.goto('/login');
  await page.getByLabel('Email').fill('[email protected]');
  await page
    .getByLabel('Password', { exact: true })
    .fill('super-secret-password');
  await page.getByRole('button', { name: 'Sign in', exact: true }).click();
  await page.waitForLoadState('load');
  expect(page.url()).not.toContain('super-secret-password');
});

test('the dev login button signs in with one click when DEV_LOGIN is set', async ({
  page,
  context,
  seed,
  request,
}) => {
  const providers = await request.get(`${apiUrl()}/trpc/auth.providers`);
  const configured = (await providers.json()).result.data.json.devLogin;
  test.skip(!configured, 'DEV_LOGIN is not set on this API');

  const issues = watchIssues(page);
  await gotoHydrated(page, '/login');
  await page.screenshot({ path: `${SHOTS_DIR}/login-dev-button.png` });

  const signedIn = page.waitForResponse((response) =>
    response.url().includes(DEV_SIGN_IN_REQUEST)
  );
  await page.getByRole('button', { name: 'Dev login' }).click();
  expect((await signedIn).status()).toBe(200);
  await expect(page).toHaveURL(new RegExp(`/${seed.organizationId}$`), {
    timeout: 20_000,
  });
  expect((await context.cookies()).map((cookie) => cookie.name)).toContain(
    SESSION_COOKIE
  );
  expect(issues.unexpected()).toEqual([]);
});