diff options
| author | jw255 | 2018-03-07 00:47:33 +0000 |
|---|---|---|
| committer | jw255 | 2018-03-07 00:47:33 +0000 |
| commit | 5ff3755aac9555843c32ea3cddab94c95a12e456 (patch) | |
| tree | ca02ba6835d0c59fee864e8f7613a310a6843fe4 /test | |
| parent | authentication tests (diff) | |
| download | medicine-5ff3755aac9555843c32ea3cddab94c95a12e456.tar.gz medicine-5ff3755aac9555843c32ea3cddab94c95a12e456.zip | |
fixed mocha/chai
Diffstat (limited to 'test')
| -rw-r--r-- | test/integration/auth.routes.test.ts | 62 | ||||
| -rw-r--r-- | test/integration/index.routes.test.ts | 3 | ||||
| -rw-r--r-- | test/integration/protocol.routes.test.ts | 53 |
3 files changed, 72 insertions, 46 deletions
diff --git a/test/integration/auth.routes.test.ts b/test/integration/auth.routes.test.ts index d9fba0c..888883f 100644 --- a/test/integration/auth.routes.test.ts +++ b/test/integration/auth.routes.test.ts @@ -6,7 +6,6 @@ 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: @@ -20,6 +19,20 @@ import 'mocha'; import * as App from '../../src/app'; const app = App.TestApp(); +const badRequestErr = + { status: 'error', + error: 'invalid_request', + error_description: 'Missing parameters' }; +const unsupportedGrantTypeErr = + { status: 'error', + error: 'unsupported_grant_type', + error_description: 'invalid grant type' } +const authorisationErr = + { status: 'error', + error: 'not_authorised', + error_description: 'user does not have correct authorisation for task' } + + const bobbyCredentials = { username: 'bobby', password: 'tables', @@ -60,55 +73,51 @@ before((done: any) => { describe('authentication', () => { context('invalid password authentication', () => { it('should reject invalid usernames', () => { - request.agent(app) + chai.request(app) .post('/cs3099group-be-4/login') .send({...bobbyCredentials, username: 'invalid'}) - .end((err, res) => { - expect(err).to.not.be.undefined; + .then((res) => { expect(res).to.have.status(401); }); }); it('should reject invalid passwords', () => { - request.agent(app) + chai.request(app) .post('/cs3099group-be-4/login') .send({...bobbyCredentials, password: 'asasasas'}) - .end((err, res) => { - expect(err).to.not.be.undefined; + .then((res) => { expect(res).to.have.status(401); }); }); it('should reject invalid grant_type', () => { - request.agent(app) + chai.request(app) .post('/cs3099group-be-4/login') .send({...bobbyCredentials, grant_type: 'invalid'}) .catch((reason) => { - expect(reason).to.eql('unsupported_grant_type: invalid grant type'); + expect(reason.response.body).to.eql(unsupportedGrantTypeErr); }); }); it('should reject empty usernames', () => { - request.agent(app) + chai.request(app) .post('/cs3099group-be-4/login') .send({...bobbyCredentials, username: ''}) - .end((err, res) => { - expect(err).to.not.be.undefined; - expect(res).to.have.status(401); + .catch((reason) => { + expect(reason.response.body).to.eql(badRequestErr); }); }); it('should reject empty passwords', () => { - request.agent(app) + chai.request(app) .post('/cs3099group-be-4/login') .send({...bobbyCredentials, password: ''}) - .end((err, res) => { - expect(err).to.not.be.undefined; - expect(res).to.have.status(401); + .catch((reason) => { + expect(reason.response.body).to.eql(badRequestErr); }); }); it('should reject empty grant_type', () => { - request.agent(app) + chai.request(app) .post('/cs3099group-be-4/login') .send({...bobbyCredentials, grant_type: ''}) .catch((reason) => { - expect(reason).to.eql('unsupported_grant_type: invalid grant type'); + expect(reason.response.body).to.eql(badRequestErr); }); }); }); @@ -117,11 +126,10 @@ describe('authentication', () => { resetDatabase(done); }); it('should accept correct credentials', () => { - request.agent(app) + chai.request(app) .post('/cs3099group-be-4/login') .send(bobbyCredentials) - .end((err, res) => { - expect(err).to.be.null; + .then((res) => { expect(res).to.have.status(200); }); }); @@ -130,7 +138,7 @@ describe('authentication', () => { let bobbyToken: string; before((done: any) => { resetDatabase(() => { - request.agent(app) + chai.request(app) .post('/cs3099group-be-4/login') .send(bobbyCredentials) .end((err, res) => { @@ -143,16 +151,16 @@ describe('authentication', () => { it('should reject invalid tokens', () => { return chai.request(app).get('/') .set('Authorization', 'Bearer ' + 'invalid') - .end((err, res) => { - expect(err).to.not.be.undefined; + .then((res) => { expect(res).to.have.status(401); + }).catch((reason) => { + expect(reason.response.body).to.eql(authorisationErr); }); }); it('should accept valid tokens', () => { return chai.request(app).get('/') .set('Authorization', 'Bearer ' + bobbyToken) - .end((err, res) => { - expect(err).to.be.null; + .then((res) => { expect(res).to.have.status(200); }); }); diff --git a/test/integration/index.routes.test.ts b/test/integration/index.routes.test.ts index 3a5d5ea..70671d8 100644 --- a/test/integration/index.routes.test.ts +++ b/test/integration/index.routes.test.ts @@ -8,7 +8,6 @@ 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: @@ -61,7 +60,7 @@ describe('routes : index', () => { context('with authentication', () => { before(function(done) { - request.agent(app) + chai.request(app) .post('/cs3099group-be-4/login') .send(userCredentials) .end(function(err, res) { diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts index 9b152fc..863bec9 100644 --- a/test/integration/protocol.routes.test.ts +++ b/test/integration/protocol.routes.test.ts @@ -5,7 +5,6 @@ const expect: Chai.ExpectStatic = chai.expect; chai.use(require('chai-http')); import 'mocha'; import forEach = require('mocha-each'); -import request = require('supertest'); import * as App from '../../src/app'; import * as ex from 'express'; @@ -37,47 +36,67 @@ const errorResponseTemplate: Template = { type MochaForEachInput = [ string, string, Template, number ]; -const userCredentials: Object = { +const bobbyCredentials = { username: 'bobby', - password: 'pass', + password: 'tables', grant_type: 'password' }; -const authenticatedUser = request.agent(app); -let token: string; +let bobbyToken: string; -async function addUser(done: any) { - await seq.authenticate(); +async function resetDatabase(done: any){ await seq.sync({ force: true }); + const u: string = bobbyCredentials.username + const p: string = bobbyCredentials.password const newUser: User = new User({ - username: 'bobby', - password: 'pass', + username: u, + password: p, userGroups: [] }); await newUser.save(); done(); } +async function setupDatabase(done: any) { + await seq.authenticate(); + await seq.sync({ + force: true + }); + done(); +} + // Add user -before(function(done: any) { - addUser(done); +before((done: any) => { + setupDatabase(() => { + resetDatabase(() => { + chai.request(app) + .post('/cs3099group-be-4/login') + .send(bobbyCredentials) + .end((err, res) => { + if (err) { throw err; } + bobbyToken = res.body.access_token; + done(); + }); + }); + }); + }); describe('routes : errors', () => { describe('GET invalid routes', function() { it('should have response code 404', () => { return chai.request(app).get('/invalid/route') - .set('Authorization', 'Bearer ' + token) + .set('Authorization', 'Bearer ' + bobbyToken) .catch(function(err: any) { expect(err.status).to.equal(404); }); }); it('should conform to the error protocol', function() { return chai.request(app).get('/invalid/route') - .set('Authorization', 'Bearer ' + token) + .set('Authorization', 'Bearer ' + bobbyToken) .catch(function(err: any) { match(errorResponseTemplate)(err.response.body).should.be.true; }); @@ -218,7 +237,7 @@ 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); + request.set('Authorization', 'Bearer ' + bobbyToken); return request.then((res: ChaiHttp.Response) => { expect(res.type).to.equal('application/json'); }).catch(function(err: any) { @@ -231,7 +250,7 @@ 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); + request.set('Authorization', 'Bearer ' + bobbyToken); return request.then((res: ChaiHttp.Response) => { expect(res).to.have.status(res_code); }).catch(function(err: any) { @@ -244,7 +263,7 @@ 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); + request.set('Authorization', 'Bearer ' + bobbyToken); return request.then((res: ChaiHttp.Response) => { match(responseTemplate, JSON.parse(res.body)).shoul.be.true; }).catch(function(err: any) { @@ -255,7 +274,7 @@ describe('routes : protocol', () => { '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).set('Authorization', 'Bearer ' + token) + return chai.request(app).get(base + path).set('Authorization', 'Bearer ' + bobbyToken) .then((res: ChaiHttp.Response) => { match(temp)(JSON.parse(res.body)).should.be.true; }).catch(function(err: any) { |
