@ -2,7 +2,7 @@ const FSE = require('fs-extra');
const isEqual = require ( 'lodash/isEqual' ) ;
const Path = require ( 'path' ) ;
const stringToArrayBuffer = require ( 'string-to-arraybuffer' ) ;
const t e mpy = require ( 't e mpy ') ;
const t mp = require ( 't mp') ;
const { assert } = require ( 'chai' ) ;
const {
@ -12,11 +12,15 @@ const {
} = require ( '../../../../app/types/attachment/write_attachment_data' ) ;
const PREFIX _LENGTH = 3 ;
const NUM _SEPARATORS = 1 ;
const NAME _LENGTH = 64 ;
const PATH _LENGTH = PREFIX _LENGTH + NUM _SEPARATORS + NAME _LENGTH ;
describe ( 'writeAttachmentData' , ( ) => {
let TEMPORARY _DIRECTORY = null ;
before ( ( ) => {
// Sync!
TEMPORARY _DIRECTORY = tempy . directory ( ) ;
TEMPORARY _DIRECTORY = tmp . dirSync ( ) . name ;
} ) ;
after ( async ( ) => {
@ -26,33 +30,25 @@ describe('writeAttachmentData', () => {
it ( 'should write file to disk and return path' , async ( ) => {
const input = stringToArrayBuffer ( 'test string' ) ;
const tempDirectory = Path . join ( TEMPORARY _DIRECTORY , 'writeAttachmentData' ) ;
const expectedPath = Path . join (
tempDirectory ,
'd55/d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b'
) ;
const outputPath = await writeAttachmentData ( tempDirectory ) ( input ) ;
const output = await FSE . readFile ( outputPath ) ;
assert . strictEqual( outputPath , expectedPath ) ;
assert . lengthOf( Path . relative ( tempDirectory , outputPath ) , PATH _LENGTH ) ;
const inputBuffer = Buffer . from ( input ) ;
assert . isTrue ( isEqual ( inputBuffer , output ) ) ;
} ) ;
describe ( '_getAttachmentName' , ( ) => {
it ( 'should return correct name' , ( ) => {
const input = Buffer . from ( 'test string' , 'utf8' ) ;
const expected = 'd5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b' ;
assert . strictEqual ( _getAttachmentName ( input ) , expected ) ;
it ( 'should return random file name with correct length' , ( ) => {
assert . lengthOf ( _getAttachmentName ( ) , NAME _LENGTH ) ;
} ) ;
} ) ;
describe ( '_getAttachmentPath' , ( ) => {
it ( 'should return correct path' , ( ) => {
const input = Buffer . from ( 'test string' , 'utf8' ) ;
const expected = 'd55/d5579c46dfcc7f18207013e65b44e4cb4e2c2298f4ac457ba8f82743f31e930b' ;
assert . strictEqual ( _getAttachmentPath ( input ) , expected ) ;
assert . lengthOf ( _getAttachmentPath ( ) , PATH _LENGTH ) ;
} ) ;
} ) ;
} ) ;