diff options
| author | jw255 | 2018-03-04 17:36:41 +0000 |
|---|---|---|
| committer | jw255 | 2018-03-04 17:36:41 +0000 |
| commit | ecd7bc6f4eb2e330af36a336a3aa931319aa2c32 (patch) | |
| tree | b18fc1928c8a326a6f939dd6ad4bec56df559539 | |
| parent | Merge branch 'trh/payload-checker' into testing-middleware (diff) | |
| download | medicine-ecd7bc6f4eb2e330af36a336a3aa931319aa2c32.tar.gz medicine-ecd7bc6f4eb2e330af36a336a3aa931319aa2c32.zip | |
fixed authentication and DB initialisation
| -rw-r--r-- | package.json | 1 | ||||
| -rw-r--r-- | test/integration/auth.routes.test.ts | 4 | ||||
| -rw-r--r-- | test/integration/index.routes.test.ts | 104 | ||||
| -rw-r--r-- | test/integration/protocol.routes.test.ts | 85 | ||||
| -rw-r--r-- | test/unit/database.unit.test.ts | 110 |
5 files changed, 167 insertions, 137 deletions
diff --git a/package.json b/package.json index 6ffbd00..1047a03 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "docs": "typedoc --out 'docs' --readme 'README.md' --name 'St Andrews Junior Honours 2017/18 - Team BE4' --module commonjs --target ES5 --exclude '*.test.ts' --includeDeclarations --excludeExternals 'src'", "start": "ts-node src/index.ts", "lint": "tslint --fix --force -p './tsconfig.json'", + "lint:tests": "tslint --fix --force ./test/**/*", "_gl-lint": "tslint -p './tsconfig.json'" }, "repository": { 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; } }); diff --git a/test/unit/database.unit.test.ts b/test/unit/database.unit.test.ts index a746cd0..cad28e0 100644 --- a/test/unit/database.unit.test.ts +++ b/test/unit/database.unit.test.ts @@ -1,63 +1,63 @@ import * as chai from 'chai'; chai.should(); -const sample_project:any = null; -const sample_user:any = null; -const sample_file:any = null; +const sample_project: any = null; +const sample_user: any = null; +const sample_file: any = null; // tslint:disable-next-line:no-require-imports describe('database:', () => { describe('SQL Model', () => { - // before each test initialise the database - let database; - beforeEach(function() { - // import database - }); + // before each test initialise the database + let database; + beforeEach(function() { + // import database + }); - context('initialisation', () => { - it('should complete successfully', () => { - // check no errors were thrown on import / answers to ping - }); - it('should recreate schema', () => { - // check all tables and functions are present according to some schema - }); - it('should be empty', () => { - // check projects, users, etc tables are empty - }); - it('should have initial admin user', () => { - // check admin user exists - }); - }); - context('usage', () => { - context('users', () => { - it('should be possible to add users', () => { - // add sample user and check no error was thrown - }); - it('should be possible to retreive users', () => { - // add sample user and retreive user, then compare username, uuid, etc - }); - }); - context('projects', () => { - it('should be possible to add projects', () => { - // add sample project and check no error was thrown - }); - it('should be possible to retreive projects', () => { - // add sample project and retreive project, then compare properties - }); - }); - context('files', () => { - it('should be possible to add files', () => { - // add sample file and check no error was thrown - }); - it('should be possible to retreive files', () => { - // add sample file and retreive file, then compare properties - }); - }); - // to be expanded - }); - // to be expaned - }); - describe('ORM Model', () => { - // to be expanded - }); -});
\ No newline at end of file + context('initialisation', () => { + it('should complete successfully', () => { + // check no errors were thrown on import / answers to ping + }); + it('should recreate schema', () => { + // check all tables and functions are present according to some schema + }); + it('should be empty', () => { + // check projects, users, etc tables are empty + }); + it('should have initial admin user', () => { + // check admin user exists + }); + }); + context('usage', () => { + context('users', () => { + it('should be possible to add users', () => { + // add sample user and check no error was thrown + }); + it('should be possible to retreive users', () => { + // add sample user and retreive user, then compare username, uuid, etc + }); + }); + context('projects', () => { + it('should be possible to add projects', () => { + // add sample project and check no error was thrown + }); + it('should be possible to retreive projects', () => { + // add sample project and retreive project, then compare properties + }); + }); + context('files', () => { + it('should be possible to add files', () => { + // add sample file and check no error was thrown + }); + it('should be possible to retreive files', () => { + // add sample file and retreive file, then compare properties + }); + }); + // to be expanded + }); + // to be expaned + }); + describe('ORM Model', () => { + // to be expanded + }); +}); |
