From f66a9fa7f132f41b2e40029f0fb535521c3fb716 Mon Sep 17 00:00:00 2001 From: jw255 Date: Sat, 24 Feb 2018 17:13:43 +0000 Subject: restructuring of tests + yarn install script --- test/integration/auth.routes.test.ts | 42 +++++++ test/integration/index.routes.test.ts | 42 +++++++ test/integration/protocol.routes.test.ts | 204 +++++++++++++++++++++++++++++++ 3 files changed, 288 insertions(+) create mode 100644 test/integration/auth.routes.test.ts create mode 100644 test/integration/index.routes.test.ts create mode 100644 test/integration/protocol.routes.test.ts (limited to 'test/integration') diff --git a/test/integration/auth.routes.test.ts b/test/integration/auth.routes.test.ts new file mode 100644 index 0000000..edfbd0a --- /dev/null +++ b/test/integration/auth.routes.test.ts @@ -0,0 +1,42 @@ +/* tslint:disable:newline-per-chained-call */ +/* Chai assertions look silly when formatted multiline. */ +/* tslint:disable:no-unused-expression */ +/* Chai assertions do not require assignation. */ + +import * as chai from 'chai'; +const expect: Chai.ExpectStatic = chai.expect; + +// Chai-http must be imported this way: +// tslint:disable-next-line:no-var-requires no-require-imports +chai.use(require('chai-http')); + +// Mocha must be imported as a side-effect library: +// tslint:disable-next-line:no-import-side-effect +import 'mocha'; + +import * as App from '../../src/app'; +let app = App.TestApp(); + +describe('baseRoute', () => { + it('should be json', () => { + return chai.request(app).get('/') + .then((res: ChaiHttp.Response) => { + expect(res.type).to.eql('application/json'); + }); + }); + + it('should have a message prop', () => { + return chai.request(app).get('/') + .then((res: ChaiHttp.Response) => { + expect(res.body.message).to.eql('Welcome to the CS3099 BE4 server!'); + }); + }); + + it('should have an important prop', () => { + return chai.request(app).get('/') + .then((res: ChaiHttp.Response) => { + expect(res.body.important).to.eql('Endpoints start from /cs3099group-be-4/'); + }); + }); + +}); \ No newline at end of file diff --git a/test/integration/index.routes.test.ts b/test/integration/index.routes.test.ts new file mode 100644 index 0000000..edfbd0a --- /dev/null +++ b/test/integration/index.routes.test.ts @@ -0,0 +1,42 @@ +/* tslint:disable:newline-per-chained-call */ +/* Chai assertions look silly when formatted multiline. */ +/* tslint:disable:no-unused-expression */ +/* Chai assertions do not require assignation. */ + +import * as chai from 'chai'; +const expect: Chai.ExpectStatic = chai.expect; + +// Chai-http must be imported this way: +// tslint:disable-next-line:no-var-requires no-require-imports +chai.use(require('chai-http')); + +// Mocha must be imported as a side-effect library: +// tslint:disable-next-line:no-import-side-effect +import 'mocha'; + +import * as App from '../../src/app'; +let app = App.TestApp(); + +describe('baseRoute', () => { + it('should be json', () => { + return chai.request(app).get('/') + .then((res: ChaiHttp.Response) => { + expect(res.type).to.eql('application/json'); + }); + }); + + it('should have a message prop', () => { + return chai.request(app).get('/') + .then((res: ChaiHttp.Response) => { + expect(res.body.message).to.eql('Welcome to the CS3099 BE4 server!'); + }); + }); + + it('should have an important prop', () => { + return chai.request(app).get('/') + .then((res: ChaiHttp.Response) => { + expect(res.body.important).to.eql('Endpoints start from /cs3099group-be-4/'); + }); + }); + +}); \ No newline at end of file diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts new file mode 100644 index 0000000..7123beb --- /dev/null +++ b/test/integration/protocol.routes.test.ts @@ -0,0 +1,204 @@ +/* tslint:disable:newline-per-chained-call */ +/* Chai assertions look silly when formatted multiline. */ +/* tslint:disable:no-unused-expression */ +/* Chai assertions do not require assignation. */ + +import * as chai from 'chai'; +const expect: Chai.ExpectStatic = chai.expect; + +// Chai-http must be imported this way: +// tslint:disable-next-line:no-var-requires no-require-imports +chai.use(require('chai-http')); + +// Mocha must be imported as a side-effect library: +// tslint:disable-next-line:no-import-side-effect +import 'mocha'; + +// Mocha-each must be imported using a require call: +// tslint:disable-next-line:no-require-imports +import forEach = require('mocha-each'); + +import { Template, alternative, array, exact, match, optional + } from '../src/matcher/options'; +import { default as types } from '../src/matcher/types'; + +import * as App from '../src/app'; +let app = App.TestApp(); + + +const responseTemplate: Template = { + status: types.string, + data: optional(types.anything), + error: optional(types.string), + error_description: optional(types.string), + user_message: optional(types.string), + error_data: optional(types.anything) +}; + +type MochaForEachInput = [ string, string, Template ]; + +describe('Protocol', () => { + const completeProtocol: MochaForEachInput[] = [ + ['get', '/_supported_protocols_', exact({ + supported: array(types.string), + required: array(types.string) + })], + ['get', '/log', types.array({ + component: types.string, + level: alternative([ + 'info', + 'security', + 'warning', + 'error', + 'critical' + ]), + value: types.anything, + username: types.string, + timestamp: types.string + })], + ['post', '/log', null], + ['get', '/properties', types.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 + ]) + })], + ['post', '/properties', null], + ['get', '/user_privileges', array({ + privilege: types.string, + description: types.string, + internal: types.boolean + })], + ['get', '/users', array({ + 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 + })], + // TODO types.metadata + ['get', '/users/:username', { + 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 + }], + ['post', '/users/:username', null], + ['get', '/users/:username/properties', { + data: optional(types.anything) + }], + ['get', '/current_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 + }], + ['post', '/current_user', null], + ['get', '/project_roles', array({ + role: types.string, + description: types.string, + internal: types.boolean + })], + ['get', '/projects', array({ + project_name: types.string, + users: array({ + username: types.string, + access_level: types.string + }), + public_metadata: types.anything, + private_metadata: optional(types.anything), + admin_metadata: optional(types.anything) + })], + ['get', '/projects/:project_name', { + 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) + }], + ['post', '/projects/:project_name', null], + ['get', '/projects/:project_name/properties', { + data: optional(types.anything) + }], + ['get', '/projects/:project_name/files/:path', null], + ['post', '/projects/:project_name/files/:id', null], + ['get', '/projects/:project_name/files_by_id/:id', null] + ]; + + describe('Response specification', () => { + forEach(completeProtocol).it( + '%s %s should be json', + (method: string, path: string, temp: Template) => { + const request: ChaiHttp.Request = method === 'get' + ? chai.request(app).get(path) + : chai.request(app).post(path); + request.then((res: ChaiHttp.Response) => { + expect(res.type).to.equal('application/json'); + }); + }); + forEach(completeProtocol).it( + '%s %s should have a status 200', + (method: string, path: string, temp: Template) => { + const request: ChaiHttp.Request = method === 'get' + ? chai.request(app).get(path) + : chai.request(app).post(path); + request.then((res: ChaiHttp.Response) => { + expect(res).to.have.status(200); + }); + }); + forEach(completeProtocol).it( + '%s %s should conform to the standard response protocol', + (method: string, path: string, temp: Template) => { + const request: ChaiHttp.Request = method === 'get' + ? chai.request(app).get(path) + : chai.request(app).post(path); + request.then((res: ChaiHttp.Response) => { + match(responseTemplate, JSON.parse(res.body)).shoul.be.true; + }); + }); + forEach(completeProtocol).it( + 'the response for %s %s should conform to its specific response protocol', + (method: string, path: string, temp: Template) => { + if (method === 'get' && temp != null) { + return chai.request(app).get(path) + .then((res: ChaiHttp.Response) => { + match(temp)(JSON.parse(res.body)).should.be.true; + }); + } + }); + }); +}); -- cgit v1.2.1 From 80f5aeb37b3d997015238173499f4a067ec0c266 Mon Sep 17 00:00:00 2001 From: jw255 Date: Sat, 24 Feb 2018 19:34:20 +0000 Subject: tests now actually test what they are supposed to and catch errors if needed --- test/integration/auth.routes.test.ts | 2 +- test/integration/index.routes.test.ts | 43 +++++++++++-------- test/integration/protocol.routes.test.ts | 73 ++++++++++++++++++++++++-------- 3 files changed, 82 insertions(+), 36 deletions(-) (limited to 'test/integration') diff --git a/test/integration/auth.routes.test.ts b/test/integration/auth.routes.test.ts index edfbd0a..e0fb723 100644 --- a/test/integration/auth.routes.test.ts +++ b/test/integration/auth.routes.test.ts @@ -17,7 +17,7 @@ import 'mocha'; import * as App from '../../src/app'; let app = App.TestApp(); -describe('baseRoute', () => { +describe('authentication', () => { it('should be json', () => { return chai.request(app).get('/') .then((res: ChaiHttp.Response) => { diff --git a/test/integration/index.routes.test.ts b/test/integration/index.routes.test.ts index edfbd0a..7728db8 100644 --- a/test/integration/index.routes.test.ts +++ b/test/integration/index.routes.test.ts @@ -5,6 +5,7 @@ import * as chai from 'chai'; const expect: Chai.ExpectStatic = chai.expect; +const should = chai.should(); // Chai-http must be imported this way: // tslint:disable-next-line:no-var-requires no-require-imports @@ -17,26 +18,34 @@ import 'mocha'; import * as App from '../../src/app'; let app = App.TestApp(); -describe('baseRoute', () => { - it('should be json', () => { - return chai.request(app).get('/') - .then((res: ChaiHttp.Response) => { - expect(res.type).to.eql('application/json'); +describe('routes : index', () => { + describe('GET /', () => { + it('should return 200', () => { + return chai.request(app).get('/') + .then((res: ChaiHttp.Response) => { + res.status.should.eql(200); + }) }); - }); - it('should have a message prop', () => { - return chai.request(app).get('/') - .then((res: ChaiHttp.Response) => { - expect(res.body.message).to.eql('Welcome to the CS3099 BE4 server!'); + it('should return json', () => { + return chai.request(app).get('/') + .then((res: ChaiHttp.Response) => { + expect(res.type).to.eql('application/json'); + }); }); - }); - - it('should have an important prop', () => { - return chai.request(app).get('/') - .then((res: ChaiHttp.Response) => { - expect(res.body.important).to.eql('Endpoints start from /cs3099group-be-4/'); + + it('should return a message prop', () => { + return chai.request(app).get('/') + .then((res: ChaiHttp.Response) => { + expect(res.body.message).to.eql('Welcome to the CS3099 BE4 server!'); + }); + }); + + it('should return an important prop', () => { + return chai.request(app).get('/') + .then((res: ChaiHttp.Response) => { + expect(res.body.important).to.eql('Endpoints start from /cs3099group-be-4/'); + }); }); }); - }); \ No newline at end of file diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts index 7123beb..f2a3569 100644 --- a/test/integration/protocol.routes.test.ts +++ b/test/integration/protocol.routes.test.ts @@ -19,10 +19,10 @@ import 'mocha'; import forEach = require('mocha-each'); import { Template, alternative, array, exact, match, optional - } from '../src/matcher/options'; -import { default as types } from '../src/matcher/types'; + } from '../../src/matcher/options'; +import { default as types } from '../../src/matcher/types'; -import * as App from '../src/app'; +import * as App from '../../src/app'; let app = App.TestApp(); @@ -35,9 +35,36 @@ const responseTemplate: Template = { error_data: optional(types.anything) }; +const errorResponseTemplate: Template = { + status: 'error', + error: types.string, + error_description: optional(types.string), + user_message: optional(types.string), + error_data: optional(types.anything) +}; + type MochaForEachInput = [ string, string, Template ]; -describe('Protocol', () => { +describe('routes : errors', () => { + describe('GET invalid routes', function(){ + it('should have response code 404', () => { + return chai.request(app).get('/invalid/route') + .catch(function (res) { + expect(res.status).to.equal(404); + }); + }); + it('should conform to the error protocol', function(){ + return chai.request(app).get('/invalid/route') + .catch(function (err: any) { + match(errorResponseTemplate)(err.response.body).should.be.true; + }); + }) + }); + +}); + +describe('routes : protocol', () => { + const base: string = '/cs3099group-be-4'; const completeProtocol: MochaForEachInput[] = [ ['get', '/_supported_protocols_', exact({ supported: array(types.string), @@ -159,45 +186,55 @@ describe('Protocol', () => { ['get', '/projects/:project_name/files_by_id/:id', null] ]; - describe('Response specification', () => { + describe('GET/POST all valid routes', () => { forEach(completeProtocol).it( '%s %s should be json', (method: string, path: string, temp: Template) => { const request: ChaiHttp.Request = method === 'get' - ? chai.request(app).get(path) - : chai.request(app).post(path); - request.then((res: ChaiHttp.Response) => { + ? chai.request(app).get(base + path) + : chai.request(app).post(base + path); + return request.then((res: ChaiHttp.Response) => { expect(res.type).to.equal('application/json'); - }); + }).catch(function (err: any) { + match(errorResponseTemplate)(err.response.body).should.be.true; + });; }); forEach(completeProtocol).it( '%s %s should have a status 200', (method: string, path: string, temp: Template) => { const request: ChaiHttp.Request = method === 'get' - ? chai.request(app).get(path) - : chai.request(app).post(path); - request.then((res: ChaiHttp.Response) => { + ? chai.request(app).get(base + path) + : chai.request(app).post(base + path); + return request.then((res: ChaiHttp.Response) => { expect(res).to.have.status(200); - }); + }).catch(function (err: any) { + match(errorResponseTemplate)(err.response.body).should.be.true; + });; }); forEach(completeProtocol).it( '%s %s should conform to the standard response protocol', (method: string, path: string, temp: Template) => { const request: ChaiHttp.Request = method === 'get' - ? chai.request(app).get(path) - : chai.request(app).post(path); - request.then((res: ChaiHttp.Response) => { + ? chai.request(app).get(base + path) + : chai.request(app).post(base + path); + return request.then((res: ChaiHttp.Response) => { match(responseTemplate, JSON.parse(res.body)).shoul.be.true; - }); + }).catch(function (err: any) { + match(errorResponseTemplate)(err.response.body).should.be.true; + });; }); forEach(completeProtocol).it( 'the response for %s %s should conform to its specific response protocol', (method: string, path: string, temp: Template) => { if (method === 'get' && temp != null) { - return chai.request(app).get(path) + return chai.request(app).get(base + path) .then((res: ChaiHttp.Response) => { match(temp)(JSON.parse(res.body)).should.be.true; + }).catch(function (err: any) { + match(errorResponseTemplate)(err.response.body).should.be.true; }); + }else{ + return true; } }); }); -- cgit v1.2.1 From a33871c02e1657cc625bf296595d6c2f63c1590d Mon Sep 17 00:00:00 2001 From: jw255 Date: Tue, 27 Feb 2018 11:12:18 +0000 Subject: fixed compilation issues --- test/integration/protocol.routes.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test/integration') diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts index ccacbd4..9c51e23 100644 --- a/test/integration/protocol.routes.test.ts +++ b/test/integration/protocol.routes.test.ts @@ -22,8 +22,8 @@ import * as App from '../../src/app'; let app = App.TestApp(); -chai.use(chaiHttp); -const expect = chai.expect; +//chai.use(chaiHttp); +//const expect = chai.expect; const errorResponseTemplate: Template = { status: 'error', -- cgit v1.2.1 From 0caebd4bfdfd9c8d7840fc379412d5d1f867575f Mon Sep 17 00:00:00 2001 From: jw255 Date: Sat, 3 Mar 2018 14:53:31 +0000 Subject: pulled latest files from trh/payload-checker --- test/integration/protocol.routes.test.ts | 95 +++++++++++++++++--------------- 1 file changed, 52 insertions(+), 43 deletions(-) (limited to 'test/integration') diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts index 9c51e23..808fe50 100644 --- a/test/integration/protocol.routes.test.ts +++ b/test/integration/protocol.routes.test.ts @@ -2,29 +2,18 @@ import * as mocha from 'mocha'; import * as chai from 'chai'; const expect: Chai.ExpectStatic = chai.expect; -// Chai-http must be imported this way: -// tslint:disable-next-line:no-var-requires no-require-imports chai.use(require('chai-http')); - -// Mocha must be imported as a side-effect library: -// tslint:disable-next-line:no-import-side-effect import 'mocha'; - -// Mocha-each must be imported using a require call: -// tslint:disable-next-line:no-require-imports import forEach = require('mocha-each'); +import request = require('supertest'); import { Template, alternative, array, exact, match, optional } from '../../src/matcher/options'; import { default as types } from '../../src/matcher/types'; - import * as App from '../../src/app'; let app = App.TestApp(); - -//chai.use(chaiHttp); -//const expect = chai.expect; - +// The error response template according to protocol const errorResponseTemplate: Template = { status: 'error', error: types.string, @@ -33,14 +22,32 @@ const errorResponseTemplate: Template = { error_data: optional(types.anything) }; -type MochaForEachInput = [ string, string, Template ]; +type MochaForEachInput = [ string, string, Template, number ]; + +const userCredentials = { + email: 'sponge@bob.com', + password: 'garyTheSnail' +} + +//now let's login the user before we run any tests +let authenticatedUser = request.agent(app); + +before(function(done){ + authenticatedUser + .post('/login') + .send(userCredentials) + .end(function(err, response){ + expect(response.statusCode).to.equal(200); + done(); + }); +}); describe('routes : errors', () => { describe('GET invalid routes', function(){ it('should have response code 404', () => { return chai.request(app).get('/invalid/route') - .catch(function (res) { - expect(res.status).to.equal(404); + .catch(function (err: any) { + expect(err.status).to.equal(404); }); }); it('should conform to the error protocol', function(){ @@ -55,11 +62,13 @@ describe('routes : errors', () => { describe('routes : protocol', () => { const base: string = '/cs3099group-be-4'; + // format: + // const completeProtocol: MochaForEachInput[] = [ ['get', '/_supported_protocols_', exact({ supported: array(types.string), required: array(types.string) - })], + }), 200], ['get', '/log', types.array({ component: types.string, level: alternative([ @@ -72,8 +81,8 @@ describe('routes : protocol', () => { value: types.anything, username: types.string, timestamp: types.string - })], - ['post', '/log', null], + }), 500], + ['post', '/log', null, 500], ['get', '/properties', types.array({ id: types.string, display: optional(match({ @@ -93,13 +102,13 @@ describe('routes : protocol', () => { types.integer, types.boolean ]) - })], - ['post', '/properties', null], + }), 200], + ['post', '/properties', null, 200], ['get', '/user_privileges', array({ privilege: types.string, description: types.string, internal: types.boolean - })], + }), 200], ['get', '/users', array({ username: types.string, privileges: array(types.string), @@ -111,8 +120,8 @@ describe('routes : protocol', () => { private_user_metadata: types.anything, public_admin_metadata: types.anything, private_admin_metadata: types.anything - })], - // TODO types.metadata + }), 401], + // todo types.metadata ['get', '/users/:username', { username: types.string, privileges: array(types.string), @@ -124,11 +133,11 @@ describe('routes : protocol', () => { private_user_metadata: types.anything, public_admin_metadata: types.anything, private_admin_metadata: types.anything - }], - ['post', '/users/:username', null], + }, 401], + ['post', '/users/:username', null, 401], ['get', '/users/:username/properties', { data: optional(types.anything) - }], + }, 401], ['get', '/current_user', { username: types.string, privileges: array(types.string), @@ -140,13 +149,13 @@ describe('routes : protocol', () => { private_user_metadata: types.anything, public_admin_metadata: types.anything, private_admin_metadata: types.anything - }], - ['post', '/current_user', null], + }, 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: array({ @@ -156,7 +165,7 @@ describe('routes : protocol', () => { public_metadata: types.anything, private_metadata: optional(types.anything), admin_metadata: optional(types.anything) - })], + }), 200], ['get', '/projects/:project_name', { project_name: types.string, users: array({ @@ -166,20 +175,20 @@ describe('routes : protocol', () => { public_metadata: types.anything, private_metadata: types.anything, admin_metadata: optional(types.anything) - }], - ['post', '/projects/:project_name', null], + }, 200], + ['post', '/projects/:project_name', null, 200], ['get', '/projects/:project_name/properties', { data: optional(types.anything) - }], - ['get', '/projects/:project_name/files/:path', null], - ['post', '/projects/:project_name/files/:id', null], - ['get', '/projects/:project_name/files_by_id/:id', null] + }, 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] ]; describe('GET/POST all valid routes', () => { forEach(completeProtocol).it( '%s %s should be json', - (method: string, path: string, temp: Template) => { + (method: string, path: string, temp: Template, res_code: number) => { const request: ChaiHttp.Request = method === 'get' ? chai.request(app).get(base + path) : chai.request(app).post(base + path); @@ -190,20 +199,20 @@ describe('routes : protocol', () => { });; }); forEach(completeProtocol).it( - '%s %s should have a status 200', - (method: string, path: string, temp: Template) => { + '%s %s should have a status %d', + (method: string, path: string, temp: Template, res_code: number) => { const request: ChaiHttp.Request = method === 'get' ? chai.request(app).get(base + path) : chai.request(app).post(base + path); return request.then((res: ChaiHttp.Response) => { - expect(res).to.have.status(200); + expect(res).to.have.status(res_code); }).catch(function (err: any) { match(errorResponseTemplate)(err.response.body).should.be.true; });; }); forEach(completeProtocol).it( '%s %s should conform to the standard response protocol', - (method: string, path: string, temp: Template) => { + (method: string, path: string, temp: Template, res_code: number) => { const request: ChaiHttp.Request = method === 'get' ? chai.request(app).get(base + path) : chai.request(app).post(base + path); @@ -215,7 +224,7 @@ describe('routes : protocol', () => { }); forEach(completeProtocol).it( 'the response for %s %s should conform to its specific response protocol', - (method: string, path: string, temp: Template) => { + (method: string, path: string, temp: Template, res_code: number) => { if (method === 'get' && temp != null) { return chai.request(app).get(base + path) .then((res: ChaiHttp.Response) => { -- cgit v1.2.1 From 407a5a3c6c1d6d4c170d7646f921bb6f2fb2f380 Mon Sep 17 00:00:00 2001 From: jw255 Date: Sat, 3 Mar 2018 16:18:01 +0000 Subject: now compiles. also added stubs for database testing --- test/integration/protocol.routes.test.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'test/integration') diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts index 808fe50..e60cd81 100644 --- a/test/integration/protocol.routes.test.ts +++ b/test/integration/protocol.routes.test.ts @@ -13,6 +13,16 @@ import { default as types } from '../../src/matcher/types'; import * as App from '../../src/app'; let app = App.TestApp(); +// The general response template according to protocol +const responseTemplate: Template = { + status: types.string, + data: optional(types.anything), + error: optional(types.string), + error_description: optional(types.string), + user_message: optional(types.string), + error_data: optional(types.anything) +}; + // The error response template according to protocol const errorResponseTemplate: Template = { status: 'error', @@ -36,8 +46,8 @@ before(function(done){ authenticatedUser .post('/login') .send(userCredentials) - .end(function(err, response){ - expect(response.statusCode).to.equal(200); + .end(function(err, res){ + expect(res.status).to.equal(200); done(); }); }); @@ -199,7 +209,7 @@ describe('routes : protocol', () => { });; }); forEach(completeProtocol).it( - '%s %s should have a status %d', + '%s %s should have a status %4$d', (method: string, path: string, temp: Template, res_code: number) => { const request: ChaiHttp.Request = method === 'get' ? chai.request(app).get(base + path) -- cgit v1.2.1 From 46f6e6c7bf1d69d1b3257f6cc186326a57c2b51d Mon Sep 17 00:00:00 2001 From: jw255 Date: Sun, 4 Mar 2018 16:06:18 +0000 Subject: started authenticating --- test/integration/index.routes.test.ts | 94 ++++++++++++++++++++++++-------- test/integration/protocol.routes.test.ts | 4 +- 2 files changed, 73 insertions(+), 25 deletions(-) (limited to 'test/integration') diff --git a/test/integration/index.routes.test.ts b/test/integration/index.routes.test.ts index 7728db8..31af6cf 100644 --- a/test/integration/index.routes.test.ts +++ b/test/integration/index.routes.test.ts @@ -6,6 +6,9 @@ import * as chai from 'chai'; const expect: Chai.ExpectStatic = chai.expect; const should = chai.should(); +import request = require('supertest'); +import { default as User } from '../../src/db/model/User'; + // Chai-http must be imported this way: // tslint:disable-next-line:no-var-requires no-require-imports @@ -18,34 +21,79 @@ import 'mocha'; import * as App from '../../src/app'; let app = App.TestApp(); +const userCredentials = { + username: 'bobby', //'admin', + password: 'pass', //'eprprJacR0hBpmWvs5IDJZTnjRAY2gM3tSm0b1af', + grant_type: 'password' +} +let authenticatedUser = request.agent(app); +let token: JSON; + +// add user +before(async function(done){ + const newUser: User = new User({ + username: 'bobby', + password: 'pass', + userGroups: [] + }); + await newUser.save(); + done(); +}) + describe('routes : index', () => { describe('GET /', () => { - it('should return 200', () => { - return chai.request(app).get('/') - .then((res: ChaiHttp.Response) => { - res.status.should.eql(200); - }) - }); - - it('should return json', () => { - return chai.request(app).get('/') - .then((res: ChaiHttp.Response) => { - expect(res.type).to.eql('application/json'); + it('should return 401 without authentication', () => { + chai.request(app).get('/') + .catch(function (err: any) { + return expect(err).to.have.status(401); }); }); - - it('should return a message prop', () => { - return chai.request(app).get('/') - .then((res: ChaiHttp.Response) => { - expect(res.body.message).to.eql('Welcome to the CS3099 BE4 server!'); + + context('with authentication', () => { + before(function(done){ + authenticatedUser + .post('/cs3099group-be-4/login') // cs3099group-be-4/login + //.set('username', 'admin') + //.set('password', 'eprprJacR0hBpmWvs5IDJZTnjRAY2gM3tSm0b1af') + .send(userCredentials) + .end(function(err, res){ + console.log("***") + console.log(res.body); + console.log("***") + if (err) throw err; + token = JSON.parse("{ 'access_token': " + res.body.token + " }"); + done(); + }); }); - }); - - it('should return an important prop', () => { - return chai.request(app).get('/') - .then((res: ChaiHttp.Response) => { - expect(res.body.important).to.eql('Endpoints start from /cs3099group-be-4/'); + it('should return 200', () => { + return chai.request(app).get('/') + .set('Authorization', JSON.stringify(token)) + .then((res: ChaiHttp.Response) => { + res.status.should.eql(200); + }) }); - }); + // it('should return json', () => { + // return chai.request(app).get('/') + // .then((res: ChaiHttp.Response) => { + // expect(res.type).to.eql('application/json'); + // }); + // }); + + // it('should return a message prop', () => { + // return chai.request(app).get('/') + // .then((res: ChaiHttp.Response) => { + // expect(res.body.message).to.eql('Welcome to the CS3099 BE4 server!'); + // }); + // }); + + // it('should return an important prop', () => { + // return chai.request(app).get('/') + // .then((res: ChaiHttp.Response) => { + // expect(res.body.important).to.eql('Endpoints start from /cs3099group-be-4/'); + // }); + // }); + }) + }); + }); \ No newline at end of file diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts index e60cd81..6e71232 100644 --- a/test/integration/protocol.routes.test.ts +++ b/test/integration/protocol.routes.test.ts @@ -195,7 +195,7 @@ describe('routes : protocol', () => { ['get', '/projects/:project_name/files_by_id/:id', null, 200] ]; - describe('GET/POST all valid routes', () => { + describe('Access all valid routes', () => { forEach(completeProtocol).it( '%s %s should be json', (method: string, path: string, temp: Template, res_code: number) => { @@ -221,7 +221,7 @@ describe('routes : protocol', () => { });; }); forEach(completeProtocol).it( - '%s %s should conform to the standard response protocol', + '%s %s should conform to the general response protocol', (method: string, path: string, temp: Template, res_code: number) => { const request: ChaiHttp.Request = method === 'get' ? chai.request(app).get(base + path) -- cgit v1.2.1 From ecd7bc6f4eb2e330af36a336a3aa931319aa2c32 Mon Sep 17 00:00:00 2001 From: jw255 Date: Sun, 4 Mar 2018 17:36:41 +0000 Subject: fixed authentication and DB initialisation --- test/integration/auth.routes.test.ts | 4 +- test/integration/index.routes.test.ts | 104 +++++++++++++++++-------------- test/integration/protocol.routes.test.ts | 85 +++++++++++++++---------- 3 files changed, 111 insertions(+), 82 deletions(-) (limited to 'test/integration') diff --git a/test/integration/auth.routes.test.ts b/test/integration/auth.routes.test.ts index e0fb723..4eeb1b2 100644 --- a/test/integration/auth.routes.test.ts +++ b/test/integration/auth.routes.test.ts @@ -15,7 +15,7 @@ chai.use(require('chai-http')); import 'mocha'; import * as App from '../../src/app'; -let app = App.TestApp(); +const app = App.TestApp(); describe('authentication', () => { it('should be json', () => { @@ -39,4 +39,4 @@ describe('authentication', () => { }); }); -}); \ No newline at end of file +}); diff --git a/test/integration/index.routes.test.ts b/test/integration/index.routes.test.ts index 31af6cf..ff687c7 100644 --- a/test/integration/index.routes.test.ts +++ b/test/integration/index.routes.test.ts @@ -6,10 +6,11 @@ import * as chai from 'chai'; const expect: Chai.ExpectStatic = chai.expect; const should = chai.should(); +import * as ex from 'express'; +import { default as seq } from '../../src/db/orm'; import request = require('supertest'); import { default as User } from '../../src/db/model/User'; - // Chai-http must be imported this way: // tslint:disable-next-line:no-var-requires no-require-imports chai.use(require('chai-http')); @@ -19,18 +20,23 @@ chai.use(require('chai-http')); import 'mocha'; import * as App from '../../src/app'; -let app = App.TestApp(); +const app: ex.Express = App.TestApp(); -const userCredentials = { - username: 'bobby', //'admin', - password: 'pass', //'eprprJacR0hBpmWvs5IDJZTnjRAY2gM3tSm0b1af', +const userCredentials: Object = { + username: 'bobby', + password: 'pass', grant_type: 'password' -} -let authenticatedUser = request.agent(app); -let token: JSON; +}; + +const authenticatedUser = request.agent(app); +let token: string; + +async function addUser(done: any) { + await seq.authenticate(); + await seq.sync({ + force: true + }); -// add user -before(async function(done){ const newUser: User = new User({ username: 'bobby', password: 'pass', @@ -38,62 +44,66 @@ before(async function(done){ }); await newUser.save(); done(); -}) +} + +// Add user +before(function(done: any) { + addUser(done); +}); describe('routes : index', () => { describe('GET /', () => { it('should return 401 without authentication', () => { chai.request(app).get('/') - .catch(function (err: any) { + .catch(function(err: any) { return expect(err).to.have.status(401); }); }); context('with authentication', () => { - before(function(done){ + before(function(done) { authenticatedUser .post('/cs3099group-be-4/login') // cs3099group-be-4/login - //.set('username', 'admin') - //.set('password', 'eprprJacR0hBpmWvs5IDJZTnjRAY2gM3tSm0b1af') .send(userCredentials) - .end(function(err, res){ - console.log("***") - console.log(res.body); - console.log("***") - if (err) throw err; - token = JSON.parse("{ 'access_token': " + res.body.token + " }"); + .end(function(err, res) { + if (err) { throw err; } + token = res.body.access_token; done(); }); }); it('should return 200', () => { return chai.request(app).get('/') - .set('Authorization', JSON.stringify(token)) + .set('Authorization', 'Bearer ' + token) .then((res: ChaiHttp.Response) => { res.status.should.eql(200); - }) + }); }); - // it('should return json', () => { - // return chai.request(app).get('/') - // .then((res: ChaiHttp.Response) => { - // expect(res.type).to.eql('application/json'); - // }); - // }); - - // it('should return a message prop', () => { - // return chai.request(app).get('/') - // .then((res: ChaiHttp.Response) => { - // expect(res.body.message).to.eql('Welcome to the CS3099 BE4 server!'); - // }); - // }); - - // it('should return an important prop', () => { - // return chai.request(app).get('/') - // .then((res: ChaiHttp.Response) => { - // expect(res.body.important).to.eql('Endpoints start from /cs3099group-be-4/'); - // }); - // }); - }) - + it('should return json', () => { + return chai.request(app).get('/') + .set('Authorization', 'Bearer ' + token) + .then((res: ChaiHttp.Response) => { + expect(res.type).to.eql('application/json'); + }); + }); + + it('should return a message prop', () => { + return chai.request(app).get('/') + .set('Authorization', 'Bearer ' + token) + .then((res: ChaiHttp.Response) => { + expect(res.body.message).to.eql('Welcome to the CS3099 BE4 server!'); + }); + }); + + it('should return an important prop', () => { + return chai.request(app).get('/') + .set('Authorization', 'Bearer ' + token) + .then((res: ChaiHttp.Response) => { + expect(res.body.important) + .to.eql('Endpoints start from /cs3099group-be-4/'); + }); + }); + }); + }); - -}); \ No newline at end of file + +}); diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts index 6e71232..9b152fc 100644 --- a/test/integration/protocol.routes.test.ts +++ b/test/integration/protocol.routes.test.ts @@ -1,5 +1,5 @@ -import * as mocha from 'mocha'; import * as chai from 'chai'; +import * as mocha from 'mocha'; const expect: Chai.ExpectStatic = chai.expect; chai.use(require('chai-http')); @@ -7,13 +7,16 @@ import 'mocha'; import forEach = require('mocha-each'); import request = require('supertest'); +import * as App from '../../src/app'; +import * as ex from 'express'; import { Template, alternative, array, exact, match, optional } from '../../src/matcher/options'; +import { default as seq } from '../../src/db/orm'; import { default as types } from '../../src/matcher/types'; -import * as App from '../../src/app'; -let app = App.TestApp(); +import { default as User } from '../../src/db/model/User'; +const app = App.TestApp(); -// The general response template according to protocol +// The general response template according to protocol const responseTemplate: Template = { status: types.string, data: optional(types.anything), @@ -34,38 +37,51 @@ const errorResponseTemplate: Template = { type MochaForEachInput = [ string, string, Template, number ]; -const userCredentials = { - email: 'sponge@bob.com', - password: 'garyTheSnail' +const userCredentials: Object = { + username: 'bobby', + password: 'pass', + grant_type: 'password' +}; + +const authenticatedUser = request.agent(app); +let token: string; + +async function addUser(done: any) { + await seq.authenticate(); + await seq.sync({ + force: true + }); + + const newUser: User = new User({ + username: 'bobby', + password: 'pass', + userGroups: [] + }); + await newUser.save(); + done(); } - -//now let's login the user before we run any tests -let authenticatedUser = request.agent(app); - -before(function(done){ - authenticatedUser - .post('/login') - .send(userCredentials) - .end(function(err, res){ - expect(res.status).to.equal(200); - done(); - }); + +// Add user +before(function(done: any) { + addUser(done); }); describe('routes : errors', () => { - describe('GET invalid routes', function(){ + describe('GET invalid routes', function() { it('should have response code 404', () => { return chai.request(app).get('/invalid/route') - .catch(function (err: any) { + .set('Authorization', 'Bearer ' + token) + .catch(function(err: any) { expect(err.status).to.equal(404); }); }); - it('should conform to the error protocol', function(){ + it('should conform to the error protocol', function() { return chai.request(app).get('/invalid/route') - .catch(function (err: any) { + .set('Authorization', 'Bearer ' + token) + .catch(function(err: any) { match(errorResponseTemplate)(err.response.body).should.be.true; }); - }) + }); }); }); @@ -202,11 +218,12 @@ describe('routes : protocol', () => { 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) => { expect(res.type).to.equal('application/json'); - }).catch(function (err: any) { + }).catch(function(err: any) { match(errorResponseTemplate)(err.response.body).should.be.true; - });; + }); }); forEach(completeProtocol).it( '%s %s should have a status %4$d', @@ -214,11 +231,12 @@ describe('routes : protocol', () => { 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) => { expect(res).to.have.status(res_code); - }).catch(function (err: any) { + }).catch(function(err: any) { match(errorResponseTemplate)(err.response.body).should.be.true; - });; + }); }); forEach(completeProtocol).it( '%s %s should conform to the general response protocol', @@ -226,23 +244,24 @@ describe('routes : protocol', () => { 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; - }).catch(function (err: any) { + }).catch(function(err: any) { match(errorResponseTemplate)(err.response.body).should.be.true; - });; + }); }); forEach(completeProtocol).it( 'the response for %s %s should conform to its specific response protocol', (method: string, path: string, temp: Template, res_code: number) => { if (method === 'get' && temp != null) { - return chai.request(app).get(base + path) + return chai.request(app).get(base + path).set('Authorization', 'Bearer ' + token) .then((res: ChaiHttp.Response) => { match(temp)(JSON.parse(res.body)).should.be.true; - }).catch(function (err: any) { + }).catch(function(err: any) { match(errorResponseTemplate)(err.response.body).should.be.true; }); - }else{ + } else { return true; } }); -- cgit v1.2.1 From dfa461025cece1c17d70f0763848ab434a62ed8d Mon Sep 17 00:00:00 2001 From: jw255 Date: Mon, 5 Mar 2018 17:57:19 +0000 Subject: authentication tests in progress --- test/integration/auth.routes.test.ts | 88 ++++++++++++++++++++++++++++------- test/integration/index.routes.test.ts | 5 +- 2 files changed, 73 insertions(+), 20 deletions(-) (limited to 'test/integration') diff --git a/test/integration/auth.routes.test.ts b/test/integration/auth.routes.test.ts index 4eeb1b2..f8265a9 100644 --- a/test/integration/auth.routes.test.ts +++ b/test/integration/auth.routes.test.ts @@ -5,6 +5,9 @@ import * as chai from 'chai'; const expect: Chai.ExpectStatic = chai.expect; +import { default as seq } from '../../src/db/orm'; +import request = require('supertest'); +import { default as User } from '../../src/db/model/User'; // Chai-http must be imported this way: // tslint:disable-next-line:no-var-requires no-require-imports @@ -17,26 +20,77 @@ import 'mocha'; import * as App from '../../src/app'; const app = App.TestApp(); -describe('authentication', () => { - it('should be json', () => { - return chai.request(app).get('/') - .then((res: ChaiHttp.Response) => { - expect(res.type).to.eql('application/json'); - }); - }); +const bobbyCredentials = { + username: 'bobby', + password: 'tables', + grant_type: 'password' +}; - it('should have a message prop', () => { - return chai.request(app).get('/') - .then((res: ChaiHttp.Response) => { - expect(res.body.message).to.eql('Welcome to the CS3099 BE4 server!'); - }); +let bobbyToken: string; + +async function addUser(credentials: any, done: any) { + await seq.authenticate(); + await seq.sync({ + force: true }); - it('should have an important prop', () => { - return chai.request(app).get('/') - .then((res: ChaiHttp.Response) => { - expect(res.body.important).to.eql('Endpoints start from /cs3099group-be-4/'); - }); + const u: string = credentials.username + const p: string = credentials.password + console.log(u + " " + p) + + const newUser: User = new User({ + username: u, + password: p, + userGroups: [] }); + await newUser.save(); + done(); +} +// Add user +before(function(done: any) { + addUser(bobbyCredentials, done); +}); + +describe('authentication', () => { + context('invalid authentication', () => { + it('should reject invalid usernames', () => { + request.agent(app) + .post('/cs3099group-be-4/login') + .send({...bobbyCredentials, username: 'invalid'}) + .end(function(err, res) { + expect(err).to.not.be.undefined; + expect(res).to.have.status(401); + }); + }); + it('should reject invalid passwords', () => { + request.agent(app) + .post('/cs3099group-be-4/login') + .send({...bobbyCredentials, password: 'invalid'}) + .end(function(err, res) { + expect(err).to.not.be.undefined; + expect(res).to.have.status(401); + }); + }); + it('should reject invalid grant_type', () => { + request.agent(app) + .post('/cs3099group-be-4/login') + .send({...bobbyCredentials, grant_type: 'invalid'}) + .end(function(err, res) { + expect(err).to.not.be.undefined; + expect(res).to.have.status(400); + }); + }); + }) + context('valid authentication', () => { + it('should accept correct credentials', () => { + request.agent(app) + .post('/cs3099group-be-4/login') + .send(bobbyCredentials) + .end(function(err, res) { + expect(err).to.be.undefined; + expect(res).to.have.status(200); + }); + }); + }) }); diff --git a/test/integration/index.routes.test.ts b/test/integration/index.routes.test.ts index ff687c7..9ddd1de 100644 --- a/test/integration/index.routes.test.ts +++ b/test/integration/index.routes.test.ts @@ -28,7 +28,6 @@ const userCredentials: Object = { grant_type: 'password' }; -const authenticatedUser = request.agent(app); let token: string; async function addUser(done: any) { @@ -62,8 +61,8 @@ describe('routes : index', () => { context('with authentication', () => { before(function(done) { - authenticatedUser - .post('/cs3099group-be-4/login') // cs3099group-be-4/login + request.agent(app) + .post('/cs3099group-be-4/login') .send(userCredentials) .end(function(err, res) { if (err) { throw err; } -- cgit v1.2.1