2023-10-17 19:02:33 +02:00
|
|
|
import { mock, MockProxy } from "jest-mock-extended";
|
2024-02-06 20:51:02 +01:00
|
|
|
import { firstValueFrom } from "rxjs";
|
2022-07-12 20:25:18 +02:00
|
|
|
|
2023-10-17 19:02:33 +02:00
|
|
|
import { makeStaticByteArray } from "../../../../spec";
|
2024-02-06 20:51:02 +01:00
|
|
|
import { FakeAccountService, mockAccountServiceWith } from "../../../../spec/fake-account-service";
|
|
|
|
import { FakeActiveUserState } from "../../../../spec/fake-state";
|
|
|
|
import { FakeStateProvider } from "../../../../spec/fake-state-provider";
|
2023-06-06 22:34:53 +02:00
|
|
|
import { CryptoService } from "../../../platform/abstractions/crypto.service";
|
|
|
|
import { EncryptService } from "../../../platform/abstractions/encrypt.service";
|
|
|
|
import { I18nService } from "../../../platform/abstractions/i18n.service";
|
2024-02-06 20:51:02 +01:00
|
|
|
import { Utils } from "../../../platform/misc/utils";
|
2023-06-06 22:34:53 +02:00
|
|
|
import { EncString } from "../../../platform/models/domain/enc-string";
|
2024-01-17 13:27:44 +01:00
|
|
|
import { SymmetricCryptoKey } from "../../../platform/models/domain/symmetric-crypto-key";
|
2024-02-06 20:51:02 +01:00
|
|
|
import { UserId } from "../../../types/guid";
|
2024-01-17 13:27:44 +01:00
|
|
|
import { UserKey } from "../../../types/key";
|
2023-01-31 22:08:37 +01:00
|
|
|
import { CipherService } from "../../abstractions/cipher.service";
|
|
|
|
import { FolderData } from "../../models/data/folder.data";
|
|
|
|
import { FolderView } from "../../models/view/folder.view";
|
|
|
|
import { FolderService } from "../../services/folder/folder.service";
|
2024-02-06 20:51:02 +01:00
|
|
|
import { FOLDER_ENCRYPTED_FOLDERS } from "../key-state/folder.state";
|
2022-07-12 20:25:18 +02:00
|
|
|
|
|
|
|
describe("Folder Service", () => {
|
|
|
|
let folderService: FolderService;
|
|
|
|
|
2023-10-17 19:02:33 +02:00
|
|
|
let cryptoService: MockProxy<CryptoService>;
|
|
|
|
let encryptService: MockProxy<EncryptService>;
|
|
|
|
let i18nService: MockProxy<I18nService>;
|
|
|
|
let cipherService: MockProxy<CipherService>;
|
2024-02-06 20:51:02 +01:00
|
|
|
let stateProvider: FakeStateProvider;
|
|
|
|
|
|
|
|
const mockUserId = Utils.newGuid() as UserId;
|
|
|
|
let accountService: FakeAccountService;
|
|
|
|
let folderState: FakeActiveUserState<Record<string, FolderData>>;
|
2022-07-12 20:25:18 +02:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2023-10-17 19:02:33 +02:00
|
|
|
cryptoService = mock<CryptoService>();
|
|
|
|
encryptService = mock<EncryptService>();
|
|
|
|
i18nService = mock<I18nService>();
|
|
|
|
cipherService = mock<CipherService>();
|
2024-02-06 20:51:02 +01:00
|
|
|
|
|
|
|
accountService = mockAccountServiceWith(mockUserId);
|
|
|
|
stateProvider = new FakeStateProvider(accountService);
|
2022-07-12 20:25:18 +02:00
|
|
|
|
2023-10-17 19:02:33 +02:00
|
|
|
i18nService.collator = new Intl.Collator("en");
|
|
|
|
|
|
|
|
cryptoService.hasUserKey.mockResolvedValue(true);
|
|
|
|
cryptoService.getUserKeyWithLegacySupport.mockResolvedValue(
|
|
|
|
new SymmetricCryptoKey(makeStaticByteArray(32)) as UserKey,
|
|
|
|
);
|
|
|
|
encryptService.decryptToUtf8.mockResolvedValue("DEC");
|
|
|
|
|
2024-04-16 18:37:03 +02:00
|
|
|
folderService = new FolderService(cryptoService, i18nService, cipherService, stateProvider);
|
2022-07-12 20:25:18 +02:00
|
|
|
|
2024-02-06 20:51:02 +01:00
|
|
|
folderState = stateProvider.activeUser.getFake(FOLDER_ENCRYPTED_FOLDERS);
|
|
|
|
|
|
|
|
// Initial state
|
|
|
|
folderState.nextState({ "1": folderData("1", "test") });
|
2022-07-12 20:25:18 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("encrypt", async () => {
|
|
|
|
const model = new FolderView();
|
|
|
|
model.id = "2";
|
|
|
|
model.name = "Test Folder";
|
|
|
|
|
2023-10-17 19:02:33 +02:00
|
|
|
cryptoService.encrypt.mockResolvedValue(new EncString("ENC"));
|
2022-07-12 20:25:18 +02:00
|
|
|
|
|
|
|
const result = await folderService.encrypt(model);
|
|
|
|
|
|
|
|
expect(result).toEqual({
|
|
|
|
id: "2",
|
|
|
|
name: {
|
|
|
|
encryptedString: "ENC",
|
|
|
|
encryptionType: 0,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("get", () => {
|
|
|
|
it("exists", async () => {
|
|
|
|
const result = await folderService.get("1");
|
|
|
|
|
|
|
|
expect(result).toEqual({
|
|
|
|
id: "1",
|
|
|
|
name: {
|
|
|
|
encryptedString: "test",
|
|
|
|
encryptionType: 0,
|
|
|
|
},
|
|
|
|
revisionDate: null,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("not exists", async () => {
|
|
|
|
const result = await folderService.get("2");
|
|
|
|
|
|
|
|
expect(result).toBe(undefined);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("upsert", async () => {
|
|
|
|
await folderService.upsert(folderData("2", "test 2"));
|
|
|
|
|
|
|
|
expect(await firstValueFrom(folderService.folders$)).toEqual([
|
|
|
|
{
|
|
|
|
id: "1",
|
|
|
|
name: {
|
|
|
|
encryptedString: "test",
|
|
|
|
encryptionType: 0,
|
|
|
|
},
|
|
|
|
revisionDate: null,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: "2",
|
|
|
|
name: {
|
|
|
|
encryptedString: "test 2",
|
|
|
|
encryptionType: 0,
|
|
|
|
},
|
|
|
|
revisionDate: null,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("replace", async () => {
|
|
|
|
await folderService.replace({ "2": folderData("2", "test 2") });
|
|
|
|
|
|
|
|
expect(await firstValueFrom(folderService.folders$)).toEqual([
|
|
|
|
{
|
|
|
|
id: "2",
|
|
|
|
name: {
|
|
|
|
encryptedString: "test 2",
|
|
|
|
encryptionType: 0,
|
|
|
|
},
|
|
|
|
revisionDate: null,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("delete", async () => {
|
|
|
|
await folderService.delete("1");
|
|
|
|
|
|
|
|
expect((await firstValueFrom(folderService.folders$)).length).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("clearCache", async () => {
|
|
|
|
await folderService.clearCache();
|
|
|
|
|
|
|
|
expect((await firstValueFrom(folderService.folders$)).length).toBe(1);
|
|
|
|
expect((await firstValueFrom(folderService.folderViews$)).length).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("clear", () => {
|
|
|
|
it("null userId", async () => {
|
|
|
|
await folderService.clear();
|
|
|
|
|
|
|
|
expect((await firstValueFrom(folderService.folders$)).length).toBe(0);
|
|
|
|
expect((await firstValueFrom(folderService.folderViews$)).length).toBe(0);
|
|
|
|
});
|
|
|
|
|
2024-02-06 20:51:02 +01:00
|
|
|
/**
|
|
|
|
* TODO: Fix this test to address the problem where the fakes for the active user state is not
|
|
|
|
* updated as expected
|
|
|
|
*/
|
|
|
|
// it("matching userId", async () => {
|
|
|
|
// stateService.getUserId.mockResolvedValue("1");
|
|
|
|
// await folderService.clear("1" as UserId);
|
|
|
|
|
|
|
|
// expect((await firstValueFrom(folderService.folders$)).length).toBe(0);
|
|
|
|
// });
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: Fix this test to address the problem where the fakes for the active user state is not
|
|
|
|
* updated as expected
|
|
|
|
*/
|
|
|
|
// it("mismatching userId", async () => {
|
|
|
|
// await folderService.clear("12" as UserId);
|
|
|
|
|
|
|
|
// expect((await firstValueFrom(folderService.folders$)).length).toBe(1);
|
|
|
|
// expect((await firstValueFrom(folderService.folderViews$)).length).toBe(2);
|
|
|
|
// });
|
2022-07-12 20:25:18 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
function folderData(id: string, name: string) {
|
|
|
|
const data = new FolderData({} as any);
|
|
|
|
data.id = id;
|
|
|
|
data.name = name;
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
});
|