Skip to content

The open alternative to Salesforce, designed for AI.

5.3 KB
Raw Blame
import { gql } from 'graphql-tag';
import {
  APPLICATION_ONLY_REST_REQUEST_FACTORIES,
  makeApplicationOnlyRestRequest,
} from 'test/integration/metadata/suites/application/utils/application-only-endpoint-request-factories.util';
import {
  type ApplicationWithAppConnection,
  cleanupApplicationWithAppConnection,
  setupApplicationWithAppConnection,
} from 'test/integration/metadata/suites/connection-provider/utils/setup-application-with-app-connection.util';
import { makeMetadataApiRequest } from 'test/integration/metadata/suites/utils/make-metadata-api-request.util';
import { isDefined } from 'twenty-shared/utils';

import { USER_WORKSPACE_DATA_SEED_IDS } from 'src/engine/workspace-manager/dev-seeder/core/utils/seed-user-workspaces.util';

const APP_CONNECTION_FIELDS = gql`
  fragment AppConnectionFields on AppConnection {
    id
    providerName
    handle
    scope
    visibility
    userWorkspaceId
    providerType
    accessToken
  }
`;

const LIST_APP_CONNECTIONS_QUERY = gql`
  ${APP_CONNECTION_FIELDS}
  query AppConnections($filter: ListAppConnectionsInput) {
    appConnections(filter: $filter) {
      ...AppConnectionFields
    }
  }
`;

const GET_APP_CONNECTION_QUERY = gql`
  ${APP_CONNECTION_FIELDS}
  query AppConnection($id: ID!) {
    appConnection(id: $id) {
      ...AppConnectionFields
    }
  }
`;

const SCOPE_FILTER_CASES = [
  {
    title: 'the matching scope',
    filter: { scope: 'WORKSPACE' },
    isListed: true,
  },
  { title: 'another scope', filter: { scope: 'USER' }, isListed: false },
  {
    title: 'the matching deprecated visibility',
    filter: { visibility: 'workspace' },
    isListed: true,
  },
  {
    title: 'another deprecated visibility',
    filter: { visibility: 'user' },
    isListed: false,
  },
  {
    title: 'a scope that wins over the deprecated visibility',
    filter: { scope: 'USER', visibility: 'workspace' },
    isListed: false,
  },
];

describe('app connections API (e2e)', () => {
  let application: ApplicationWithAppConnection;

  const expectedConnection = () => ({
    id: application.connectedAccountId,
    providerName: application.connectionProviderName,
    handle: application.connectedAccountHandle,
    scope: 'WORKSPACE',
    visibility: 'workspace',
    userWorkspaceId: USER_WORKSPACE_DATA_SEED_IDS.JANE,
    providerType: 'oauth',
    accessToken: application.connectedAccountAccessToken,
  });

  beforeAll(async () => {
    application = await setupApplicationWithAppConnection({
      name: 'App Connections',
    });
  }, 120000);

  afterAll(async () => {
    if (isDefined(application)) {
      await cleanupApplicationWithAppConnection(application);
    }
  }, 120000);

  it('lists the connections of the calling application', async () => {
    const response = await makeMetadataApiRequest(
      { query: LIST_APP_CONNECTIONS_QUERY },
      application.applicationToken,
    );

    expect(response.body.errors).toBeUndefined();
    expect(response.body.data.appConnections).toEqual([expectedConnection()]);
  });

  it.each(SCOPE_FILTER_CASES)(
    'filters the connections on $title',
    async ({ filter, isListed }) => {
      const response = await makeMetadataApiRequest(
        { query: LIST_APP_CONNECTIONS_QUERY, variables: { filter } },
        application.applicationToken,
      );

      expect(response.body.errors).toBeUndefined();
      expect(response.body.data.appConnections).toEqual(
        isListed ? [expectedConnection()] : [],
      );
    },
  );

  it('returns one connection of the calling application', async () => {
    const response = await makeMetadataApiRequest(
      {
        query: GET_APP_CONNECTION_QUERY,
        variables: { id: application.connectedAccountId },
      },
      application.applicationToken,
    );

    expect(response.body.errors).toBeUndefined();
    expect(response.body.data.appConnection).toEqual(expectedConnection());
  });

  it('lists the connections of the calling application over the deprecated REST endpoint', async () => {
    const response = await makeApplicationOnlyRestRequest(
      APPLICATION_ONLY_REST_REQUEST_FACTORIES['POST /apps/connections/list'](),
      application.applicationToken,
    );

    expect(response.status).toBe(200);
    expect(response.body).toEqual([
      expect.objectContaining(expectedConnection()),
    ]);
  });

  it.each(SCOPE_FILTER_CASES)(
    'filters the connections on $title over the deprecated REST endpoint',
    async ({ filter, isListed }) => {
      const response = await makeApplicationOnlyRestRequest(
        {
          ...APPLICATION_ONLY_REST_REQUEST_FACTORIES[
            'POST /apps/connections/list'
          ](),
          body: filter,
        },
        application.applicationToken,
      );

      expect(response.status).toBe(200);
      expect(response.body).toEqual(
        isListed ? [expect.objectContaining(expectedConnection())] : [],
      );
    },
  );

  it('returns one connection of the calling application over the deprecated REST endpoint', async () => {
    const response = await makeApplicationOnlyRestRequest(
      APPLICATION_ONLY_REST_REQUEST_FACTORIES['POST /apps/connections/get'](
        application,
      ),
      application.applicationToken,
    );

    expect(response.status).toBe(200);
    expect(response.body).toEqual(
      expect.objectContaining(expectedConnection()),
    );
  });
});