From 77bfad0307f4e5daea0584a30b0dcf24ccc9ec42 Mon Sep 17 00:00:00 2001 From: jw255 Date: Thu, 22 Feb 2018 16:57:45 +0000 Subject: restructuring of tests --- src/app.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src/app.ts') diff --git a/src/app.ts b/src/app.ts index 4f0e0d7..ef4485e 100644 --- a/src/app.ts +++ b/src/app.ts @@ -5,17 +5,19 @@ import * as logger from 'morgan'; class App { public express: ex.Express; - public logEnabled: boolean = true; + private logEnabled: boolean; - public constructor() { + public constructor(enableLog: boolean) { this.express = ex(); this.middleware(); this.mountRoutes(); + this.logEnabled = enableLog; } // Configure Express middleware. private middleware(): void { if (this.logEnabled) { + console.log("log enabled") this.express.use(logger('combined')); } this.express.use(bodyParser.json()); @@ -36,4 +38,7 @@ class App { } } -export default new App().express; +export default new App(true).express; +export function TestApp(){ + return new App(false).express; +} -- cgit v1.2.1 From 80f5aeb37b3d997015238173499f4a067ec0c266 Mon Sep 17 00:00:00 2001 From: jw255 Date: Sat, 24 Feb 2018 19:34:20 +0000 Subject: tests now actually test what they are supposed to and catch errors if needed --- src/app.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src/app.ts') diff --git a/src/app.ts b/src/app.ts index ef4485e..612ba49 100644 --- a/src/app.ts +++ b/src/app.ts @@ -33,8 +33,18 @@ class App { important : 'Endpoints start from /cs3099group-be-4/' }); }); - this.express.use('/', defRouter); + defRouter.get('/*', (req: ex.Request, res: ex.Response): void => { + res.status(404) + .json({ + status: 'error', + error : 'invalid_route', + error_description: 'Endpoints start from /cs3099group-be-4/\n' + +' Please refer to \'https://github.com/CS3099JH2017/cs3099jh/blob/master/protocols/BE01.md\'' + + ' for further details.' + }); + }) this.express.use('/cs3099group-be-4', FileRouter); + this.express.use('/', defRouter); } } -- cgit v1.2.1 From 0caebd4bfdfd9c8d7840fc379412d5d1f867575f Mon Sep 17 00:00:00 2001 From: jw255 Date: Sat, 3 Mar 2018 14:53:31 +0000 Subject: pulled latest files from trh/payload-checker --- src/app.ts | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 154 insertions(+), 6 deletions(-) (limited to 'src/app.ts') diff --git a/src/app.ts b/src/app.ts index 612ba49..355ad76 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,23 +1,50 @@ import * as bodyParser from 'body-parser'; import * as ex from 'express'; -import FileRouter from './FileRouter'; +import * as expressJwt from 'express-jwt'; import * as logger from 'morgan'; +import * as passport from 'passport'; + +import { default as authRouter } from './auth'; + +import { RequestError, errorHandler } from './errors/errorware'; +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'; class App { public express: ex.Express; - private logEnabled: boolean; + private logEnabled: boolean = true; public constructor(enableLog: boolean) { + this.logEnabled = enableLog; this.express = ex(); + this.precondition(); this.middleware(); this.mountRoutes(); - this.logEnabled = enableLog; + this.express.use( + async(req: ex.Request, res: ex.Response, next: ex.NextFunction): + Promise => { + res.json({ + status: 'success', + data: await res.locals.data + }); + } + ); + this.errorware(); } // Configure Express middleware. private middleware(): void { + this.express.use(expressJwt({secret: 'Mr Secret'}) + .unless({path: [ + '/cs3099group-be-4/login', + '/cs3099group-be-4/_supported_protocols_' + ] }) + ); + this.express.use(passport.initialize()); if (this.logEnabled) { - console.log("log enabled") this.express.use(logger('combined')); } this.express.use(bodyParser.json()); @@ -30,7 +57,7 @@ class App { defRouter.get('/', (req: ex.Request, res: ex.Response): void => { res.json({ message: 'Welcome to the CS3099 BE4 server!', - important : 'Endpoints start from /cs3099group-be-4/' + important: 'Endpoints start from /cs3099group-be-4/' }); }); defRouter.get('/*', (req: ex.Request, res: ex.Response): void => { @@ -44,11 +71,132 @@ class App { }); }) this.express.use('/cs3099group-be-4', FileRouter); + this.express.use('/cs3099group-be-4', authRouter); this.express.use('/', defRouter); } + + private errorware(): void { + // TODO: this.express.use(authRouter.unauthorisedErr); + this.express.use(errorHandler); + } + + private precondition(): void { + // Fetch a user from their name: + this.express.param( + 'username', + async( + req: ex.Request, res: ex.Response, next: ex.NextFunction, + name: string + ): Promise => { + const user: User | null = await User.findOne({ + where: { + username: name + } + }); + if (user === null) { + next(new RequestError(404, 'user_not_found')); + return; + } else { + res.locals.user = user; + } + next(); + } + ); + + // Fetch a project from its name: + this.express.param( + 'project_name', + async( + req: ex.Request, res: ex.Response, next: ex.NextFunction, + project: string + ): Promise => { + res.locals.project = await Project.findOne({ + where: { + name: project + } + }); + next(); + } + ); + + // Fetch a file from its path: + this.express.param( + 'file', + async( + req: ex.Request, res: ex.Response, next: ex.NextFunction, + filename: string + ): Promise => { + const fileNames: string[] = req.params.path.split('/'); + const project: Project | null = res.locals.project; + if (project === null) { + next(new RequestError(404, 'project_not_found')); + return; + } + let file: File = project.rootFolder; + // TODO Rewrite using sub-queries instead of repeated queries. + while (fileNames.length > 2) { + const fileOrNull: File | null = await File.findOne({ + where: { + name: fileNames[0], + parentFolder: file + } + }); + if (fileOrNull === null) { + next(); + return; + } else { + file = fileOrNull; + } + fileNames.splice(0, 1); + } + const parentFolder: File | null = await File.findOne({ + include: [ + { model: File, as: 'containedFiles' } + ], + where: { + parentFolder: file, + name: fileNames[0] + } + }); + if (parentFolder === null) { + next(); + return; + } + res.locals.parentFolder = parentFolder; + const tFile: File | undefined = parentFolder.containedFiles.find( + (f: File): boolean => f.name === fileNames[1] + ); + res.locals.file = tFile; + res.locals.filename = fileNames[1]; + next(); + } + ); + + // Fetch a file from its UUID: + this.express.param( + 'id', + async( + req: ex.Request, res: ex.Response, next: ex.NextFunction, + fileId: string + ): Promise => { + const file: File | null = await File.findOne({ + where: { + uuid: fileId + } + }); + if (file === null) { + next(new RequestError(404, 'file_not_found')); + return; + } else { + res.locals.file = file; + } + next(); + } + ); + } } export default new App(true).express; export function TestApp(){ return new App(false).express; -} +} -- cgit v1.2.1 From 46f6e6c7bf1d69d1b3257f6cc186326a57c2b51d Mon Sep 17 00:00:00 2001 From: jw255 Date: Sun, 4 Mar 2018 16:06:18 +0000 Subject: started authenticating --- src/app.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/app.ts') diff --git a/src/app.ts b/src/app.ts index 355ad76..8f86717 100644 --- a/src/app.ts +++ b/src/app.ts @@ -4,7 +4,7 @@ import * as expressJwt from 'express-jwt'; import * as logger from 'morgan'; import * as passport from 'passport'; -import { default as authRouter } from './auth'; +import { unauthorisedErr, default as authRouter } from './auth'; import { RequestError, errorHandler } from './errors/errorware'; import FileRouter from './FileRouter'; @@ -77,6 +77,7 @@ class App { private errorware(): void { // TODO: this.express.use(authRouter.unauthorisedErr); + this.express.use(unauthorisedErr); this.express.use(errorHandler); } -- cgit v1.2.1