Skip to content

The open alternative to Salesforce, designed for AI.

7.9 KB
Raw Blame
import gql from 'graphql-tag';
import request from 'supertest';
import { deleteOneRoleOperationFactory } from 'test/integration/graphql/utils/delete-one-role-operation-factory.util';
import { makeMetadataApiRequest } from 'test/integration/metadata/suites/utils/make-metadata-api-request.util';
import { findOneRoleByLabel } from 'test/integration/metadata/suites/role/utils/find-one-role-by-label.util';
import { updateWorkspaceMemberRole } from 'test/integration/metadata/suites/role/utils/update-workspace-member-role.util';
import { PermissionFlagType } from 'twenty-shared/constants';

import { ErrorCode } from 'src/engine/core-modules/graphql/utils/graphql-errors.util';
import { WORKSPACE_MEMBER_DATA_SEED_IDS } from 'src/engine/workspace-manager/dev-seeder/data/constants/workspace-member-data-seeds.constant';

const client = request(`http://localhost:${APP_PORT}`);

describe('API key role assignment permission', () => {
  let customRoleId: string;
  let adminRoleId: string;
  let originalMemberRoleId: string;
  let apiKeyId: string;
  let applicationRegistrationId: string;

  const rotateClientSecretAs = (token?: string) =>
    makeMetadataApiRequest(
      {
        query: gql`
          mutation RotateSecret($id: String!) {
            rotateApplicationRegistrationClientSecret(id: $id) {
              clientSecret
            }
          }
        `,
        variables: { id: applicationRegistrationId },
      },
      token,
    );

  const generateApiKeyTokenAs = (token?: string) =>
    makeMetadataApiRequest(
      {
        query: gql`
          mutation GenerateApiKeyToken($apiKeyId: UUID!, $expiresAt: String!) {
            generateApiKeyToken(apiKeyId: $apiKeyId, expiresAt: $expiresAt) {
              token
            }
          }
        `,
        variables: { apiKeyId, expiresAt: '2099-01-01T00:00:00Z' },
      },
      token,
    );

  const createApiKeyAsJony = (roleId: string) =>
    makeMetadataApiRequest(
      {
        query: gql`
          mutation CreateApiKey($input: CreateApiKeyInput!) {
            createApiKey(input: $input) {
              id
            }
          }
        `,
        variables: {
          input: {
            name: 'Escalation attempt',
            expiresAt: '2099-01-01T00:00:00Z',
            roleId,
          },
        },
      },
      APPLE_JONY_MEMBER_ACCESS_TOKEN,
    );

  beforeAll(async () => {
    originalMemberRoleId = (await findOneRoleByLabel({ label: 'Member' })).id;
    adminRoleId = (await findOneRoleByLabel({ label: 'Admin' })).id;

    const createRoleResponse = await makeMetadataApiRequest({
      query: gql`
        mutation CreateOneRole {
          createOneRole(
            createRoleInput: {
              label: "Api Keys Only Role"
              canUpdateAllSettings: false
              canReadAllObjectRecords: true
              canUpdateAllObjectRecords: true
              canSoftDeleteAllObjectRecords: false
              canDestroyAllObjectRecords: false
            }
          ) {
            id
          }
        }
      `,
    });

    customRoleId = createRoleResponse.body.data.createOneRole.id;

    await makeMetadataApiRequest({
      query: gql`
        mutation UpsertPermissionFlags {
          upsertPermissionFlags(
            upsertPermissionFlagsInput: {
              roleId: "${customRoleId}"
              permissionFlagKeys: ["${PermissionFlagType.API_KEYS_AND_WEBHOOKS}"]
            }
          ) {
            id
          }
        }
      `,
    });

    const createApiKeyResponse = await makeMetadataApiRequest({
      query: gql`
        mutation CreateApiKey($input: CreateApiKeyInput!) {
          createApiKey(input: $input) {
            id
          }
        }
      `,
      variables: {
        input: {
          name: 'Seed key',
          expiresAt: '2099-01-01T00:00:00Z',
          roleId: adminRoleId,
        },
      },
    });

    apiKeyId = createApiKeyResponse.body.data.createApiKey.id;

    const createRegistrationResponse = await makeMetadataApiRequest({
      query: gql`
        mutation CreateApplicationRegistration(
          $input: CreateApplicationRegistrationInput!
        ) {
          createApplicationRegistration(input: $input) {
            applicationRegistration {
              id
            }
          }
        }
      `,
      variables: { input: { name: 'Rotation escalation target' } },
    });

    applicationRegistrationId =
      createRegistrationResponse.body.data.createApplicationRegistration
        .applicationRegistration.id;

    await updateWorkspaceMemberRole({
      input: {
        roleId: customRoleId,
        workspaceMemberId: WORKSPACE_MEMBER_DATA_SEED_IDS.JONY,
      },
      expectToFail: false,
    });
  });

  afterAll(async () => {
    await updateWorkspaceMemberRole({
      input: {
        roleId: originalMemberRoleId,
        workspaceMemberId: WORKSPACE_MEMBER_DATA_SEED_IDS.JONY,
      },
      expectToFail: false,
    });

    await testDataSource
      .query('DELETE FROM core."apiKey" WHERE id = $1', [apiKeyId])
      .catch(() => {});

    await testDataSource
      .query('DELETE FROM core."applicationRegistration" WHERE id = $1', [
        applicationRegistrationId,
      ])
      .catch(() => {});

    await client
      .post('/metadata')
      .set('Authorization', `Bearer ${APPLE_JANE_ADMIN_ACCESS_TOKEN}`)
      .send(deleteOneRoleOperationFactory(customRoleId));
  });

  it('denies creating an API key with the Admin role when the caller lacks ROLES permission', async () => {
    const response = await createApiKeyAsJony(adminRoleId);

    expect(response.body.data?.createApiKey ?? null).toBeNull();
    expect(response.body.errors).toBeDefined();
    expect(response.body.errors[0].extensions.code).toBe(ErrorCode.FORBIDDEN);
  });

  it('denies assigning a role to an API key when the caller lacks ROLES permission', async () => {
    const response = await makeMetadataApiRequest(
      {
        query: gql`
          mutation AssignRoleToApiKey($apiKeyId: UUID!, $roleId: UUID!) {
            assignRoleToApiKey(apiKeyId: $apiKeyId, roleId: $roleId)
          }
        `,
        variables: { apiKeyId, roleId: adminRoleId },
      },
      APPLE_JONY_MEMBER_ACCESS_TOKEN,
    );

    expect(response.body.errors).toBeDefined();
    expect(response.body.errors[0].extensions.code).toBe(ErrorCode.FORBIDDEN);
  });

  it('denies generating a token for an Admin API key when the caller lacks ROLES permission', async () => {
    const response = await generateApiKeyTokenAs(
      APPLE_JONY_MEMBER_ACCESS_TOKEN,
    );

    expect(response.body.data?.generateApiKeyToken ?? null).toBeNull();
    expect(response.body.errors).toBeDefined();
    expect(response.body.errors[0].extensions.code).toBe(ErrorCode.FORBIDDEN);
  });

  it('allows generating a token for an API key with ROLES permission', async () => {
    const response = await generateApiKeyTokenAs();

    expect(response.body.errors).toBeUndefined();
    expect(response.body.data.generateApiKeyToken.token).toEqual(
      expect.any(String),
    );
  });

  // A rotated secret is redeemable at /oauth/token for the application's own
  // role, so rotating binds that role to the caller just as creating an API
  // key does.
  it('denies rotating an application registration client secret when the caller lacks ROLES permission', async () => {
    const response = await rotateClientSecretAs(APPLE_JONY_MEMBER_ACCESS_TOKEN);

    expect(
      response.body.data?.rotateApplicationRegistrationClientSecret ?? null,
    ).toBeNull();
    expect(response.body.errors).toBeDefined();
    expect(response.body.errors[0].extensions.code).toBe(ErrorCode.FORBIDDEN);
  });

  it('allows rotating an application registration client secret with ROLES permission', async () => {
    const response = await rotateClientSecretAs();

    expect(response.body.errors).toBeUndefined();
    expect(
      response.body.data.rotateApplicationRegistrationClientSecret.clientSecret,
    ).toEqual(expect.any(String));
  });
});