1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-12 14:23:32 +00:00

[ADR-0006][AC-319] Migrate all tests to use jest mock instead of substitute (#6520)

Standardize on using jest mock instead of having two mocking frameworks which can be confusing.
This commit is contained in:
Oscar Hinton
2023-10-17 19:02:33 +02:00
committed by GitHub
parent 5cacd79d8c
commit ffb67be0a2
17 changed files with 198 additions and 208 deletions

View File

@@ -1,10 +1,9 @@
// eslint-disable-next-line no-restricted-imports
import { Arg, Substitute, SubstituteOf } from "@fluffy-spoon/substitute";
import { mock, MockProxy } from "jest-mock-extended";
import { EncryptService } from "@bitwarden/common/platform/abstractions/encrypt.service";
import { Utils } from "@bitwarden/common/platform/misc/utils";
import { EncString } from "@bitwarden/common/platform/models/domain/enc-string";
import { SymmetricCryptoKey } from "@bitwarden/common/platform/models/domain/symmetric-crypto-key";
import { EncryptServiceImplementation } from "@bitwarden/common/platform/services/cryptography/encrypt.service.implementation";
import BrowserLocalStorageService from "./browser-local-storage.service";
import BrowserMemoryStorageService from "./browser-memory-storage.service";
@@ -12,8 +11,8 @@ import { KeyGenerationService } from "./key-generation.service";
import { LocalBackedSessionStorageService } from "./local-backed-session-storage.service";
describe("Browser Session Storage Service", () => {
let encryptService: SubstituteOf<EncryptServiceImplementation>;
let keyGenerationService: SubstituteOf<KeyGenerationService>;
let encryptService: MockProxy<EncryptService>;
let keyGenerationService: MockProxy<KeyGenerationService>;
let cache: Map<string, any>;
const testObj = { a: 1, b: 2 };
@@ -28,8 +27,8 @@ describe("Browser Session Storage Service", () => {
let sut: LocalBackedSessionStorageService;
beforeEach(() => {
encryptService = Substitute.for();
keyGenerationService = Substitute.for();
encryptService = mock<EncryptService>();
keyGenerationService = mock<KeyGenerationService>();
sut = new LocalBackedSessionStorageService(encryptService, keyGenerationService);
@@ -138,6 +137,8 @@ describe("Browser Session Storage Service", () => {
jest.spyOn(sessionStorage, "get").mockResolvedValue(null);
jest.spyOn(localStorage, "save").mockResolvedValue();
jest.spyOn(sessionStorage, "save").mockResolvedValue();
encryptService.encrypt.mockResolvedValue(mockEnc("{}"));
});
it("should remove key from cache if value is null", async () => {
@@ -205,7 +206,7 @@ describe("Browser Session Storage Service", () => {
describe("new key creation", () => {
beforeEach(() => {
jest.spyOn(sessionStorage, "get").mockResolvedValue(null);
keyGenerationService.makeEphemeralKey().resolves(key);
keyGenerationService.makeEphemeralKey.mockResolvedValue(key);
jest.spyOn(sut, "setSessionEncKey").mockResolvedValue();
});
@@ -213,7 +214,7 @@ describe("Browser Session Storage Service", () => {
const result = await sut.getSessionEncKey();
expect(result).toStrictEqual(key);
keyGenerationService.received(1).makeEphemeralKey();
expect(keyGenerationService.makeEphemeralKey).toBeCalledTimes(1);
});
it("should store a symmetric crypto key if it makes one", async () => {
@@ -244,19 +245,23 @@ describe("Browser Session Storage Service", () => {
});
it("should decrypt returned sessions", async () => {
encryptService.decryptToUtf8(encSession, key).resolves(decryptedSession);
encryptService.decryptToUtf8
.calledWith(expect.anything(), key)
.mockResolvedValue(decryptedSession);
await sut.getLocalSession(key);
encryptService.received(1).decryptToUtf8(encSession, key);
expect(encryptService.decryptToUtf8).toHaveBeenNthCalledWith(1, encSession, key);
});
it("should parse session", async () => {
encryptService.decryptToUtf8(encSession, key).resolves(decryptedSession);
encryptService.decryptToUtf8
.calledWith(expect.anything(), key)
.mockResolvedValue(decryptedSession);
const result = await sut.getLocalSession(key);
expect(result).toEqual(session);
});
it("should remove state if decryption fails", async () => {
encryptService.decryptToUtf8(Arg.any(), Arg.any()).resolves(null);
encryptService.decryptToUtf8.mockResolvedValue(null);
const setSessionEncKeySpy = jest.spyOn(sut, "setSessionEncKey").mockResolvedValue();
const removeLocalSessionSpy = jest.spyOn(localStorage, "remove").mockResolvedValue();
@@ -274,15 +279,15 @@ describe("Browser Session Storage Service", () => {
const testJSON = JSON.stringify(testSession);
it("should encrypt a stringified session", async () => {
encryptService.encrypt(Arg.any(), Arg.any()).mimicks(mockEnc);
encryptService.encrypt.mockImplementation(mockEnc);
jest.spyOn(localStorage, "save").mockResolvedValue();
await sut.setLocalSession(testSession, key);
encryptService.received(1).encrypt(testJSON, key);
expect(encryptService.encrypt).toHaveBeenNthCalledWith(1, testJSON, key);
});
it("should remove local session if null", async () => {
encryptService.encrypt(Arg.any(), Arg.any()).resolves(null);
encryptService.encrypt.mockResolvedValue(null);
const spy = jest.spyOn(localStorage, "remove").mockResolvedValue();
await sut.setLocalSession(null, key);
@@ -290,7 +295,7 @@ describe("Browser Session Storage Service", () => {
});
it("should save encrypted string", async () => {
encryptService.encrypt(Arg.any(), Arg.any()).mimicks(mockEnc);
encryptService.encrypt.mockImplementation(mockEnc);
const spy = jest.spyOn(localStorage, "save").mockResolvedValue();
await sut.setLocalSession(testSession, key);

View File

@@ -1,8 +1,6 @@
import { NO_ERRORS_SCHEMA } from "@angular/core";
import { ComponentFixture, TestBed } from "@angular/core/testing";
import { ActivatedRoute } from "@angular/router";
// eslint-disable-next-line no-restricted-imports
import { Substitute } from "@fluffy-spoon/substitute";
import { mock, MockProxy } from "jest-mock-extended";
import { I18nPipe } from "@bitwarden/angular/platform/pipes/i18n.pipe";
@@ -28,15 +26,15 @@ describe("GeneratorComponent", () => {
providers: [
{
provide: PasswordGenerationServiceAbstraction,
useClass: Substitute.for<PasswordGenerationServiceAbstraction>(),
useValue: mock<PasswordGenerationServiceAbstraction>(),
},
{
provide: UsernameGenerationServiceAbstraction,
useClass: Substitute.for<UsernameGenerationServiceAbstraction>(),
useValue: mock<UsernameGenerationServiceAbstraction>(),
},
{
provide: StateService,
useClass: Substitute.for<StateService>(),
useValue: mock<StateService>(),
},
{
provide: PlatformUtilsService,
@@ -44,15 +42,15 @@ describe("GeneratorComponent", () => {
},
{
provide: I18nService,
useClass: Substitute.for<I18nService>(),
useValue: mock<I18nService>(),
},
{
provide: ActivatedRoute,
useClass: Substitute.for<ActivatedRoute>(),
useValue: mock<ActivatedRoute>(),
},
{
provide: LogService,
useClass: Substitute.for<LogService>(),
useValue: mock<LogService>(),
},
],
schemas: [NO_ERRORS_SCHEMA],

View File

@@ -15,8 +15,7 @@
"@bitwarden/web-vault/*",
"src/**/*",
"bitwarden_license"
],
"paths": ["@fluffy-spoon/substitute"]
]
}
]
}