diff options
| -rw-r--r-- | src/FileRouter.ts | 194 | ||||
| -rw-r--r-- | src/app.ts | 13 | ||||
| -rw-r--r-- | src/logger.ts | 4 | ||||
| -rw-r--r-- | test/postman.json | 267 |
4 files changed, 394 insertions, 84 deletions
diff --git a/src/FileRouter.ts b/src/FileRouter.ts index d6d49da..bbcc0f2 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -32,8 +32,10 @@ export type Middleware = * "required": array(string) * }) */ -const getProtocols: Middleware = - (req: Request, res: Response, next: NextFunction): void => { +const getProtocols: Middleware = ( + req: Request, res: Response, next: NextFunction +): void => { + logger.debug('Fetching supported protocols'); res.locals.data = { supported: [], required: [ @@ -60,8 +62,10 @@ const getProtocols: Middleware = * "value": anything * }) */ -const postLog: Middleware = - (req: Request, res: Response, next: NextFunction): void => { +const postLog: Middleware = ( + req: Request, res: Response, next: NextFunction +): void => { + logger.debug('Adding forwarded log'); if (!req.user.hasPrivilege('logging')) { next(new RequestError(401, 'invalid_privilege')); return; @@ -100,8 +104,10 @@ const postLog: Middleware = * "timestamp": string * }) */ -const getLog: Middleware = - (req: Request, res: Response, next: NextFunction): void => { +const getLog: Middleware = ( + req: Request, res: Response, next: NextFunction +): void => { + logger.debug('Fetching log'); if (!req.user.hasPrivilege('admin')) { next(new RequestError(401, 'invalid_privilege')); } @@ -135,8 +141,10 @@ const getLog: Middleware = * "value": alternative([string, integer, boolean]) * }) */ -const getProperties: Middleware = - (req: Request, res: Response, next: NextFunction): void => { +const getProperties: Middleware = ( + req: Request, res: Response, next: NextFunction +): void => { + logger.debug('Listing server properties'); res.locals.data = serverConfig; next(); }; @@ -153,8 +161,10 @@ const getProperties: Middleware = * "value": alternative([string, integer, boolean]) * }) */ -const postProperties: Middleware = - (req: Request, res: Response, next: NextFunction): void => { +const postProperties: Middleware = ( + req: Request, res: Response, next: NextFunction +): void => { + logger.debug('Editing server properties'); for (const newProp of req.body) { const prop: Property | undefined = serverConfig.find( (p: Property): boolean => p.id === newProp.id @@ -188,8 +198,10 @@ const postProperties: Middleware = * "internal": boolean * }) */ -const getUserPrivileges: Middleware = - async(req: Request, res: Response, next: NextFunction): Promise<void> => { +const getUserPrivileges: Middleware = async( + req: Request, res: Response, next: NextFunction +): Promise<void> => { + logger.debug('Listing all user privileges'); const usergroups: UserGroup[] = await UserGroup.findAll(); res.locals.data = usergroups.map( (ug: UserGroup) => ug.getPrivilege() @@ -220,6 +232,7 @@ const getUserPrivileges: Middleware = const getUsers: Middleware = async( req: Request, res: Response, next: NextFunction ): Promise<void> => { + logger.debug('Listing all registered users'); const users: User[] = await User.findAll({ include: [ UserGroup, @@ -240,8 +253,10 @@ const getUsers: Middleware = async( /** * Expose additional configuration options for individual users */ -const getUserProperties: Middleware = - (req: Request, res: Response, next: NextFunction): void => { +const getUserProperties: Middleware = ( + req: Request, res: Response, next: NextFunction +): void => { + logger.debug('Listing additional properties for a user'); res.locals.data = res.locals.user.properties; next(); }; @@ -266,8 +281,10 @@ const getUserProperties: Middleware = * "private_admin_metadata": metadata * } */ -const getUsername: Middleware = - (req: Request, res: Response, next: NextFunction): void => { +const getUsername: Middleware = ( + req: Request, res: Response, next: NextFunction +): void => { + logger.debug(`Listing user ${res.locals.user}`); if (res.locals.user == null) { next(new RequestError(404, 'user_not_found')); return; @@ -276,8 +293,10 @@ const getUsername: Middleware = next(); }; -const getCurUser: Middleware = - (req: Request, res: Response, next: NextFunction): void => { +const getCurUser: Middleware = ( + req: Request, res: Response, next: NextFunction +): void => { + logger.debug('Listing current user'); res.locals.data = req.user.fullInfo; next(); }; @@ -300,8 +319,10 @@ const getCurUser: Middleware = * "private_admin_metadata": not_present * } */ -const postCurUser: Middleware = - async(req: Request, res: Response, next: NextFunction): Promise<void> => { +const postCurUser: Middleware = async( + req: Request, res: Response, next: NextFunction +): Promise<void> => { + logger.debug('Editing current user'); try { await req.user.updateInfo(req.body); await req.user.save(); @@ -328,15 +349,18 @@ const postCurUser: Middleware = * "private_admin_metadata": optional(metadata) * } */ -const postUsername: Middleware = - async(req: Request, res: Response, next: NextFunction): Promise<void> => { +const postUsername: Middleware = async( + req: Request, res: Response, next: NextFunction +): Promise<void> => { let user: User | null | undefined = res.locals.user; if (user == null) { + logger.debug('Adding new user'); user = new User({ username: req.params.username, password: req.params.password }); } else { + logger.debug('Editing user'); user.password = req.params.password; } user.metadata = req.body; @@ -354,8 +378,10 @@ const postUsername: Middleware = * "internal": boolean * }) */ -const getProjectRoles: Middleware = - async(req: Request, res: Response, next: NextFunction): Promise<void> => { +const getProjectRoles: Middleware = async( + req: Request, res: Response, next: NextFunction +): Promise<void> => { + logger.debug('Listing available project roles'); const roles: ContributorGroup[] = await ContributorGroup.findAll(); res.locals.data = roles.filter( (cg: ContributorGroup): boolean => !cg.isInternal @@ -382,8 +408,10 @@ const getProjectRoles: Middleware = * "admin_metadata": optional(metadata) * }) */ -const getProjects: Middleware = - async(req: Request, res: Response, next: NextFunction): Promise<void> => { +const getProjects: Middleware = async( + req: Request, res: Response, next: NextFunction +): Promise<void> => { + logger.debug('Listing all projects'); const projects: Project[] = await Project.findAll({ include: [ User @@ -412,16 +440,15 @@ const getProjects: Middleware = * "admin_metadata": optional(metadata) * } */ -const getProjectName: Middleware = - async(req: Request, res: Response, next: NextFunction): Promise<void> => { +const getProjectName: Middleware = async( + req: Request, res: Response, next: NextFunction +): Promise<void> => { + logger.debug('Getting project properties'); const project: Project | null = res.locals.project; if (project == null) { - console.log("was null") return next(new RequestError(404, 'project_not_found')); } else { - console.log(project.contributors); res.locals.data = project.fullInfo; - console.log("wasnt " + res.locals.data) next(); } }; @@ -439,37 +466,39 @@ const getProjectName: Middleware = * "admin_metadata": optional(metadata) * } */ -const postProjectName: Middleware = - async(req: Request, res: Response, next: NextFunction): Promise<void> => { +const postProjectName: Middleware = async( + req: Request, res: Response, next: NextFunction +): Promise<void> => { let project: Project | null = res.locals.project; let promise: PromiseLike<File> | null = null; if (project == null) { + logger.debug('Adding new project'); const file: File = new File({ uuid: uuid.generate(), mimetype: 'inode/directory' }); - console.log(req.params.project_name) promise = file.save(); project = new Project({ name: req.params.project_name, rootFolder: file }); + } else { + logger.debug('Updating project properties'); } project.metadata = req.body; // tslint:disable-next-line:await-promise - console.log("anything prequel") await promise; - console.log("anything") await project.save(); - console.log("anything part 2") next(); }; /** * additional configuration options for individual projects */ -const getProjectProperties: Middleware = - (req: Request, res: Response, next: NextFunction): void => { +const getProjectProperties: Middleware = ( + req: Request, res: Response, next: NextFunction +): void => { + logger.debug('Getting project properties'); if (res.locals.project == null) { next(new RequestError(404, 'project_not_found')); } else { @@ -484,6 +513,7 @@ const getProjectProperties: Middleware = const getFile: Middleware = async( req: Request, res: Response, next: NextFunction ): Promise<void> => { + logger.debug(`Getting file in ${req.query.view} view`); const file: File | null = res.locals.file; if (file == null) { return next(new RequestError(404, 'file_not_found')); @@ -508,8 +538,10 @@ const getFileId: Middleware = getFile; /** * Post a File. / delete / move / etc */ -const postFilePath: Middleware = - (req: Request, res: Response, next: NextFunction): void => { +const postFilePath: Middleware = ( + req: Request, res: Response, next: NextFunction +): void => { + logger.debug('Receiving file'); if (res.locals.file != null) { next(new RequestError(400, 'file_exists')); } @@ -534,6 +566,39 @@ export class FileRouter { } /** + * Take each handler, and attach to one of the Express.Router's + * endpoints. + */ + public init(): void { + this.precondition(); + + // General + this.router.get ('/_supported_protocols_', getProtocols); + this.router.get ('/log', getLog); + this.router.post('/log', postLog); + this.router.get ('/properties', getProperties); + this.router.post('/properties', postProperties); + // Users + this.router.get ('/user_privileges', getUserPrivileges); + this.router.get ('/users', getUsers); + this.router.get ('/users/:username', getUsername); + this.router.post('/users/:username', postUsername); + this.router.get ('/users/:username/properties', getUserProperties); + this.router.get ('/current_user', getCurUser); + this.router.post('/current_user', postCurUser); + // Projects + this.router.get ('/project_roles', getProjectRoles); + this.router.get ('/projects', getProjects); + this.router.get ('/projects/:project_name', getProjectName); + this.router.post('/projects/:project_name', postProjectName); + this.router.get ('/projects/:project_name/properties', getProjectProperties); + // File access + this.router.get ('/projects/:project_name/files/:path', getFilePath); + this.router.post('/projects/:project_name/files/:id', postFilePath); + this.router.get ('/projects/:project_name/files_by_id/:id', getFileId); + } + + /** * Introduces variable-preconditioning middleware to the Express app. * * These middleware functions pre-process Express' matching variables @@ -542,6 +607,7 @@ export class FileRouter { * value is set to `null` instead. */ private precondition(): void { + logger.debug('Adding parameter preconditioning to router'); // Fetch a user from their name: this.router.param( 'username', @@ -549,6 +615,7 @@ export class FileRouter { req: Request, res: Response, next: NextFunction, name: string ): Promise<void> => { + logger.debug(`Searching for user ${name}`); const user: User | null = await User.findOne({ where: { username: name @@ -568,14 +635,14 @@ export class FileRouter { 'project_name', async( req: Request, res: Response, next: NextFunction, - project: string - ): Promise<void> => { + projectName: string + ): Promise<void> => { + logger.debug(`Searching for project ${projectName}`); res.locals.project = await Project.findOne({ where: { - name: project + name: projectName } }); - console.log("## as " + res.locals.project); next(); } ); @@ -587,9 +654,11 @@ export class FileRouter { req: Request, res: Response, next: NextFunction, filename: string ): Promise<void> => { + logger.debug(`Searching for file ${filename}`); const fileNames: string[] = req.params.path.split('/'); const project: Project | null = res.locals.project; if (project == null) { + logger.debug('Project was not found: skipping finding file'); return next(new RequestError(404, 'project_not_found')); } let file: File = project.rootFolder; @@ -637,6 +706,7 @@ export class FileRouter { req: Request, res: Response, next: NextFunction, fileId: string ): Promise<void> => { + logger.debug(`Searching for file ${fileId}`); const file: File | null = await File.findOne({ where: { uuid: fileId @@ -651,43 +721,9 @@ export class FileRouter { } ); } - - /** - * Take each handler, and attach to one of the Express.Router's - * endpoints. - */ - public init(): void { - this.precondition(); - - // General - this.router.get ('/_supported_protocols_', getProtocols); - this.router.get ('/log', getLog); - this.router.post('/log', postLog); - this.router.get ('/properties', getProperties); - this.router.post('/properties', postProperties); - // Users - this.router.get ('/user_privileges', getUserPrivileges); - this.router.get ('/users', getUsers); - this.router.get ('/users/:username', getUsername); - this.router.post('/users/:username', postUsername); - this.router.get ('/users/:username/properties', getUserProperties); - this.router.get ('/current_user', getCurUser); - this.router.post('/current_user', postCurUser); - // Projects - this.router.get ('/project_roles', getProjectRoles); - this.router.get ('/projects', getProjects); - this.router.get ('/projects/:project_name', getProjectName); - this.router.post('/projects/:project_name', postProjectName); - this.router.get ('/projects/:project_name/properties', getProjectProperties); - // File access - this.router.get ('/projects/:project_name/files/:path', getFilePath); - this.router.post('/projects/:project_name/files/:id', postFilePath); - this.router.get ('/projects/:project_name/files_by_id/:id', getFileId); - } } // Create the FileRouter, and export its configured Express.Router const fileRoutes: FileRouter = new FileRouter(); -fileRoutes.init(); export default fileRoutes.router; @@ -4,6 +4,7 @@ import * as expressJwt from 'express-jwt'; import * as passport from 'passport'; import { default as authRouter, unauthorisedErr } from './auth'; +import { logger } from './logger'; import { RequestError, errorHandler } from './errors/errorware'; import FileRouter from './FileRouter'; @@ -39,11 +40,15 @@ class App { * server. */ public constructor(enableLog: boolean) { + logger.debug('Constructing router'); this.logEnabled = enableLog; this.express = ex(); - this.express.use(( - req: ex.Request, res: ex.Response, next: ex.NextFunction - ): void => { console.log(req.url); next(); }); + this.express.use( + (req: ex.Request, res: ex.Response, next: ex.NextFunction): void => { + logger.info(`Connection from ${req.ip} requesting ${req.url}`); + next(); + } + ); this.middleware(); this.mountRoutes(); this.express.use( @@ -57,6 +62,7 @@ class App { data: await res.locals.data }); } + logger.success(`Responded to ${req.ip}`); } ); this.errorware(); @@ -66,6 +72,7 @@ class App { * Configures regular Express middleware. */ private middleware(): void { + logger.debug('Loading basic middleware'); this.express.use(expressJwt({secret: 'Mr Secret'}) .unless({path: [ '/cs3099group-be-4/oauth/token', diff --git a/src/logger.ts b/src/logger.ts index bdcb390..aefb751 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -115,8 +115,8 @@ export let enabled: boolean = true; const consoleTransport: winston.TransportInstance = new winston.transports.Console({ name: 'consoleTransport', - level: 'info', - silent: false, + level: 'verbose', + silent: process.env.NODE_ENV === 'test', handleExceptions: true, humanReadableUncaughtException: true, formatter: ( diff --git a/test/postman.json b/test/postman.json new file mode 100644 index 0000000..164bdc1 --- /dev/null +++ b/test/postman.json @@ -0,0 +1,267 @@ +{ + "info": { + "name": "JH", + "_postman_id": "323a3189-bd95-4f51-b985-d2ca626300ec", + "description": "", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json" + }, + "item": [ + { + "name": "authorised", + "description": null, + "item": [ + { + "name": "/projects", + "request": { + "auth": { + "type": "bearer", + "bearer": [ + { + "key": "token", + "value": "{{access_token}}", + "type": "string" + } + ] + }, + "method": "GET", + "header": [ + { + "key": "", + "value": "", + "disabled": true + } + ], + "body": {}, + "url": { + "raw": "{{host}}/{{endpoint}}/projects", + "host": [ + "{{host}}" + ], + "path": [ + "{{endpoint}}", + "projects" + ] + }, + "description": "" + }, + "response": [] + }, + { + "name": "/log", + "request": { + "auth": { + "type": "bearer", + "bearer": [ + { + "key": "token", + "value": "{{access_token}}", + "type": "string" + } + ] + }, + "method": "GET", + "header": [ + { + "key": "", + "value": "", + "disabled": true + } + ], + "body": {}, + "url": { + "raw": "{{host}}/{{endpoint}}/log", + "host": [ + "{{host}}" + ], + "path": [ + "{{endpoint}}", + "log" + ] + }, + "description": "" + }, + "response": [] + }, + { + "name": "/_supported_protocols", + "request": { + "auth": { + "type": "noauth" + }, + "method": "GET", + "header": [ + { + "key": "", + "value": "", + "disabled": true + } + ], + "body": {}, + "url": { + "raw": "{{host}}/{{endpoint}}/_supported_protocols_", + "host": [ + "{{host}}" + ], + "path": [ + "{{endpoint}}", + "_supported_protocols_" + ] + }, + "description": "" + }, + "response": [] + }, + { + "name": "/project_roles", + "request": { + "auth": { + "type": "bearer", + "bearer": [ + { + "key": "token", + "value": "{{access_token}}", + "type": "string" + } + ] + }, + "method": "GET", + "header": [ + { + "key": "", + "value": "", + "disabled": true + } + ], + "body": {}, + "url": { + "raw": "{{host}}/{{endpoint}}/project_roles", + "host": [ + "{{host}}" + ], + "path": [ + "{{endpoint}}", + "project_roles" + ] + }, + "description": "" + }, + "response": [] + }, + { + "name": "/projects/", + "request": { + "auth": { + "type": "bearer", + "bearer": [ + { + "key": "token", + "value": "{{access_token}}", + "type": "string" + } + ] + }, + "method": "POST", + "header": [ + { + "key": "Content-Type", + "value": "application/json" + } + ], + "body": { + "mode": "raw", + "raw": "{}" + }, + "url": { + "raw": "{{host}}/{{endpoint}}/projects/{{dummy_project}}", + "host": [ + "{{host}}" + ], + "path": [ + "{{endpoint}}", + "projects", + "{{dummy_project}}" + ] + }, + "description": "" + }, + "response": [] + } + ] + }, + { + "name": "/oauth/token", + "event": [ + { + "listen": "prerequest", + "script": { + "id": "7cbb2980-4092-4675-af5e-cfcbd1c00b81", + "type": "text/javascript", + "exec": [ + "" + ] + } + }, + { + "listen": "test", + "script": { + "id": "7ce27395-de92-457b-97cc-7728824c821d", + "type": "text/javascript", + "exec": [ + "let res = JSON.parse(responseBody);", + "postman.setEnvironmentVariable(\"access_token\", res.access_token);", + "postman.setEnvironmentVariable(\"refresh_token\", res.refresh_token);" + ] + } + } + ], + "request": { + "auth": { + "type": "noauth" + }, + "method": "POST", + "header": [ + { + "key": "Content-Type", + "value": "application/x-www-form-urlencoded" + } + ], + "body": { + "mode": "urlencoded", + "urlencoded": [ + { + "key": "grant_type", + "value": "password", + "description": "", + "type": "text" + }, + { + "key": "username", + "value": "{{dummy_admin_username}}", + "description": "", + "type": "text" + }, + { + "key": "password", + "value": "{{dummy_admin_pass}}", + "description": "", + "type": "text" + } + ] + }, + "url": { + "raw": "{{host}}/{{endpoint}}/oauth/token", + "host": [ + "{{host}}" + ], + "path": [ + "{{endpoint}}", + "oauth", + "token" + ] + }, + "description": "wefrwrbjk;" + }, + "response": [] + } + ] +}
\ No newline at end of file |
