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 /test/integration/index.routes.test.ts | |
| parent | Merge branch 'trh/payload-checker' into testing-middleware (diff) | |
| download | medicine-ecd7bc6f4eb2e330af36a336a3aa931319aa2c32.tar.gz medicine-ecd7bc6f4eb2e330af36a336a3aa931319aa2c32.zip | |
fixed authentication and DB initialisation
Diffstat (limited to 'test/integration/index.routes.test.ts')
| -rw-r--r-- | test/integration/index.routes.test.ts | 104 |
1 files changed, 57 insertions, 47 deletions
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 + +}); |
