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(-) (limited to 'test/integration') 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 --- test/integration/protocol.routes.test.ts | 36 +++++++++++++++++++------------- 1 file changed, 22 insertions(+), 14 deletions(-) (limited to 'test/integration') 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'); -- cgit v1.2.1 From 22efa1b090df7a427bfc1990d705624ae53d6231 Mon Sep 17 00:00:00 2001 From: Hafeez Date: Thu, 12 Apr 2018 15:30:34 +0100 Subject: not much progress tbh --- test/integration/protocol.routes.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/integration') diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts index e23fd60..6a08278 100644 --- a/test/integration/protocol.routes.test.ts +++ b/test/integration/protocol.routes.test.ts @@ -240,7 +240,7 @@ describe('routes : protocol', () => { beforeEach(getToken); describe('Access all valid routes', () => { - forEach(completeProtocol).it( + forEach(completeProtocol).it.only( '%s %s should be json', (method: string, path: string, temp: Template, resCode: number) => { const request: ChaiHttp.Request = method === 'get' -- cgit v1.2.1 From 8f013438e4e4c96594b7ed567e7a58dfc1b7ce5a Mon Sep 17 00:00:00 2001 From: Hafeez Date: Fri, 13 Apr 2018 15:53:10 +0100 Subject: fixed mock file tests --- test/integration/protocol.routes.test.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'test/integration') diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts index 6a08278..7554f0e 100644 --- a/test/integration/protocol.routes.test.ts +++ b/test/integration/protocol.routes.test.ts @@ -8,7 +8,7 @@ import * as chai from 'chai'; const expect: Chai.ExpectStatic = chai.expect; import { Credentials, Database, addUser, initDB, resetDB, - Projec, addProjec } from '../test-db'; + Projec, addProjec, Filee, addFilee } from '../test-db'; import { default as User } from '../../src/db/model/User'; // Chai-http must be imported this way: @@ -30,6 +30,7 @@ let database: Database; let mockUser: Credentials; let token: string; let mockProj: Projec; +let mockFile: Filee; const initDatabase = async() => { database = initDB(); @@ -40,6 +41,7 @@ const populateDatabase = async() => { await resetDB(database); mockUser = await addUser(database, true); mockProj = await addProjec(database, mockUser); + mockFile = await addFilee(database, mockUser); }; const getToken = async() => { @@ -228,9 +230,9 @@ describe('routes : protocol', () => { ['get', '/projects/mocky/properties', { data: optional(types.anything) }, 200], - ['get', '/projects/mocky/files/:path', null, 200], - ['post', '/projects/mocky/files/:id', null, 200], - ['get', '/projects/mocky/files_by_id/:id', null, 200] + ['get', '/projects/mocky/files/example/path', null, 200], + ['post', '/projects/mocky/files/mockyfile2', null, 200], + ['get', '/projects/mocky/files_by_id/file1', null, 200] ]; /* tslint:enable:align */ -- cgit v1.2.1 From deab81f8ef469c4119838d72b56683b8dd4cbcfe Mon Sep 17 00:00:00 2001 From: jw255 Date: Fri, 13 Apr 2018 16:10:32 +0100 Subject: fixed some privs --- test/integration/protocol.routes.test.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'test/integration') diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts index 6a08278..3eaafe5 100644 --- a/test/integration/protocol.routes.test.ts +++ b/test/integration/protocol.routes.test.ts @@ -239,8 +239,22 @@ describe('routes : protocol', () => { beforeEach(populateDatabase); beforeEach(getToken); + it.only('test log', () => { + return chai.request(app).get(base + '/log') + .set('Authorization', `Bearer ${token}`) + .catch((err) => { + console.log("a") + console.log(err.response.body) + expect(false); + }).then((res: ChaiHttp.Response) => { + console.log("b") + console.log(res.body) + expect(false); + }); + }); + describe('Access all valid routes', () => { - forEach(completeProtocol).it.only( + forEach(completeProtocol).it( '%s %s should be json', (method: string, path: string, temp: Template, resCode: number) => { const request: ChaiHttp.Request = method === 'get' -- cgit v1.2.1 From f8fa729bb950f1a51eb1cea586483b2132827951 Mon Sep 17 00:00:00 2001 From: jw255 Date: Fri, 13 Apr 2018 16:25:29 +0100 Subject: fixed privileges in testing database --- test/integration/protocol.routes.test.ts | 14 -------------- 1 file changed, 14 deletions(-) (limited to 'test/integration') diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts index 906f3bd..ca20da9 100644 --- a/test/integration/protocol.routes.test.ts +++ b/test/integration/protocol.routes.test.ts @@ -241,20 +241,6 @@ describe('routes : protocol', () => { beforeEach(populateDatabase); beforeEach(getToken); - it.only('test log', () => { - return chai.request(app).get(base + '/log') - .set('Authorization', `Bearer ${token}`) - .catch((err) => { - console.log("a") - console.log(err.response.body) - expect(false); - }).then((res: ChaiHttp.Response) => { - console.log("b") - console.log(res.body) - expect(false); - }); - }); - describe('Access all valid routes', () => { forEach(completeProtocol).it( '%s %s should be json', -- cgit v1.2.1 From a7567c50bbf01e3a8489d732d2825fa01921f2d3 Mon Sep 17 00:00:00 2001 From: jw255 Date: Fri, 13 Apr 2018 16:35:50 +0100 Subject: got it down to 34 errors in the protocol --- test/integration/protocol.routes.test.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'test/integration') diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts index ca20da9..2a7cf54 100644 --- a/test/integration/protocol.routes.test.ts +++ b/test/integration/protocol.routes.test.ts @@ -170,7 +170,7 @@ describe('routes : protocol', () => { private_user_metadata: types.anything, public_admin_metadata: types.anything, private_admin_metadata: types.anything - }), 401], + }), 200], // TODO types.metadata ['get', '/users/mock_user', { username: types.string, @@ -183,11 +183,11 @@ describe('routes : protocol', () => { private_user_metadata: types.anything, public_admin_metadata: types.anything, private_admin_metadata: types.anything - }, 401], - ['post', '/users/mock_user', null, 401], + }, 200], + ['post', '/users/mock_user', null, 200], ['get', '/users/mock_user/properties', { data: optional(types.anything) - }, 401], + }, 200], ['get', '/current_user', { username: types.string, privileges: array(types.string), -- cgit v1.2.1 From c9a574ec946dc7fda6637db45f3223acd0a08e9f Mon Sep 17 00:00:00 2001 From: jw255 Date: Fri, 13 Apr 2018 16:45:32 +0100 Subject: fixed user not found --- test/integration/protocol.routes.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/integration') diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts index 2a7cf54..866ac2a 100644 --- a/test/integration/protocol.routes.test.ts +++ b/test/integration/protocol.routes.test.ts @@ -184,7 +184,7 @@ describe('routes : protocol', () => { public_admin_metadata: types.anything, private_admin_metadata: types.anything }, 200], - ['post', '/users/mock_user', null, 200], + ['post', '/users/new_user', null, 200], ['get', '/users/mock_user/properties', { data: optional(types.anything) }, 200], -- cgit v1.2.1 From 74b2aeed25fbf4c8bcb4d4ae34044e3d1705db0e Mon Sep 17 00:00:00 2001 From: jw255 Date: Fri, 13 Apr 2018 16:58:06 +0100 Subject: fixed template matching in test suite --- test/integration/protocol.routes.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'test/integration') diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts index 866ac2a..ebe18c2 100644 --- a/test/integration/protocol.routes.test.ts +++ b/test/integration/protocol.routes.test.ts @@ -266,14 +266,14 @@ describe('routes : protocol', () => { }); }); forEach(completeProtocol).it( - '%s %s should conform to the general response protocol', + '%s %s should conform to the general response template', (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); request.set('Authorization', `Bearer ${token}`); return request.then((res: ChaiHttp.Response) => { - match(responseTemplate, JSON.parse(res.body)).shoul.be.true; + expect(match(responseTemplate)(res.body)).to.be.true; }); }); forEach(completeProtocol).it( @@ -284,7 +284,7 @@ describe('routes : protocol', () => { .get(base + path) .set('Authorization', `Bearer ${token}`) .then((res: ChaiHttp.Response) => { - expect(match(temp)(JSON.parse(res.body))).to.be.true; + expect(match(temp)(res.body)).to.be.true; }); } else { return true; -- cgit v1.2.1 From 9c0ff9a0b7d73e5e2a12c4f87d1aa586c59adec9 Mon Sep 17 00:00:00 2001 From: jw255 Date: Fri, 13 Apr 2018 21:12:53 +0100 Subject: only 12 failing tests :D --- test/integration/protocol.routes.test.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'test/integration') diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts index ebe18c2..7f9f3e7 100644 --- a/test/integration/protocol.routes.test.ts +++ b/test/integration/protocol.routes.test.ts @@ -10,6 +10,7 @@ const expect: Chai.ExpectStatic = chai.expect; import { Credentials, Database, addUser, initDB, resetDB, Projec, addProjec, Filee, addFilee } from '../test-db'; import { default as User } from '../../src/db/model/User'; +import * as fs from 'fs'; // Chai-http must be imported this way: // tslint:disable-next-line:no-var-requires no-require-imports @@ -153,7 +154,7 @@ describe('routes : protocol', () => { types.boolean ]) }), 200], - ['post', '/properties', null, 200], + ['post', '/properties?action=update', null, 200], ['get', '/user_privileges', array({ privilege: types.string, description: types.string, @@ -231,7 +232,7 @@ describe('routes : protocol', () => { data: optional(types.anything) }, 200], ['get', '/projects/mocky/files/example/path', null, 200], - ['post', '/projects/mocky/files/mockyfile2', null, 200], + ['post', '/projects/mocky/files/some_file?action=overwrite', {}, 200], ['get', '/projects/mocky/files_by_id/file1', null, 200] ]; @@ -257,11 +258,19 @@ describe('routes : protocol', () => { forEach(completeProtocol).it( '%s %s should have a status %4$d', (method: string, path: string, temp: Template, resCode: number) => { - const request: ChaiHttp.Request = method === 'get' + const request: any = method === 'get' ? chai.request(app).get(base + path) : chai.request(app).post(base + path); + /*if (path.includes('/projects/mocky/files/some_file')) { + fs.createReadStream('test/mockUI/ui.html').pipe( + request); + console.log("tasdasdas") + }*/ request.set('Authorization', `Bearer ${token}`); return request.then((res: ChaiHttp.Response) => { + if (path.includes('/projects/mocky/files/some_file')) { + console.log("something") + } expect(res).to.have.status(resCode); }); }); @@ -273,6 +282,10 @@ describe('routes : protocol', () => { : chai.request(app).post(base + path); request.set('Authorization', `Bearer ${token}`); return request.then((res: ChaiHttp.Response) => { + if(path == '/properties' || path == '/current_user'){ + //console.log(res.body) + //console.log(responseTemplate); + } expect(match(responseTemplate)(res.body)).to.be.true; }); }); -- cgit v1.2.1 From 8d355d34cd309122e67f694e22f2023db205794f Mon Sep 17 00:00:00 2001 From: jw255 Date: Fri, 13 Apr 2018 22:07:19 +0100 Subject: much better --- test/integration/protocol.routes.test.ts | 38 ++++++++++++++++---------------- 1 file changed, 19 insertions(+), 19 deletions(-) (limited to 'test/integration') diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts index 7f9f3e7..ea5dd8e 100644 --- a/test/integration/protocol.routes.test.ts +++ b/test/integration/protocol.routes.test.ts @@ -58,8 +58,7 @@ const getToken = async() => { }); }; -import { Template, alternative, array, exact, match, optional - } from '../../src/matcher/options'; +import options, { Template, alternative, array, exact, match, optional } from '../../src/matcher/options'; import { default as types } from '../../src/matcher/types'; // The general response template according to protocol @@ -112,11 +111,11 @@ describe('routes : protocol', () => { // Format: // const completeProtocol: MochaForEachInput[] = [ - ['get', '/_supported_protocols_', exact({ + ['get', '/_supported_protocols_', { supported: array(types.string), required: array(types.string) - }), 200], - ['get', '/log', types.array({ + }, 200], + ['get', '/log', array({ component: types.string, level: alternative([ 'info', @@ -134,7 +133,7 @@ describe('routes : protocol', () => { level: 'info', value: 'sdasdasdasdasdasd' }], 200], - ['get', '/properties', types.array({ + ['get', '/properties', array({ id: types.string, display: optional(match({ category: types.string, @@ -162,8 +161,8 @@ describe('routes : protocol', () => { }), 200], ['get', '/users', array({ username: types.string, - privileges: array(types.string), - projects: array({ + privileges: types.array(types.string), + projects: types.array({ project_name: types.string, access_level: types.string }), @@ -191,8 +190,8 @@ describe('routes : protocol', () => { }, 200], ['get', '/current_user', { username: types.string, - privileges: array(types.string), - projects: array({ + privileges: types.array(types.string), + projects: types.array({ project_name: types.string, access_level: types.string }), @@ -209,7 +208,7 @@ describe('routes : protocol', () => { }), 200], ['get', '/projects', array({ project_name: types.string, - users: array({ + users: types.array({ username: types.string, access_level: types.string }), @@ -228,9 +227,7 @@ describe('routes : protocol', () => { admin_metadata: optional(types.anything) }, 200], ['post', '/projects/mocky', null, 200], - ['get', '/projects/mocky/properties', { - data: optional(types.anything) - }, 200], + ['get', '/projects/mocky/properties', null, 200], ['get', '/projects/mocky/files/example/path', null, 200], ['post', '/projects/mocky/files/some_file?action=overwrite', {}, 200], ['get', '/projects/mocky/files_by_id/file1', null, 200] @@ -282,10 +279,7 @@ describe('routes : protocol', () => { : chai.request(app).post(base + path); request.set('Authorization', `Bearer ${token}`); return request.then((res: ChaiHttp.Response) => { - if(path == '/properties' || path == '/current_user'){ - //console.log(res.body) - //console.log(responseTemplate); - } + expect(match(responseTemplate)(res.body)).to.be.true; }); }); @@ -297,7 +291,13 @@ describe('routes : protocol', () => { .get(base + path) .set('Authorization', `Bearer ${token}`) .then((res: ChaiHttp.Response) => { - expect(match(temp)(res.body)).to.be.true; + if(!match(temp)(res.body.data)){ + console.log("******" + method + " - " + path + "*****8") + console.log(res.body) + console.log(temp); + console.log("*************************************9") + } + expect(match(temp)(res.body.data)).to.be.true; }); } else { return true; -- cgit v1.2.1 From a156860837cb6c044d16802aa5a0cd06ec05648b Mon Sep 17 00:00:00 2001 From: jw255 Date: Sat, 14 Apr 2018 16:49:47 +0100 Subject: fixed 1 test in auth.routes.test.ts --- test/integration/auth.routes.test.ts | 22 ++++++++-------- test/integration/protocol.routes.test.ts | 44 ++++++++++++++++---------------- 2 files changed, 34 insertions(+), 32 deletions(-) (limited to 'test/integration') diff --git a/test/integration/auth.routes.test.ts b/test/integration/auth.routes.test.ts index af3f696..29c0cfc 100644 --- a/test/integration/auth.routes.test.ts +++ b/test/integration/auth.routes.test.ts @@ -65,13 +65,15 @@ const authorisationErr = { error_description: 'user does not have correct authorisation for task' }; +const base: string = '/cs3099group-be-4'; + describe('authentication', () => { before(initDatabase); context('invalid password authentication', () => { before(populateDatabase); it('should reject login to invalid endpoints', () => { return chai.request(app) - .post('/') + .post('/invalid endpoint') .send({ username: mockUser.username, password: mockUser.password, @@ -86,7 +88,7 @@ describe('authentication', () => { }); it('should reject invalid usernames', () => { return chai.request(app) - .post('/cs3099group-be-4/oauth/token') + .post(`${base}/oauth/token`) .send({ username: 'invalid', password: mockUser.password, @@ -97,7 +99,7 @@ describe('authentication', () => { }); it('should reject invalid passwords', () => { return chai.request(app) - .post('/cs3099group-be-4/oauth/token') + .post(`${base}/oauth/token`) .send({ username: mockUser.username, password: 'asasasas', @@ -108,7 +110,7 @@ describe('authentication', () => { }); it('should reject invalid grant_type', () => { return chai.request(app) - .post('/cs3099group-be-4/oauth/token') + .post(`${base}/oauth/token`) .send({ username: mockUser.username, password: mockUser.password, @@ -119,7 +121,7 @@ describe('authentication', () => { }); it('should reject empty usernames', () => { return chai.request(app) - .post('/cs3099group-be-4/oauth/token') + .post(`${base}/oauth/token`) .send({ username: '', password: mockUser.password, @@ -130,7 +132,7 @@ describe('authentication', () => { }); it('should reject empty passwords', () => { return chai.request(app) - .post('/cs3099group-be-4/oauth/token') + .post(`${base}/oauth/token`) .send({ username: mockUser.username, password: '', @@ -141,7 +143,7 @@ describe('authentication', () => { }); it('should reject empty grant_type', () => { return chai.request(app) - .post('/cs3099group-be-4/oauth/token') + .post(`${base}/oauth/token`) .send({ username: mockUser.username, password: mockUser.password, @@ -155,7 +157,7 @@ describe('authentication', () => { before(populateDatabase); it('should accept correct credentials', () => { return chai.request(app) - .post('/cs3099group-be-4/oauth/token') + .post(`${base}/oauth/token`) .send({ username: mockUser.username, password: mockUser.password, @@ -169,7 +171,7 @@ describe('authentication', () => { beforeEach(populateDatabase); beforeEach(getToken); it('should reject invalid tokens', () => { - return chai.request(app).get('/') + return chai.request(app).get(`${base}/users`) .set('Authorization', 'Bearer ' + 'invalid') .then((res) => { expect(res).to.have.status(401); @@ -178,7 +180,7 @@ describe('authentication', () => { }); }); it('should accept valid tokens', () => { - return chai.request(app).get('/') + return chai.request(app).get(`${base}/_supported_protocols_`) .set('Authorization', `Bearer ${token}`) .then((res) => { expect(res).to.have.status(200); diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts index ea5dd8e..ad9b892 100644 --- a/test/integration/protocol.routes.test.ts +++ b/test/integration/protocol.routes.test.ts @@ -58,7 +58,7 @@ const getToken = async() => { }); }; -import options, { Template, alternative, array, exact, match, optional } from '../../src/matcher/options'; +import options, { Template, alternative, array, exact, match, optional }from '../../src/matcher/options'; import { default as types } from '../../src/matcher/types'; // The general response template according to protocol @@ -82,27 +82,6 @@ const errorResponseTemplate: Template = { type MochaForEachInput = [ string, string, Template, number ]; -describe('routes : errors', () => { - before(initDatabase); - describe('GET invalid routes', () => { - // Should invalid routes also possibly be 401? - it.skip('should have response code 404', () => { - return chai.request(app).get('/invalid/route') - .set('Authorization', `Bearer ${token}`) - .catch((err) => { - expect(err.status).to.equal(404); - }); - }); - it('should conform to the error protocol', () => { - return chai.request(app).get('/invalid/route') - .set('Authorization', `Bearer ${token}`) - .catch((err) => { - expect(match(errorResponseTemplate)(err.response.body)).to.be.true; - }); - }); - }); -}); - describe('routes : protocol', () => { const base: string = '/cs3099group-be-4'; @@ -304,4 +283,25 @@ describe('routes : protocol', () => { } }); }); + + describe('test edge cases', () => { + before(initDatabase); + describe('GET invalid routes', () => { + // Should invalid routes also possibly be 401? + it.skip('should have response code 404', () => { + return chai.request(app).get('/invalid/route') + .set('Authorization', `Bearer ${token}`) + .catch((err) => { + expect(err.status).to.equal(404); + }); + }); + it('should conform to the error protocol', () => { + return chai.request(app).get('/invalid/route') + .set('Authorization', `Bearer ${token}`) + .catch((err) => { + expect(match(errorResponseTemplate)(err.response.body)).to.be.true; + }); + }); + }); + }); }); -- cgit v1.2.1 From 1e6cb08fcdec54f1263eb1f6fdae6e0f438dfc97 Mon Sep 17 00:00:00 2001 From: jw255 Date: Sun, 15 Apr 2018 17:24:10 +0100 Subject: fixed logging --- test/integration/protocol.routes.test.ts | 315 ++++++++++++++++++------------- 1 file changed, 180 insertions(+), 135 deletions(-) (limited to 'test/integration') diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts index ad9b892..1e8bf63 100644 --- a/test/integration/protocol.routes.test.ts +++ b/test/integration/protocol.routes.test.ts @@ -82,137 +82,138 @@ const errorResponseTemplate: Template = { type MochaForEachInput = [ string, string, Template, number ]; -describe('routes : protocol', () => { - const base: string = '/cs3099group-be-4'; +const base: string = '/cs3099group-be-4'; - /* tslint:disable:align */ +/* tslint:disable:align */ - // Format: - // - const completeProtocol: MochaForEachInput[] = [ - ['get', '/_supported_protocols_', { - supported: array(types.string), - required: array(types.string) - }, 200], - ['get', '/log', array({ - component: types.string, - level: alternative([ - 'info', - 'security', - 'warning', - 'error', - 'critical' - ]), - value: types.anything, - username: types.string, - timestamp: types.string - }), 200], - ['post', '/log', [{ - component: 'tesssst', - level: 'info', - value: 'sdasdasdasdasdasd' - }], 200], - ['get', '/properties', array({ - id: types.string, - display: optional(match({ - category: types.string, - group: types.string, - display_name: types.string, - description: types.string - })), - read_only: types.boolean, - type: alternative([ - 'string', - 'integer', - 'boolean' - ]), - value: alternative([ - types.string, - types.integer, - types.boolean - ]) - }), 200], - ['post', '/properties?action=update', null, 200], - ['get', '/user_privileges', array({ - privilege: types.string, - description: types.string, - internal: types.boolean - }), 200], - ['get', '/users', array({ - username: types.string, - privileges: types.array(types.string), - projects: types.array({ - project_name: types.string, - access_level: types.string - }), - public_user_metadata: types.anything, - private_user_metadata: types.anything, - public_admin_metadata: types.anything, - private_admin_metadata: types.anything - }), 200], - // TODO types.metadata - ['get', '/users/mock_user', { - username: types.string, - privileges: array(types.string), - projects: array({ - project_name: types.string, - access_level: types.string - }), - public_user_metadata: types.anything, - private_user_metadata: types.anything, - public_admin_metadata: types.anything, - private_admin_metadata: types.anything - }, 200], - ['post', '/users/new_user', null, 200], - ['get', '/users/mock_user/properties', { - data: optional(types.anything) - }, 200], - ['get', '/current_user', { - username: types.string, - privileges: types.array(types.string), - projects: types.array({ - project_name: types.string, - access_level: types.string - }), - public_user_metadata: types.anything, - private_user_metadata: types.anything, - public_admin_metadata: types.anything, - private_admin_metadata: types.anything - }, 200], - ['post', '/current_user', null, 200], - ['get', '/project_roles', array({ - role: types.string, - description: types.string, - internal: types.boolean - }), 200], - ['get', '/projects', array({ +// Format: +// +const completeProtocol: MochaForEachInput[] = [ + ['get', '/_supported_protocols_', { + supported: array(types.string), + required: array(types.string) + }, 200], + ['get', '/log', array({ + component: types.string, + level: alternative([ + 'info', + 'security', + 'warning', + 'error', + 'critical' + ]), + value: types.anything, + username: types.string, + timestamp: types.string + }), 200], + ['post', '/log', [{ + component: 'tesssst', + level: 'info', + value: 'sdasdasdasdasdasd' + }], 200], + ['get', '/properties', array({ + id: types.string, + display: optional(match({ + category: types.string, + group: types.string, + display_name: types.string, + description: types.string + })), + read_only: types.boolean, + type: alternative([ + 'string', + 'integer', + 'boolean' + ]), + value: alternative([ + types.string, + types.integer, + types.boolean + ]) + }), 200], + ['post', '/properties?action=update', null, 200], + ['get', '/user_privileges', array({ + privilege: types.string, + description: types.string, + internal: types.boolean + }), 200], + ['get', '/users', array({ + username: types.string, + privileges: types.array(types.string), + projects: types.array({ + project_name: types.string, + access_level: types.string + }), + public_user_metadata: types.anything, + private_user_metadata: types.anything, + public_admin_metadata: types.anything, + private_admin_metadata: types.anything + }), 200], + // TODO types.metadata + ['get', '/users/mock_user', { + username: types.string, + privileges: array(types.string), + projects: array({ project_name: types.string, - users: types.array({ - username: types.string, - access_level: types.string - }), - public_metadata: types.anything, - private_metadata: optional(types.anything), - admin_metadata: optional(types.anything) - }), 200], - ['get', '/projects/mocky', { + access_level: types.string + }), + public_user_metadata: types.anything, + private_user_metadata: types.anything, + public_admin_metadata: types.anything, + private_admin_metadata: types.anything + }, 200], + ['post', '/users/new_user', null, 200], + ['get', '/users/mock_user/properties', { + data: optional(types.anything) + }, 200], + ['get', '/current_user', { + username: types.string, + privileges: types.array(types.string), + projects: types.array({ project_name: types.string, - users: array({ - username: types.string, - access_level: types.string - }), - public_metadata: types.anything, - private_metadata: types.anything, - admin_metadata: optional(types.anything) - }, 200], - ['post', '/projects/mocky', null, 200], - ['get', '/projects/mocky/properties', null, 200], - ['get', '/projects/mocky/files/example/path', null, 200], - ['post', '/projects/mocky/files/some_file?action=overwrite', {}, 200], - ['get', '/projects/mocky/files_by_id/file1', null, 200] - ]; + access_level: types.string + }), + public_user_metadata: types.anything, + private_user_metadata: types.anything, + public_admin_metadata: types.anything, + private_admin_metadata: types.anything + }, 200], + ['post', '/current_user', null, 200], + ['get', '/project_roles', array({ + role: types.string, + description: types.string, + internal: types.boolean + }), 200], + ['get', '/projects', array({ + project_name: types.string, + users: types.array({ + username: types.string, + access_level: types.string + }), + public_metadata: types.anything, + private_metadata: optional(types.anything), + admin_metadata: optional(types.anything) + }), 200], + ['get', '/projects/mocky', { + project_name: types.string, + users: array({ + username: types.string, + access_level: types.string + }), + public_metadata: types.anything, + private_metadata: types.anything, + admin_metadata: optional(types.anything) + }, 200], + ['post', '/projects/mocky', null, 200], + ['get', '/projects/mocky/properties', null, 200], + ['get', '/projects/mocky/files/example/path', null, 200], + ['post', '/projects/mocky/files/some_file?action=overwrite', {}, 200], + ['get', '/projects/mocky/files_by_id/file1', null, 200] +]; - /* tslint:enable:align */ +/* tslint:enable:align */ + +describe('routes : protocol', () => { before(initDatabase); beforeEach(populateDatabase); @@ -236,7 +237,7 @@ describe('routes : protocol', () => { (method: string, path: string, temp: Template, resCode: number) => { const request: any = method === 'get' ? chai.request(app).get(base + path) - : chai.request(app).post(base + path); + : chai.request(app).post(base + path).send(temp); /*if (path.includes('/projects/mocky/files/some_file')) { fs.createReadStream('test/mockUI/ui.html').pipe( request); @@ -245,7 +246,7 @@ describe('routes : protocol', () => { request.set('Authorization', `Bearer ${token}`); return request.then((res: ChaiHttp.Response) => { if (path.includes('/projects/mocky/files/some_file')) { - console.log("something") + //console.log("something") } expect(res).to.have.status(resCode); }); @@ -255,26 +256,33 @@ 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).send(temp);; request.set('Authorization', `Bearer ${token}`); return request.then((res: ChaiHttp.Response) => { - + if (!(match(responseTemplate)(res.body))) { + console.log(`${method} ${path} ******************`); + console.log(res.body); + console.log("^^^^^^ received || template vvvvvvvv") + console.log(responseTemplate); + console.log('--------------------------------') + } expect(match(responseTemplate)(res.body)).to.be.true; }); }); forEach(completeProtocol).it( - 'the response for %s %s should conform to its specific response protocol', + '%s %s should conform to its specific response template', (method: string, path: string, temp: Template, resCode: number) => { if (method === 'get' && temp != null) { return chai.request(app) .get(base + path) .set('Authorization', `Bearer ${token}`) .then((res: ChaiHttp.Response) => { - if(!match(temp)(res.body.data)){ - console.log("******" + method + " - " + path + "*****8") - console.log(res.body) + if (!(match(temp)(res.body.data))) { + console.log(`${method} ${path} ******************`); + console.log(res.body.data); + console.log("^^^^^^ received || template vvvvvvvv") console.log(temp); - console.log("*************************************9") + console.log('--------------------------------') } expect(match(temp)(res.body.data)).to.be.true; }); @@ -286,6 +294,9 @@ describe('routes : protocol', () => { describe('test edge cases', () => { before(initDatabase); + beforeEach(populateDatabase); + beforeEach(getToken); + describe('GET invalid routes', () => { // Should invalid routes also possibly be 401? it.skip('should have response code 404', () => { @@ -303,5 +314,39 @@ describe('routes : protocol', () => { }); }); }); + + describe('POST invalid routes', () => { + // Should invalid routes also possibly be 401? + it.skip('should have response code 404', () => { + return chai.request(app).post('/invalid/route') + .set('Authorization', `Bearer ${token}`) + .catch((err) => { + expect(err.status).to.equal(404); + }); + }); + it('should conform to the error protocol', () => { + return chai.request(app).post('/invalid/route') + .set('Authorization', `Bearer ${token}`) + .catch((err) => { + expect(match(errorResponseTemplate)(err.response.body)).to.be.true; + }); + }); + }); +/* + describe('access valid routes with invalid parameters', () => { + forEach(completeProtocol.filter((item, index, arr) => { + return (item[0] == 'post'); + })).it( + '%s %s with null body should throw error', + (method: string, path: string, temp: Template, resCode: number) => { + const request: any = chai.request(app).post(base + path); + request.set('Authorization', `Bearer ${token}`) + .then((res: any) => { + expect(res).to.have.status(400); + }).catch((reason: any) => { + expect(reason.response.body).to.eql("errererere"); + }); + }); + });*/ }); }); -- cgit v1.2.1