2018-03-16 21:32:17 +00:00
|
|
|
const fse = require('fs-extra');
|
|
|
|
const path = require('path');
|
|
|
|
const tmp = require('tmp');
|
|
|
|
const { assert } = require('chai');
|
|
|
|
|
|
|
|
const Attachments = require('../../app/attachments');
|
2018-03-23 19:49:22 +00:00
|
|
|
const { stringToArrayBuffer } = require('../../js/modules/string_to_array_buffer');
|
2018-03-16 21:32:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
const PREFIX_LENGTH = 2;
|
|
|
|
const NUM_SEPARATORS = 1;
|
|
|
|
const NAME_LENGTH = 64;
|
|
|
|
const PATH_LENGTH = PREFIX_LENGTH + NUM_SEPARATORS + NAME_LENGTH;
|
|
|
|
|
|
|
|
describe('Attachments', () => {
|
2018-04-04 01:06:29 +00:00
|
|
|
describe('createWriterForNew', () => {
|
2018-03-19 23:44:14 +00:00
|
|
|
let tempRootDirectory = null;
|
2018-03-16 21:32:17 +00:00
|
|
|
before(() => {
|
2018-03-19 23:44:14 +00:00
|
|
|
tempRootDirectory = tmp.dirSync().name;
|
2018-03-16 21:32:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
2018-03-19 23:44:14 +00:00
|
|
|
await fse.remove(tempRootDirectory);
|
2018-03-16 21:32:17 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should write file to disk and return path', async () => {
|
|
|
|
const input = stringToArrayBuffer('test string');
|
2018-04-04 01:06:29 +00:00
|
|
|
const tempDirectory = path.join(
|
|
|
|
tempRootDirectory,
|
|
|
|
'Attachments_createWriterForNew'
|
|
|
|
);
|
2018-03-16 21:32:17 +00:00
|
|
|
|
2018-04-04 01:06:29 +00:00
|
|
|
const outputPath = await Attachments.createWriterForNew(tempDirectory)(input);
|
2018-03-16 21:32:17 +00:00
|
|
|
const output = await fse.readFile(path.join(tempDirectory, outputPath));
|
|
|
|
|
|
|
|
assert.lengthOf(outputPath, PATH_LENGTH);
|
|
|
|
|
|
|
|
const inputBuffer = Buffer.from(input);
|
2018-03-19 23:45:03 +00:00
|
|
|
assert.deepEqual(inputBuffer, output);
|
2018-03-16 21:32:17 +00:00
|
|
|
});
|
2018-03-19 23:45:22 +00:00
|
|
|
});
|
|
|
|
|
2018-04-03 19:25:24 +00:00
|
|
|
describe('createReader', () => {
|
2018-03-19 23:45:22 +00:00
|
|
|
let tempRootDirectory = null;
|
|
|
|
before(() => {
|
|
|
|
tempRootDirectory = tmp.dirSync().name;
|
|
|
|
});
|
2018-03-16 21:32:17 +00:00
|
|
|
|
2018-03-19 23:45:22 +00:00
|
|
|
after(async () => {
|
|
|
|
await fse.remove(tempRootDirectory);
|
2018-03-16 21:32:17 +00:00
|
|
|
});
|
|
|
|
|
2018-03-19 23:45:22 +00:00
|
|
|
it('should read file from disk', async () => {
|
2018-04-03 19:25:24 +00:00
|
|
|
const tempDirectory = path.join(tempRootDirectory, 'Attachments_createReader');
|
2018-03-19 23:45:22 +00:00
|
|
|
|
|
|
|
const relativePath = Attachments.getRelativePath(Attachments.createName());
|
|
|
|
const fullPath = path.join(tempDirectory, relativePath);
|
|
|
|
const input = stringToArrayBuffer('test string');
|
|
|
|
|
|
|
|
const inputBuffer = Buffer.from(input);
|
|
|
|
await fse.ensureFile(fullPath);
|
|
|
|
await fse.writeFile(fullPath, inputBuffer);
|
2018-04-03 19:25:24 +00:00
|
|
|
const output = await Attachments.createReader(tempDirectory)(relativePath);
|
2018-03-19 23:45:22 +00:00
|
|
|
|
|
|
|
assert.deepEqual(input, output);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-04-03 19:25:24 +00:00
|
|
|
describe('createDeleter', () => {
|
2018-03-19 23:50:14 +00:00
|
|
|
let tempRootDirectory = null;
|
|
|
|
before(() => {
|
|
|
|
tempRootDirectory = tmp.dirSync().name;
|
|
|
|
});
|
|
|
|
|
|
|
|
after(async () => {
|
|
|
|
await fse.remove(tempRootDirectory);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should delete file from disk', async () => {
|
2018-04-03 19:25:24 +00:00
|
|
|
const tempDirectory = path.join(tempRootDirectory, 'Attachments_createDeleter');
|
2018-03-19 23:50:14 +00:00
|
|
|
|
|
|
|
const relativePath = Attachments.getRelativePath(Attachments.createName());
|
|
|
|
const fullPath = path.join(tempDirectory, relativePath);
|
|
|
|
const input = stringToArrayBuffer('test string');
|
|
|
|
|
|
|
|
const inputBuffer = Buffer.from(input);
|
|
|
|
await fse.ensureFile(fullPath);
|
|
|
|
await fse.writeFile(fullPath, inputBuffer);
|
2018-04-03 19:25:24 +00:00
|
|
|
await Attachments.createDeleter(tempDirectory)(relativePath);
|
2018-03-19 23:50:14 +00:00
|
|
|
|
|
|
|
const existsFile = await fse.exists(fullPath);
|
|
|
|
assert.isFalse(existsFile);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-03-19 23:45:22 +00:00
|
|
|
describe('createName', () => {
|
|
|
|
it('should return random file name with correct length', () => {
|
|
|
|
assert.lengthOf(Attachments.createName(), NAME_LENGTH);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getRelativePath', () => {
|
|
|
|
it('should return correct path', () => {
|
|
|
|
const name = '608ce3bc536edbf7637a6aeb6040bdfec49349140c0dd43e97c7ce263b15ff7e';
|
|
|
|
assert.lengthOf(Attachments.getRelativePath(name), PATH_LENGTH);
|
2018-03-16 21:32:17 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|