Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

2.2 KB
Raw Blame
import { afterEach, describe, expect, it, vi } from 'vitest';
import { httpClient } from '@activepieces/pieces-common';
import { asanaAuth } from './auth';

const server = { apiUrl: 'http://127.0.0.1:4200/api/', publicUrl: 'http://127.0.0.1:4200/api/' };

function resolveIdentifier(data: Record<string, unknown>) {
  return asanaAuth.getConnectionIdentifier?.({ auth: { access_token: 'token', data }, server });
}

function mockUsersMe(body: unknown) {
  return vi.spyOn(httpClient, 'sendRequest').mockResolvedValue({ status: 200, headers: {}, body });
}

describe('asanaAuth.getConnectionIdentifier', () => {
  afterEach(() => {
    vi.restoreAllMocks();
  });

  it('returns the email from the user Asana puts in the token response, without calling the API', async () => {
    const sendRequest = mockUsersMe({});

    const identifier = await resolveIdentifier({ data: { gid: '1', name: 'Pat Doe', email: '[email protected]' } });

    expect(identifier).toBe('[email protected]');
    expect(sendRequest).not.toHaveBeenCalled();
  });

  it('falls back to the user name when the token user has no email', async () => {
    const sendRequest = mockUsersMe({});

    expect(await resolveIdentifier({ data: { gid: '1', name: 'Pat Doe' } })).toBe('Pat Doe');
    expect(sendRequest).not.toHaveBeenCalled();
  });

  it('asks GET /users/me when the token response carries no user', async () => {
    const sendRequest = mockUsersMe({ data: { email: '[email protected]', name: 'Me' } });

    expect(await resolveIdentifier({})).toBe('[email protected]');
    expect(sendRequest).toHaveBeenCalledWith(
      expect.objectContaining({
        url: 'https://app.asana.com/api/1.0/users/me',
        headers: { Authorization: 'Bearer token' },
      }),
    );
  });

  it('ignores a token user that is not an object', async () => {
    mockUsersMe({ data: { email: '[email protected]' } });

    expect(await resolveIdentifier({ data: 'unexpected' })).toBe('[email protected]');
  });

  it('returns undefined instead of throwing when the API call fails', async () => {
    vi.spyOn(httpClient, 'sendRequest').mockRejectedValue(new Error('401 Unauthorized'));

    expect(await resolveIdentifier({})).toBeUndefined();
  });
});