Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PM-17465] refactor PolicyService.getAll$ to make userId not optional #13031

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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 apps/browser/src/services/families-policy.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class FamiliesPolicyService {
map((organizations) => organizations.find((org) => org.canManageSponsorships)?.id),
switchMap((enterpriseOrgId) =>
this.policyService
.getAll$(PolicyType.FreeFamiliesSponsorshipPolicy)
.getAll$(PolicyType.FreeFamiliesSponsorshipPolicy, userId)
.pipe(
map(
(policies) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { PolicyType } from "@bitwarden/common/admin-console/enums";
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { getUserId } from "@bitwarden/common/auth/services/account.service";
import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";

Expand Down Expand Up @@ -110,14 +111,18 @@
});
}

return this.policyService.getAll$(PolicyType.FreeFamiliesSponsorshipPolicy).pipe(
map((policies) => ({
isFreeFamilyPolicyEnabled: policies.some(
(policy) => policy.organizationId === organizationId && policy.enabled,
return getUserId(this.accountService.activeAccount$).pipe(

Check warning on line 114 in apps/web/src/app/billing/services/free-families-policy.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/free-families-policy.service.ts#L114

Added line #L114 was not covered by tests
switchMap((userId) =>
this.policyService.getAll$(PolicyType.FreeFamiliesSponsorshipPolicy, userId).pipe(
map((policies) => ({

Check warning on line 117 in apps/web/src/app/billing/services/free-families-policy.service.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/services/free-families-policy.service.ts#L116-L117

Added lines #L116 - L117 were not covered by tests
isFreeFamilyPolicyEnabled: policies.some(
(policy) => policy.organizationId === organizationId && policy.enabled,
),
belongToOneEnterpriseOrgs,
belongToMultipleEnterpriseOrgs,
})),
eliykat marked this conversation as resolved.
Show resolved Hide resolved
),
belongToOneEnterpriseOrgs,
belongToMultipleEnterpriseOrgs,
})),
),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class SponsoredFamiliesComponent implements OnInit, OnDestroy {

this.availableSponsorshipOrgs$ = combineLatest([
this.organizationService.organizations$(userId),
this.policyService.getAll$(PolicyType.FreeFamiliesSponsorshipPolicy),
this.policyService.getAll$(PolicyType.FreeFamiliesSponsorshipPolicy, userId),
]).pipe(
map(([organizations, policies]) =>
organizations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import { PolicyType } from "@bitwarden/common/admin-console/enums";
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { getUserId } from "@bitwarden/common/auth/services/account.service";
import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
Expand Down Expand Up @@ -38,6 +40,7 @@
private toastService: ToastService,
private configService: ConfigService,
private policyService: PolicyService,
private accountService: AccountService,

Check warning on line 43 in apps/web/src/app/billing/settings/sponsoring-org-row.component.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/settings/sponsoring-org-row.component.ts#L43

Added line #L43 was not covered by tests
) {}

async ngOnInit() {
Expand All @@ -53,9 +56,11 @@
FeatureFlag.DisableFreeFamiliesSponsorship,
);

const userId = await firstValueFrom(getUserId(this.accountService.activeAccount$));

Check warning on line 59 in apps/web/src/app/billing/settings/sponsoring-org-row.component.ts

View check run for this annotation

Codecov / codecov/patch

apps/web/src/app/billing/settings/sponsoring-org-row.component.ts#L59

Added line #L59 was not covered by tests
eliykat marked this conversation as resolved.
Show resolved Hide resolved

if (this.isFreeFamilyFlagEnabled) {
this.isFreeFamilyPolicyEnabled$ = this.policyService
.getAll$(PolicyType.FreeFamiliesSponsorshipPolicy)
.getAll$(PolicyType.FreeFamiliesSponsorshipPolicy, userId)
.pipe(
map(
(policies) =>
Expand Down
5 changes: 4 additions & 1 deletion libs/angular/src/tools/send/add-edit.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import { PolicyType } from "@bitwarden/common/admin-console/enums";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { getUserId } from "@bitwarden/common/auth/services/account.service";

Check warning on line 19 in libs/angular/src/tools/send/add-edit.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/angular/src/tools/send/add-edit.component.ts#L19

Added line #L19 was not covered by tests
import { BillingAccountProfileStateService } from "@bitwarden/common/billing/abstractions/account/billing-account-profile-state.service";
import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
Expand Down Expand Up @@ -164,8 +165,10 @@
}
});

const userId = await firstValueFrom(getUserId(this.accountService.activeAccount$));

Check warning on line 168 in libs/angular/src/tools/send/add-edit.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/angular/src/tools/send/add-edit.component.ts#L168

Added line #L168 was not covered by tests
eliykat marked this conversation as resolved.
Show resolved Hide resolved

this.policyService
.getAll$(PolicyType.SendOptions)
.getAll$(PolicyType.SendOptions, userId)
.pipe(
map((policies) => policies?.some((p) => p.data.disableHideEmail)),
takeUntil(this.destroy$),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export abstract class PolicyService {
* A policy "applies" if it is enabled and the user is not exempt (e.g. because they are an Owner).
* @param policyType the {@link PolicyType} to search for
*/
getAll$: (policyType: PolicyType, userId?: UserId) => Observable<Policy[]>;
getAll$: (policyType: PolicyType, userId: UserId) => Observable<Policy[]>;

/**
* All {@link Policy} objects for the specified user (from sync data).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { mock, MockProxy } from "jest-mock-extended";
import { firstValueFrom, of } from "rxjs";

import { FakeStateProvider, mockAccountServiceWith } from "../../../../spec";
import { FakeActiveUserState } from "../../../../spec/fake-state";
import { FakeActiveUserState, FakeSingleUserState } from "../../../../spec/fake-state";
import {
OrganizationUserStatusType,
OrganizationUserType,
Expand All @@ -24,6 +24,7 @@ describe("PolicyService", () => {
let stateProvider: FakeStateProvider;
let organizationService: MockProxy<OrganizationService>;
let activeUserState: FakeActiveUserState<Record<PolicyId, PolicyData>>;
let singleUserState: FakeSingleUserState<Record<PolicyId, PolicyData>>;

let policyService: PolicyService;

Expand All @@ -33,6 +34,7 @@ describe("PolicyService", () => {
organizationService = mock<OrganizationService>();

activeUserState = stateProvider.activeUser.getFake(POLICIES);
singleUserState = stateProvider.singleUser.getFake(activeUserState.userId, POLICIES);

const organizations$ = of([
// User
Expand Down Expand Up @@ -295,7 +297,7 @@ describe("PolicyService", () => {

describe("getAll$", () => {
it("returns the specified PolicyTypes", async () => {
activeUserState.nextState(
singleUserState.nextState(
arrayToRecord([
policyData("policy1", "org4", PolicyType.DisablePersonalVaultExport, true),
policyData("policy2", "org1", PolicyType.ActivateAutofill, true),
Expand All @@ -305,7 +307,7 @@ describe("PolicyService", () => {
);

const result = await firstValueFrom(
policyService.getAll$(PolicyType.DisablePersonalVaultExport),
policyService.getAll$(PolicyType.DisablePersonalVaultExport, activeUserState.userId),
);

expect(result).toEqual([
Expand All @@ -331,7 +333,7 @@ describe("PolicyService", () => {
});

it("does not return disabled policies", async () => {
activeUserState.nextState(
singleUserState.nextState(
arrayToRecord([
policyData("policy1", "org4", PolicyType.DisablePersonalVaultExport, true),
policyData("policy2", "org1", PolicyType.ActivateAutofill, true),
Expand All @@ -341,7 +343,7 @@ describe("PolicyService", () => {
);

const result = await firstValueFrom(
policyService.getAll$(PolicyType.DisablePersonalVaultExport),
policyService.getAll$(PolicyType.DisablePersonalVaultExport, activeUserState.userId),
);

expect(result).toEqual([
Expand All @@ -361,7 +363,7 @@ describe("PolicyService", () => {
});

it("does not return policies that do not apply to the user because the user's role is exempt", async () => {
activeUserState.nextState(
singleUserState.nextState(
arrayToRecord([
policyData("policy1", "org4", PolicyType.DisablePersonalVaultExport, true),
policyData("policy2", "org1", PolicyType.ActivateAutofill, true),
Expand All @@ -371,7 +373,7 @@ describe("PolicyService", () => {
);

const result = await firstValueFrom(
policyService.getAll$(PolicyType.DisablePersonalVaultExport),
policyService.getAll$(PolicyType.DisablePersonalVaultExport, activeUserState.userId),
);

expect(result).toEqual([
Expand All @@ -391,7 +393,7 @@ describe("PolicyService", () => {
});

it("does not return policies for organizations that do not use policies", async () => {
activeUserState.nextState(
singleUserState.nextState(
arrayToRecord([
policyData("policy1", "org4", PolicyType.DisablePersonalVaultExport, true),
policyData("policy2", "org1", PolicyType.ActivateAutofill, true),
Expand All @@ -401,7 +403,7 @@ describe("PolicyService", () => {
);

const result = await firstValueFrom(
policyService.getAll$(PolicyType.DisablePersonalVaultExport),
policyService.getAll$(PolicyType.DisablePersonalVaultExport, activeUserState.userId),
);

expect(result).toEqual([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class PolicyService implements InternalPolicyServiceAbstraction {
);
}

getAll$(policyType: PolicyType, userId?: UserId) {
getAll$(policyType: PolicyType, userId: UserId) {
const filteredPolicies$ = this.stateProvider.getUserState$(POLICIES, userId).pipe(
map((policyData) => policyRecordToArray(policyData)),
map((policies) => policies.filter((p) => p.type === policyType)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import { Component, Input, OnInit } from "@angular/core";
import { takeUntilDestroyed } from "@angular/core/rxjs-interop";
import { FormBuilder, ReactiveFormsModule } from "@angular/forms";
import { firstValueFrom, map } from "rxjs";
import { firstValueFrom, map, switchMap } from "rxjs";

Check warning on line 7 in libs/tools/send/send-ui/src/send-form/components/options/send-options.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/tools/send/send-ui/src/send-form/components/options/send-options.component.ts#L7

Added line #L7 was not covered by tests

import { JslibModule } from "@bitwarden/angular/jslib.module";
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import { PolicyType } from "@bitwarden/common/admin-console/enums";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { getUserId } from "@bitwarden/common/auth/services/account.service";

Check warning on line 13 in libs/tools/send/send-ui/src/send-form/components/options/send-options.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/tools/send/send-ui/src/send-form/components/options/send-options.component.ts#L12-L13

Added lines #L12 - L13 were not covered by tests
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { SendView } from "@bitwarden/common/tools/send/models/view/send.view";
import { SendApiService } from "@bitwarden/common/tools/send/services/send-api.service.abstraction";
Expand Down Expand Up @@ -89,13 +91,18 @@
private i18nService: I18nService,
private toastService: ToastService,
private generatorService: CredentialGeneratorService,
private accountService: AccountService,

Check warning on line 94 in libs/tools/send/send-ui/src/send-form/components/options/send-options.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/tools/send/send-ui/src/send-form/components/options/send-options.component.ts#L94

Added line #L94 was not covered by tests
) {
this.sendFormContainer.registerChildForm("sendOptionsForm", this.sendOptionsForm);
this.policyService
.getAll$(PolicyType.SendOptions)

getUserId(this.accountService.activeAccount$)

Check warning on line 98 in libs/tools/send/send-ui/src/send-form/components/options/send-options.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/tools/send/send-ui/src/send-form/components/options/send-options.component.ts#L98

Added line #L98 was not covered by tests
eliykat marked this conversation as resolved.
Show resolved Hide resolved
.pipe(
map((policies) => policies?.some((p) => p.data.disableHideEmail)),
takeUntilDestroyed(),
eliykat marked this conversation as resolved.
Show resolved Hide resolved
switchMap((userId) =>
this.policyService

Check warning on line 102 in libs/tools/send/send-ui/src/send-form/components/options/send-options.component.ts

View check run for this annotation

Codecov / codecov/patch

libs/tools/send/send-ui/src/send-form/components/options/send-options.component.ts#L102

Added line #L102 was not covered by tests
.getAll$(PolicyType.SendOptions, userId)
.pipe(map((policies) => policies?.some((p) => p.data.disableHideEmail))),
),
eliykat marked this conversation as resolved.
Show resolved Hide resolved
)
.subscribe((disableHideEmail) => {
this.disableHideEmail = disableHideEmail;
Expand Down
Loading