bitwarden-estensione-browser/libs/common/spec/models/view/cipherView.spec.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

76 lines
2.7 KiB
TypeScript
Raw Normal View History

import { CipherRepromptType } from "@bitwarden/common/enums/cipherRepromptType";
import { CipherType } from "@bitwarden/common/enums/cipherType";
2022-06-28 02:28:26 +02:00
import { AttachmentView } from "@bitwarden/common/models/view/attachmentView";
import { CardView } from "@bitwarden/common/models/view/cardView";
2022-06-28 01:42:35 +02:00
import { CipherView } from "@bitwarden/common/models/view/cipherView";
import { FieldView } from "@bitwarden/common/models/view/fieldView";
import { LoginView } from "@bitwarden/common/models/view/loginView";
import { PasswordHistoryView } from "@bitwarden/common/models/view/passwordHistoryView";
2022-06-28 01:42:35 +02:00
jest.mock("@bitwarden/common/models/view/loginView");
2022-06-28 02:28:26 +02:00
jest.mock("@bitwarden/common/models/view/attachmentView");
jest.mock("@bitwarden/common/models/view/fieldView");
jest.mock("@bitwarden/common/models/view/passwordHistoryView");
2022-06-28 01:42:35 +02:00
2022-06-29 05:23:53 +02:00
const testValues = {
id: "myId",
organizationId: "myOrgId",
folderId: "myFolderId",
name: "my Cipher",
notes: "lorem ipsum",
type: CipherType.Login,
favorite: true,
organizationUseTotp: true,
edit: true,
viewPassword: false,
localData: { lastUsedDate: "123" },
login: "myLogin",
attachments: ["attachment1", "attachment2"],
fields: ["field1", "field2"],
passwordHistory: ["ph1", "ph2", "ph3"],
collectionIds: ["collection1", "collection2"],
revisionDate: new Date(),
deletedDate: new Date(),
reprompt: CipherRepromptType.Password,
};
2022-06-28 03:36:45 +02:00
2022-06-29 05:23:53 +02:00
describe("CipherView", () => {
2022-06-28 01:42:35 +02:00
beforeEach(() => {
(LoginView as any).mockClear();
2022-06-28 01:42:35 +02:00
(AttachmentView as any).mockClear();
(FieldView as any).mockClear();
(PasswordHistoryView as any).mockClear();
2022-06-28 01:42:35 +02:00
});
2022-06-29 05:23:53 +02:00
it("toJSON creates object for serialization", () => {
const cipher = new CipherView();
2022-06-29 05:23:53 +02:00
Object.assign(cipher, testValues);
2022-06-28 03:36:45 +02:00
2022-06-29 05:42:15 +02:00
const actual = cipher.toJSON();
expect(actual).toEqual(testValues);
2022-06-28 03:36:45 +02:00
});
2022-06-29 05:23:53 +02:00
it("fromJSON hydrates new view object", () => {
2022-06-29 06:00:05 +02:00
const mockFromJson = (key: any) => (key + "fromJSON") as any;
2022-06-28 03:46:29 +02:00
jest.spyOn(LoginView, "fromJSON").mockImplementation(mockFromJson);
jest.spyOn(AttachmentView, "fromJSON").mockImplementation(mockFromJson);
jest.spyOn(FieldView, "fromJSON").mockImplementation(mockFromJson);
jest.spyOn(PasswordHistoryView, "fromJSON").mockImplementation(mockFromJson);
2022-06-28 03:36:45 +02:00
2022-06-29 05:42:15 +02:00
const parsed = JSON.parse(JSON.stringify(testValues));
const actual = CipherView.fromJSON(parsed);
2022-06-28 03:36:45 +02:00
2022-06-29 05:23:53 +02:00
const expected = new CipherView();
Object.assign(expected, testValues, {
2022-06-28 03:46:29 +02:00
login: "myLoginfromJSON",
attachments: ["attachment1fromJSON", "attachment2fromJSON"],
fields: ["field1fromJSON", "field2fromJSON"],
passwordHistory: ["ph1fromJSON", "ph2fromJSON", "ph3fromJSON"],
});
2022-06-29 05:23:53 +02:00
expect(actual).toEqual(expected);
expect(actual).toBeInstanceOf(CipherView);
2022-06-28 01:42:35 +02:00
});
});