mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 07:37:25 +01:00
misc fixes and improvements, starts adding route tests
This commit is contained in:
@@ -1,30 +1,13 @@
|
|||||||
import {
|
import { requester } from '../../../../helpers/api-integration.helper';
|
||||||
generateRes,
|
|
||||||
generateReq,
|
|
||||||
generateNext,
|
|
||||||
} from '../../../../helpers/api-unit.helper';
|
|
||||||
|
|
||||||
import notFoundHandler from '../../../../../website/src/middlewares/api-v3/notFound';
|
describe('notFound Middleware', () => {
|
||||||
|
it('returns a 404 error when the resource is not found', () => {
|
||||||
|
let request = requester().get('/api/v3/dummy-url');
|
||||||
|
|
||||||
import { NotFound } from '../../../../../website/src/libs/api-v3/errors';
|
return expect(request)
|
||||||
|
.to.eventually.be.rejected.and.eql({
|
||||||
describe('notFoundHandler', () => {
|
error: "NotFound",
|
||||||
let res, req, next;
|
message: "Not found.",
|
||||||
|
});
|
||||||
beforeEach(() => {
|
|
||||||
res = generateRes();
|
|
||||||
req = generateReq();
|
|
||||||
next = generateNext();
|
|
||||||
});
|
|
||||||
|
|
||||||
xit('sends NotFound error if the resource isn\'t found', () => {
|
|
||||||
expect(res.status).to.be.calledOnce;
|
|
||||||
expect(res.json).to.be.calledOnce;
|
|
||||||
|
|
||||||
expect(res.status).to.be.calledWith(404);
|
|
||||||
expect(res.json).to.be.calledWith({
|
|
||||||
error: 'NotFound',
|
|
||||||
message: 'Not found.',
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -22,11 +22,11 @@ let api = {};
|
|||||||
*/
|
*/
|
||||||
api.exampleRoute = {
|
api.exampleRoute = {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
url: '/example/:param',
|
url: '/example/:id',
|
||||||
middlewares: [],
|
middlewares: [],
|
||||||
handler (req, res) {
|
handler (req, res) {
|
||||||
res.status(200).send({
|
res.status(200).send({
|
||||||
status: 'ok',
|
status: req.params.id,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -14,8 +14,6 @@ var IS_PROD = nconf.get('IS_PROD');
|
|||||||
var IS_DEV = nconf.get('IS_DEV');
|
var IS_DEV = nconf.get('IS_DEV');
|
||||||
var cores = Number(nconf.get('WEB_CONCURRENCY')) || 0;
|
var cores = Number(nconf.get('WEB_CONCURRENCY')) || 0;
|
||||||
|
|
||||||
if (IS_DEV) Error.stackTraceLimit = Infinity;
|
|
||||||
|
|
||||||
// Setup the cluster module
|
// Setup the cluster module
|
||||||
if (cores !== 0 && cluster.isMaster && (IS_DEV || IS_PROD)) {
|
if (cores !== 0 && cluster.isMaster && (IS_DEV || IS_PROD)) {
|
||||||
// Fork workers. If config.json has CORES=x, use that - otherwise, use all cpus-1 (production)
|
// Fork workers. If config.json has CORES=x, use that - otherwise, use all cpus-1 (production)
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ if (IS_PROD) {
|
|||||||
logger
|
logger
|
||||||
.add(winston.transports.Console, {
|
.add(winston.transports.Console, {
|
||||||
colorize: true,
|
colorize: true,
|
||||||
|
prettyPrint: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import fs from 'fs';
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
|
||||||
const CONTROLLERS_PATH = path.join(__dirname, '/../../controllers/api-v3/');
|
const CONTROLLERS_PATH = path.join(__dirname, '/../../controllers/api-v3/');
|
||||||
let router = express.Router(); // eslint-disable-line new-cap
|
let router = express.Router(); // eslint-disable-line new-cap
|
||||||
|
|
||||||
|
|||||||
@@ -5,9 +5,15 @@ import errorHandler from './errorHandler';
|
|||||||
import bodyParser from 'body-parser';
|
import bodyParser from 'body-parser';
|
||||||
import routes from '../../libs/api-v3/setupRoutes';
|
import routes from '../../libs/api-v3/setupRoutes';
|
||||||
import notFoundHandler from './notFound';
|
import notFoundHandler from './notFound';
|
||||||
|
import nconf from 'nconf';
|
||||||
|
import morgan from 'morgan';
|
||||||
|
|
||||||
|
const IS_PROD = nconf.get('IS_PROD');
|
||||||
|
const DISABLE_LOGGING = nconf.get('DISABLE_REQUEST_LOGGING');
|
||||||
|
|
||||||
export default function attachMiddlewares (app) {
|
export default function attachMiddlewares (app) {
|
||||||
// Parse query parameters and json bodies
|
if (!IS_PROD && !DISABLE_LOGGING) app.use(morgan('dev'));
|
||||||
|
|
||||||
// TODO handle errors
|
// TODO handle errors
|
||||||
app.use(bodyParser.urlencoded({
|
app.use(bodyParser.urlencoded({
|
||||||
extended: true, // Uses 'qs' library as old connect middleware
|
extended: true, // Uses 'qs' library as old connect middleware
|
||||||
@@ -15,7 +21,7 @@ export default function attachMiddlewares (app) {
|
|||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
app.use(analytics);
|
app.use(analytics);
|
||||||
|
|
||||||
app.use(routes);
|
app.use('/api/v3', routes);
|
||||||
app.use(notFoundHandler);
|
app.use(notFoundHandler);
|
||||||
|
|
||||||
// Error handler middleware, define as the last one
|
// Error handler middleware, define as the last one
|
||||||
|
|||||||
@@ -89,7 +89,7 @@ app.use(domainMiddleware(server, mongoose));
|
|||||||
// Matches all request except the ones going to /api/v3/**
|
// Matches all request except the ones going to /api/v3/**
|
||||||
app.all(/^(?!\/api\/v3).+/i, oldApp);
|
app.all(/^(?!\/api\/v3).+/i, oldApp);
|
||||||
// Matches all requests going to /api/v3
|
// Matches all requests going to /api/v3
|
||||||
app.all('/api/v3', newApp);
|
app.all('/api/*', newApp);
|
||||||
|
|
||||||
// Mount middlewares for the new app
|
// Mount middlewares for the new app
|
||||||
attachMiddlewares(newApp);
|
attachMiddlewares(newApp);
|
||||||
|
|||||||
Reference in New Issue
Block a user