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/shelf
|
||||
**/.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 should from 'should/as-function';
|
||||
import db from '../db';
|
||||
import TestHelper from "../helpers/test";
|
||||
|
||||
|
||||
describe('GET /', () => {
|
||||
describe('/', () => {
|
||||
let server;
|
||||
before(done => TestHelper.before(done));
|
||||
beforeEach(done => server = TestHelper.beforeEach(server, done));
|
||||
afterEach(done => TestHelper.afterEach(server, done));
|
||||
|
||||
before(done => {
|
||||
process.env.port = '2999';
|
||||
process.env.NODE_ENV = 'test';
|
||||
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);
|
||||
describe('GET /', () => {
|
||||
it('returns the root message', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'get',
|
||||
url: '/',
|
||||
httpStatus: 200,
|
||||
res: {status: 'API server up and running!'}
|
||||
});
|
||||
});
|
||||
});
|
||||
afterEach(done => {
|
||||
server.close(done);
|
||||
});
|
||||
it('returns the root message', done => {
|
||||
supertest(server)
|
||||
.get('/')
|
||||
.expect('Content-type', /json/)
|
||||
.expect(200)
|
||||
.end((err, res) => {
|
||||
if (err) done (err);
|
||||
should(res.body).be.eql({status: 'API server up and running!'});
|
||||
done();
|
||||
|
||||
describe('Unknown routes', () => {
|
||||
it('return a 404 message', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'get',
|
||||
url: '/unknownroute',
|
||||
httpStatus: 404
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
it('return a 404 message', done => {
|
||||
supertest(server)
|
||||
.get('/unknownroute')
|
||||
.expect(404)
|
||||
.end((err, res) => {
|
||||
if (err) done (err);
|
||||
should(res.body).be.eql({status: 'Not found'});
|
||||
done();
|
||||
|
||||
describe('An unauthorized request', () => {
|
||||
it('returns a 401 message', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'get',
|
||||
url: '/authorized',
|
||||
httpStatus: 401
|
||||
});
|
||||
});
|
||||
it('does not work with correct username', 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', () => {
|
||||
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);
|
||||
describe('An authorized request', () => {
|
||||
it('works with an API key', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'get',
|
||||
url: '/authorized',
|
||||
auth: {key: 'admin'},
|
||||
httpStatus: 200,
|
||||
res: {status: 'Authorization successful', method: 'key'}
|
||||
});
|
||||
});
|
||||
});
|
||||
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();
|
||||
it('works with basic auth', done => {
|
||||
TestHelper.request(server, done, {
|
||||
method: 'get',
|
||||
url: '/authorized',
|
||||
auth: {basic: 'admin'},
|
||||
httpStatus: 200,
|
||||
res: {status: 'Authorization successful', method: 'basic'}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user