From 56b06b5ee36ed83fc44d0775b6218aa8fed85b5e Mon Sep 17 00:00:00 2001 From: jw255 Date: Tue, 10 Apr 2018 21:29:15 +0100 Subject: fixed auth tests --- src/app.ts | 4 +++- src/auth.ts | 7 ++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/app.ts b/src/app.ts index c627027..91b8f95 100644 --- a/src/app.ts +++ b/src/app.ts @@ -101,7 +101,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', @@ -112,6 +113,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); } /** 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) { -- cgit v1.2.1 From ff1034dc6f427238b19f895b052e33fc181f0829 Mon Sep 17 00:00:00 2001 From: jw255 Date: Tue, 10 Apr 2018 21:37:42 +0100 Subject: progress on testing --- test/integration/protocol.routes.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts index 18ee1e3..9467f13 100644 --- a/test/integration/protocol.routes.test.ts +++ b/test/integration/protocol.routes.test.ts @@ -111,7 +111,7 @@ describe('routes : protocol', () => { required: array(types.string) }), 200], ['get', '/log', types.array({ - component: types.string, + //component: types.string, level: alternative([ 'info', 'security', -- cgit v1.2.1 From f7220be4e1b80cdba0fbaf41f7382cbf18561e2b Mon Sep 17 00:00:00 2001 From: jw255 Date: Tue, 10 Apr 2018 23:30:51 +0100 Subject: sth works maybe --- src/FileRouter.ts | 6 ++++-- src/app.ts | 1 + src/logger.ts | 15 ++++++++----- test/integration/protocol.routes.test.ts | 36 +++++++++++++++++++------------- test/test-db.ts | 23 ++++++++++++++++++++ 5 files changed, 60 insertions(+), 21 deletions(-) diff --git a/src/FileRouter.ts b/src/FileRouter.ts index 4d6eb16..4d68b6d 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 => { 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(); } }; diff --git a/src/app.ts b/src/app.ts index 91b8f95..1378177 100644 --- a/src/app.ts +++ b/src/app.ts @@ -161,6 +161,7 @@ class App { req: ex.Request, res: ex.Response, next: ex.NextFunction, project: string ): Promise => { + console.log("## as " + project) res.locals.project = await Project.findOne({ where: { name: project diff --git a/src/logger.ts b/src/logger.ts index bbb0a73..4bcb267 100644 --- a/src/logger.ts +++ b/src/logger.ts @@ -186,12 +186,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; @@ -205,7 +210,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 9467f13..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() => { @@ -111,7 +114,7 @@ describe('routes : protocol', () => { required: array(types.string) }), 200], ['get', '/log', types.array({ - //component: types.string, + component: types.string, level: alternative([ 'info', 'security', @@ -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 = 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 = async( + database: Database, user: Credentials +): Promise => { + const mockProject: Projec = { + name: 'mocky', + contributors: [user], + creationDate: new Date(), + lastActivity: new Date() + }; + + await new Project(mockProject).save(); + return mockProject; +}; -- cgit v1.2.1 From 59d2c5c4b1dbc929f4f22192b781c0acd35eb78e Mon Sep 17 00:00:00 2001 From: jw255 Date: Wed, 11 Apr 2018 00:06:25 +0100 Subject: oh god --- src/FileRouter.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/FileRouter.ts b/src/FileRouter.ts index 4d68b6d..630f64f 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -444,8 +444,10 @@ const postProjectName: Middleware = let promise: PromiseLike | 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, @@ -454,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(); }; -- cgit v1.2.1