2022-02-09 20:04:27 +02:00
|
|
|
import { supportDir, setupDatabaseAndSynchronizer, switchClient } from '../testing/test-utils';
|
|
|
|
import Folder from '../models/Folder';
|
|
|
|
import Note from '../models/Note';
|
|
|
|
import Resource from '../models/Resource';
|
|
|
|
import shim from '../shim';
|
2019-07-30 09:35:42 +02:00
|
|
|
|
2021-05-21 15:17:21 +02:00
|
|
|
const testImagePath = `${supportDir}/photo.jpg`;
|
2019-05-12 12:38:33 +02:00
|
|
|
|
2021-09-25 20:51:44 +02:00
|
|
|
describe('models/Resource', function() {
|
2018-10-11 18:18:24 +02:00
|
|
|
|
|
|
|
beforeEach(async (done) => {
|
|
|
|
await setupDatabaseAndSynchronizer(1);
|
|
|
|
await switchClient(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should have a "done" fetch_status when created locally', (async () => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folder1 = await Folder.save({ title: 'folder1' });
|
|
|
|
const note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
2019-05-12 12:38:33 +02:00
|
|
|
await shim.attachFileToNote(note1, testImagePath);
|
2020-03-14 01:46:14 +02:00
|
|
|
const resource1 = (await Resource.all())[0];
|
|
|
|
const ls = await Resource.localState(resource1);
|
2018-11-13 02:45:08 +02:00
|
|
|
expect(ls.fetch_status).toBe(Resource.FETCH_STATUS_DONE);
|
|
|
|
}));
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should have a default local state', (async () => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folder1 = await Folder.save({ title: 'folder1' });
|
|
|
|
const note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
2019-05-12 12:38:33 +02:00
|
|
|
await shim.attachFileToNote(note1, testImagePath);
|
2020-03-14 01:46:14 +02:00
|
|
|
const resource1 = (await Resource.all())[0];
|
|
|
|
const ls = await Resource.localState(resource1);
|
2018-11-13 02:45:08 +02:00
|
|
|
expect(!ls.id).toBe(true);
|
|
|
|
expect(ls.resource_id).toBe(resource1.id);
|
|
|
|
expect(ls.fetch_status).toBe(Resource.FETCH_STATUS_DONE);
|
|
|
|
}));
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should save and delete local state', (async () => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folder1 = await Folder.save({ title: 'folder1' });
|
|
|
|
const note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
2019-05-12 12:38:33 +02:00
|
|
|
await shim.attachFileToNote(note1, testImagePath);
|
2020-03-14 01:46:14 +02:00
|
|
|
const resource1 = (await Resource.all())[0];
|
2018-11-13 02:45:08 +02:00
|
|
|
await Resource.setLocalState(resource1, { fetch_status: Resource.FETCH_STATUS_IDLE });
|
|
|
|
|
|
|
|
let ls = await Resource.localState(resource1);
|
|
|
|
expect(!!ls.id).toBe(true);
|
|
|
|
expect(ls.fetch_status).toBe(Resource.FETCH_STATUS_IDLE);
|
|
|
|
|
|
|
|
await Resource.delete(resource1.id);
|
|
|
|
ls = await Resource.localState(resource1);
|
|
|
|
expect(!ls.id).toBe(true);
|
2018-10-11 18:18:24 +02:00
|
|
|
}));
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should resize the resource if the image is below the required dimensions', (async () => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folder1 = await Folder.save({ title: 'folder1' });
|
|
|
|
const note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
2019-05-12 12:38:33 +02:00
|
|
|
const previousMax = Resource.IMAGE_MAX_DIMENSION;
|
|
|
|
Resource.IMAGE_MAX_DIMENSION = 5;
|
|
|
|
await shim.attachFileToNote(note1, testImagePath);
|
|
|
|
Resource.IMAGE_MAX_DIMENSION = previousMax;
|
2020-03-14 01:46:14 +02:00
|
|
|
const resource1 = (await Resource.all())[0];
|
2019-05-12 12:38:33 +02:00
|
|
|
|
|
|
|
const originalStat = await shim.fsDriver().stat(testImagePath);
|
|
|
|
const newStat = await shim.fsDriver().stat(Resource.fullPath(resource1));
|
|
|
|
|
|
|
|
expect(newStat.size < originalStat.size).toBe(true);
|
|
|
|
}));
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should not resize the resource if the image is below the required dimensions', (async () => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folder1 = await Folder.save({ title: 'folder1' });
|
|
|
|
const note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
2019-05-12 12:38:33 +02:00
|
|
|
await shim.attachFileToNote(note1, testImagePath);
|
2020-03-14 01:46:14 +02:00
|
|
|
const resource1 = (await Resource.all())[0];
|
2019-05-12 12:38:33 +02:00
|
|
|
|
|
|
|
const originalStat = await shim.fsDriver().stat(testImagePath);
|
|
|
|
const newStat = await shim.fsDriver().stat(Resource.fullPath(resource1));
|
|
|
|
|
|
|
|
expect(originalStat.size).toBe(newStat.size);
|
|
|
|
}));
|
|
|
|
|
2022-02-09 20:04:27 +02:00
|
|
|
// it('should encrypt a shared resource using the correct encryption key', (async () => {
|
|
|
|
// const folder1 = await Folder.save({ title: 'folder1' });
|
|
|
|
// const note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
|
|
|
// await shim.attachFileToNote(note1, testImagePath);
|
|
|
|
|
|
|
|
// Resource.shareService_ = {
|
|
|
|
// shareById: () => {
|
|
|
|
// return {
|
|
|
|
// master_key_id: '',
|
|
|
|
// };
|
|
|
|
// },
|
|
|
|
// } as any;
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
|
|
|
// } finally {
|
|
|
|
// Resource.shareService_ = null;
|
|
|
|
// }
|
|
|
|
// }));
|
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
});
|