// Copyright IBM Corp. 2013,2019. All Rights Reserved. // Node module: loopback-component-storage // This file is licensed under the Artistic License 2.0. // License text available at https://opensource.org/licenses/Artistic-2.0 'use strict'; var FileSystemProvider = require('../lib/providers/filesystem/index.js').Client; var assert = require('assert'); var path = require('path'); function verifyMetadata(fileOrContainer, name) { assert(fileOrContainer.getMetadata()); assert.equal(fileOrContainer.getMetadata().name, name); assert(fileOrContainer.getMetadata().uid === undefined); assert(fileOrContainer.getMetadata().gid === undefined); assert(fileOrContainer.getMetadata().atime); assert(fileOrContainer.getMetadata().ctime); assert(fileOrContainer.getMetadata().mtime); assert.equal(typeof fileOrContainer.getMetadata().size, 'number'); } describe('FileSystem based storage provider', function() { describe('container apis', function() { var client = null; it('should require an existing directory as the root', function(done) { client = new FileSystemProvider({ root: path.join(__dirname, 'storage'), }); process.nextTick(done); }); it('should work even it is ran from other path', function(done) { process.chdir('../../../'); try { console.log(`running from ${process.cwd()}`); client = new FileSystemProvider({ root: path.join(__dirname, 'storage'), }); process.nextTick(done); } catch (error) { process.nextTick(done(error)); } }); it('should complain if the root directory doesn\'t exist', function(done) { try { client = new FileSystemProvider({ root: path.join(__dirname, '_storage'), }); process.nextTick(done.bind(null, 'Error')); } catch (err) { // Should be here process.nextTick(done); } }); it('should return an empty list of containers', function(done) { client.getContainers(function(err, containers) { assert(!err); assert.equal(containers.length, 0); done(err, containers); }); }); it('should return an empty list of containers - promise', function(done) { client.getContainers() .then(function(containers) { assert.equal(containers.length, 0); done(); }) .catch(done); }); it('should create a new container', function(done) { client.createContainer({name: 'c1'}, function(err, container) { assert(!err); verifyMetadata(container, 'c1'); done(err, container); }); }); it('should create a new container - promise', function(done) { client.createContainer({name: 'c3'}) .then(function(container) { verifyMetadata(container, 'c3'); done(); }) .catch(done); }); it('should get a container c1', function(done) { client.getContainer('c1', function(err, container) { assert(!err); verifyMetadata(container, 'c1'); done(err, container); }); }); it('should get a container c1 - promise', function(done) { client.getContainer('c1') .then(function(container) { verifyMetadata(container, 'c1'); done(); }) .catch(done); }); it('should not get a container c2', function(done) { client.getContainer('c2', function(err, container) { assert(err); done(null, container); }); }); it('should destroy a container c3 - promise', function(done) { client.destroyContainer('c3') .then(function(container) { done(null, container); }) .catch(done); }); it('should return one container', function(done) { client.getContainers(function(err, containers) { assert(!err); assert.equal(containers.length, 1); done(err, containers); }); }); it('should destroy a container c1', function(done) { client.destroyContainer('c1', function(err, container) { assert(!err); done(err, container); }); }); it('should not get a container c1 after destroy', function(done) { client.getContainer('c1', function(err, container) { assert(err); done(null, container); }); }); }); describe('file apis', function() { var fs = require('fs'); var client = new FileSystemProvider({ root: path.join(__dirname, 'storage'), }); it('should create a new container', function(done) { client.createContainer({name: 'c1'}, function(err, container) { assert(!err); done(err, container); }); }); it('should upload a file', function(done) { var writer = client.upload({container: 'c1', remote: 'f1.txt'}); fs.createReadStream(path.join(__dirname, 'files/f1.txt')).pipe(writer); writer.on('finish', done); writer.on('error', done); }); /* eslint-disable mocha/handle-done-callback */ it('should fail to upload a file with invalid characters', function(done) { var writer = client.upload({container: 'c1', remote: 'a/f1.txt'}); fs.createReadStream(path.join(__dirname, 'files/f1.txt')).pipe(writer); var cb = done; var clearCb = function() {}; writer.on('error', function() { cb(); cb = clearCb; }); writer.on('finish', function() { cb(new Error('Should have finished with error callback')); cb = clearCb; }); }); /* eslint-enable mocha/handle-done-callback */ it('should download a file', function(done) { var reader = client.download({ container: 'c1', remote: 'f1.txt', }); reader.pipe( fs.createWriteStream(path.join(__dirname, 'files/f1_downloaded.txt')) ); reader.on('end', done); reader.on('error', done); }); /* eslint-disable mocha/handle-done-callback */ it('should fail to download a file with invalid characters', function(done) { var reader = client.download({container: 'c1', remote: 'a/f1.txt'}); reader.pipe( fs.createWriteStream(path.join(__dirname, 'files/a-f1_downloaded.txt')) ); var cb = done; var clearCb = function() {}; reader.on('error', function() { cb(); cb = clearCb; }); reader.on('end', function() { cb(new Error('Expected error: Invalid name')); cb = clearCb; }); }); /* eslint-enable mocha/handle-done-callback */ it('should get files for a container', function(done) { client.getFiles('c1', function(err, files) { assert(!err); assert.equal(files.length, 1); done(err, files); }); }); it('should get files for a container - promise', function(done) { client.getFiles('c1') .then(function(files) { assert.equal(files.length, 1); done(); }) .catch(done); }); it('should get a file', function(done) { client.getFile('c1', 'f1.txt', function(err, f) { assert(!err); assert.ok(f); verifyMetadata(f, 'f1.txt'); done(err, f); }); }); it('should get a file - promise', function(done) { client.getFile('c1', 'f1.txt') .then(function(f) { assert.ok(f); verifyMetadata(f, 'f1.txt'); done(); }) .catch(done); }); it('should remove a file', function(done) { client.removeFile('c1', 'f1.txt', function(err) { assert(!err); done(err); }); }); it('should remove a file - promise', function(done) { createFile('c1', 'f1.txt').then(function() { return client.removeFile('c1', 'f1.txt') .then(function() { done(); }); }) .catch(done); }); it('should get no files from a container', function(done) { client.getFiles('c1', function(err, files) { assert(!err); assert.equal(files.length, 0); done(err, files); }); }); it('should get no files from a container - promise', function(done) { client.getFiles('c1') .then(function(files) { assert.equal(files.length, 0); done(); }) .catch(done); }); it('should not get a file from a container', function(done) { client.getFile('c1', 'f2.txt', function(err, f) { assert(err); assert.equal(err.code, 'ENOENT'); assert(!f); done(); }); }); it('should not get a file from a container - promise', function(done) { client.getFile('c1', 'f2.txt') .then(function() { throw new Error('should not be throw'); }) .catch(function(err) { assert.equal(err.code, 'ENOENT'); done(); }); }); it('should destroy a container c1', function(done) { client.destroyContainer('c1', function(err, container) { assert(!err); done(err, container); }); }); function createFile(container, file) { return new Promise(function(resolve, reject) { var writer = client.upload({container: container, remote: file}); fs.createReadStream(path.join(__dirname, 'files/f1.txt')).pipe(writer); writer.on('finish', resolve); writer.on('error', reject); }); } }); });