added test helper and rewrote tests
This commit is contained in:
parent
7a917c1f6b
commit
eaa6484dca
1
.gitignore
vendored
1
.gitignore
vendored
@ -112,3 +112,4 @@ dist
|
|||||||
**/.idea/tasks.xml
|
**/.idea/tasks.xml
|
||||||
**/.idea/shelf
|
**/.idea/shelf
|
||||||
**/.idea/*.iml
|
**/.idea/*.iml
|
||||||
|
/tmp/
|
||||||
|
88
src/helpers/test.ts
Normal file
88
src/helpers/test.ts
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
import supertest from 'supertest';
|
||||||
|
import should from 'should/as-function';
|
||||||
|
import db from "../db";
|
||||||
|
|
||||||
|
|
||||||
|
export default class TestHelper {
|
||||||
|
public static auth = {
|
||||||
|
admin: {pass: 'Abc123!#', key: '5ea131671feb9c2ee0aafc9a'},
|
||||||
|
janedoe: {pass: 'Xyz890*)', key: '5ea0450ed851c30a90e70899'}
|
||||||
|
}
|
||||||
|
public static res = {
|
||||||
|
400: {status: 'Bad request'},
|
||||||
|
401: {status: 'Unauthorized'},
|
||||||
|
403: {status: 'Forbidden'},
|
||||||
|
404: {status: 'Not found'},
|
||||||
|
}
|
||||||
|
|
||||||
|
static before (done) {
|
||||||
|
process.env.port = '2999';
|
||||||
|
process.env.NODE_ENV = 'test';
|
||||||
|
db.connect('test', done);
|
||||||
|
}
|
||||||
|
|
||||||
|
static beforeEach (server, done) {
|
||||||
|
delete require.cache[require.resolve('../index')]; // prevent loading from cache
|
||||||
|
server = require('../index');
|
||||||
|
db.drop(err => { // reset database
|
||||||
|
if (err) return done(err);
|
||||||
|
db.loadJson(require('../test/db.json'), done);
|
||||||
|
});
|
||||||
|
return server
|
||||||
|
}
|
||||||
|
|
||||||
|
static afterEach (server, done) {
|
||||||
|
server.close(done);
|
||||||
|
}
|
||||||
|
|
||||||
|
static request (server, done, options) { // options in form: {method, url, auth: {key/basic: 'name' or 'key'/{name, pass}}, httpStatus, req, res}
|
||||||
|
let st = supertest(server);
|
||||||
|
if (options.hasOwnProperty('auth') && options.auth.hasOwnProperty('key')) {
|
||||||
|
options.url += '?key=' + (this.auth.hasOwnProperty(options.auth.key)? this.auth[options.auth.key].key : options.auth.key);
|
||||||
|
}
|
||||||
|
switch (options.method) {
|
||||||
|
case 'get':
|
||||||
|
st = st.get(options.url)
|
||||||
|
break;
|
||||||
|
case 'post':
|
||||||
|
st = st.post(options.url)
|
||||||
|
break;
|
||||||
|
case 'put':
|
||||||
|
st = st.put(options.url)
|
||||||
|
break;
|
||||||
|
case 'delete':
|
||||||
|
st = st.delete(options.url)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (options.hasOwnProperty('req')) {
|
||||||
|
st = st.send(options.req);
|
||||||
|
}
|
||||||
|
if (options.hasOwnProperty('auth') && options.auth.hasOwnProperty('basic')) {
|
||||||
|
if (this.auth.hasOwnProperty(options.auth.basic)) {
|
||||||
|
st = st.auth(options.auth.basic, this.auth[options.auth.basic].pass)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
st = st.auth(options.auth.basic.name, options.auth.basic.pass)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
st = st.expect('Content-type', /json/)
|
||||||
|
.expect(options.httpStatus);
|
||||||
|
if (options.hasOwnProperty('res')) {
|
||||||
|
return st.end((err, res) => {
|
||||||
|
if (err) return done (err);
|
||||||
|
should(res.body).be.eql(options.res);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else if (this.res.hasOwnProperty(options.httpStatus) && options.default !== false) {
|
||||||
|
return st.end((err, res) => {
|
||||||
|
if (err) return done (err);
|
||||||
|
should(res.body).be.eql(this.res[options.httpStatus]);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return st;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,145 +1,69 @@
|
|||||||
import supertest from 'supertest';
|
import TestHelper from "../helpers/test";
|
||||||
import should from 'should/as-function';
|
|
||||||
import db from '../db';
|
|
||||||
|
|
||||||
|
|
||||||
describe('GET /', () => {
|
describe('/', () => {
|
||||||
let server;
|
let server;
|
||||||
|
before(done => TestHelper.before(done));
|
||||||
|
beforeEach(done => server = TestHelper.beforeEach(server, done));
|
||||||
|
afterEach(done => TestHelper.afterEach(server, done));
|
||||||
|
|
||||||
before(done => {
|
describe('GET /', () => {
|
||||||
process.env.port = '2999';
|
it('returns the root message', done => {
|
||||||
process.env.NODE_ENV = 'test';
|
TestHelper.request(server, done, {
|
||||||
db.connect('test', done);
|
method: 'get',
|
||||||
});
|
url: '/',
|
||||||
beforeEach(done => {
|
httpStatus: 200,
|
||||||
delete require.cache[require.resolve('../index')]; // prevent loading from cache
|
res: {status: 'API server up and running!'}
|
||||||
server = require('../index');
|
});
|
||||||
db.drop(err => { // reset database
|
|
||||||
if (err) return done(err);
|
|
||||||
db.loadJson(require('../test/db.json'), done);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
afterEach(done => {
|
|
||||||
server.close(done);
|
describe('Unknown routes', () => {
|
||||||
});
|
it('return a 404 message', done => {
|
||||||
it('returns the root message', done => {
|
TestHelper.request(server, done, {
|
||||||
supertest(server)
|
method: 'get',
|
||||||
.get('/')
|
url: '/unknownroute',
|
||||||
.expect('Content-type', /json/)
|
httpStatus: 404
|
||||||
.expect(200)
|
|
||||||
.end((err, res) => {
|
|
||||||
if (err) done (err);
|
|
||||||
should(res.body).be.eql({status: 'API server up and running!'});
|
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Unknown routes', () => {
|
|
||||||
let server;
|
|
||||||
|
|
||||||
before(done => {
|
|
||||||
db.connect('test', done);
|
|
||||||
});
|
|
||||||
beforeEach(done => {
|
|
||||||
delete require.cache[require.resolve('../index')]; // prevent loading from cache
|
|
||||||
server = require('../index');
|
|
||||||
db.drop(err => { // reset database
|
|
||||||
if (err) return done(err);
|
|
||||||
db.loadJson(require('../test/db.json'), done);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
afterEach(done => {
|
|
||||||
server.close(done);
|
describe('An unauthorized request', () => {
|
||||||
});
|
it('returns a 401 message', done => {
|
||||||
it('return a 404 message', done => {
|
TestHelper.request(server, done, {
|
||||||
supertest(server)
|
method: 'get',
|
||||||
.get('/unknownroute')
|
url: '/authorized',
|
||||||
.expect(404)
|
httpStatus: 401
|
||||||
.end((err, res) => {
|
});
|
||||||
if (err) done (err);
|
});
|
||||||
should(res.body).be.eql({status: 'Not found'});
|
it('does not work with correct username', done => {
|
||||||
done();
|
TestHelper.request(server, done, {
|
||||||
|
method: 'get',
|
||||||
|
url: '/authorized',
|
||||||
|
auth: {name: 'admin', pass: 'Abc123!!'},
|
||||||
|
httpStatus: 401
|
||||||
});
|
});
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('An unauthorized request', () => {
|
|
||||||
let server;
|
|
||||||
|
|
||||||
before(done => {
|
|
||||||
db.connect('test', done);
|
|
||||||
});
|
|
||||||
beforeEach(done => {
|
|
||||||
delete require.cache[require.resolve('../index')]; // prevent loading from cache
|
|
||||||
server = require('../index');
|
|
||||||
db.drop(err => { // reset database
|
|
||||||
if (err) return done(err);
|
|
||||||
db.loadJson(require('../test/db.json'), done);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
afterEach(done => {
|
|
||||||
server.close(done);
|
|
||||||
});
|
|
||||||
it('returns a 401 message', done => {
|
|
||||||
supertest(server)
|
|
||||||
.get('/authorized')
|
|
||||||
.expect(401)
|
|
||||||
.end((err, res) => {
|
|
||||||
if (err) done (err);
|
|
||||||
should(res.body).be.eql({status: 'Unauthorized'});
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
it('does not work with correct username', done => {
|
|
||||||
supertest(server)
|
|
||||||
.get('/authorized')
|
|
||||||
.auth('admin', 'Abc123!!')
|
|
||||||
.expect(401)
|
|
||||||
.end((err, res) => {
|
|
||||||
if (err) done (err);
|
|
||||||
should(res.body).be.eql({status: 'Unauthorized'});
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('An authorized request', () => {
|
describe('An authorized request', () => {
|
||||||
let server;
|
it('works with an API key', done => {
|
||||||
|
TestHelper.request(server, done, {
|
||||||
before(done => {
|
method: 'get',
|
||||||
db.connect('test', done);
|
url: '/authorized',
|
||||||
});
|
auth: {key: 'admin'},
|
||||||
beforeEach(done => {
|
httpStatus: 200,
|
||||||
delete require.cache[require.resolve('../index')]; // prevent loading from cache
|
res: {status: 'Authorization successful', method: 'key'}
|
||||||
server = require('../index');
|
});
|
||||||
db.drop(err => { // reset database
|
});
|
||||||
if (err) return done(err);
|
it('works with basic auth', done => {
|
||||||
db.loadJson(require('../test/db.json'), done);
|
TestHelper.request(server, done, {
|
||||||
|
method: 'get',
|
||||||
|
url: '/authorized',
|
||||||
|
auth: {basic: 'admin'},
|
||||||
|
httpStatus: 200,
|
||||||
|
res: {status: 'Authorization successful', method: 'basic'}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
afterEach(done => {
|
|
||||||
server.close(done);
|
|
||||||
});
|
|
||||||
it('works with an API key', done => {
|
|
||||||
supertest(server)
|
|
||||||
.get('/authorized?key=5ea131671feb9c2ee0aafc9a')
|
|
||||||
.expect(200)
|
|
||||||
.end((err, res) => {
|
|
||||||
if (err) done (err);
|
|
||||||
should(res.body).be.eql({status: 'Authorization successful', method: 'key'});
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
it('works with basic auth', done => {
|
|
||||||
supertest(server)
|
|
||||||
.get('/authorized')
|
|
||||||
.auth('admin', 'Abc123!#')
|
|
||||||
.expect(200)
|
|
||||||
.end((err, res) => {
|
|
||||||
if (err) done (err);
|
|
||||||
should(res.body).be.eql({status: 'Authorization successful', method: 'basic'});
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user