4
import ComposioClient from '@composio/client';
5
import { ConnectedAccountRetrieveResponse } from '@composio/client/resources/connected-accounts.mjs';
6
import {
7
+ ComposioAclOnlyForSharedError,
8
ComposioConnectedAccountNotFoundError,
9
ComposioFailedToCreateConnectedAccountLink,
10
} from '../../src/errors';
11
+import { BadRequestError } from '@composio/client';
12
import { ConnectedAccountStatuses } from '../../src/types/connectedAccounts.types';
13
import { ComposioMultipleConnectedAccountsError } from '../../src/errors';
14
import { AuthSchemeTypes } from '../../src/types/authConfigs.types';
24
retrieve: vi.fn(),
25
delete: vi.fn(),
26
refresh: vi.fn(),
27
+ patch: vi.fn(),
28
updateStatus: vi.fn(),
29
createConnectedAccountLink: vi.fn(),
30
},
1520
expect(connectionRequest).toHaveProperty('id', 'conn_new');
1521
});
1522
});
1523
+
1524
+ describe('link with accountType + ACL (Hermes #9860, #9902)', () => {
1525
+ beforeEach(() => {
1526
+ extendedMockClient.connectedAccounts.list.mockResolvedValue({
1527
+ items: [],
1528
+ next_cursor: null,
1529
+ total_pages: 0,
1530
+ });
1531
+ extendedMockClient.link.create.mockResolvedValue({
1532
+ connected_account_id: 'conn_shared_abc',
1533
+ redirect_url: 'https://connect.composio.dev/auth?token=xyz',
1534
+ });
1535
+ });
1536
+
1537
+ it('forwards accountType=SHARED + nested aclConfigForShared to client.link.create', async () => {
1538
+ await connectedAccounts.link('user_123', 'auth_config_123', {
1539
+ accountType: 'SHARED',
1540
+ aclConfigForShared: {
1541
+ allowAllUsers: true,
1542
+ notAllowedUserIds: ['user_bob'],
1543
+ },
1544
+ });
1545
+
1546
+ expect(extendedMockClient.link.create).toHaveBeenCalledWith({
1547
+ auth_config_id: 'auth_config_123',
1548
+ user_id: 'user_123',
1549
+ account_type: 'SHARED',
1550
+ acl_config_for_shared: {
1551
+ allow_all_users: true,
1552
+ not_allowed_user_ids: ['user_bob'],
1553
+ },
1554
+ });
1555
+ });
1556
+
1557
+ it('omits acl_config_for_shared when aclConfigForShared is undefined', async () => {
1558
+ await connectedAccounts.link('user_123', 'auth_config_123', { accountType: 'SHARED' });
1559
+
1560
+ const body = extendedMockClient.link.create.mock.calls[0][0];
1561
+ expect(body.account_type).toBe('SHARED');
1562
+ expect('acl_config_for_shared' in body).toBe(false);
1563
+ });
1564
+
1565
+ it('serializes only the fields the caller provided (PATCH-style on inner block)', async () => {
1566
+ await connectedAccounts.link('user_123', 'auth_config_123', {
1567
+ accountType: 'SHARED',
1568
+ aclConfigForShared: { allowedUserIds: ['user_alice'] },
1569
+ });
1570
+
1571
+ expect(extendedMockClient.link.create).toHaveBeenCalledWith({
1572
+ auth_config_id: 'auth_config_123',
1573
+ user_id: 'user_123',
1574
+ account_type: 'SHARED',
1575
+ acl_config_for_shared: { allowed_user_ids: ['user_alice'] },
1576
+ });
1577
+ });
1578
+
1579
+ it('preserves explicit empty arrays in the serialized body', async () => {
1580
+ await connectedAccounts.link('user_123', 'auth_config_123', {
1581
+ accountType: 'SHARED',
1582
+ aclConfigForShared: { allowedUserIds: [], notAllowedUserIds: [] },
1583
+ });
1584
+
1585
+ expect(extendedMockClient.link.create).toHaveBeenCalledWith({
1586
+ auth_config_id: 'auth_config_123',
1587
+ user_id: 'user_123',
1588
+ account_type: 'SHARED',
1589
+ acl_config_for_shared: {
1590
+ allowed_user_ids: [],
1591
+ not_allowed_user_ids: [],
1592
+ },
1593
+ });
1594
+ });
1595
+
1596
+ it('maps 400 AclOnlyForShared to ComposioAclOnlyForSharedError', async () => {
1597
+ extendedMockClient.link.create.mockReset();
1598
+ extendedMockClient.link.create.mockRejectedValueOnce(
1599
+ Object.assign(new BadRequestError(400, undefined, 'acl_config_for_shared is only valid on SHARED connections.', {}), {})
1600
+ );
1601
+
1602
+ await expect(
1603
+ connectedAccounts.link('user_123', 'auth_config_123', {
1604
+ accountType: 'PRIVATE',
1605
+ aclConfigForShared: { allowAllUsers: true },
1606
+ })
1607
+ ).rejects.toBeInstanceOf(ComposioAclOnlyForSharedError);
1608
+ });
1609
+
1610
+ it('falls back to ComposioFailedToCreateConnectedAccountLink on unrelated errors', async () => {
1611
+ extendedMockClient.link.create.mockReset();
1612
+ extendedMockClient.link.create.mockRejectedValueOnce(new Error('network died'));
1613
+
1614
+ await expect(connectedAccounts.link('user_123', 'auth_config_123')).rejects.toBeInstanceOf(
1615
+ ComposioFailedToCreateConnectedAccountLink
1616
+ );
1617
+ });
1618
+ });
1619
+
1620
+ describe('updateAcl', () => {
1621
+ it('serializes PATCH body with nested acl_config_for_shared', async () => {
1622
+ extendedMockClient.connectedAccounts.patch.mockResolvedValueOnce({
1623
+ id: 'ca_abc',
1624
+ status: 'ACTIVE',
1625
+ success: true,
1626
+ });
1627
+
1628
+ const result = await connectedAccounts.updateAcl('ca_abc', {
1629
+ allowAllUsers: true,
1630
+ notAllowedUserIds: ['user_bob'],
1631
+ });
1632
+
1633
+ expect(extendedMockClient.connectedAccounts.patch).toHaveBeenCalledWith('ca_abc', {
1634
+ acl_config_for_shared: {
1635
+ allow_all_users: true,
1636
+ not_allowed_user_ids: ['user_bob'],
1637
+ },
1638
+ });
1639
+ expect(result).toEqual({ id: 'ca_abc', status: 'ACTIVE', success: true });
1640
+ });
1641
+
1642
+ it('omits absent fields from the inner block (PATCH semantics)', async () => {
1643
+ extendedMockClient.connectedAccounts.patch.mockResolvedValueOnce({
1644
+ id: 'ca_abc',
1645
+ status: 'ACTIVE',
1646
+ success: true,
1647
+ });
1648
+
1649
+ await connectedAccounts.updateAcl('ca_abc', { allowedUserIds: ['user_alice'] });
1650
+
1651
+ expect(extendedMockClient.connectedAccounts.patch).toHaveBeenCalledWith('ca_abc', {
1652
+ acl_config_for_shared: { allowed_user_ids: ['user_alice'] },
1653
+ });
1654
+ });
1655
+
1656
+ it('preserves empty array to clear a list', async () => {
1657
+ extendedMockClient.connectedAccounts.patch.mockResolvedValueOnce({
1658
+ id: 'ca_abc',
1659
+ status: 'ACTIVE',
1660
+ success: true,
1661
+ });
1662
+
1663
+ await connectedAccounts.updateAcl('ca_abc', { allowedUserIds: [] });
1664
+
1665
+ expect(extendedMockClient.connectedAccounts.patch).toHaveBeenCalledWith('ca_abc', {
1666
+ acl_config_for_shared: { allowed_user_ids: [] },
1667
+ });
1668
+ });
1669
+
1670
+ it('rejects an empty params object via the refine', async () => {
1671
+ await expect(connectedAccounts.updateAcl('ca_abc', {})).rejects.toMatchObject({
1672
+ name: 'ValidationError',
1673
+ });
1674
+ expect(extendedMockClient.connectedAccounts.patch).not.toHaveBeenCalled();
1675
+ });
1676
+
1677
+ it('maps 400 AclOnlyForShared to ComposioAclOnlyForSharedError', async () => {
1678
+ extendedMockClient.connectedAccounts.patch.mockRejectedValueOnce(
1679
+ new BadRequestError(400, undefined, 'acl_config_for_shared is only valid on SHARED connections.', {})
1680
+ );
1681
+
1682
+ await expect(
1683
+ connectedAccounts.updateAcl('ca_abc', { allowAllUsers: true })
1684
+ ).rejects.toBeInstanceOf(ComposioAclOnlyForSharedError);
1685
+ });
1686
+
1687
+ it('rethrows non-AclOnlyForShared errors unchanged', async () => {
1688
+ const otherError = new Error('connection lost');
1689
+ extendedMockClient.connectedAccounts.patch.mockRejectedValueOnce(otherError);
1690
+
1691
+ await expect(
1692
+ connectedAccounts.updateAcl('ca_abc', { allowAllUsers: true })
1693
+ ).rejects.toBe(otherError);
1694
+ });
1695
+ });
1696
});