2022-02-07 16:33:10 +01:00
|
|
|
import { Arg, Substitute, SubstituteOf } from "@fluffy-spoon/substitute";
|
2020-12-30 22:08:02 +01:00
|
|
|
|
2021-06-03 18:58:57 +02:00
|
|
|
import { ApiService } from "jslib-common/abstractions/api.service";
|
|
|
|
import { CipherService } from "jslib-common/abstractions/cipher.service";
|
|
|
|
import { CryptoService } from "jslib-common/abstractions/crypto.service";
|
2022-02-07 16:33:10 +01:00
|
|
|
import { CryptoFunctionService } from "jslib-common/abstractions/cryptoFunction.service";
|
2021-06-03 18:58:57 +02:00
|
|
|
import { FolderService } from "jslib-common/abstractions/folder.service";
|
2020-12-30 22:08:02 +01:00
|
|
|
|
2021-06-03 18:58:57 +02:00
|
|
|
import { ExportService } from "jslib-common/services/export.service";
|
2020-12-30 22:08:02 +01:00
|
|
|
|
2021-06-03 18:58:57 +02:00
|
|
|
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 { CipherWithIds as CipherExport } from "jslib-common/models/export/cipherWithIds";
|
2020-12-30 22:08:02 +01:00
|
|
|
|
2021-06-03 18:58:57 +02:00
|
|
|
import { CipherType } from "jslib-common/enums/cipherType";
|
2022-02-07 16:33:10 +01:00
|
|
|
import { KdfType } from "jslib-common/enums/kdfType";
|
|
|
|
|
|
|
|
import { Utils } from "jslib-common/misc/utils";
|
2021-06-03 18:58:57 +02:00
|
|
|
import { CipherView } from "jslib-common/models/view/cipherView";
|
|
|
|
import { LoginView } from "jslib-common/models/view/loginView";
|
2020-12-30 22:08:02 +01:00
|
|
|
|
|
|
|
import { BuildTestObject, GetUniqueString } from "../../utils";
|
|
|
|
|
|
|
|
const UserCipherViews = [
|
|
|
|
generateCipherView(false),
|
|
|
|
generateCipherView(false),
|
2021-02-08 21:11:44 +01:00
|
|
|
generateCipherView(true),
|
2020-12-30 22:08:02 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
const UserCipherDomains = [
|
|
|
|
generateCipherDomain(false),
|
|
|
|
generateCipherDomain(false),
|
2021-02-08 21:11:44 +01:00
|
|
|
generateCipherDomain(true),
|
2020-12-30 22:08:02 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
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"),
|
2021-04-21 02:16:19 +02:00
|
|
|
notes: new EncString(GetUniqueString("notes")),
|
2020-12-30 22:08:02 +01:00
|
|
|
type: CipherType.Login,
|
|
|
|
login: BuildTestObject<Login>(
|
|
|
|
{
|
2021-04-21 02:16:19 +02:00
|
|
|
username: new EncString(GetUniqueString("username")),
|
|
|
|
password: new EncString(GetUniqueString("password")),
|
2020-12-30 22:08:02 +01:00
|
|
|
},
|
|
|
|
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);
|
|
|
|
});
|
2021-12-16 13:36:21 +01:00
|
|
|
|
2020-12-30 22:08:02 +01:00
|
|
|
expect(actual).toEqual(JSON.stringify(items));
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("ExportService", () => {
|
|
|
|
let exportService: ExportService;
|
|
|
|
let apiService: SubstituteOf<ApiService>;
|
2022-02-07 16:33:10 +01:00
|
|
|
let cryptoFunctionService: SubstituteOf<CryptoFunctionService>;
|
2020-12-30 22:08:02 +01:00
|
|
|
let cipherService: SubstituteOf<CipherService>;
|
|
|
|
let folderService: SubstituteOf<FolderService>;
|
2021-05-13 02:58:59 +02:00
|
|
|
let cryptoService: SubstituteOf<CryptoService>;
|
2020-12-30 22:08:02 +01:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
apiService = Substitute.for<ApiService>();
|
2022-02-07 16:33:10 +01:00
|
|
|
cryptoFunctionService = Substitute.for<CryptoFunctionService>();
|
2020-12-30 22:08:02 +01:00
|
|
|
cipherService = Substitute.for<CipherService>();
|
|
|
|
folderService = Substitute.for<FolderService>();
|
2021-05-13 02:58:59 +02:00
|
|
|
cryptoService = Substitute.for<CryptoService>();
|
2020-12-30 22:08:02 +01:00
|
|
|
|
|
|
|
folderService.getAllDecrypted().resolves([]);
|
|
|
|
folderService.getAll().resolves([]);
|
|
|
|
|
2022-02-07 16:33:10 +01:00
|
|
|
exportService = new ExportService(
|
|
|
|
folderService,
|
|
|
|
cipherService,
|
|
|
|
apiService,
|
|
|
|
cryptoService,
|
|
|
|
cryptoFunctionService
|
|
|
|
);
|
2020-12-30 22:08:02 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
2022-02-07 16:33:10 +01:00
|
|
|
|
|
|
|
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 = "mac";
|
|
|
|
data.encryptedString = "encData";
|
|
|
|
|
|
|
|
spyOn(Utils, "fromBufferToB64").and.returnValue(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 format", () => {
|
|
|
|
expect(exportObject).toEqual(jasmine.objectContaining({ format: jasmine.any(String) }));
|
|
|
|
});
|
|
|
|
|
|
|
|
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", () => {
|
|
|
|
cryptoService.encrypt(Arg.any(), Arg.any()).resolves(mac);
|
|
|
|
expect(exportObject.encKeyValidation_DO_NOT_EDIT).toEqual(mac.encryptedString);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("has data property", () => {
|
|
|
|
cryptoService.encrypt(Arg.any(), Arg.any()).resolves(data);
|
|
|
|
expect(exportObject.data).toEqual(data.encryptedString);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("encrypts the data property", async () => {
|
|
|
|
const unencrypted = await exportService.getExport();
|
|
|
|
expect(exportObject.data).not.toEqual(unencrypted);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-12-30 22:08:02 +01:00
|
|
|
});
|