First tests

This commit is contained in:
Thomas Rittson 2022-06-28 09:42:35 +10:00
parent 540bd5d5ca
commit 888a60edc2
5 changed files with 93 additions and 2 deletions

View File

@ -66,4 +66,13 @@ describe("SymmetricCryptoKey", () => {
expect(t).toThrowError("Unable to determine encType.");
});
});
it("serializes and deserializes", () => {
const key = new SymmetricCryptoKey(makeStaticByteArray(64));
const serialized = JSON.stringify(key);
const newKey = SymmetricCryptoKey.fromJSON(JSON.parse(serialized));
expect(newKey).toEqual(key);
});
});

View File

@ -0,0 +1,30 @@
import { SymmetricCryptoKey } from "@bitwarden/common/models/domain/symmetricCryptoKey";
import { AttachmentView } from "@bitwarden/common/models/view/attachmentView";
jest.mock("@bitwarden/common/models/domain/symmetricCryptoKey");
describe("AttachmentView", () => {
beforeEach(() => {
(SymmetricCryptoKey as any).mockClear();
});
it("serializes and deserializes", () => {
const attachment = new AttachmentView();
attachment.id = "1234";
attachment.url = "http://example.com";
attachment.size = "1000";
attachment.sizeName = "kb";
attachment.fileName = "my filename";
attachment.key = new SymmetricCryptoKey(null);
jest.spyOn(SymmetricCryptoKey.prototype, "toJSON").mockImplementation(() => "not null value");
jest.spyOn(SymmetricCryptoKey, "fromJSON").mockImplementation(() => attachment.key);
const stringify = JSON.stringify(attachment);
const newAttachment = AttachmentView.fromJSON(JSON.parse(stringify));
expect(newAttachment).toEqual(attachment);
expect(SymmetricCryptoKey.fromJSON).toHaveBeenCalledTimes(1);
});
});

View File

@ -0,0 +1,52 @@
import { UriMatchType } from "@bitwarden/common/enums/uriMatchType";
import { CipherView } from "@bitwarden/common/models/view/cipherView";
import { LoginUriView } from "@bitwarden/common/models/view/loginUriView";
import { AttachmentView } from "../../src/models/view/attachmentView";
jest.mock("../../src/models/view/attachmentView");
const primitiveFields = {
id: "myId",
folderId: "folderId",
organizationId: "organizationId",
};
describe("CipherView", () => {
let cipherView: CipherView;
beforeEach(() => {
(AttachmentView as any).mockClear();
cipherView = new CipherView();
Object.assign(cipherView, primitiveFields);
});
it("serializes", () => {
// cipherView.attachments = [new AttachmentView(), new AttachmentView()];
// const actual = JSON.stringify(cipherView);
// expect(cipherView.attachments[0].toJSON).toHaveBeenCalledTimes(1);
// expect(cipherView.attachments[1].toJSON).toHaveBeenCalledTimes(1);
// expect(actual).toEqual(JSON.stringify(primitiveFields));
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",
});
});
// it("deserializes", () => {
// const stringify = JSON.stringify(cipherView);
// const actual = CipherView.fromJSON(stringify)
// expect(actual).toEqual(primitiveFields)
// });
});

View File

@ -56,7 +56,7 @@ export class SymmetricCryptoKey {
}
}
toJSON() {
toJSON(): any {
// The whole object is constructed from the initial key, so just store the B64 key
return { keyB64: this.keyB64 };
}

View File

@ -43,7 +43,7 @@ export class AttachmentView implements View {
url: null,
size: null,
sizeName: null,
filename: null,
fileName: null,
},
AttachmentView
);