2020-05-08 09:58:12 +02:00
|
|
|
import TestHelper from "../test/helper";
|
2020-06-05 08:50:06 +02:00
|
|
|
import should from 'should/as-function';
|
2020-05-28 11:47:51 +02:00
|
|
|
import db from '../db';
|
2020-01-14 13:25:13 +01:00
|
|
|
|
|
|
|
|
2020-04-27 11:44:28 +02:00
|
|
|
describe('/', () => {
|
2020-04-22 17:24:15 +02:00
|
|
|
let server;
|
2020-04-27 11:44:28 +02:00
|
|
|
before(done => TestHelper.before(done));
|
|
|
|
beforeEach(done => server = TestHelper.beforeEach(server, done));
|
|
|
|
afterEach(done => TestHelper.afterEach(server, done));
|
2020-05-28 11:47:51 +02:00
|
|
|
after(done => TestHelper.after(done));
|
2020-01-14 13:25:13 +01:00
|
|
|
|
2020-04-27 11:44:28 +02:00
|
|
|
describe('GET /', () => {
|
|
|
|
it('returns the root message', done => {
|
|
|
|
TestHelper.request(server, done, {
|
|
|
|
method: 'get',
|
|
|
|
url: '/',
|
|
|
|
httpStatus: 200,
|
|
|
|
res: {status: 'API server up and running!'}
|
2020-01-14 13:25:13 +01:00
|
|
|
});
|
2020-04-22 17:24:15 +02:00
|
|
|
});
|
|
|
|
});
|
2020-04-23 13:59:45 +02:00
|
|
|
|
2020-06-05 08:50:06 +02:00
|
|
|
describe('GET /changelog/{timestamp}/{page}/{pagesize}', () => {
|
|
|
|
it('returns the first page', done => {
|
|
|
|
TestHelper.request(server, done, {
|
|
|
|
method: 'get',
|
|
|
|
url: '/changelog/1979-07-28T06:04:51.000Z/0/2',
|
|
|
|
auth: {basic: 'admin'},
|
|
|
|
httpStatus: 200
|
|
|
|
}).end((err, res) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
should(res.body).have.lengthOf(2);
|
|
|
|
should(res.body[0].date).be.eql('1979-07-28T06:04:51.000Z');
|
|
|
|
should(res.body[1].date).be.eql('1979-07-28T06:04:50.000Z');
|
|
|
|
should(res.body).matchEach(log => {
|
|
|
|
should(log).have.only.keys('date', 'action', 'collection', 'conditions', 'data');
|
|
|
|
should(log).have.property('action', 'PUT /sample/400000000000000000000001');
|
|
|
|
should(log).have.property('collection', 'samples');
|
|
|
|
should(log).have.property('conditions', {_id: '400000000000000000000001'});
|
|
|
|
should(log).have.property('data', {type: 'part', status: 0});
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('returns another page', done => {
|
|
|
|
TestHelper.request(server, done, {
|
|
|
|
method: 'get',
|
|
|
|
url: '/changelog/1979-07-28T06:04:51.000Z/1/2',
|
|
|
|
auth: {basic: 'admin'},
|
|
|
|
httpStatus: 200
|
|
|
|
}).end((err, res) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
should(res.body).have.lengthOf(1);
|
|
|
|
should(res.body[0].date).be.eql('1979-07-28T06:04:49.000Z');
|
|
|
|
should(res.body).matchEach(log => {
|
|
|
|
should(log).have.only.keys('date', 'action', 'collection', 'conditions', 'data');
|
|
|
|
should(log).have.property('action', 'PUT /sample/400000000000000000000001');
|
|
|
|
should(log).have.property('collection', 'samples');
|
|
|
|
should(log).have.property('conditions', {_id: '400000000000000000000001'});
|
|
|
|
should(log).have.property('data', {type: 'part', status: 0});
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('returns an empty array for a page with no results', done => {
|
|
|
|
TestHelper.request(server, done, {
|
|
|
|
method: 'get',
|
|
|
|
url: '/changelog/1979-07-28T06:04:51.000Z/10/2',
|
|
|
|
auth: {basic: 'admin'},
|
|
|
|
httpStatus: 200
|
|
|
|
}).end((err, res) => {
|
|
|
|
if (err) return done(err);
|
|
|
|
should(res.body).have.lengthOf(0);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('rejects timestamps pre unix epoch', done => {
|
|
|
|
TestHelper.request(server, done, {
|
|
|
|
method: 'get',
|
|
|
|
url: '/changelog/1879-07-28T06:04:51.000Z/10/2',
|
|
|
|
auth: {basic: 'admin'},
|
|
|
|
httpStatus: 400,
|
|
|
|
res: {status: 'Invalid body format', details: '"timestamp" must be larger than or equal to "1970-01-01T00:00:00.000Z"'}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('rejects invalid timestamps', done => {
|
|
|
|
TestHelper.request(server, done, {
|
|
|
|
method: 'get',
|
|
|
|
url: '/changelog/1979-14-28T06:04:51.000Z/10/2',
|
|
|
|
auth: {basic: 'admin'},
|
|
|
|
httpStatus: 400,
|
|
|
|
res: {status: 'Invalid body format', details: '"timestamp" must be in ISO 8601 date format'}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('rejects negative page numbers', done => {
|
|
|
|
TestHelper.request(server, done, {
|
|
|
|
method: 'get',
|
|
|
|
url: '/changelog/1979-07-28T06:04:51.000Z/-10/2',
|
|
|
|
auth: {basic: 'admin'},
|
|
|
|
httpStatus: 400,
|
|
|
|
res: {status: 'Invalid body format', details: '"page" must be larger than or equal to 0'}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('rejects negative pagesizes', done => {
|
|
|
|
TestHelper.request(server, done, {
|
|
|
|
method: 'get',
|
|
|
|
url: '/changelog/1979-07-28T06:04:51.000Z/10/-2',
|
|
|
|
auth: {basic: 'admin'},
|
|
|
|
httpStatus: 400,
|
|
|
|
res: {status: 'Invalid body format', details: '"pagesize" must be larger than or equal to 0'}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('rejects request from a write user', done => {
|
|
|
|
TestHelper.request(server, done, {
|
|
|
|
method: 'get',
|
|
|
|
url: '/changelog/1979-07-28T06:04:51.000Z/10/2',
|
|
|
|
auth: {basic: 'janedoe'},
|
|
|
|
httpStatus: 403
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('rejects requests from an API key', done => {
|
|
|
|
TestHelper.request(server, done, {
|
|
|
|
method: 'get',
|
|
|
|
url: '/changelog/1979-07-28T06:04:51.000Z/10/2',
|
|
|
|
auth: {key: 'admin'},
|
|
|
|
httpStatus: 401
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('rejects unauthorized requests', done => {
|
|
|
|
TestHelper.request(server, done, {
|
|
|
|
method: 'get',
|
|
|
|
url: '/changelog/1979-07-28T06:04:51.000Z/10/2',
|
|
|
|
httpStatus: 401
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-04-27 11:44:28 +02:00
|
|
|
describe('Unknown routes', () => {
|
|
|
|
it('return a 404 message', done => {
|
|
|
|
TestHelper.request(server, done, {
|
|
|
|
method: 'get',
|
|
|
|
url: '/unknownroute',
|
|
|
|
httpStatus: 404
|
|
|
|
});
|
2020-04-23 13:59:45 +02:00
|
|
|
});
|
|
|
|
});
|
2020-04-27 11:44:28 +02:00
|
|
|
|
|
|
|
describe('An unauthorized request', () => {
|
|
|
|
it('returns a 401 message', done => {
|
|
|
|
TestHelper.request(server, done, {
|
|
|
|
method: 'get',
|
|
|
|
url: '/authorized',
|
|
|
|
httpStatus: 401
|
2020-04-23 13:59:45 +02:00
|
|
|
});
|
2020-04-27 11:44:28 +02:00
|
|
|
});
|
|
|
|
it('does not work with correct username', done => {
|
|
|
|
TestHelper.request(server, done, {
|
|
|
|
method: 'get',
|
|
|
|
url: '/authorized',
|
2020-05-28 11:47:51 +02:00
|
|
|
auth: {basic: {name: 'admin', pass: 'Abc123!!'}},
|
|
|
|
httpStatus: 401
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('does not work with incorrect username', done => {
|
|
|
|
TestHelper.request(server, done, {
|
|
|
|
method: 'get',
|
|
|
|
url: '/authorized',
|
|
|
|
auth: {basic: {name: 'adminxx', pass: 'Abc123!!'}},
|
2020-04-27 11:44:28 +02:00
|
|
|
httpStatus: 401
|
2020-04-23 13:59:45 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-04-27 11:44:28 +02:00
|
|
|
|
|
|
|
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'}
|
2020-04-23 13:59:45 +02:00
|
|
|
});
|
2020-04-27 11:44:28 +02:00
|
|
|
});
|
|
|
|
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'}
|
2020-04-23 13:59:45 +02:00
|
|
|
});
|
2020-04-27 11:44:28 +02:00
|
|
|
});
|
2020-04-22 17:24:15 +02:00
|
|
|
});
|
2020-05-28 11:47:51 +02:00
|
|
|
|
|
|
|
describe('An invalid JSON body', () => {
|
|
|
|
it('is rejected', done => {
|
|
|
|
TestHelper.request(server, done, {
|
|
|
|
method: 'post',
|
|
|
|
url: '/',
|
|
|
|
httpStatus: 400,
|
|
|
|
reqType: 'json',
|
|
|
|
req: '{"xxx"}',
|
|
|
|
res: {status: 'Invalid JSON body'}
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-28 12:18:38 +02:00
|
|
|
describe('A not connected database', () => { // RUN AS LAST OR RECONNECT DATABASE!!
|
2020-05-28 11:47:51 +02:00
|
|
|
it('resolves to an 500 error', done => {
|
|
|
|
db.disconnect(() => {
|
|
|
|
TestHelper.request(server, done, {
|
|
|
|
method: 'get',
|
|
|
|
url: '/',
|
|
|
|
httpStatus: 500
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-05-28 12:18:38 +02:00
|
|
|
});
|
2020-05-28 11:47:51 +02:00
|
|
|
|
2020-05-28 12:18:38 +02:00
|
|
|
describe('The /api/{url} redirect', () => {
|
|
|
|
let server;
|
|
|
|
let counter = 0; // count number of current test method
|
|
|
|
before(done => {
|
|
|
|
process.env.port = '2999';
|
|
|
|
db.connect('test', done);
|
|
|
|
});
|
|
|
|
beforeEach(done => {
|
|
|
|
process.env.NODE_ENV = counter === 1 ? 'production' : 'test';
|
|
|
|
counter ++;
|
|
|
|
server = TestHelper.beforeEach(server, done);
|
|
|
|
});
|
|
|
|
afterEach(done => TestHelper.afterEach(server, done));
|
|
|
|
after(done => TestHelper.after(done));
|
|
|
|
|
|
|
|
|
|
|
|
it('returns the right method', done => {
|
|
|
|
TestHelper.request(server, done, {
|
|
|
|
method: 'get',
|
|
|
|
url: '/api/authorized',
|
|
|
|
auth: {basic: 'admin'},
|
|
|
|
httpStatus: 200,
|
|
|
|
res: {status: 'Authorization successful', method: 'basic'}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('is disabled in production', done => {
|
|
|
|
TestHelper.request(server, done, {
|
|
|
|
method: 'get',
|
|
|
|
url: '/api/authorized',
|
|
|
|
auth: {basic: 'admin'},
|
|
|
|
httpStatus: 404
|
|
|
|
});
|
|
|
|
});
|
2020-04-22 17:24:15 +02:00
|
|
|
});
|