From fd4fe61de6f580ca4c51f322aa8e0e92bbea5f9c Mon Sep 17 00:00:00 2001 From: JbIPS Date: Tue, 30 Jan 2018 23:34:03 +0100 Subject: [PATCH] Supports Buffer as input Checks that `writeFile()` could handle a Buffer as the content of the file. --- lib/unifile-github.js | 5 +++++ test/unifile-dropbox.js | 10 ++++++++++ test/unifile-fs.js | 12 ++++++++++-- test/unifile-ftp.js | 9 ++++++++- test/unifile-github.js | 33 +++++++++++++++++---------------- test/unifile-sftp.js | 6 ++++++ 6 files changed, 56 insertions(+), 19 deletions(-) diff --git a/lib/unifile-github.js b/lib/unifile-github.js index ff8a68f..8cd671f 100644 --- a/lib/unifile-github.js +++ b/lib/unifile-github.js @@ -204,6 +204,10 @@ class GitHubConnector { .then(() => { session.token = sessionCopy.token; return session.token; + }) + .catch((e) => { + // Override default error message ('Requires authentication') + if(e.code === UnifileError.EACCES) throw new UnifileError(401, 'Bad credentials'); }); } @@ -896,6 +900,7 @@ class GitHubConnector { const {code, message} = (() => { const defaultMessage = JSON.parse(body).message; switch (res.statusCode) { + case 401: // fallthrough case 403: return {code: UnifileError.EACCES, message: defaultMessage}; case 404: diff --git a/test/unifile-dropbox.js b/test/unifile-dropbox.js index 2ea061d..0cf0798 100644 --- a/test/unifile-dropbox.js +++ b/test/unifile-dropbox.js @@ -440,6 +440,16 @@ describe('DropboxConnector', function() { }); }); + it('writes into a file with a Buffer', function() { + return connector.writeFile(session, 'unifile_writeFile/file1.txt', Buffer.from(data)) + .then(() => { + return connector.readFile(session, 'unifile_writeFile/file1.txt'); + }) + .then((content) => { + return expect(content.toString()).to.equal(data); + }); + }); + after('Remove folder', function() { if(isEnvValid()) connector.rmdir(session, 'unifile_writeFile'); else this.skip(); diff --git a/test/unifile-fs.js b/test/unifile-fs.js index 1957884..5837281 100644 --- a/test/unifile-fs.js +++ b/test/unifile-fs.js @@ -1,8 +1,9 @@ 'use strict'; +const Promise = require('bluebird'); +const Fs = Promise.promisifyAll(require('fs'), {suffix: 'Promised'}); const Path = require('path'); const Os = require('os'); -const Fs = require('fs'); const {Readable, Writable} = require('stream'); const chai = require('chai'); chai.use(require('chai-as-promised')); @@ -315,7 +316,14 @@ describe('FsConnector', function() { it('writes into a file', function() { return connector.writeFile({}, Path.join(Os.tmpdir(), filename), data) .then(() => { - Fs.statSync(Path.join(Os.tmpdir(), filename)).should.exist; + return Fs.readFilePromised(Path.join(Os.tmpdir(), filename), 'utf8').should.become(data); + }); + }); + + it('writes into a file with a Buffer', function() { + return connector.writeFile({}, Path.join(Os.tmpdir(), filename), Buffer.from(data)) + .then(() => { + return Fs.readFilePromised(Path.join(Os.tmpdir(), filename), 'utf8').should.become(data); }); }); diff --git a/test/unifile-ftp.js b/test/unifile-ftp.js index 7c9799e..8f8099c 100644 --- a/test/unifile-ftp.js +++ b/test/unifile-ftp.js @@ -327,7 +327,14 @@ describe('FtpConnector', function() { it('writes into a file', function() { return connector.writeFile(session, 'tmp.test', data) .then(() => { - Fs.readFilePromised('tmp.test', 'utf8').should.become(data); + return Fs.readFilePromised('tmp.test', 'utf8').should.become(data); + }); + }); + + it('writes into a file with a Buffer', function() { + return connector.writeFile(session, 'tmp.test', Buffer.from(data)) + .then(() => { + return Fs.readFilePromised('tmp.test', 'utf8').should.become(data); }); }); diff --git a/test/unifile-github.js b/test/unifile-github.js index 832631c..99e6e71 100644 --- a/test/unifile-github.js +++ b/test/unifile-github.js @@ -540,9 +540,23 @@ describe('GitHubConnector', function() { it('writes into a file', function() { return connector.writeFile(session, 'unifile_writeFile/test/testFile', data) .then(() => { - return connector.readFile(session, 'unifile_writeFile/test/testFile').then((content) => { - return expect(content.toString()).to.equal(data); - }); + return connector.readFile(session, 'unifile_writeFile/test/testFile'); + }) + .then((content) => { + return expect(content.toString()).to.equal(data); + }) + .then(() => { + return connector.unlink(session, 'unifile_writeFile/test/testFile'); + }); + }); + + it('writes into a file with a Buffer', function() { + return connector.writeFile(session, 'unifile_writeFile/test/testFile', Buffer.from(data)) + .then(() => { + return connector.readFile(session, 'unifile_writeFile/test/testFile'); + }) + .then((content) => { + return expect(content.toString()).to.equal(data); }) .then(() => { return connector.unlink(session, 'unifile_writeFile/test/testFile'); @@ -676,19 +690,6 @@ describe('GitHubConnector', function() { } else this.skip(); }); - it('throws an error if wrong credentials', function(done) { - const stream = connector.createReadStream(Object.assign({}, session, { - token: 'aoa' - }), 'aouoeuoeu'); - stream.on('error', (err) => { - expect(err.message).to.equal('Bad credentials'); - done(); - }); - stream.on('data', () => { - done(new Error('Should not emit this event')); - }); - }); - it('throws an error if the path does not exist', function(done) { const stream = connector.createReadStream(session, 'aouoeuoeu'); stream.on('error', (err) => { diff --git a/test/unifile-sftp.js b/test/unifile-sftp.js index 25139d1..4559fa1 100644 --- a/test/unifile-sftp.js +++ b/test/unifile-sftp.js @@ -513,6 +513,12 @@ describe('SFTPConnector', function() { .then(() => Fs.readFilePromised('tmp.test', 'utf8').should.become(data)) .then(() => Fs.unlinkPromised('tmp.test')); }); + + it('writes into a file with a Buffer', function() { + return connector.writeFile(session, 'tmp.test', Buffer.from(data)) + .then(() => Fs.readFilePromised('tmp.test', 'utf8').should.become(data)) + .then(() => Fs.unlinkPromised('tmp.test')); + }); }); describe('createWriteStream()', function() {