aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Harley2018-03-27 19:19:16 +0100
committerTom Harley2018-03-27 19:19:16 +0100
commit150115e855e2c406b23aa569dd675597d6957570 (patch)
tree7cb6cbbc7e7f2911596bb1f03dbcedd8b10e0e49 /src
parentMerge branch 'cjd24/conversion' into development (diff)
parentCentralise test configuration and introduce SQLite (diff)
downloadmedicine-150115e855e2c406b23aa569dd675597d6957570.tar.gz
medicine-150115e855e2c406b23aa569dd675597d6957570.zip

Merge branch ‘testing-middleware’ into development

Diffstat (limited to 'src')
-rw-r--r--src/FileRouter.ts2
-rw-r--r--src/app.ts9
-rw-r--r--src/auth.ts27
-rw-r--r--src/db/initPostgresDB.ts2
4 files changed, 23 insertions, 17 deletions
diff --git a/src/FileRouter.ts b/src/FileRouter.ts
index f8f569d..b3b1a38 100644
--- a/src/FileRouter.ts
+++ b/src/FileRouter.ts
@@ -19,7 +19,7 @@ const BASE_FILE_STORAGE: string = 'TODO: figure this out';
/**
* The type of an Express middleware callback.
*/
-type Middleware =
+export type Middleware =
(req: Request, res: Response, next: NextFunction) => void;
/**
diff --git a/src/app.ts b/src/app.ts
index 025164b..03f2cdb 100644
--- a/src/app.ts
+++ b/src/app.ts
@@ -128,8 +128,7 @@ class App {
}
});
if (user === null) {
- next(new RequestError(404, 'user_not_found'));
- return;
+ return next(new RequestError(404, 'user_not_found'));
} else {
res.locals.user = user;
}
@@ -176,8 +175,7 @@ class App {
}
});
if (fileOrNull === null) {
- next();
- return;
+ return next();
} else {
file = fileOrNull;
}
@@ -193,8 +191,7 @@ class App {
}
});
if (parentFolder === null) {
- next();
- return;
+ return next();
}
res.locals.parentFolder = parentFolder;
const tFile: File | undefined = parentFolder.containedFiles.find(
diff --git a/src/auth.ts b/src/auth.ts
index 57c375a..5395728 100644
--- a/src/auth.ts
+++ b/src/auth.ts
@@ -1,5 +1,8 @@
-import { RequestError } from './errors/errorware';
+/* tslint:disable:prefer-function-over-method */
+
+import { Errorware, RequestError } from './errors/errorware';
import { NextFunction, Request, Response, Router } from 'express';
+import { Middleware } from './FileRouter';
import * as jwt from 'jsonwebtoken';
import * as passport from 'passport';
import { Strategy } from 'passport-local';
@@ -7,8 +10,6 @@ import { default as Project } from './db/model/Project';
import { default as User } from './db/model/User';
import { default as UserGroup } from './db/model/UserGroup';
-import { Errorware } from './errors/errorware';
-
/**
* The module for system authentication.
*/
@@ -98,12 +99,14 @@ export class AuthRouter {
*/
public checkErr(req: Request, res: Response, next: NextFunction): void {
if (!req.body.grant_type || !req.body.username || !req.body.password) {
- next(new RequestError(400, 'invalid_request', 'Missing parameters'));
+ return next(new RequestError(400, 'invalid_request', 'Missing parameters'));
}
if (req.body.grant_type !== 'refresh_token'
&& req.body.grant_type !== 'password') {
- next(new RequestError(400, 'unsupported_grant_type', 'invalid grant type'));
+ return next(new RequestError(
+ 400, 'unsupported_grant_type', 'invalid grant type'
+ ));
}
next();
@@ -135,7 +138,12 @@ export class AuthRouter {
public init(): void {
this.configStrategy();
- this.router.post('/oauth/token', this.checkErr, this.authenticate, this.genToken);
+ this.router.post(
+ '/oauth/token',
+ this.checkErr,
+ this.authenticate,
+ this.genToken
+ );
this.router.get(
'/logout',
(req: Request, res: Response): void => { req.logout(); }
@@ -173,11 +181,12 @@ export const unauthorisedErr: Errorware =
* @param res The Express http response.
* @param next The next Express middleware function.
*/
-export const isAdmin: any =
+export const isAdmin: Middleware =
(req: Request, res: Response, next: NextFunction): void => {
- let isAdmin: boolean = req.user.object.userGroups.some((x: UserGroup): boolean => x.name === 'admin');
+ const admin: boolean = req.user.object.userGroups
+ .some((x: UserGroup): boolean => x.name === 'admin');
- if (!isAdmin) {
+ if (!admin) {
next(new RequestError(
401, 'not_authorised',
'user is not authorised to perform task'
diff --git a/src/db/initPostgresDB.ts b/src/db/initPostgresDB.ts
index b6844d9..eb979f4 100644
--- a/src/db/initPostgresDB.ts
+++ b/src/db/initPostgresDB.ts
@@ -1,6 +1,6 @@
import * as Sequelize from 'sequelize';
const gres: Sequelize.Sequelize = new Sequelize(
- 'postgres://admin:eprprJacR0hBpmWvs5IDJZTnjRAY2gM3tSm0b1af@postgres:5432/be4',
+ 'postgres://admin:eprprJacR0hBpmWvs5IDJZTnjRAY2gM3tSm0b1af@localhost:5432/be4',
{
operatorsAliases : false
}