Add secureNoteView test and WIP cipherView

This commit is contained in:
Thomas Rittson 2022-06-28 11:04:02 +10:00
parent 3bdefcd0e8
commit a85b6c2bb4
3 changed files with 63 additions and 42 deletions

View File

@ -1,52 +1,57 @@
import { UriMatchType } from "@bitwarden/common/enums/uriMatchType";
import { CipherRepromptType } from "@bitwarden/common/enums/cipherRepromptType";
import { CipherType } from "@bitwarden/common/enums/cipherType";
import { AttachmentView } from "@bitwarden/common/models/view/attachmentView";
import { CardView } from "@bitwarden/common/models/view/cardView";
import { CipherView } from "@bitwarden/common/models/view/cipherView";
import { LoginUriView } from "@bitwarden/common/models/view/loginUriView";
import { FieldView } from "@bitwarden/common/models/view/fieldView";
import { LoginView } from "@bitwarden/common/models/view/loginView";
import { PasswordHistoryView } from "@bitwarden/common/models/view/passwordHistoryView";
jest.mock("@bitwarden/common/models/view/loginView");
jest.mock("@bitwarden/common/models/view/attachmentView");
const primitiveFields = {
id: "myId",
folderId: "folderId",
organizationId: "organizationId",
};
jest.mock("@bitwarden/common/models/view/fieldView");
jest.mock("@bitwarden/common/models/view/passwordHistoryView");
describe("CipherView", () => {
let cipherView: CipherView;
beforeEach(() => {
(LoginView as any).mockClear();
(AttachmentView as any).mockClear();
cipherView = new CipherView();
Object.assign(cipherView, primitiveFields);
(FieldView as any).mockClear();
(PasswordHistoryView as any).mockClear();
});
it("serializes", () => {
// cipherView.attachments = [new AttachmentView(), new AttachmentView()];
it("serializes and deserializes", () => {
const cipher = new CipherView();
cipher.id = "myId";
cipher.organizationId = "myOrgId";
cipher.folderId = "myFolderId";
cipher.name = "my Cipher";
cipher.notes = "lorem ipsum";
cipher.type = CipherType.Login;
cipher.favorite = true;
cipher.organizationUseTotp = true;
cipher.edit = true;
cipher.viewPassword = false;
cipher.localData = { lastUsedDate: "123" };
cipher.login = new LoginView();
cipher.attachments = [new AttachmentView(), new AttachmentView()];
cipher.fields = [new FieldView(), new FieldView()];
cipher.passwordHistory = [new PasswordHistoryView(), new PasswordHistoryView()];
cipher.collectionIds = ["collection1", "collection2"];
cipher.revisionDate = new Date();
cipher.deletedDate = new Date();
cipher.reprompt = CipherRepromptType.Password;
// const actual = JSON.stringify(cipherView);
jest.spyOn(LoginView, "fromJSON").mockImplementation(() => cipher.login);
jest.spyOn(AttachmentView, "fromJSON").mockImplementation(() => cipher.attachments[0]);
jest.spyOn(FieldView, "fromJSON").mockImplementation(() => cipher.fields[0]);
jest.spyOn(PasswordHistoryView, "fromJSON").mockImplementation(() => cipher.passwordHistory[0]);
// expect(cipherView.attachments[0].toJSON).toHaveBeenCalledTimes(1);
// expect(cipherView.attachments[1].toJSON).toHaveBeenCalledTimes(1);
const stringified = JSON.stringify(cipher);
// expect(actual).toEqual(JSON.stringify(primitiveFields));
const newCipher = CipherView.fromJSON(JSON.parse(stringified));
const uri = new LoginUriView();
uri.match = UriMatchType.Domain;
uri.uri = "test";
const parsed = JSON.parse(JSON.stringify(uri));
expect(parsed).toEqual({
match: UriMatchType.Domain,
uri: "test",
});
expect(newCipher).toEqual(cipher);
expect(newCipher).toBeInstanceOf(CipherView);
});
// it("deserializes", () => {
// const stringify = JSON.stringify(cipherView);
// const actual = CipherView.fromJSON(stringify)
// expect(actual).toEqual(primitiveFields)
// });
});

View File

@ -0,0 +1,15 @@
import { SecureNoteType } from "@bitwarden/common/enums/secureNoteType";
import { SecureNoteView } from "@bitwarden/common/models/view/secureNoteView";
describe("SecureNoteView", () => {
it("serializes and deserializes", () => {
const secureNote = new SecureNoteView();
secureNote.type = SecureNoteType.Generic;
const stringified = JSON.stringify(secureNote);
const newSecureNote = SecureNoteView.fromJSON(JSON.parse(stringified));
expect(newSecureNote).toEqual(secureNote);
expect(newSecureNote).toBeInstanceOf(SecureNoteView);
});
});

View File

@ -16,7 +16,7 @@ import { View } from "./view";
const serializedProperties: any = {
id: null,
corganizationId: null,
organizationId: null,
folderId: null,
name: null,
notes: null,
@ -31,10 +31,6 @@ const serializedProperties: any = {
revisionDate: null,
deletedDate: null,
attachments: null,
fields: null,
passwordHistory: null,
};
export class CipherView implements View {
@ -158,7 +154,12 @@ export class CipherView implements View {
}
toJSON() {
const obj = Utils.copyToNewObject(this, serializedProperties);
const obj = Utils.copyToNewObject(this, {
attachments: null,
fields: null,
passwordHistory: null,
...serializedProperties,
});
switch (this.type) {
case CipherType.Card: