Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/libs/actions/getCompanyCardBankConnection/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ function getCompanyCardBankConnection(policyID?: string, bankName?: string | nul
// embedded in the CompanyCardFeedWithDomainID. This lets the server refresh credentials on the feed the cards
// actually belong to (e.g. a Classic domain-level feed surfaced into a workspace via "preferred workspace")
// instead of always targeting the synthetic workspace-policy domain.
const domainID = feed ? splitCardFeedWithDomainID(feed)?.domainID : undefined;
const selectedFeed = splitCardFeedWithDomainID(feed);
const domainID = selectedFeed?.domainID;
const queryParams: Record<string, string> = domainID ? {...params, domainAccountID: String(domainID)} : params;
if (selectedFeed) {
queryParams.feed = selectedFeed.feedName;
}

const bank = CONST.COMPANY_CARDS.BANK_CONNECTIONS[bankConnection as keyof typeof CONST.COMPANY_CARDS.BANK_CONNECTIONS];

Expand Down
58 changes: 58 additions & 0 deletions tests/unit/GetCompanyCardBankConnectionTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import {getCompanyCardBankConnection, getPersonalCardBankConnection} from '@libs/actions/getCompanyCardBankConnection';
import type * as ApiUtilsModule from '@libs/ApiUtils';
import {getApiRoot} from '@libs/ApiUtils';
import * as NetworkStore from '@libs/Network/NetworkStore';

import CONST from '@src/CONST';

jest.mock('@libs/ApiUtils', () => ({
...jest.requireActual<typeof ApiUtilsModule>('@libs/ApiUtils'),
getApiRoot: jest.fn(() => 'https://www.expensify.com/'),
}));
jest.mock('@libs/Network/NetworkStore', () => ({getAuthToken: jest.fn(() => 'test-token')}));

describe('getCompanyCardBankConnection', () => {
it('preserves the selected Amex feed suffix and its originating domain', () => {
// Given a domain-level Amex feed displayed in a workspace
const feed = 'oauth.americanexpressfdx.com 4001#4055089';

// When its reconnect URL is generated
const url = getCompanyCardBankConnection('ABC123', CONST.COMPANY_CARDS.BANKS.AMEX, feed);

// Then the callback receives the exact feed key without the client-side domain suffix
expect(url).not.toBeNull();
const parsedURL = new URL(url ?? '');
expect(parsedURL.searchParams.get('feed')).toBe('oauth.americanexpressfdx.com 4001');
expect(parsedURL.searchParams.get('domainAccountID')).toBe('4055089');
expect(parsedURL.searchParams.get('isCorporate')).toBe('true');
expect(parsedURL.pathname).toBe('/partners/banks/americanexpressfdx/oauth_callback.php');
expect(getApiRoot).toHaveBeenCalledWith({shouldSkipWebProxy: true}, true);
});

it('omits reconnect identifiers for a new company connection', () => {
// Given an admin adding a new Amex connection without a selected feed
jest.mocked(NetworkStore.getAuthToken).mockReturnValue('test-token');

// When the add-connection URL is generated
const url = getCompanyCardBankConnection('ABC123', CONST.COMPANY_CARDS.BANKS.AMEX);

// Then the backend can derive a new feed key from the returned accounts
const parsedURL = new URL(url ?? '');
expect(parsedURL.searchParams.has('feed')).toBe(false);
expect(parsedURL.searchParams.has('domainAccountID')).toBe(false);
});

it('does not attach company-feed identifiers to personal connections', () => {
// Given a cardholder connecting their own Amex card
const bankName = CONST.PERSONAL_CARDS.BANKS.AMEX;

// When the personal connection URL is generated
const url = getPersonalCardBankConnection(bankName);

// Then no company-feed selection reaches the personal callback
const parsedURL = new URL(url ?? '');
expect(parsedURL.searchParams.has('feed')).toBe(false);
expect(parsedURL.searchParams.has('domainAccountID')).toBe(false);
expect(parsedURL.searchParams.has('isCorporate')).toBe(false);
});
});
Loading