2022-06-28 03:04:02 +02:00
|
|
|
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";
|
2022-06-28 03:04:02 +02:00
|
|
|
import { CardView } from "@bitwarden/common/models/view/cardView";
|
2022-06-28 01:42:35 +02:00
|
|
|
import { CipherView } from "@bitwarden/common/models/view/cipherView";
|
2022-06-28 03:04:02 +02:00
|
|
|
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
|
|
|
|
2022-06-28 03:04:02 +02:00
|
|
|
jest.mock("@bitwarden/common/models/view/loginView");
|
2022-06-28 02:28:26 +02:00
|
|
|
jest.mock("@bitwarden/common/models/view/attachmentView");
|
2022-06-28 03:04:02 +02:00
|
|
|
jest.mock("@bitwarden/common/models/view/fieldView");
|
|
|
|
jest.mock("@bitwarden/common/models/view/passwordHistoryView");
|
2022-06-28 01:42:35 +02:00
|
|
|
|
|
|
|
describe("CipherView", () => {
|
2022-06-28 03:36:45 +02:00
|
|
|
const obj = {
|
|
|
|
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 01:42:35 +02:00
|
|
|
beforeEach(() => {
|
2022-06-28 03:04:02 +02:00
|
|
|
(LoginView as any).mockClear();
|
2022-06-28 01:42:35 +02:00
|
|
|
(AttachmentView as any).mockClear();
|
2022-06-28 03:04:02 +02:00
|
|
|
(FieldView as any).mockClear();
|
|
|
|
(PasswordHistoryView as any).mockClear();
|
2022-06-28 01:42:35 +02:00
|
|
|
});
|
|
|
|
|
2022-06-28 03:36:45 +02:00
|
|
|
it("toJSON() creates object for serialization", () => {
|
2022-06-28 03:04:02 +02:00
|
|
|
const cipher = new CipherView();
|
2022-06-28 03:36:45 +02:00
|
|
|
Object.assign(cipher, obj);
|
|
|
|
|
|
|
|
expect(cipher.toJSON()).toEqual(obj);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("fromJSON() populates from deserialized object", () => {
|
|
|
|
const expected = new CipherView();
|
|
|
|
Object.assign(expected, obj);
|
|
|
|
|
|
|
|
const mockCtor = (key: string) => key as any;
|
|
|
|
jest.spyOn(LoginView, "fromJSON").mockImplementation(mockCtor);
|
|
|
|
jest.spyOn(AttachmentView, "fromJSON").mockImplementation(mockCtor);
|
|
|
|
jest.spyOn(FieldView, "fromJSON").mockImplementation(mockCtor);
|
|
|
|
jest.spyOn(PasswordHistoryView, "fromJSON").mockImplementation(mockCtor);
|
|
|
|
|
|
|
|
const newCipher = CipherView.fromJSON(obj);
|
|
|
|
|
|
|
|
expect(newCipher).toEqual(expected);
|
2022-06-28 03:04:02 +02:00
|
|
|
expect(newCipher).toBeInstanceOf(CipherView);
|
2022-06-28 01:42:35 +02:00
|
|
|
});
|
|
|
|
});
|