2022-06-14 17:10:53 +02:00
|
|
|
import { PasswordHistoryData } from "@bitwarden/common/models/data/passwordHistoryData";
|
2022-10-03 22:50:43 +02:00
|
|
|
import { EncString } from "@bitwarden/common/models/domain/encString";
|
2022-06-14 17:10:53 +02:00
|
|
|
import { Password } from "@bitwarden/common/models/domain/password";
|
2022-04-16 17:18:12 +02:00
|
|
|
|
2022-10-03 22:50:43 +02:00
|
|
|
import { mockEnc, mockFromJson } from "../../utils";
|
2022-04-16 17:18:12 +02:00
|
|
|
|
|
|
|
describe("Password", () => {
|
|
|
|
let data: PasswordHistoryData;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
data = {
|
|
|
|
password: "encPassword",
|
|
|
|
lastUsedDate: "2022-01-31T12:00:00.000Z",
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Convert from empty", () => {
|
|
|
|
const data = new PasswordHistoryData();
|
|
|
|
const password = new Password(data);
|
|
|
|
|
|
|
|
expect(password).toMatchObject({
|
|
|
|
password: null,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Convert", () => {
|
|
|
|
const password = new Password(data);
|
|
|
|
|
|
|
|
expect(password).toEqual({
|
|
|
|
password: { encryptedString: "encPassword", encryptionType: 0 },
|
|
|
|
lastUsedDate: new Date("2022-01-31T12:00:00.000Z"),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("toPasswordHistoryData", () => {
|
|
|
|
const password = new Password(data);
|
|
|
|
expect(password.toPasswordHistoryData()).toEqual(data);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("Decrypt", async () => {
|
|
|
|
const password = new Password();
|
|
|
|
password.password = mockEnc("password");
|
|
|
|
password.lastUsedDate = new Date("2022-01-31T12:00:00.000Z");
|
|
|
|
|
|
|
|
const view = await password.decrypt(null);
|
|
|
|
|
|
|
|
expect(view).toEqual({
|
|
|
|
password: "password",
|
|
|
|
lastUsedDate: new Date("2022-01-31T12:00:00.000Z"),
|
|
|
|
});
|
|
|
|
});
|
2022-10-03 22:50:43 +02:00
|
|
|
|
|
|
|
describe("fromJSON", () => {
|
|
|
|
it("initializes nested objects", () => {
|
|
|
|
jest.spyOn(EncString, "fromJSON").mockImplementation(mockFromJson);
|
|
|
|
const lastUsedDate = new Date("2022-01-31T12:00:00.000Z");
|
|
|
|
|
|
|
|
const actual = Password.fromJSON({
|
|
|
|
password: "myPassword",
|
|
|
|
lastUsedDate: lastUsedDate.toISOString(),
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(actual).toEqual({
|
|
|
|
password: "myPassword_fromJSON",
|
|
|
|
lastUsedDate: lastUsedDate,
|
|
|
|
});
|
|
|
|
expect(actual).toBeInstanceOf(Password);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("returns null if object is null", () => {
|
|
|
|
expect(Password.fromJSON(null)).toBeNull();
|
|
|
|
});
|
|
|
|
});
|
2022-04-16 17:18:12 +02:00
|
|
|
});
|