mirror of
https://github.com/bitwarden/browser
synced 2025-12-22 03:03:43 +00:00
[EC-582] Add domain object serialization (#3623)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { Substitute, Arg } from "@fluffy-spoon/substitute";
|
||||
import { Jsonify } from "type-fest";
|
||||
|
||||
import { CipherRepromptType } from "@bitwarden/common/enums/cipherRepromptType";
|
||||
import { CipherType } from "@bitwarden/common/enums/cipherType";
|
||||
@@ -6,16 +7,20 @@ import { FieldType } from "@bitwarden/common/enums/fieldType";
|
||||
import { SecureNoteType } from "@bitwarden/common/enums/secureNoteType";
|
||||
import { UriMatchType } from "@bitwarden/common/enums/uriMatchType";
|
||||
import { CipherData } from "@bitwarden/common/models/data/cipherData";
|
||||
import { Attachment } from "@bitwarden/common/models/domain/attachment";
|
||||
import { Card } from "@bitwarden/common/models/domain/card";
|
||||
import { Cipher } from "@bitwarden/common/models/domain/cipher";
|
||||
import { EncString } from "@bitwarden/common/models/domain/encString";
|
||||
import { Field } from "@bitwarden/common/models/domain/field";
|
||||
import { Identity } from "@bitwarden/common/models/domain/identity";
|
||||
import { Login } from "@bitwarden/common/models/domain/login";
|
||||
import { Password } from "@bitwarden/common/models/domain/password";
|
||||
import { SecureNote } from "@bitwarden/common/models/domain/secureNote";
|
||||
import { CardView } from "@bitwarden/common/models/view/cardView";
|
||||
import { IdentityView } from "@bitwarden/common/models/view/identityView";
|
||||
import { LoginView } from "@bitwarden/common/models/view/loginView";
|
||||
|
||||
import { mockEnc } from "../../utils";
|
||||
import { mockEnc, mockFromJson } from "../../utils";
|
||||
|
||||
describe("Cipher DTO", () => {
|
||||
it("Convert from empty CipherData", () => {
|
||||
@@ -587,4 +592,63 @@ describe("Cipher DTO", () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("fromJSON", () => {
|
||||
it("initializes nested objects", () => {
|
||||
jest.spyOn(Attachment, "fromJSON").mockImplementation(mockFromJson);
|
||||
jest.spyOn(Field, "fromJSON").mockImplementation(mockFromJson);
|
||||
jest.spyOn(Password, "fromJSON").mockImplementation(mockFromJson);
|
||||
jest.spyOn(EncString, "fromJSON").mockImplementation(mockFromJson);
|
||||
|
||||
const revisionDate = new Date("2022-08-04T01:06:40.441Z");
|
||||
const deletedDate = new Date("2022-09-04T01:06:40.441Z");
|
||||
const actual = Cipher.fromJSON({
|
||||
name: "myName",
|
||||
notes: "myNotes",
|
||||
revisionDate: revisionDate.toISOString(),
|
||||
attachments: ["attachment1", "attachment2"] as any,
|
||||
fields: ["field1", "field2"] as any,
|
||||
passwordHistory: ["ph1", "ph2"] as any,
|
||||
deletedDate: deletedDate.toISOString(),
|
||||
} as Jsonify<Cipher>);
|
||||
|
||||
expect(actual).toMatchObject({
|
||||
name: "myName_fromJSON",
|
||||
notes: "myNotes_fromJSON",
|
||||
revisionDate: revisionDate,
|
||||
attachments: ["attachment1_fromJSON", "attachment2_fromJSON"],
|
||||
fields: ["field1_fromJSON", "field2_fromJSON"],
|
||||
passwordHistory: ["ph1_fromJSON", "ph2_fromJSON"],
|
||||
deletedDate: deletedDate,
|
||||
});
|
||||
expect(actual).toBeInstanceOf(Cipher);
|
||||
});
|
||||
|
||||
test.each([
|
||||
// Test description, CipherType, expected output
|
||||
["LoginView", CipherType.Login, { login: "myLogin_fromJSON" }],
|
||||
["CardView", CipherType.Card, { card: "myCard_fromJSON" }],
|
||||
["IdentityView", CipherType.Identity, { identity: "myIdentity_fromJSON" }],
|
||||
["Secure Note", CipherType.SecureNote, { secureNote: "mySecureNote_fromJSON" }],
|
||||
])("initializes %s", (description: string, cipherType: CipherType, expected: any) => {
|
||||
jest.spyOn(Login, "fromJSON").mockImplementation(mockFromJson);
|
||||
jest.spyOn(Identity, "fromJSON").mockImplementation(mockFromJson);
|
||||
jest.spyOn(Card, "fromJSON").mockImplementation(mockFromJson);
|
||||
jest.spyOn(SecureNote, "fromJSON").mockImplementation(mockFromJson);
|
||||
|
||||
const actual = Cipher.fromJSON({
|
||||
login: "myLogin",
|
||||
card: "myCard",
|
||||
identity: "myIdentity",
|
||||
secureNote: "mySecureNote",
|
||||
type: cipherType,
|
||||
} as any);
|
||||
|
||||
expect(actual).toMatchObject(expected);
|
||||
});
|
||||
|
||||
it("returns null if object is null", () => {
|
||||
expect(Cipher.fromJSON(null)).toBeNull();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user