Skip to content

Commit

Permalink
wire vNextOrganizationService in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BTreston committed Jan 15, 2025
1 parent 55870c8 commit 57ad18f
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 45 deletions.
14 changes: 11 additions & 3 deletions bitwarden_license/bit-common/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@ module.exports = {
...sharedConfig,
displayName: "bit-common tests",
testEnvironment: "jsdom",
moduleNameMapper: pathsToModuleNameMapper(compilerOptions?.paths || {}, {
prefix: "<rootDir>/",
}),
moduleNameMapper: pathsToModuleNameMapper(
{
"@bitwarden/common/spec": ["../../libs/common/spec"],
"@bitwarden/common": ["../../libs/common/src/*"],
"@bitwarden/admin-console/common": ["<rootDir>/libs/admin-console/src/common"],
...(compilerOptions?.paths ?? {}),
},
{
prefix: "<rootDir>/",
},
),
setupFilesAfterEnv: ["<rootDir>/test.setup.ts"],
transformIgnorePatterns: ["node_modules/(?!(.*\\.mjs$|@angular|rxjs|@bitwarden))"],
moduleFileExtensions: ["ts", "js", "html", "mjs"],
Expand Down
14 changes: 11 additions & 3 deletions bitwarden_license/bit-web/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ module.exports = {
...sharedConfig,
preset: "jest-preset-angular",
setupFilesAfterEnv: ["../../apps/web/test.setup.ts"],
moduleNameMapper: pathsToModuleNameMapper(compilerOptions?.paths || {}, {
prefix: "<rootDir>/",
}),
moduleNameMapper: pathsToModuleNameMapper(
{
"@bitwarden/common/spec": ["../../libs/common/spec"],
"@bitwarden/common": ["../../libs/common/src/*"],
"@bitwarden/admin-console/common": ["<rootDir>/libs/admin-console/src/common"],
...(compilerOptions?.paths ?? {}),
},
{
prefix: "<rootDir>/",
},
),
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import { TestBed } from "@angular/core/testing";
import { Router } from "@angular/router";
import { RouterTestingModule } from "@angular/router/testing";
import { MockProxy, mock } from "jest-mock-extended";
import { of } from "rxjs";

import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
import { vNextOrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/vnext.organization.service.abstraction";
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { Utils } from "@bitwarden/common/platform/misc/utils";
import { FakeAccountService, mockAccountServiceWith } from "@bitwarden/common/spec";
import { UserId } from "@bitwarden/common/types/guid";
import { ToastService } from "@bitwarden/components";

import { RouterService } from "../../../../../../../apps/web/src/app/core/router.service";
Expand All @@ -26,12 +31,14 @@ export class GuardedRouteTestComponent {}
export class RedirectTestComponent {}

describe("Project Redirect Guard", () => {
let organizationService: MockProxy<OrganizationService>;
let organizationService: MockProxy<vNextOrganizationService>;
let routerService: MockProxy<RouterService>;
let projectServiceMock: MockProxy<ProjectService>;
let i18nServiceMock: MockProxy<I18nService>;
let toastService: MockProxy<ToastService>;
let router: Router;
let accountService: FakeAccountService;
const userId = Utils.newGuid() as UserId;

const smOrg1 = { id: "123", canAccessSecretsManager: true } as Organization;
const projectView = {
Expand All @@ -45,11 +52,12 @@ describe("Project Redirect Guard", () => {
} as ProjectView;

beforeEach(async () => {
organizationService = mock<OrganizationService>();
organizationService = mock<vNextOrganizationService>();
routerService = mock<RouterService>();
projectServiceMock = mock<ProjectService>();
i18nServiceMock = mock<I18nService>();
toastService = mock<ToastService>();
accountService = mockAccountServiceWith(userId);

TestBed.configureTestingModule({
imports: [
Expand All @@ -70,7 +78,8 @@ describe("Project Redirect Guard", () => {
]),
],
providers: [
{ provide: OrganizationService, useValue: organizationService },
{ provide: vNextOrganizationService, useValue: organizationService },
{ provide: AccountService, useValue: accountService },
{ provide: RouterService, useValue: routerService },
{ provide: ProjectService, useValue: projectServiceMock },
{ provide: I18nService, useValue: i18nServiceMock },
Expand All @@ -83,7 +92,7 @@ describe("Project Redirect Guard", () => {

it("redirects to sm/{orgId}/projects/{projectId} if project exists", async () => {
// Arrange
organizationService.getAll.mockResolvedValue([smOrg1]);
organizationService.organizations$.mockReturnValue(of([smOrg1]));
projectServiceMock.getByProjectId.mockReturnValue(Promise.resolve(projectView));

// Act
Expand All @@ -95,7 +104,7 @@ describe("Project Redirect Guard", () => {

it("redirects to sm/projects if project does not exist", async () => {
// Arrange
organizationService.getAll.mockResolvedValue([smOrg1]);
organizationService.organizations$.mockReturnValue(of([smOrg1]));

// Act
await router.navigateByUrl("sm/123/projects/124");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ import { TestBed } from "@angular/core/testing";
import { Router } from "@angular/router";
import { RouterTestingModule } from "@angular/router/testing";
import { MockProxy, mock } from "jest-mock-extended";
import { of } from "rxjs";

import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
import { vNextOrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/vnext.organization.service.abstraction";
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { Utils } from "@bitwarden/common/platform/misc/utils";
import { FakeAccountService, mockAccountServiceWith } from "@bitwarden/common/spec";
import { UserId } from "@bitwarden/common/types/guid";
import { ToastService } from "@bitwarden/components";

import { RouterService } from "../../../../../../../../clients/apps/web/src/app/core/router.service";
Expand All @@ -26,12 +31,14 @@ export class GuardedRouteTestComponent {}
export class RedirectTestComponent {}

describe("Service account Redirect Guard", () => {
let organizationService: MockProxy<OrganizationService>;
let organizationService: MockProxy<vNextOrganizationService>;
let routerService: MockProxy<RouterService>;
let serviceAccountServiceMock: MockProxy<ServiceAccountService>;
let i18nServiceMock: MockProxy<I18nService>;
let toastService: MockProxy<ToastService>;
let router: Router;
let accountService: FakeAccountService;
const userId = Utils.newGuid() as UserId;

const smOrg1 = { id: "123", canAccessSecretsManager: true } as Organization;
const serviceAccountView = {
Expand All @@ -41,11 +48,12 @@ describe("Service account Redirect Guard", () => {
} as ServiceAccountView;

beforeEach(async () => {
organizationService = mock<OrganizationService>();
organizationService = mock<vNextOrganizationService>();
routerService = mock<RouterService>();
serviceAccountServiceMock = mock<ServiceAccountService>();
i18nServiceMock = mock<I18nService>();
toastService = mock<ToastService>();
accountService = mockAccountServiceWith(userId);

TestBed.configureTestingModule({
imports: [
Expand All @@ -66,7 +74,8 @@ describe("Service account Redirect Guard", () => {
]),
],
providers: [
{ provide: OrganizationService, useValue: organizationService },
{ provide: vNextOrganizationService, useValue: organizationService },
{ provide: AccountService, useValue: accountService },
{ provide: RouterService, useValue: routerService },
{ provide: ServiceAccountService, useValue: serviceAccountServiceMock },
{ provide: I18nService, useValue: i18nServiceMock },
Expand All @@ -79,7 +88,7 @@ describe("Service account Redirect Guard", () => {

it("redirects to sm/{orgId}/machine-accounts/{serviceAccountId} if machine account exists", async () => {
// Arrange
organizationService.getAll.mockResolvedValue([smOrg1]);
organizationService.organizations$.mockReturnValue(of([smOrg1]));
serviceAccountServiceMock.getByServiceAccountId.mockReturnValue(
Promise.resolve(serviceAccountView),
);
Expand All @@ -93,7 +102,7 @@ describe("Service account Redirect Guard", () => {

it("redirects to sm/machine-accounts if machine account does not exist", async () => {
// Arrange
organizationService.getAll.mockResolvedValue([smOrg1]);
organizationService.organizations$.mockReturnValue(of([smOrg1]));

// Act
await router.navigateByUrl("sm/123/machine-accounts/124");
Expand Down
Loading

0 comments on commit 57ad18f

Please sign in to comment.