mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 00:03:56 +00:00
Move to libs
This commit is contained in:
69
libs/common/spec/services/cipher.service.spec.ts
Normal file
69
libs/common/spec/services/cipher.service.spec.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { Arg, Substitute, SubstituteOf } from "@fluffy-spoon/substitute";
|
||||
|
||||
import { ApiService } from "jslib-common/abstractions/api.service";
|
||||
import { CryptoService } from "jslib-common/abstractions/crypto.service";
|
||||
import { FileUploadService } from "jslib-common/abstractions/fileUpload.service";
|
||||
import { I18nService } from "jslib-common/abstractions/i18n.service";
|
||||
import { LogService } from "jslib-common/abstractions/log.service";
|
||||
import { SearchService } from "jslib-common/abstractions/search.service";
|
||||
import { SettingsService } from "jslib-common/abstractions/settings.service";
|
||||
import { StateService } from "jslib-common/abstractions/state.service";
|
||||
import { Utils } from "jslib-common/misc/utils";
|
||||
import { Cipher } from "jslib-common/models/domain/cipher";
|
||||
import { EncArrayBuffer } from "jslib-common/models/domain/encArrayBuffer";
|
||||
import { EncString } from "jslib-common/models/domain/encString";
|
||||
import { SymmetricCryptoKey } from "jslib-common/models/domain/symmetricCryptoKey";
|
||||
import { CipherService } from "jslib-common/services/cipher.service";
|
||||
|
||||
const ENCRYPTED_TEXT = "This data has been encrypted";
|
||||
const ENCRYPTED_BYTES = new EncArrayBuffer(Utils.fromUtf8ToArray(ENCRYPTED_TEXT).buffer);
|
||||
|
||||
describe("Cipher Service", () => {
|
||||
let cryptoService: SubstituteOf<CryptoService>;
|
||||
let stateService: SubstituteOf<StateService>;
|
||||
let settingsService: SubstituteOf<SettingsService>;
|
||||
let apiService: SubstituteOf<ApiService>;
|
||||
let fileUploadService: SubstituteOf<FileUploadService>;
|
||||
let i18nService: SubstituteOf<I18nService>;
|
||||
let searchService: SubstituteOf<SearchService>;
|
||||
let logService: SubstituteOf<LogService>;
|
||||
|
||||
let cipherService: CipherService;
|
||||
|
||||
beforeEach(() => {
|
||||
cryptoService = Substitute.for<CryptoService>();
|
||||
stateService = Substitute.for<StateService>();
|
||||
settingsService = Substitute.for<SettingsService>();
|
||||
apiService = Substitute.for<ApiService>();
|
||||
fileUploadService = Substitute.for<FileUploadService>();
|
||||
i18nService = Substitute.for<I18nService>();
|
||||
searchService = Substitute.for<SearchService>();
|
||||
logService = Substitute.for<LogService>();
|
||||
|
||||
cryptoService.encryptToBytes(Arg.any(), Arg.any()).resolves(ENCRYPTED_BYTES);
|
||||
cryptoService.encrypt(Arg.any(), Arg.any()).resolves(new EncString(ENCRYPTED_TEXT));
|
||||
|
||||
cipherService = new CipherService(
|
||||
cryptoService,
|
||||
settingsService,
|
||||
apiService,
|
||||
fileUploadService,
|
||||
i18nService,
|
||||
() => searchService,
|
||||
logService,
|
||||
stateService
|
||||
);
|
||||
});
|
||||
|
||||
it("attachments upload encrypted file contents", async () => {
|
||||
const fileName = "filename";
|
||||
const fileData = new Uint8Array(10).buffer;
|
||||
cryptoService.getOrgKey(Arg.any()).resolves(new SymmetricCryptoKey(new Uint8Array(32).buffer));
|
||||
|
||||
await cipherService.saveAttachmentRawWithServer(new Cipher(), fileName, fileData);
|
||||
|
||||
fileUploadService
|
||||
.received(1)
|
||||
.uploadCipherAttachment(Arg.any(), Arg.any(), new EncString(ENCRYPTED_TEXT), ENCRYPTED_BYTES);
|
||||
});
|
||||
});
|
||||
102
libs/common/spec/services/consoleLog.service.spec.ts
Normal file
102
libs/common/spec/services/consoleLog.service.spec.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
import { ConsoleLogService } from "jslib-common/services/consoleLog.service";
|
||||
|
||||
const originalConsole = console;
|
||||
let caughtMessage: any;
|
||||
|
||||
declare let console: any;
|
||||
|
||||
export function interceptConsole(interceptions: any): object {
|
||||
console = {
|
||||
log: function () {
|
||||
// eslint-disable-next-line
|
||||
interceptions.log = arguments;
|
||||
},
|
||||
warn: function () {
|
||||
// eslint-disable-next-line
|
||||
interceptions.warn = arguments;
|
||||
},
|
||||
error: function () {
|
||||
// eslint-disable-next-line
|
||||
interceptions.error = arguments;
|
||||
},
|
||||
};
|
||||
return interceptions;
|
||||
}
|
||||
|
||||
export function restoreConsole() {
|
||||
console = originalConsole;
|
||||
}
|
||||
|
||||
describe("ConsoleLogService", () => {
|
||||
let logService: ConsoleLogService;
|
||||
beforeEach(() => {
|
||||
caughtMessage = {};
|
||||
interceptConsole(caughtMessage);
|
||||
logService = new ConsoleLogService(true);
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
restoreConsole();
|
||||
});
|
||||
|
||||
it("filters messages below the set threshold", () => {
|
||||
logService = new ConsoleLogService(true, () => true);
|
||||
logService.debug("debug");
|
||||
logService.info("info");
|
||||
logService.warning("warning");
|
||||
logService.error("error");
|
||||
|
||||
expect(caughtMessage).toEqual({});
|
||||
});
|
||||
it("only writes debug messages in dev mode", () => {
|
||||
logService = new ConsoleLogService(false);
|
||||
|
||||
logService.debug("debug message");
|
||||
expect(caughtMessage.log).toBeUndefined();
|
||||
});
|
||||
|
||||
it("writes debug/info messages to console.log", () => {
|
||||
logService.debug("this is a debug message");
|
||||
expect(caughtMessage).toMatchObject({
|
||||
log: { "0": "this is a debug message" },
|
||||
});
|
||||
|
||||
logService.info("this is an info message");
|
||||
expect(caughtMessage).toMatchObject({
|
||||
log: { "0": "this is an info message" },
|
||||
});
|
||||
});
|
||||
it("writes warning messages to console.warn", () => {
|
||||
logService.warning("this is a warning message");
|
||||
expect(caughtMessage).toMatchObject({
|
||||
warn: { 0: "this is a warning message" },
|
||||
});
|
||||
});
|
||||
it("writes error messages to console.error", () => {
|
||||
logService.error("this is an error message");
|
||||
expect(caughtMessage).toMatchObject({
|
||||
error: { 0: "this is an error message" },
|
||||
});
|
||||
});
|
||||
|
||||
it("times with output to info", async () => {
|
||||
logService.time();
|
||||
await new Promise((r) => setTimeout(r, 250));
|
||||
const duration = logService.timeEnd();
|
||||
expect(duration[0]).toBe(0);
|
||||
expect(duration[1]).toBeGreaterThan(0);
|
||||
expect(duration[1]).toBeLessThan(500 * 10e6);
|
||||
|
||||
expect(caughtMessage).toEqual(expect.arrayContaining([]));
|
||||
expect(caughtMessage.log.length).toBe(1);
|
||||
expect(caughtMessage.log[0]).toEqual(expect.stringMatching(/^default: \d+\.?\d*ms$/));
|
||||
});
|
||||
|
||||
it("filters time output", async () => {
|
||||
logService = new ConsoleLogService(true, () => true);
|
||||
logService.time();
|
||||
logService.timeEnd();
|
||||
|
||||
expect(caughtMessage).toEqual({});
|
||||
});
|
||||
});
|
||||
209
libs/common/spec/services/export.service.spec.ts
Normal file
209
libs/common/spec/services/export.service.spec.ts
Normal file
@@ -0,0 +1,209 @@
|
||||
import { Arg, Substitute, SubstituteOf } from "@fluffy-spoon/substitute";
|
||||
|
||||
import { ApiService } from "jslib-common/abstractions/api.service";
|
||||
import { CipherService } from "jslib-common/abstractions/cipher.service";
|
||||
import { CryptoService } from "jslib-common/abstractions/crypto.service";
|
||||
import { CryptoFunctionService } from "jslib-common/abstractions/cryptoFunction.service";
|
||||
import { FolderService } from "jslib-common/abstractions/folder.service";
|
||||
import { CipherType } from "jslib-common/enums/cipherType";
|
||||
import { KdfType } from "jslib-common/enums/kdfType";
|
||||
import { Utils } from "jslib-common/misc/utils";
|
||||
import { Cipher } from "jslib-common/models/domain/cipher";
|
||||
import { EncString } from "jslib-common/models/domain/encString";
|
||||
import { Login } from "jslib-common/models/domain/login";
|
||||
import { CipherWithIdExport as CipherExport } from "jslib-common/models/export/cipherWithIdsExport";
|
||||
import { CipherView } from "jslib-common/models/view/cipherView";
|
||||
import { LoginView } from "jslib-common/models/view/loginView";
|
||||
import { ExportService } from "jslib-common/services/export.service";
|
||||
|
||||
import { BuildTestObject, GetUniqueString } from "../utils";
|
||||
|
||||
const UserCipherViews = [
|
||||
generateCipherView(false),
|
||||
generateCipherView(false),
|
||||
generateCipherView(true),
|
||||
];
|
||||
|
||||
const UserCipherDomains = [
|
||||
generateCipherDomain(false),
|
||||
generateCipherDomain(false),
|
||||
generateCipherDomain(true),
|
||||
];
|
||||
|
||||
function generateCipherView(deleted: boolean) {
|
||||
return BuildTestObject(
|
||||
{
|
||||
id: GetUniqueString("id"),
|
||||
notes: GetUniqueString("notes"),
|
||||
type: CipherType.Login,
|
||||
login: BuildTestObject<LoginView>(
|
||||
{
|
||||
username: GetUniqueString("username"),
|
||||
password: GetUniqueString("password"),
|
||||
},
|
||||
LoginView
|
||||
),
|
||||
collectionIds: null,
|
||||
deletedDate: deleted ? new Date() : null,
|
||||
},
|
||||
CipherView
|
||||
);
|
||||
}
|
||||
|
||||
function generateCipherDomain(deleted: boolean) {
|
||||
return BuildTestObject(
|
||||
{
|
||||
id: GetUniqueString("id"),
|
||||
notes: new EncString(GetUniqueString("notes")),
|
||||
type: CipherType.Login,
|
||||
login: BuildTestObject<Login>(
|
||||
{
|
||||
username: new EncString(GetUniqueString("username")),
|
||||
password: new EncString(GetUniqueString("password")),
|
||||
},
|
||||
Login
|
||||
),
|
||||
collectionIds: null,
|
||||
deletedDate: deleted ? new Date() : null,
|
||||
},
|
||||
Cipher
|
||||
);
|
||||
}
|
||||
|
||||
function expectEqualCiphers(ciphers: CipherView[] | Cipher[], jsonResult: string) {
|
||||
const actual = JSON.stringify(JSON.parse(jsonResult).items);
|
||||
const items: CipherExport[] = [];
|
||||
ciphers.forEach((c: CipherView | Cipher) => {
|
||||
const item = new CipherExport();
|
||||
item.build(c);
|
||||
items.push(item);
|
||||
});
|
||||
|
||||
expect(actual).toEqual(JSON.stringify(items));
|
||||
}
|
||||
|
||||
describe("ExportService", () => {
|
||||
let exportService: ExportService;
|
||||
let apiService: SubstituteOf<ApiService>;
|
||||
let cryptoFunctionService: SubstituteOf<CryptoFunctionService>;
|
||||
let cipherService: SubstituteOf<CipherService>;
|
||||
let folderService: SubstituteOf<FolderService>;
|
||||
let cryptoService: SubstituteOf<CryptoService>;
|
||||
|
||||
beforeEach(() => {
|
||||
apiService = Substitute.for<ApiService>();
|
||||
cryptoFunctionService = Substitute.for<CryptoFunctionService>();
|
||||
cipherService = Substitute.for<CipherService>();
|
||||
folderService = Substitute.for<FolderService>();
|
||||
cryptoService = Substitute.for<CryptoService>();
|
||||
|
||||
folderService.getAllDecrypted().resolves([]);
|
||||
folderService.getAll().resolves([]);
|
||||
|
||||
exportService = new ExportService(
|
||||
folderService,
|
||||
cipherService,
|
||||
apiService,
|
||||
cryptoService,
|
||||
cryptoFunctionService
|
||||
);
|
||||
});
|
||||
|
||||
it("exports unecrypted user ciphers", async () => {
|
||||
cipherService.getAllDecrypted().resolves(UserCipherViews.slice(0, 1));
|
||||
|
||||
const actual = await exportService.getExport("json");
|
||||
|
||||
expectEqualCiphers(UserCipherViews.slice(0, 1), actual);
|
||||
});
|
||||
|
||||
it("exports encrypted json user ciphers", async () => {
|
||||
cipherService.getAll().resolves(UserCipherDomains.slice(0, 1));
|
||||
|
||||
const actual = await exportService.getExport("encrypted_json");
|
||||
|
||||
expectEqualCiphers(UserCipherDomains.slice(0, 1), actual);
|
||||
});
|
||||
|
||||
it("does not unecrypted export trashed user items", async () => {
|
||||
cipherService.getAllDecrypted().resolves(UserCipherViews);
|
||||
|
||||
const actual = await exportService.getExport("json");
|
||||
|
||||
expectEqualCiphers(UserCipherViews.slice(0, 2), actual);
|
||||
});
|
||||
|
||||
it("does not encrypted export trashed user items", async () => {
|
||||
cipherService.getAll().resolves(UserCipherDomains);
|
||||
|
||||
const actual = await exportService.getExport("encrypted_json");
|
||||
|
||||
expectEqualCiphers(UserCipherDomains.slice(0, 2), actual);
|
||||
});
|
||||
|
||||
describe("password protected export", () => {
|
||||
let exportString: string;
|
||||
let exportObject: any;
|
||||
let mac: SubstituteOf<EncString>;
|
||||
let data: SubstituteOf<EncString>;
|
||||
const password = "password";
|
||||
const salt = "salt";
|
||||
|
||||
describe("export json object", () => {
|
||||
beforeEach(async () => {
|
||||
mac = Substitute.for<EncString>();
|
||||
data = Substitute.for<EncString>();
|
||||
|
||||
mac.encryptedString.returns("mac");
|
||||
data.encryptedString.returns("encData");
|
||||
|
||||
jest.spyOn(Utils, "fromBufferToB64").mockReturnValue(salt);
|
||||
cipherService.getAllDecrypted().resolves(UserCipherViews.slice(0, 1));
|
||||
|
||||
exportString = await exportService.getPasswordProtectedExport(password);
|
||||
exportObject = JSON.parse(exportString);
|
||||
});
|
||||
|
||||
it("specifies it is encrypted", () => {
|
||||
expect(exportObject.encrypted).toBe(true);
|
||||
});
|
||||
|
||||
it("specifies it's password protected", () => {
|
||||
expect(exportObject.passwordProtected).toBe(true);
|
||||
});
|
||||
|
||||
it("specifies salt", () => {
|
||||
expect(exportObject.salt).toEqual("salt");
|
||||
});
|
||||
|
||||
it("specifies kdfIterations", () => {
|
||||
expect(exportObject.kdfIterations).toEqual(100000);
|
||||
});
|
||||
|
||||
it("has kdfType", () => {
|
||||
expect(exportObject.kdfType).toEqual(KdfType.PBKDF2_SHA256);
|
||||
});
|
||||
|
||||
it("has a mac property", async () => {
|
||||
cryptoService.encrypt(Arg.any(), Arg.any()).resolves(mac);
|
||||
exportString = await exportService.getPasswordProtectedExport(password);
|
||||
exportObject = JSON.parse(exportString);
|
||||
|
||||
expect(exportObject.encKeyValidation_DO_NOT_EDIT).toEqual(mac.encryptedString);
|
||||
});
|
||||
|
||||
it("has data property", async () => {
|
||||
cryptoService.encrypt(Arg.any(), Arg.any()).resolves(data);
|
||||
exportString = await exportService.getPasswordProtectedExport(password);
|
||||
exportObject = JSON.parse(exportString);
|
||||
|
||||
expect(exportObject.data).toEqual(data.encryptedString);
|
||||
});
|
||||
|
||||
it("encrypts the data property", async () => {
|
||||
const unencrypted = await exportService.getExport();
|
||||
expect(exportObject.data).not.toEqual(unencrypted);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
72
libs/common/spec/services/import.service.spec.ts
Normal file
72
libs/common/spec/services/import.service.spec.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import Substitute, { SubstituteOf } from "@fluffy-spoon/substitute";
|
||||
|
||||
import { ApiService } from "jslib-common/abstractions/api.service";
|
||||
import { CipherService } from "jslib-common/abstractions/cipher.service";
|
||||
import { CollectionService } from "jslib-common/abstractions/collection.service";
|
||||
import { CryptoService } from "jslib-common/abstractions/crypto.service";
|
||||
import { FolderService } from "jslib-common/abstractions/folder.service";
|
||||
import { I18nService } from "jslib-common/abstractions/i18n.service";
|
||||
import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service";
|
||||
import { BitwardenPasswordProtectedImporter } from "jslib-common/importers/bitwardenPasswordProtectedImporter";
|
||||
import { Importer } from "jslib-common/importers/importer";
|
||||
import { Utils } from "jslib-common/misc/utils";
|
||||
import { ImportService } from "jslib-common/services/import.service";
|
||||
|
||||
describe("ImportService", () => {
|
||||
let importService: ImportService;
|
||||
let cipherService: SubstituteOf<CipherService>;
|
||||
let folderService: SubstituteOf<FolderService>;
|
||||
let apiService: SubstituteOf<ApiService>;
|
||||
let i18nService: SubstituteOf<I18nService>;
|
||||
let collectionService: SubstituteOf<CollectionService>;
|
||||
let platformUtilsService: SubstituteOf<PlatformUtilsService>;
|
||||
let cryptoService: SubstituteOf<CryptoService>;
|
||||
|
||||
beforeEach(() => {
|
||||
cipherService = Substitute.for<CipherService>();
|
||||
folderService = Substitute.for<FolderService>();
|
||||
apiService = Substitute.for<ApiService>();
|
||||
i18nService = Substitute.for<I18nService>();
|
||||
collectionService = Substitute.for<CollectionService>();
|
||||
platformUtilsService = Substitute.for<PlatformUtilsService>();
|
||||
cryptoService = Substitute.for<CryptoService>();
|
||||
|
||||
importService = new ImportService(
|
||||
cipherService,
|
||||
folderService,
|
||||
apiService,
|
||||
i18nService,
|
||||
collectionService,
|
||||
platformUtilsService,
|
||||
cryptoService
|
||||
);
|
||||
});
|
||||
|
||||
describe("getImporterInstance", () => {
|
||||
describe("Get bitPasswordProtected importer", () => {
|
||||
let importer: Importer;
|
||||
const organizationId = Utils.newGuid();
|
||||
const password = Utils.newGuid();
|
||||
|
||||
beforeEach(() => {
|
||||
importer = importService.getImporter(
|
||||
"bitwardenpasswordprotected",
|
||||
organizationId,
|
||||
password
|
||||
);
|
||||
});
|
||||
|
||||
it("returns an instance of BitwardenPasswordProtectedImporter", () => {
|
||||
expect(importer).toBeInstanceOf(BitwardenPasswordProtectedImporter);
|
||||
});
|
||||
|
||||
it("has the appropriate organization Id", () => {
|
||||
expect(importer.organizationId).toEqual(organizationId);
|
||||
});
|
||||
|
||||
it("has the appropriate password", () => {
|
||||
expect(Object.entries(importer)).toEqual(expect.arrayContaining([["password", password]]));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
84
libs/common/spec/services/stateMigration.service.ts
Normal file
84
libs/common/spec/services/stateMigration.service.ts
Normal file
@@ -0,0 +1,84 @@
|
||||
import { Arg, Substitute, SubstituteOf } from "@fluffy-spoon/substitute";
|
||||
|
||||
import { StorageService } from "jslib-common/abstractions/storage.service";
|
||||
import { StateVersion } from "jslib-common/enums/stateVersion";
|
||||
import { StateFactory } from "jslib-common/factories/stateFactory";
|
||||
import { Account } from "jslib-common/models/domain/account";
|
||||
import { GlobalState } from "jslib-common/models/domain/globalState";
|
||||
import { StateMigrationService } from "jslib-common/services/stateMigration.service";
|
||||
|
||||
const userId = "USER_ID";
|
||||
|
||||
describe("State Migration Service", () => {
|
||||
let storageService: SubstituteOf<StorageService>;
|
||||
let secureStorageService: SubstituteOf<StorageService>;
|
||||
let stateFactory: SubstituteOf<StateFactory>;
|
||||
|
||||
let stateMigrationService: StateMigrationService;
|
||||
|
||||
beforeEach(() => {
|
||||
storageService = Substitute.for<StorageService>();
|
||||
secureStorageService = Substitute.for<StorageService>();
|
||||
stateFactory = Substitute.for<StateFactory>();
|
||||
|
||||
stateMigrationService = new StateMigrationService(
|
||||
storageService,
|
||||
secureStorageService,
|
||||
stateFactory
|
||||
);
|
||||
});
|
||||
|
||||
describe("StateVersion 3 to 4 migration", async () => {
|
||||
beforeEach(() => {
|
||||
const globalVersion3: Partial<GlobalState> = {
|
||||
stateVersion: StateVersion.Three,
|
||||
};
|
||||
|
||||
storageService.get("global", Arg.any()).resolves(globalVersion3);
|
||||
storageService.get("authenticatedAccounts", Arg.any()).resolves([userId]);
|
||||
});
|
||||
|
||||
it("clears everBeenUnlocked", async () => {
|
||||
const accountVersion3: Account = {
|
||||
profile: {
|
||||
apiKeyClientId: null,
|
||||
convertAccountToKeyConnector: null,
|
||||
email: "EMAIL",
|
||||
emailVerified: true,
|
||||
everBeenUnlocked: true,
|
||||
hasPremiumPersonally: false,
|
||||
kdfIterations: 100000,
|
||||
kdfType: 0,
|
||||
keyHash: "KEY_HASH",
|
||||
lastSync: "LAST_SYNC",
|
||||
userId: userId,
|
||||
usesKeyConnector: false,
|
||||
forcePasswordReset: false,
|
||||
},
|
||||
};
|
||||
|
||||
const expectedAccountVersion4: Account = {
|
||||
profile: {
|
||||
...accountVersion3.profile,
|
||||
},
|
||||
};
|
||||
delete expectedAccountVersion4.profile.everBeenUnlocked;
|
||||
|
||||
storageService.get(userId, Arg.any()).resolves(accountVersion3);
|
||||
|
||||
await stateMigrationService.migrate();
|
||||
|
||||
storageService.received(1).save(userId, expectedAccountVersion4, Arg.any());
|
||||
});
|
||||
|
||||
it("updates StateVersion number", async () => {
|
||||
await stateMigrationService.migrate();
|
||||
|
||||
storageService.received(1).save(
|
||||
"global",
|
||||
Arg.is((globals: GlobalState) => globals.stateVersion === StateVersion.Four),
|
||||
Arg.any()
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user