1
0
mirror of https://github.com/bitwarden/browser.git synced 2025-01-03 18:28:13 +01:00

[EC-739 / EC-740] Add null check to policyFilter on PolicyService (#4039)

* [EC-739 / EC-740] Add null check to policyFilter on PolicyService

* [EC-739 / EC-740] Add unit tests for policy filter
This commit is contained in:
Rui Tomé 2022-11-11 15:13:27 +00:00 committed by GitHub
parent d3321ebe1c
commit 235fb8f6ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 7 deletions

View File

@ -50,6 +50,12 @@ describe("PolicyService", () => {
organizationService.getAll(null).resolves([]); organizationService.getAll(null).resolves([]);
activeAccount = new BehaviorSubject("123"); activeAccount = new BehaviorSubject("123");
activeAccountUnlocked = new BehaviorSubject(true); activeAccountUnlocked = new BehaviorSubject(true);
stateService.getDecryptedPolicies({ userId: "user" }).resolves(null);
stateService.getEncryptedPolicies({ userId: "user" }).resolves({
"1": policyData("1", "test-organization", PolicyType.MaximumVaultTimeout, true, {
minutes: 14,
}),
});
stateService.getEncryptedPolicies().resolves({ stateService.getEncryptedPolicies().resolves({
"1": policyData("1", "test-organization", PolicyType.MaximumVaultTimeout, true, { "1": policyData("1", "test-organization", PolicyType.MaximumVaultTimeout, true, {
minutes: 14, minutes: 14,
@ -296,7 +302,7 @@ describe("PolicyService", () => {
}); });
}); });
describe("policyAppliesToActiveUser", () => { describe("policyAppliesToActiveUser$", () => {
it("MasterPassword does not apply", async () => { it("MasterPassword does not apply", async () => {
const result = await firstValueFrom( const result = await firstValueFrom(
policyService.policyAppliesToActiveUser$(PolicyType.MasterPassword) policyService.policyAppliesToActiveUser$(PolicyType.MasterPassword)
@ -313,6 +319,14 @@ describe("PolicyService", () => {
expect(result).toEqual(true); expect(result).toEqual(true);
}); });
it("PolicyFilter filters result", async () => {
const result = await firstValueFrom(
policyService.policyAppliesToActiveUser$(PolicyType.MaximumVaultTimeout, (p) => false)
);
expect(result).toEqual(false);
});
it("DisablePersonalVaultExport does not apply", async () => { it("DisablePersonalVaultExport does not apply", async () => {
const result = await firstValueFrom( const result = await firstValueFrom(
policyService.policyAppliesToActiveUser$(PolicyType.DisablePersonalVaultExport) policyService.policyAppliesToActiveUser$(PolicyType.DisablePersonalVaultExport)
@ -322,6 +336,48 @@ describe("PolicyService", () => {
}); });
}); });
describe("policyAppliesToUser", () => {
it("MasterPassword does not apply", async () => {
const result = await policyService.policyAppliesToUser(
PolicyType.MasterPassword,
null,
"user"
);
expect(result).toEqual(false);
});
it("MaximumVaultTimeout applies", async () => {
const result = await policyService.policyAppliesToUser(
PolicyType.MaximumVaultTimeout,
null,
"user"
);
expect(result).toEqual(true);
});
it("PolicyFilter filters result", async () => {
const result = await policyService.policyAppliesToUser(
PolicyType.MaximumVaultTimeout,
(p) => false,
"user"
);
expect(result).toEqual(false);
});
it("DisablePersonalVaultExport does not apply", async () => {
const result = await policyService.policyAppliesToUser(
PolicyType.DisablePersonalVaultExport,
null,
"user"
);
expect(result).toEqual(false);
});
});
function policyData( function policyData(
id: string, id: string,
organizationId: string, organizationId: string,

View File

@ -124,10 +124,7 @@ export class PolicyService implements InternalPolicyServiceAbstraction {
); );
} }
policyAppliesToActiveUser$( policyAppliesToActiveUser$(policyType: PolicyType, policyFilter?: (policy: Policy) => boolean) {
policyType: PolicyType,
policyFilter: (policy: Policy) => boolean = (p) => true
) {
return this.policies$.pipe( return this.policies$.pipe(
concatMap(async (policies) => { concatMap(async (policies) => {
const userId = await this.stateService.getUserId(); const userId = await this.stateService.getUserId();
@ -257,12 +254,12 @@ export class PolicyService implements InternalPolicyServiceAbstraction {
private async checkPoliciesThatApplyToUser( private async checkPoliciesThatApplyToUser(
policies: Policy[], policies: Policy[],
policyType: PolicyType, policyType: PolicyType,
policyFilter: (policy: Policy) => boolean = (p) => true, policyFilter?: (policy: Policy) => boolean,
userId?: string userId?: string
) { ) {
const organizations = await this.organizationService.getAll(userId); const organizations = await this.organizationService.getAll(userId);
const filteredPolicies = policies.filter( const filteredPolicies = policies.filter(
(p) => p.type === policyType && p.enabled && policyFilter(p) (p) => p.type === policyType && p.enabled && (policyFilter == null || policyFilter(p))
); );
const policySet = new Set(filteredPolicies.map((p) => p.organizationId)); const policySet = new Set(filteredPolicies.map((p) => p.organizationId));