Skip to content

The open alternative to Salesforce, designed for AI.

3.6 KB
Raw Blame
import {
  extractSessionCookie,
  postMetadataOperationWithHeaders,
  signInWithCookieCapture,
} from 'test/integration/graphql/suites/auth/user-sessions/utils/sign-in-with-cookie-capture.util';
import {
  currentUserIdentityQueryFactory,
  currentUserSessionsQueryFactory,
  revokeAllOtherUserSessionsQueryFactory,
  revokeUserSessionQueryFactory,
} from 'test/integration/graphql/suites/auth/user-sessions/utils/user-session-operations.util';

import { ALLOWED_ORIGIN } from 'test/integration/graphql/suites/auth/user-sessions/constants/session-origins.constants';

type UserSessionApiEntry = { id: string; isCurrent: boolean };

// Tim is seeded in both apple and yc, proving a workspace can neither list nor revoke his sessions in the other.
describe('workspace-scoped user sessions API (integration)', () => {
  let appleCookieHeader: string;
  let ycCookieHeader: string;

  const signInTo = async (workspaceSubdomain: string): Promise<string> => {
    const response = await signInWithCookieCapture({
      workspaceSubdomain,
      originHeader: ALLOWED_ORIGIN,
    });
    const sessionCookie = extractSessionCookie(response);

    if (!sessionCookie) {
      throw new Error(`Expected a session cookie from ${workspaceSubdomain}`);
    }

    return sessionCookie.cookieHeader;
  };

  const fetchSessions = async (
    cookieHeader: string,
  ): Promise<UserSessionApiEntry[]> => {
    const response = await postMetadataOperationWithHeaders(
      currentUserSessionsQueryFactory(),
      { originHeader: ALLOWED_ORIGIN, cookieHeader },
    );

    expect(response.body.errors).toBeUndefined();

    return response.body.data.currentUserSessions;
  };

  beforeAll(async () => {
    appleCookieHeader = await signInTo('apple');
    ycCookieHeader = await signInTo('yc');
  });

  it('should list only the sessions bound to the workspace the cookie belongs to', async () => {
    const appleSessionIds = (await fetchSessions(appleCookieHeader)).map(
      (session) => session.id,
    );
    const ycSessionIds = (await fetchSessions(ycCookieHeader)).map(
      (session) => session.id,
    );

    expect(appleSessionIds.length).toBeGreaterThan(0);
    expect(ycSessionIds.length).toBeGreaterThan(0);

    expect(
      appleSessionIds.filter((sessionId) => ycSessionIds.includes(sessionId)),
    ).toHaveLength(0);
  });

  it('should refuse to revoke a session belonging to another workspace', async () => {
    const ycSessions = await fetchSessions(ycCookieHeader);
    const ycSessionId = ycSessions[0].id;

    const response = await postMetadataOperationWithHeaders(
      revokeUserSessionQueryFactory({ userSessionId: ycSessionId }),
      { originHeader: ALLOWED_ORIGIN, cookieHeader: appleCookieHeader },
    );

    expect(response.body.errors).toBeDefined();

    const ycSessionsAfter = await fetchSessions(ycCookieHeader);

    expect(ycSessionsAfter.map((session) => session.id)).toContain(ycSessionId);
  });

  it('should leave other workspaces signed in when revoking all other devices', async () => {
    await signInTo('apple');

    const response = await postMetadataOperationWithHeaders(
      revokeAllOtherUserSessionsQueryFactory(),
      { originHeader: ALLOWED_ORIGIN, cookieHeader: appleCookieHeader },
    );

    expect(response.body.errors).toBeUndefined();
    expect(
      response.body.data.revokeAllOtherUserSessions,
    ).toBeGreaterThanOrEqual(1);

    const ycResponse = await postMetadataOperationWithHeaders(
      currentUserIdentityQueryFactory(),
      { originHeader: ALLOWED_ORIGIN, cookieHeader: ycCookieHeader },
    );

    expect(ycResponse.body.errors).toBeUndefined();
    expect(ycResponse.body.data.currentUser.email).toBe('[email protected]');
  });
});