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
2 changes: 1 addition & 1 deletion src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ function getEligibleBankAccountShareRecipients(policies: OnyxCollection<Policy>
*/
function hasEligibleActiveAdminFromWorkspaces(policies: OnyxCollection<Policy> | null, currentUserLogin: string | undefined, bankAccountID: string | undefined): boolean {
const currentBankAccount = getBankAccountFromID(Number(bankAccountID));
const activePolicies = getActivePolicies(policies, currentUserLogin);
const activePolicies = getActiveAdminWorkspaces(policies, currentUserLogin);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Scope eligibility to the bank account policy

When the user is an admin on any other active workspace, this still returns true even if the bank account's own workspace has no other eligible admins, because getActiveAdminWorkspaces() is not filtered by currentBankAccount.accountData.additionalData.policyID. In that scenario the Share option remains visible for the account and the share flow can be reached based on unrelated workspaces, which is the case this change is meant to prevent; filter the active admin policies to the bank account's owning policy before checking admins.

Useful? React with 👍 / 👎.

if (!activePolicies) {
return false;
}
Expand Down
108 changes: 100 additions & 8 deletions tests/unit/PolicyUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
hasConfiguredRules,
hasDependentTags,
hasDynamicExternalWorkflow,
hasEligibleActiveAdminFromWorkspaces,
hasIndependentTags,
hasOnlyPersonalPolicies,
hasOtherControlWorkspaces,
Expand Down Expand Up @@ -1679,16 +1680,34 @@ describe('PolicyUtils', () => {
await Onyx.clear();
await waitForBatchedUpdatesWithAct();
});
it('should return empty array if no admins in policies', () => {
it('should return empty array if no admins in policies', async () => {
const bankAccountID = '1';
await Onyx.set(ONYXKEYS.BANK_ACCOUNT_LIST, {
1: {
methodID: 12345,
accountData: {
additionalData: {policyID: '1'},
},
},
});
const policies = {
'1': {...createRandomPolicy(1, CONST.POLICY.TYPE.TEAM), pendingAction: undefined},
'2': {...createRandomPolicy(2, CONST.POLICY.TYPE.TEAM), pendingAction: undefined},
};
const result = getEligibleBankAccountShareRecipients(policies, approverEmail, '1');
const result = getEligibleBankAccountShareRecipients(policies, approverEmail, bankAccountID);
expect(result).toHaveLength(0);
});
it('should return array with admins', () => {
it('should return array with admins from the bank account workspace', async () => {
const bankAccountID = '1';
const currentUserLogin = adminEmail;
await Onyx.set(ONYXKEYS.BANK_ACCOUNT_LIST, {
1: {
methodID: 12345,
accountData: {
additionalData: {policyID: '1'},
},
},
});

const policies = {
'1': {
Expand All @@ -1701,7 +1720,7 @@ describe('PolicyUtils', () => {
},
'2': {...createRandomPolicy(2, CONST.POLICY.TYPE.TEAM), pendingAction: undefined},
};
const result = getEligibleBankAccountShareRecipients(policies, approverEmail, '1');
const result = getEligibleBankAccountShareRecipients(policies, approverEmail, bankAccountID);
expect(result).toHaveLength(1);
});
it('should not return user with already shared bank account', async () => {
Expand All @@ -1711,6 +1730,7 @@ describe('PolicyUtils', () => {
1: {
methodID: 12345,
accountData: {
additionalData: {policyID: '1'},
sharees: [adminEmail],
},
},
Expand All @@ -1732,6 +1752,14 @@ describe('PolicyUtils', () => {
});
it('should not return current user for sharing account', async () => {
const bankAccountID = '1';
await Onyx.set(ONYXKEYS.BANK_ACCOUNT_LIST, {
1: {
methodID: 12345,
accountData: {
additionalData: {policyID: '1'},
},
},
});

const policies = {
'1': {
Expand All @@ -1740,19 +1768,83 @@ describe('PolicyUtils', () => {
role: CONST.POLICY.ROLE.ADMIN,
employeeList: {
[adminEmail]: {email: adminEmail, role: CONST.POLICY.ROLE.ADMIN},
[approverEmail]: {email: approverEmail, role: CONST.POLICY.ROLE.ADMIN},
},
},
'2': {
...createRandomPolicy(2, CONST.POLICY.TYPE.CORPORATE),
};
const result = getEligibleBankAccountShareRecipients(policies, adminEmail, bankAccountID);
expect(result).toHaveLength(1);
});
});

describe('hasEligibleActiveAdminFromWorkspaces', () => {
beforeEach(() => {
wrapOnyxWithWaitForBatchedUpdates(Onyx);
Onyx.set(ONYXKEYS.PERSONAL_DETAILS_LIST, personalDetails);
});
afterEach(async () => {
await Onyx.clear();
await waitForBatchedUpdatesWithAct();
});
it('should return true when another admin is available in the bank account workspace', async () => {
const bankAccountID = '1';
await Onyx.set(ONYXKEYS.BANK_ACCOUNT_LIST, {
1: {
methodID: 12345,
accountData: {
additionalData: {policyID: '1'},
},
},
});

const policies = {
'1': {
...createRandomPolicy(1, CONST.POLICY.TYPE.TEAM),
pendingAction: undefined,
role: CONST.POLICY.ROLE.ADMIN,
employeeList: {
[adminEmail]: {email: adminEmail, role: CONST.POLICY.ROLE.ADMIN},
[approverEmail]: {email: approverEmail, role: CONST.POLICY.ROLE.ADMIN},
},
},
};
const result = getEligibleBankAccountShareRecipients(policies, adminEmail, bankAccountID);
expect(result).toHaveLength(1);
const result = hasEligibleActiveAdminFromWorkspaces(policies, adminEmail, bankAccountID);
expect(result).toBe(true);
});
it('should return false when the user only joined another workspace as a member', async () => {
const bankAccountID = '1';
await Onyx.set(ONYXKEYS.BANK_ACCOUNT_LIST, {
1: {
methodID: 12345,
accountData: {
additionalData: {policyID: '1'},
},
},
});

const policies = {
// The bank account's own workspace - current user is the only admin, no one to share with
'1': {
...createRandomPolicy(1, CONST.POLICY.TYPE.TEAM),
pendingAction: undefined,
role: CONST.POLICY.ROLE.ADMIN,
employeeList: {
[adminEmail]: {email: adminEmail, role: CONST.POLICY.ROLE.ADMIN},
},
},
// Another user's workspace the current user only joined as a member - has its own admin
'2': {
...createRandomPolicy(2, CONST.POLICY.TYPE.TEAM),
pendingAction: undefined,
role: CONST.POLICY.ROLE.USER,
employeeList: {
[approverEmail]: {email: approverEmail, role: CONST.POLICY.ROLE.ADMIN},
[adminEmail]: {email: adminEmail, role: CONST.POLICY.ROLE.USER},
},
},
};
const result = hasEligibleActiveAdminFromWorkspaces(policies, adminEmail, bankAccountID);
expect(result).toBe(false);
});
});

Expand Down
Loading