2020-01-14 13:25:13 +01:00
|
|
|
import supertest from 'supertest';
|
|
|
|
import should from 'should/as-function';
|
2020-04-22 17:24:15 +02:00
|
|
|
import db from '../db';
|
2020-01-14 13:25:13 +01:00
|
|
|
|
|
|
|
|
2020-04-22 17:24:15 +02:00
|
|
|
describe('/', () => {
|
|
|
|
let server;
|
2020-01-14 13:25:13 +01:00
|
|
|
|
2020-04-22 17:24:15 +02:00
|
|
|
before(done => {
|
|
|
|
process.env.port = '2999';
|
2020-04-22 17:38:24 +02:00
|
|
|
process.env.NODE_ENV = 'test';
|
2020-04-22 17:24:15 +02:00
|
|
|
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 the root message', done => {
|
|
|
|
supertest(server)
|
2020-01-14 13:25:13 +01:00
|
|
|
.get('/')
|
|
|
|
.expect('Content-type', /json/)
|
2020-04-23 13:59:45 +02:00
|
|
|
.expect(200)
|
|
|
|
.end((err, res) => {
|
|
|
|
if (err) done (err);
|
2020-04-22 17:38:24 +02:00
|
|
|
should(res.body).be.eql({status: 'API server up and running!'});
|
2020-01-14 13:25:13 +01:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-04-22 17:24:15 +02:00
|
|
|
|
2020-04-23 13:59:45 +02:00
|
|
|
describe('Unknown routes', () => {
|
2020-04-22 17:24:15 +02:00
|
|
|
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);
|
|
|
|
});
|
2020-04-23 13:59:45 +02:00
|
|
|
it('return a 404 message', done => {
|
2020-04-22 17:24:15 +02:00
|
|
|
supertest(server)
|
|
|
|
.get('/unknownroute')
|
2020-04-23 13:59:45 +02:00
|
|
|
.expect(404)
|
|
|
|
.end((err, res) => {
|
|
|
|
if (err) done (err);
|
|
|
|
should(res.body).be.eql({status: 'Not found'});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
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();
|
|
|
|
});
|
2020-04-22 17:24:15 +02:00
|
|
|
});
|
|
|
|
});
|