Archived
2

/api/ subroutes only available in dev/test

This commit is contained in:
VLE2FE 2020-05-28 12:18:38 +02:00
parent 0ea28fa50a
commit 8276e5108c
5 changed files with 43 additions and 10 deletions

View File

@ -52,10 +52,12 @@ app.use((req, res, next) => { // no database connection error
app.use(require('./helpers/authorize')); // handle authentication
// redirect /api routes for Angular proxy in development
app.use('/api/:url', (req, res) => {
if (process.env.NODE_ENV !== 'production') {
app.use('/api/:url', (req, res) => {
req.url = '/' + req.params.url;
app.handle(req, res);
});
});
}
// require routes

View File

@ -2,7 +2,7 @@ import mongoose from 'mongoose';
import SampleModel from './sample';
import MeasurementTemplateModel from './measurement_template';
// TODO: change to sample_id
const MeasurementSchema = new mongoose.Schema({
sample_id: {type: mongoose.Schema.Types.ObjectId, ref: SampleModel},

View File

@ -5,8 +5,6 @@ import globals from '../globals';
// TODO: restore measurements for m/a
// TODO: coverage!!!
describe('/measurement', () => {
let server;

View File

@ -91,7 +91,7 @@ describe('/', () => {
});
});
describe('A not connected database', () => {
describe('A not connected database', () => { // RUN AS LAST OR RECONNECT DATABASE!!
it('resolves to an 500 error', done => {
db.disconnect(() => {
TestHelper.request(server, done, {
@ -102,6 +102,39 @@ describe('/', () => {
});
});
});
// describe('API') // TODO not in production
});
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
});
});
});

View File

@ -43,7 +43,7 @@ export default class TestHelper {
db.disconnect(done);
}
static request (server, done, options) { // options in form: {method, url, auth: {key/basic: 'name' or 'key'/{name, pass}}, httpStatus, req, res}
static request (server, done, options) { // options in form: {method, url, auth: {key/basic: 'name' or 'key'/{name, pass}}, httpStatus, req, res, default (set to false if you want to dismiss default .end handling)}
let st = supertest(server);
if (options.hasOwnProperty('auth') && options.auth.hasOwnProperty('key')) { // resolve API key
options.url += '?key=' + (this.auth.hasOwnProperty(options.auth.key)? this.auth[options.auth.key].key : options.auth.key);