aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/FileRouter.ts6
-rw-r--r--src/app.ts1
-rw-r--r--src/logger.ts15
-rw-r--r--test/integration/protocol.routes.test.ts36
-rw-r--r--test/test-db.ts23
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<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();
}
};
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<void> => {
+ 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<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;
+};