/** * Created by PengLing on 2017/12/26. */ 'use strict'; const expect = require('chai').expect; const path = require('path'); const fs = require('fs'); const Attachment = require('../lib/attachment'); const opts = require('./config')(); describe('attachment', function () { this.timeout(10000); let attachment, key; let p = path.posix.join(__dirname, 'files', 'test-文档1.txt'); // let p = 'test-文档1.txt'; before(async () => { attachment = new Attachment(opts); makeFile(p); }); after(async () => { await deleteFile(p); }); it('upload file to local server successfully', function (done) { (async function () { try { let rslt = await attachment.upload(p); key = rslt.key; expect(rslt).to.be.an('object').that.has.all.keys('key'); expect(rslt.key).to.be.a('string'); done(); } catch (err) { done(err); } })() }) it('upload file successfully', function (done) { (async function () { try { let rslt = await attachment.upload(p); key = rslt.key; expect(rslt).to.be.an('object').that.has.all.keys('key'); expect(rslt.key).to.be.a('string'); done(); } catch (err) { done(err); } })(); }); it('download file successfully', function (done) { (async function () { try { let publicDownloadUrl = await attachment.download(key); expect(publicDownloadUrl).to.be.a('string'); done(); } catch (err) { done(err); } })(); }); it('remove file successfully', function (done) { (async function () { try { let statusCode = await attachment.remove(key); expect(statusCode).to.be.a('number'); done(); } catch (err) { done(err); } })(); }); function makeFile(p) { return new Promise((resolve, reject) => { fs.writeFile(p, "hello world!", function (err) { if (err) { reject(err); } resolve("The file was saved!"); }); }); } function deleteFile(p) { return new Promise((resolve, reject) => { fs.unlink(p, (err) => { if (err) { reject(`p: ${p}, error: delete file ${err}`); } else { resolve('success'); } }); }); } });