Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
Add tests for domain models (#768)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hinton committed Apr 16, 2022
1 parent f8ac1ed commit 6bcadc4
Show file tree
Hide file tree
Showing 23 changed files with 1,933 additions and 6 deletions.
83 changes: 83 additions & 0 deletions common/spec/domain/attachment.spec.ts
@@ -0,0 +1,83 @@
import Substitute, { Arg } from "@fluffy-spoon/substitute";

import { CryptoService } from "jslib-common/abstractions/crypto.service";
import { AttachmentData } from "jslib-common/models/data/attachmentData";
import { Attachment } from "jslib-common/models/domain/attachment";
import { SymmetricCryptoKey } from "jslib-common/models/domain/symmetricCryptoKey";
import { ContainerService } from "jslib-common/services/container.service";

import { makeStaticByteArray, mockEnc } from "../utils";

describe("Attachment", () => {
let data: AttachmentData;

beforeEach(() => {
data = {
id: "id",
url: "url",
fileName: "fileName",
key: "key",
size: "1100",
sizeName: "1.1 KB",
};
});

it("Convert from empty", () => {
const data = new AttachmentData();
const attachment = new Attachment(data);

expect(attachment).toEqual({
id: null,
url: null,
size: undefined,
sizeName: null,
key: null,
fileName: null,
});
});

it("Convert", () => {
const attachment = new Attachment(data);

expect(attachment).toEqual({
size: "1100",
id: "id",
url: "url",
sizeName: "1.1 KB",
fileName: { encryptedString: "fileName", encryptionType: 0 },
key: { encryptedString: "key", encryptionType: 0 },
});
});

it("toAttachmentData", () => {
const attachment = new Attachment(data);
expect(attachment.toAttachmentData()).toEqual(data);
});

it("Decrypt", async () => {
const attachment = new Attachment();
attachment.id = "id";
attachment.url = "url";
attachment.size = "1100";
attachment.sizeName = "1.1 KB";
attachment.key = mockEnc("key");
attachment.fileName = mockEnc("fileName");

const cryptoService = Substitute.for<CryptoService>();
cryptoService.getOrgKey(null).resolves(null);
cryptoService.decryptToBytes(Arg.any(), Arg.any()).resolves(makeStaticByteArray(32));

(window as any).bitwardenContainerService = new ContainerService(cryptoService);

const view = await attachment.decrypt(null);

expect(view).toEqual({
id: "id",
url: "url",
size: "1100",
sizeName: "1.1 KB",
fileName: "fileName",
key: expect.any(SymmetricCryptoKey),
});
});
});
73 changes: 73 additions & 0 deletions common/spec/domain/card.spec.ts
@@ -0,0 +1,73 @@
import { CardData } from "jslib-common/models/data/cardData";
import { Card } from "jslib-common/models/domain/card";

import { mockEnc } from "../utils";

describe("Card", () => {
let data: CardData;

beforeEach(() => {
data = {
cardholderName: "encHolder",
brand: "encBrand",
number: "encNumber",
expMonth: "encMonth",
expYear: "encYear",
code: "encCode",
};
});

it("Convert from empty", () => {
const data = new CardData();
const card = new Card(data);

expect(card).toEqual({
cardholderName: null,
brand: null,
number: null,
expMonth: null,
expYear: null,
code: null,
});
});

it("Convert", () => {
const card = new Card(data);

expect(card).toEqual({
cardholderName: { encryptedString: "encHolder", encryptionType: 0 },
brand: { encryptedString: "encBrand", encryptionType: 0 },
number: { encryptedString: "encNumber", encryptionType: 0 },
expMonth: { encryptedString: "encMonth", encryptionType: 0 },
expYear: { encryptedString: "encYear", encryptionType: 0 },
code: { encryptedString: "encCode", encryptionType: 0 },
});
});

it("toCardData", () => {
const card = new Card(data);
expect(card.toCardData()).toEqual(data);
});

it("Decrypt", async () => {
const card = new Card();
card.cardholderName = mockEnc("cardHolder");
card.brand = mockEnc("brand");
card.number = mockEnc("number");
card.expMonth = mockEnc("expMonth");
card.expYear = mockEnc("expYear");
card.code = mockEnc("code");

const view = await card.decrypt(null);

expect(view).toEqual({
_brand: "brand",
_number: "number",
_subTitle: null,
cardholderName: "cardHolder",
code: "code",
expMonth: "expMonth",
expYear: "expYear",
});
});
});

0 comments on commit 6bcadc4

Please sign in to comment.