test: fix backup tests

This commit is contained in:
thecodrr
2022-01-17 12:27:33 +05:00
parent b80b514bc5
commit 17eeaeb72c
2 changed files with 9 additions and 9 deletions

View File

@@ -43,6 +43,7 @@ function encrypt(password, data) {
function decrypt(key, data) { function decrypt(key, data) {
if ( if (
!key || !key ||
!data.key ||
key.password === data.key.password || key.password === data.key.password ||
key.key.password === data.key.password key.key.password === data.key.password
) )
@@ -62,8 +63,8 @@ async function hash(password, userId) {
return password; return password;
} }
async function generateCryptoKey(password) { async function generateCryptoKey(password, salt) {
return password; return { password, salt };
} }
module.exports = { module.exports = {

View File

@@ -6,6 +6,7 @@ import {
} from "./utils"; } from "./utils";
import v52Backup from "./__fixtures__/backup.v5.2.json"; import v52Backup from "./__fixtures__/backup.v5.2.json";
import v52BackupCopy from "./__fixtures__/backup.v5.2.copy.json"; import v52BackupCopy from "./__fixtures__/backup.v5.2.copy.json";
import { qclone } from "qclone";
beforeEach(() => { beforeEach(() => {
StorageInterface.clear(); StorageInterface.clear();
@@ -37,7 +38,7 @@ test("import backup", () =>
notebookTest().then(async ({ db, id }) => { notebookTest().then(async ({ db, id }) => {
const exp = await db.backup.export("node"); const exp = await db.backup.export("node");
StorageInterface.clear(); StorageInterface.clear();
await db.backup.import(exp); await db.backup.import(JSON.parse(exp));
expect(db.notebooks.notebook(id).data.id).toBe(id); expect(db.notebooks.notebook(id).data.id).toBe(id);
}) })
)); ));
@@ -47,7 +48,7 @@ test("import encrypted backup", () =>
notebookTest().then(async ({ db, id }) => { notebookTest().then(async ({ db, id }) => {
const exp = await db.backup.export("node", true); const exp = await db.backup.export("node", true);
StorageInterface.clear(); StorageInterface.clear();
await db.backup.import(exp); await db.backup.import(JSON.parse(exp), "password");
expect(db.notebooks.notebook(id).data.id).toBe(id); expect(db.notebooks.notebook(id).data.id).toBe(id);
}) })
)); ));
@@ -59,9 +60,7 @@ test("import tempered backup", () =>
StorageInterface.clear(); StorageInterface.clear();
const backup = JSON.parse(exp); const backup = JSON.parse(exp);
backup.data.hello = "world"; backup.data.hello = "world";
await expect( await expect(db.backup.import(backup)).rejects.toThrowError(/tempered/);
db.backup.import(JSON.stringify(backup))
).rejects.toThrowError(/tempered/);
}) })
)); ));
@@ -71,7 +70,7 @@ describe.each([
])("testing backup version: %s", (version, data) => { ])("testing backup version: %s", (version, data) => {
test(`import ${version} backup`, () => { test(`import ${version} backup`, () => {
return databaseTest().then(async (db) => { return databaseTest().then(async (db) => {
await db.backup.import(JSON.stringify(data)); await db.backup.import(qclone(data));
expect(db.settings.raw.id).toBeDefined(); expect(db.settings.raw.id).toBeDefined();
expect(db.settings.raw.dateModified).toBeDefined(); expect(db.settings.raw.dateModified).toBeDefined();
@@ -131,7 +130,7 @@ describe.each([
test(`verify indices of ${version} backup`, () => { test(`verify indices of ${version} backup`, () => {
return databaseTest().then(async (db) => { return databaseTest().then(async (db) => {
await db.backup.import(JSON.stringify(data)); await db.backup.import(qclone(data));
verifyIndex(data, db, "notes", "notes"); verifyIndex(data, db, "notes", "notes");
verifyIndex(data, db, "notebooks", "notebooks"); verifyIndex(data, db, "notebooks", "notebooks");