Tests for all nested objects
This commit is contained in:
parent
888a60edc2
commit
580ef99e47
|
@ -74,5 +74,6 @@ describe("SymmetricCryptoKey", () => {
|
|||
const newKey = SymmetricCryptoKey.fromJSON(JSON.parse(serialized));
|
||||
|
||||
expect(newKey).toEqual(key);
|
||||
expect(newKey).toBeInstanceOf(SymmetricCryptoKey);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -25,6 +25,8 @@ describe("AttachmentView", () => {
|
|||
|
||||
const newAttachment = AttachmentView.fromJSON(JSON.parse(stringify));
|
||||
expect(newAttachment).toEqual(attachment);
|
||||
expect(newAttachment).toBeInstanceOf(AttachmentView);
|
||||
|
||||
expect(SymmetricCryptoKey.fromJSON).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
import { CardView } from "@bitwarden/common/models/view/cardView";
|
||||
|
||||
describe("CardView", () => {
|
||||
it("serializes and deserializes", () => {
|
||||
const card = new CardView();
|
||||
card.cardholderName = "my cardholder name";
|
||||
card.expMonth = "08";
|
||||
card.expYear = "2030";
|
||||
card.code = "123";
|
||||
card.brand = "ExampleCard Co";
|
||||
card.number = "1234 5678 9101";
|
||||
|
||||
const stringify = JSON.stringify(card);
|
||||
const newCard = CardView.fromJSON(JSON.parse(stringify));
|
||||
|
||||
expect(newCard).toEqual(card);
|
||||
expect(newCard).toBeInstanceOf(CardView);
|
||||
});
|
||||
});
|
|
@ -0,0 +1,22 @@
|
|||
import { FieldType } from "@bitwarden/common/enums/fieldType";
|
||||
import { LoginLinkedId } from "@bitwarden/common/enums/linkedIdType";
|
||||
import { FieldView } from "@bitwarden/common/models/view/fieldView";
|
||||
|
||||
describe("FieldView", () => {
|
||||
it("serializes and deserializes", () => {
|
||||
const field = new FieldView();
|
||||
field.name = "myFieldName";
|
||||
field.value = "myValue";
|
||||
field.type = FieldType.Hidden;
|
||||
field.newField = true;
|
||||
field.showValue = true;
|
||||
field.showCount = true;
|
||||
field.linkedId = LoginLinkedId.Password;
|
||||
|
||||
const stringified = JSON.stringify(field);
|
||||
const newField = FieldView.fromJSON(JSON.parse(stringified));
|
||||
|
||||
expect(newField).toEqual(field);
|
||||
expect(newField).toBeInstanceOf(FieldView);
|
||||
});
|
||||
});
|
|
@ -0,0 +1,31 @@
|
|||
import { IdentityView } from "@bitwarden/common/models/view/identityView";
|
||||
|
||||
describe("IdentityView", () => {
|
||||
it("serializes and deserializes", () => {
|
||||
const identity = new IdentityView();
|
||||
identity.title = "Mr";
|
||||
identity.firstName = "First";
|
||||
identity.middleName = "Middle";
|
||||
identity.lastName = "Last";
|
||||
identity.address1 = "123";
|
||||
identity.address2 = "Fake St";
|
||||
identity.address3 = "Business Park";
|
||||
identity.city = "Sydney";
|
||||
identity.state = "NSW";
|
||||
identity.postalCode = "2000";
|
||||
identity.country = "Australia";
|
||||
identity.company = "Bitwarden";
|
||||
identity.email = "example@ex.com";
|
||||
identity.phone = "1234";
|
||||
identity.ssn = "09876";
|
||||
identity.username = "myUsername0";
|
||||
identity.passportNumber = "A12387";
|
||||
identity.licenseNumber = "asdf";
|
||||
|
||||
const stringified = JSON.stringify(identity);
|
||||
const newIdentity = IdentityView.fromJSON(JSON.parse(stringified));
|
||||
|
||||
expect(newIdentity).toEqual(identity);
|
||||
expect(newIdentity).toBeInstanceOf(IdentityView);
|
||||
});
|
||||
});
|
|
@ -0,0 +1,16 @@
|
|||
import { UriMatchType } from "@bitwarden/common/enums/uriMatchType";
|
||||
import { LoginUriView } from "@bitwarden/common/models/view/loginUriView";
|
||||
|
||||
describe("LoginUriView", () => {
|
||||
it("serializes and deserializes", () => {
|
||||
const uri = new LoginUriView();
|
||||
uri.match = UriMatchType.Host;
|
||||
uri.uri = "http://example.com/login";
|
||||
|
||||
const stringified = JSON.stringify(uri);
|
||||
const newUri = LoginUriView.fromJSON(JSON.parse(stringified));
|
||||
|
||||
expect(newUri).toEqual(uri);
|
||||
expect(newUri).toBeInstanceOf(LoginUriView);
|
||||
});
|
||||
});
|
|
@ -0,0 +1,31 @@
|
|||
import { LoginUriView } from "@bitwarden/common/models/view/loginUriView";
|
||||
import { LoginView } from "@bitwarden/common/models/view/loginView";
|
||||
|
||||
jest.mock("@bitwarden/common/models/view/loginUriView");
|
||||
|
||||
describe("LoginView", () => {
|
||||
beforeEach(() => {
|
||||
(LoginUriView as any).mockClear();
|
||||
});
|
||||
|
||||
it("serializes and deserializes", () => {
|
||||
const login = new LoginView();
|
||||
login.username = "myUsername";
|
||||
login.password = "myPassword";
|
||||
login.totp = "totpSeed";
|
||||
login.autofillOnPageLoad = true;
|
||||
login.passwordRevisionDate = new Date();
|
||||
login.uris = [new LoginUriView()];
|
||||
|
||||
jest
|
||||
.spyOn(LoginUriView.prototype, "toJSON")
|
||||
.mockImplementation(() => ({ mock: "not null value" }));
|
||||
jest.spyOn(LoginUriView, "fromJSON").mockImplementation(() => login.uris[0]);
|
||||
|
||||
const stringified = JSON.stringify(login);
|
||||
const newLogin = LoginView.fromJSON(JSON.parse(stringified));
|
||||
|
||||
expect(newLogin).toEqual(login);
|
||||
expect(newLogin).toBeInstanceOf(LoginView);
|
||||
});
|
||||
});
|
|
@ -0,0 +1,15 @@
|
|||
import { PasswordHistoryView } from "@bitwarden/common/models/view/passwordHistoryView";
|
||||
|
||||
describe("PasswordHistoryView", () => {
|
||||
it("serializes and deserializes", () => {
|
||||
const ph = new PasswordHistoryView();
|
||||
ph.lastUsedDate = new Date();
|
||||
ph.password = "mySecretPass";
|
||||
|
||||
const stringified = JSON.stringify(ph);
|
||||
const newPh = PasswordHistoryView.fromJSON(JSON.parse(stringified));
|
||||
|
||||
expect(newPh).toEqual(ph);
|
||||
expect(newPh).toBeInstanceOf(PasswordHistoryView);
|
||||
});
|
||||
});
|
|
@ -132,12 +132,12 @@ export class LoginUriView implements View {
|
|||
: this.uri;
|
||||
}
|
||||
|
||||
toJSON() {
|
||||
toJSON(): any {
|
||||
// Needed to serialize getters which are not included by JSON.stringify
|
||||
return Utils.copyToNewObject(this, serializedProperties);
|
||||
}
|
||||
|
||||
static fromJSON(obj: any) {
|
||||
static fromJSON(obj: any): LoginUriView {
|
||||
return Utils.copyToNewObject(obj, serializedProperties, LoginUriView);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,13 +17,16 @@ export class PasswordHistoryView implements View {
|
|||
}
|
||||
|
||||
static fromJSON(obj: any): PasswordHistoryView {
|
||||
return Utils.copyToNewObject(
|
||||
const ph = Utils.copyToNewObject(
|
||||
obj,
|
||||
{
|
||||
password: null,
|
||||
lastUsedDate: null,
|
||||
},
|
||||
PasswordHistoryView
|
||||
);
|
||||
|
||||
ph.lastUsedDate = obj.lastUsedDate == null ? null : new Date(obj.lastUsedDate);
|
||||
|
||||
return ph;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue