mirror of
https://github.com/bitwarden/browser.git
synced 2025-01-30 22:41:33 +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:
parent
d3321ebe1c
commit
235fb8f6ee
@ -50,6 +50,12 @@ describe("PolicyService", () => {
|
||||
organizationService.getAll(null).resolves([]);
|
||||
activeAccount = new BehaviorSubject("123");
|
||||
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({
|
||||
"1": policyData("1", "test-organization", PolicyType.MaximumVaultTimeout, true, {
|
||||
minutes: 14,
|
||||
@ -296,7 +302,7 @@ describe("PolicyService", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("policyAppliesToActiveUser", () => {
|
||||
describe("policyAppliesToActiveUser$", () => {
|
||||
it("MasterPassword does not apply", async () => {
|
||||
const result = await firstValueFrom(
|
||||
policyService.policyAppliesToActiveUser$(PolicyType.MasterPassword)
|
||||
@ -313,6 +319,14 @@ describe("PolicyService", () => {
|
||||
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 () => {
|
||||
const result = await firstValueFrom(
|
||||
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(
|
||||
id: string,
|
||||
organizationId: string,
|
||||
|
@ -124,10 +124,7 @@ export class PolicyService implements InternalPolicyServiceAbstraction {
|
||||
);
|
||||
}
|
||||
|
||||
policyAppliesToActiveUser$(
|
||||
policyType: PolicyType,
|
||||
policyFilter: (policy: Policy) => boolean = (p) => true
|
||||
) {
|
||||
policyAppliesToActiveUser$(policyType: PolicyType, policyFilter?: (policy: Policy) => boolean) {
|
||||
return this.policies$.pipe(
|
||||
concatMap(async (policies) => {
|
||||
const userId = await this.stateService.getUserId();
|
||||
@ -257,12 +254,12 @@ export class PolicyService implements InternalPolicyServiceAbstraction {
|
||||
private async checkPoliciesThatApplyToUser(
|
||||
policies: Policy[],
|
||||
policyType: PolicyType,
|
||||
policyFilter: (policy: Policy) => boolean = (p) => true,
|
||||
policyFilter?: (policy: Policy) => boolean,
|
||||
userId?: string
|
||||
) {
|
||||
const organizations = await this.organizationService.getAll(userId);
|
||||
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));
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user