From 8f39a1403d3c94f3d51693e41843750352408cba Mon Sep 17 00:00:00 2001 From: Tom Harley Date: Thu, 29 Mar 2018 08:32:54 +0100 Subject: Address numerous errors in test suites Many errors still remian. --- src/FileRouter.ts | 40 +++++++++++++++++++++------------------- src/app.ts | 16 ++++++++++++++++ src/auth.ts | 33 ++++++++++++++++++++++++++++----- src/db/model/Project.ts | 4 +--- src/db/model/User.ts | 1 - src/errors/errorware.ts | 8 +++++--- src/files.ts | 5 +++-- src/logger.ts | 5 +++++ 8 files changed, 79 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/FileRouter.ts b/src/FileRouter.ts index b3b1a38..4d6eb16 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -217,24 +217,23 @@ const getUserPrivileges: Middleware = * "private_admin_metadata": metadata * }) */ -const getUsers: Middleware = - async(req: Request, res: Response, next: NextFunction): Promise => { +const getUsers: Middleware = async( + req: Request, res: Response, next: NextFunction +): Promise => { const users: User[] = await User.findAll({ include: [ UserGroup, Project ] }); - res.locals.data = users.map( - (u: User) => { - const info: UserFullInfo = u.fullInfo; - if (!req.user.hasPrivilege('admin')) { - delete info.private_user_metadata; - delete info.private_admin_metadata; - } - return info; + res.locals.data = users.map((u: User): UserFullInfo => { + const info: UserFullInfo = u.fullInfo; + if (!req.user.hasPrivilege('admin')) { + delete info.private_user_metadata; + delete info.private_admin_metadata; } - ); + return info; + }); next(); }; @@ -416,8 +415,8 @@ const getProjects: Middleware = const getProjectName: Middleware = async(req: Request, res: Response, next: NextFunction): Promise => { const project: Project | null = res.locals.project; - if (project === null) { - next(new RequestError(404, 'project_not_found')); + if (project == null) { + return next(new RequestError(404, 'project_not_found')); } else { res.locals.data = project.fullInfo; next(); @@ -441,7 +440,7 @@ const postProjectName: Middleware = async(req: Request, res: Response, next: NextFunction): Promise => { let project: Project | null = res.locals.project; let promise: PromiseLike | null = null; - if (project === null) { + if (project == null) { const file: File = new File({ mimetype: 'inode/directory' }); @@ -474,12 +473,15 @@ const getProjectProperties: Middleware = /** * GET a File. */ -const getFilePath: Middleware = - async(req: Request, res: Response, next: NextFunction): Promise => { +const getFilePath: Middleware = async( + req: Request, res: Response, next: NextFunction +): Promise => { const file: File | null = res.locals.file; - if (file === null) { - next(new RequestError(404, 'file_not_found')); - return; + const project: Project | null = res.locals.project; + if (file == null) { + return next(new RequestError(404, 'file_not_found')); + } else if (project == null) { + return next(new RequestError(404, 'project_not_found')); } res.locals.function = res.sendFile; res.locals.data = diff --git a/src/app.ts b/src/app.ts index ad00105..c627027 100644 --- a/src/app.ts +++ b/src/app.ts @@ -12,6 +12,7 @@ import FileRouter from './FileRouter'; import { default as File } from './db/model/File'; import { default as Project } from './db/model/Project'; import { default as User } from './db/model/User'; +import { default as UserGroup } from './db/model/UserGroup'; /** * Configuration of the Express server app, including installation of @@ -67,6 +68,21 @@ class App { ] }) ); this.express.use(passport.initialize()); + this.express.use(async( + req: ex.Request, res: ex.Response, next: ex.NextFunction + ): Promise => { + if (req.user != null) { + req.user = await User.findOne({ + where: { + username: req.user.object.username + }, + include: [ + UserGroup + ] + }); + } + next(); + }); if (this.logEnabled) { this.express.use(logger('combined')); } diff --git a/src/auth.ts b/src/auth.ts index 368f130..8a6b248 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -38,6 +38,7 @@ const configStrategy: () => void = (): void => { * * @param req The Express http request. * @param res The Express http response. + * @param next The next Express middleware. */ const checkErr: Middleware = ( req: Request, res: Response, next: NextFunction @@ -89,9 +90,10 @@ const authenticate: Middleware = ( * * @param req The Express http request. * @param res The Express http response. + * @param next The next Express middleware - only used for errors. */ const genToken: Middleware = async( - req: Request, res: Response + req: Request, res: Response, next: NextFunction ): Promise => { const user: User | null = await User.findOne({ where: { @@ -102,7 +104,11 @@ const genToken: Middleware = async( if (user === null) { // Whoa, what an error! - return; + return next(new RequestError( + 400, + 'invalid_grant', + 'Invalid username' + )); } const token: string = jwt.sign( @@ -155,6 +161,22 @@ export class AuthRouter { } } +interface UnauthorizedError { + name: 'UnauthorizedError'; + message: string; + code: string; + status: number; +} + +const isUnauthorizedError: (item: object) => item is UnauthorizedError = ( + item: object +): item is UnauthorizedError => { + return (item as Partial).name === 'UnauthorizedError' + && (item as Partial).message !== undefined + && (item as Partial).code !== undefined + && (item as Partial).status !== undefined; +}; + /** * Error-handling middleware used to handle authentication errors. * @@ -168,10 +190,11 @@ export class AuthRouter { */ export const unauthorisedErr: Errorware = (err: Error, req: Request, res: Response, next: NextFunction): void => { - if (err.name === 'UnauthorizedError') { + if (isUnauthorizedError(err)) { return next(new RequestError( - 401, 'not_authorised', - 'user does not have correct authorisation for task' + err.status, + err.code, + err.message )); } next(err); diff --git a/src/db/model/Project.ts b/src/db/model/Project.ts index 3618f03..d57eaaa 100644 --- a/src/db/model/Project.ts +++ b/src/db/model/Project.ts @@ -107,9 +107,7 @@ export default class Project extends Model { return { project_name: this.name, users: this.contributors - .map( - (u: User) => this.getUserInfo(u) - ), + .map((u: User): UserInfo => this.getUserInfo(u)), public_metadata: { creation_date: this.creationDate }, diff --git a/src/db/model/User.ts b/src/db/model/User.ts index 7315393..171e899 100644 --- a/src/db/model/User.ts +++ b/src/db/model/User.ts @@ -110,7 +110,6 @@ export default class User extends Model { } public hasPrivilege(privilege: string): boolean { - // FIXME: Fetch these things if they aren't already present if (this.userGroups === undefined) { throw new Error('usergroups undefined'); } diff --git a/src/errors/errorware.ts b/src/errors/errorware.ts index 05c714d..6d87bcb 100644 --- a/src/errors/errorware.ts +++ b/src/errors/errorware.ts @@ -28,7 +28,7 @@ export class RequestError extends Error { * @returns `true` if the object is a [[RequestError]]; `false` * otherwise. */ - public static is(test: Object): test is RequestError { + public static is(test: object): test is RequestError { return (test as Partial).code !== undefined && (test as Partial).name !== undefined && (test as Partial).responseBlock !== undefined; @@ -110,9 +110,11 @@ export type Errorware = * @param err The Error to act on. * @param req The Express http request. * @param res The Express http response. + * @param next The next Express middleware function. */ -export const errorHandler: Errorware = - (err: Error, req: Request, res: Response): void => { +export const errorHandler: Errorware = ( + err: Error, req: Request, res: Response, next: NextFunction +): void => { if (RequestError.is(err)) { res.status(err.code) .json(err.responseBlock()); diff --git a/src/files.ts b/src/files.ts index fb22013..644d24e 100644 --- a/src/files.ts +++ b/src/files.ts @@ -15,8 +15,9 @@ const writableStream: }; const logPath: - (type: string, projectName?: string) => string = - (type: string, projectName?: string): string => { +(type: string, projectName?: string) => string = ( + type: string, projectName?: string +): string => { return projectName ? `${LOG_BASE_DIRECTORY}/projects/${projectName}/${type}` : `${LOG_BASE_DIRECTORY}/general/${type}`; diff --git a/src/logger.ts b/src/logger.ts index 953a341..bbb0a73 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -31,6 +31,8 @@ interface LoggingMetadata { [key: string]: any; } +export let enabled: boolean = true; + const consoleTransport: winston.TransportInstance = new winston.transports.Console({ name: 'consoleTransport', @@ -162,6 +164,9 @@ const makeLogFunction: (level: LogLevel) => LogFunction = ( return ( message: string, meta: LoggingMetadata = {} ): void => { + if (!enabled) { + return; + } meta.user = meta.user || '_BE4_system'; meta.component = meta.component || 'core'; basicLogger.log(level, message, meta); -- cgit v1.2.1