diff options
| -rw-r--r-- | src/FileRouter.ts | 11 | ||||
| -rw-r--r-- | src/app.ts | 5 | ||||
| -rw-r--r-- | src/auth.ts | 7 | ||||
| -rw-r--r-- | src/logger.ts | 15 | ||||
| -rw-r--r-- | test/integration/protocol.routes.test.ts | 34 | ||||
| -rw-r--r-- | test/test-db.ts | 23 |
6 files changed, 71 insertions, 24 deletions
diff --git a/src/FileRouter.ts b/src/FileRouter.ts index ee10662..d922023 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -63,7 +63,7 @@ const getProtocols: Middleware = const postLog: Middleware = (req: Request, res: Response, next: NextFunction): void => { if (!req.user.hasPrivilege('logging')) { - next(new RequestError(400, 'invalid_privilege')); + next(new RequestError(401, 'invalid_privilege')); return; } else if (!logger.isEnabled()) { next(new RequestError(501, 'logging_not_enabled')); @@ -103,7 +103,7 @@ const postLog: Middleware = const getLog: Middleware = (req: Request, res: Response, next: NextFunction): void => { if (!req.user.hasPrivilege('admin')) { - next(new RequestError(400, 'invalid_privilege')); + next(new RequestError(401, 'invalid_privilege')); } res.locals.data = logger.fetch( req.params.level, { @@ -416,9 +416,11 @@ const getProjectName: Middleware = async(req: Request, res: Response, next: NextFunction): Promise<void> => { const project: Project | null = res.locals.project; if (project == null) { + console.log("was null") return next(new RequestError(404, 'project_not_found')); } else { res.locals.data = project.fullInfo; + console.log("wasnt " + res.locals.data) next(); } }; @@ -442,8 +444,10 @@ const postProjectName: Middleware = let promise: PromiseLike<File> | null = null; if (project == null) { 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, @@ -452,8 +456,11 @@ const postProjectName: Middleware = } 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(); }; @@ -104,7 +104,8 @@ class App { important: 'Endpoints start from /cs3099group-be-4/' }); }); - defRouter.get('/*', (req: ex.Request, res: ex.Response): void => { + const errRouter: ex.Router = ex.Router(); + errRouter.get('/*', (req: ex.Request, res: ex.Response): void => { res.status(404) .json({ status: 'error', @@ -115,6 +116,7 @@ class App { this.express.use('/cs3099group-be-4', FileRouter); this.express.use('/cs3099group-be-4', authRouter); this.express.use('/', defRouter); + this.express.use('/XX*', errRouter); } /** @@ -162,6 +164,7 @@ class App { req: ex.Request, res: ex.Response, next: ex.NextFunction, project: string ): Promise<void> => { + console.log("## as " + project) res.locals.project = await Project.findOne({ where: { name: project diff --git a/src/auth.ts b/src/auth.ts index 8a6b248..14e1170 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -193,8 +193,8 @@ export const unauthorisedErr: Errorware = if (isUnauthorizedError(err)) { return next(new RequestError( err.status, - err.code, - err.message + 'not_authorised', + 'user does not have correct authorisation for task' )); } next(err); @@ -210,7 +210,8 @@ export const unauthorisedErr: Errorware = */ export const isAdmin: Middleware = (req: Request, res: Response, next: NextFunction): void => { - const admin: boolean = req.user.object.userGroups + // tslint:disable-next-line:no-non-null-assertion + const admin: boolean = req!.user!.object!.userGroups! .some((x: UserGroup): boolean => x.name === 'admin'); if (!admin) { diff --git a/src/logger.ts b/src/logger.ts index 069cdf5..bdcb390 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -265,12 +265,17 @@ interface LogItem { timestamp: string; } -const fetchLogs: (level: LogLevel, params?: FetchParams) => LogItem[] = ( - level: LogLevel, params: FetchParams = {} +const fetchLogs: (level?: LogLevel, params?: FetchParams) => LogItem[] = ( + level?: LogLevel, params: FetchParams = {} ): LogItem[] => { - const matches: (log: any, level: LogLevel, params?: FetchParams) => boolean = ( - log: any, minLevel: LogLevel, fetchParams: FetchParams = {} + const matches: (log: any, level?: LogLevel, params?: FetchParams) + => boolean = ( + log: any, minLevel?: LogLevel, fetchParams: FetchParams = {} ): boolean => { + if (!minLevel) { + // tslint:disable-next-line:no-parameter-reassignment + minLevel = 'info'; + } const time: Date = new Date(log.timestamp); if (params.before && params.before < time) { return false; @@ -284,7 +289,7 @@ const fetchLogs: (level: LogLevel, params?: FetchParams) => LogItem[] = ( basicLogger.stream({ level }).on('log', (log: any): void => { if (matches(log, level, params)) { out.push({ - component: log.meta.component, + component: log.component, level: log.level, value: log.message, username: log.user, diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts index 18ee1e3..e23fd60 100644 --- a/test/integration/protocol.routes.test.ts +++ b/test/integration/protocol.routes.test.ts @@ -7,7 +7,8 @@ import * as chai from 'chai'; const expect: Chai.ExpectStatic = chai.expect; -import { Credentials, Database, addUser, initDB, resetDB } from '../test-db'; +import { Credentials, Database, addUser, initDB, resetDB, + Projec, addProjec } from '../test-db'; import { default as User } from '../../src/db/model/User'; // Chai-http must be imported this way: @@ -28,6 +29,7 @@ const app = App.TestApp(); let database: Database; let mockUser: Credentials; let token: string; +let mockProj: Projec; const initDatabase = async() => { database = initDB(); @@ -37,6 +39,7 @@ const initDatabase = async() => { const populateDatabase = async() => { await resetDB(database); mockUser = await addUser(database, true); + mockProj = await addProjec(database, mockUser); }; const getToken = async() => { @@ -122,8 +125,12 @@ describe('routes : protocol', () => { value: types.anything, username: types.string, timestamp: types.string - }), 500], - ['post', '/log', null, 500], + }), 200], + ['post', '/log', [{ + component: 'tesssst', + level: 'info', + value: 'sdasdasdasdasdasd' + }], 200], ['get', '/properties', types.array({ id: types.string, display: optional(match({ @@ -163,7 +170,7 @@ describe('routes : protocol', () => { private_admin_metadata: types.anything }), 401], // TODO types.metadata - ['get', '/users/:username', { + ['get', '/users/mock_user', { username: types.string, privileges: array(types.string), projects: array({ @@ -175,8 +182,8 @@ describe('routes : protocol', () => { public_admin_metadata: types.anything, private_admin_metadata: types.anything }, 401], - ['post', '/users/:username', null, 401], - ['get', '/users/:username/properties', { + ['post', '/users/mock_user', null, 401], + ['get', '/users/mock_user/properties', { data: optional(types.anything) }, 401], ['get', '/current_user', { @@ -207,7 +214,7 @@ describe('routes : protocol', () => { private_metadata: optional(types.anything), admin_metadata: optional(types.anything) }), 200], - ['get', '/projects/:project_name', { + ['get', '/projects/mocky', { project_name: types.string, users: array({ username: types.string, @@ -217,13 +224,13 @@ describe('routes : protocol', () => { private_metadata: types.anything, admin_metadata: optional(types.anything) }, 200], - ['post', '/projects/:project_name', null, 200], - ['get', '/projects/:project_name/properties', { + ['post', '/projects/mocky', null, 200], + ['get', '/projects/mocky/properties', { data: optional(types.anything) }, 200], - ['get', '/projects/:project_name/files/:path', null, 200], - ['post', '/projects/:project_name/files/:id', null, 200], - ['get', '/projects/:project_name/files_by_id/:id', null, 200] + ['get', '/projects/mocky/files/:path', null, 200], + ['post', '/projects/mocky/files/:id', null, 200], + ['get', '/projects/mocky/files_by_id/:id', null, 200] ]; /* tslint:enable:align */ @@ -238,7 +245,8 @@ describe('routes : protocol', () => { (method: string, path: string, temp: Template, resCode: number) => { const request: ChaiHttp.Request = method === 'get' ? chai.request(app).get(base + path) - : chai.request(app).post(base + path); + : chai.request(app).post(base + path) + .set('content-type', 'application/json').send(temp); request.set('Authorization', `Bearer ${token}`); return request.then((res: ChaiHttp.Response) => { expect(res.type).to.equal('application/json'); diff --git a/test/test-db.ts b/test/test-db.ts index f20608a..fa62ad7 100644 --- a/test/test-db.ts +++ b/test/test-db.ts @@ -1,3 +1,4 @@ +import { default as Project } from '../src/db/model/Project'; import { default as User } from '../src/db/model/User'; import { default as UserGroup } from '../src/db/model/UserGroup'; @@ -29,6 +30,13 @@ export interface Credentials { usergroups: UserGroup[]; } +export interface Projec { + name: string; + contributors: Credentials[]; + creationDate: Date; + lastActivity: Date; +} + export const addUser: (database?: Database, admin?: boolean) => Promise<Credentials> = async( database?: Database, admin?: boolean @@ -55,3 +63,18 @@ export const addUser: (database?: Database, admin?: boolean) => await new User(mockUser).save(); return mockUser; }; + +export const addProjec: (database: Database, user: Credentials) => + Promise<Projec> = async( + database: Database, user: Credentials +): Promise<Projec> => { + const mockProject: Projec = { + name: 'mocky', + contributors: [user], + creationDate: new Date(), + lastActivity: new Date() + }; + + await new Project(mockProject).save(); + return mockProject; +}; |
