import fs from 'fs'; import rimraf from 'rimraf'; import chai, { assert, expect } from 'chai'; import chaiAsPromised from 'chai-as-promised'; import { join } from 'path'; import { BlobService } from '../'; import fsBlobStore from 'fs-blob-store'; import { bufferToHash } from '../../utils/buffer'; import { getBase64DataURI } from 'dauria'; import MockRes from 'mock-res'; chai.should(); chai.use(chaiAsPromised); describe('blob-store', () => { const blobStore = fsBlobStore(join(__dirname, 'blobs')); const service = new BlobService({ Model: blobStore }); const content = new Buffer('hello world!'); const contentHash = bufferToHash(content); const contentType = 'text/plain'; const contentUri = getBase64DataURI(content, contentType); const contentExt = 'txt'; const contentId = `${ contentHash }.${ contentExt }`; it('creates a blob', () => { return service.create({ uri: contentUri }, {}).then(blob => blob.id).should.eventually.equal(contentId); }); it('fetches a blob', () => { const response = new MockRes(); response.on('finish', () => { expect(response._getString()).to.equal('hello world!'); }); return service.create({ uri: contentUri }, {}) .then(blob => blob.id) .then(id => service.get(id, { response })) .then(blob => blob.id).should.eventually.equal(contentId); }); it('removes a blob', () => { return service.create({ uri: contentUri }, {}) .then(blob => blob.id) .then(id => service.remove(id)).should.eventually.equal(contentId); }); it('fails to get a non-existent blob', () => { const response = new MockRes(); return service.get(contentId, { response }).should.be.rejected; }); afterEach(() => { // remove the file after each test // have to check if the file exists first b/c the remove test will (obviously) // remove the file and `unlink` will fail. fs.access(join(__dirname, 'blobs', contentId), fs.F_OK, error => { if (typeof error === 'undefined') { fs.unlink(join(__dirname, 'blobs', contentId)); } }); }); after(() => { // after all tests, remove the blob directory rimraf(join(__dirname, 'blobs'), {}, error => { assert.equal(error, null); }); }); });