Skip to content

Commit

Permalink
test(restbox) coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Nov 8, 2018
1 parent a2852e9 commit 7763c48
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 4 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Restbox [![License][LicenseIMGURL]][LicenseURL] [![NPM version][NPMIMGURL]][NPMURL] [![Dependency Status][DependencyStatusIMGURL]][DependencyStatusURL] [![Build Status][BuildStatusIMGURL]][BuildStatusURL]
# Restbox [![License][LicenseIMGURL]][LicenseURL] [![NPM version][NPMIMGURL]][NPMURL] [![Dependency Status][DependencyStatusIMGURL]][DependencyStatusURL] [![Build Status][BuildStatusIMGURL]][BuildStatusURL] [![Coverage][CoverageIMGURL]][CoverageURL]

**REST** for **CRUD** file operations on `dropbox`.

Expand Down Expand Up @@ -48,7 +48,7 @@ const ip = '0.0.0.0';
app.use(restbox({
token: 'your dropbox token',
prefix: '/dropbox', // default
root: '/', // default, coud be string or function
root: '/', // default, can be string or function
}));

app.use(express.static(__dirname));
Expand All @@ -69,3 +69,6 @@ MIT
[DependencyStatusURL]: https://david-dm.org/coderaiser/restbox "Dependency Status"
[LicenseURL]: https://tldrlegal.com/license/mit-license "MIT License"

[CoverageURL]: https://coveralls.io/github/coderaiser/try-to-tape?branch=master
[CoverageIMGURL]: https://coveralls.io/repos/coderaiser/try-to-tape/badge.svg?branch=master&service=github

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"string-to-stream": "^1.1.1",
"tape": "^4.5.1",
"through2": "^3.0.0",
"try-to-catch": "^1.0.2"
"try-to-catch": "^1.0.2",
"try-to-tape": "^1.0.0"
},
"repository": {
"type": "git",
Expand Down
22 changes: 22 additions & 0 deletions server/rest/common/operate.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';

const test = require('tape');
const tryToCatch = require('try-to-catch');
const sinon = require('sinon');

const operate = require('./operate');

const reject = Promise.reject.bind(Promise);

test('restbox: operate', async (t) => {
const copy = sinon
.stub()
.returns(reject(Error('error')));

const [e] = await tryToCatch(operate, copy, 'token', 'dir', [
'1.txt'
]);

t.equal(e.message, 'error', 'should equal');
t.end();
});
3 changes: 2 additions & 1 deletion server/rest/get.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ test('restbox: get: readbox: hash', async (t) => {
.stub()
.returns(31337);

const {body} = await request.get('/dropbox/fs/?hash');
const res = await request.get('/dropbox/fs/?hash');
const {body} = res;

Math.random = random;

Expand Down
99 changes: 99 additions & 0 deletions server/rest/move.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
'use strict';

const calledWithDiff = require('sinon-called-with-diff');
const sinon = calledWithDiff(require('sinon'));
const test = require('tape');

const mockRequire = require('mock-require');
const {reRequire} = mockRequire;

const restbox = require('../restbox');
const {request} = require('serve-once')(restbox);

const pathPut = './move';
const pathRestbox = '../restbox';

test('restbox: move: no token', async (t) => {
const {body} = await request.put('/dropbox/mv', {
body: {
from: '/',
to: '/tmp',
names: ['1.txt'],
}
});

t.equal(body, 'token should be a string!', 'should return error message');
t.end();
});

test('restbox: move', async (t) => {
const operate = sinon
.stub()
.returns(Promise.resolve());

mockRequire('./common/operate', operate);

reRequire(pathPut);
const restbox = reRequire(pathRestbox);
const {request} = reRequire('serve-once')(restbox);
const {move} = require('@cloudcmd/dropbox');

const token = 'hello'
const options = {
token,
};

const names = [
'1.txs',
];

const files = {
from: '/',
to: '/tmp',
names,
};

await request.put('/dropbox/mv', {
body: files,
options,
});

t.ok(operate.calledWith(move, token, '/', '/tmp', names), 'should call mkdir with token');
t.end();
});

test('restbox: move: response', async (t) => {
const operate = sinon
.stub()
.returns(Promise.resolve());

mockRequire('./common/operate', operate);

reRequire(pathPut);
const restbox = reRequire(pathRestbox);
const {request} = reRequire('serve-once')(restbox);

const token = 'hello'
const options = {
token,
};

const names = [
'1.txt',
];

const files = {
from: '/',
to: '/tmp',
names,
};

const {body} = await request.put('/dropbox/mv', {
body: files,
options,
});

t.equal(body, 'move: ok("1.txt")', 'should equal');
t.end();
});

28 changes: 28 additions & 0 deletions server/restbox.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const {promisify} = require('util');

const express = require('express');
const http = require('http');

const restbox = require('./restbox');
const fetch = require('node-fetch');
const {serve} = require('serve-once')(restbox);

const tryToCatch = require('try-to-catch');
const currify = require('currify');

const tryToTape = require('try-to-tape');
const test = tryToTape(require('tape'));

test('restbox: no options', async (t) => {
const {port, close} = await serve();
const response = await fetch(`http://localhost:${port}/dropbox/fs/`);
const result = await response.text();

await close();

t.equal(result, 'token should be a string!', 'should equal');
t.end();
});

0 comments on commit 7763c48

Please sign in to comment.