diff options
| -rw-r--r-- | package.json | 1 | ||||
| -rw-r--r-- | src/auth.ts | 2 | ||||
| -rw-r--r-- | src/errors/errorware.ts | 2 | ||||
| -rw-r--r-- | test/integration/auth.routes.test.ts | 102 | ||||
| -rw-r--r-- | test/integration/index.routes.test.ts | 4 |
5 files changed, 86 insertions, 25 deletions
diff --git a/package.json b/package.json index e0aa817..95526c0 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "test:unit": "mocha --reporter spec -r ts-node/register './test/**/*.unit.test.ts'", "test:db": "mocha --reporter spec -r ts-node/register './test/**/*database.unit.test.ts'", "test:routes:auth": "mocha --reporter spec -r ts-node/register './test/**/*auth.routes.test.ts'", + "test:routes:protocol": "mocha --reporter spec -r ts-node/register './test/**/*protocol.routes.test.ts'", "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'", diff --git a/src/auth.ts b/src/auth.ts index 5a1707c..6339260 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -112,9 +112,7 @@ export class AuthRouter { // Handle error if user is unauthorised export const unauthorisedErr: Errorware = (err: Error, req: Request, res: Response, next: NextFunction): void => { - console.log('fffff'); if (err.name === 'UnauthorizedError') { - console.log('efewfwf'); return next(new RequestError( 401, 'not_authorised', 'user does not have correct authorisation for task' diff --git a/src/errors/errorware.ts b/src/errors/errorware.ts index e2355de..b01f83b 100644 --- a/src/errors/errorware.ts +++ b/src/errors/errorware.ts @@ -53,9 +53,7 @@ export type Errorware = export const errorHandler: Errorware = (err: Error, req: Request, res: Response, next: NextFunction): void => { - console.log(err); if (RequestError.is(err)) { - console.log('fewkuihfklsfv'); res.status(err.code) .json(err.responseBlock()); } else { diff --git a/test/integration/auth.routes.test.ts b/test/integration/auth.routes.test.ts index f8265a9..d9fba0c 100644 --- a/test/integration/auth.routes.test.ts +++ b/test/integration/auth.routes.test.ts @@ -28,15 +28,12 @@ const bobbyCredentials = { let bobbyToken: string; -async function addUser(credentials: any, done: any) { - await seq.authenticate(); +async function resetDatabase(done: any){ await seq.sync({ force: true }); - - const u: string = credentials.username - const p: string = credentials.password - console.log(u + " " + p) + const u: string = bobbyCredentials.username + const p: string = bobbyCredentials.password const newUser: User = new User({ username: u, @@ -47,18 +44,26 @@ async function addUser(credentials: any, done: any) { done(); } +async function setupDatabase(done: any) { + await seq.authenticate(); + await seq.sync({ + force: true + }); + done(); +} + // Add user -before(function(done: any) { - addUser(bobbyCredentials, done); +before((done: any) => { + setupDatabase(done); }); describe('authentication', () => { - context('invalid authentication', () => { + context('invalid password authentication', () => { it('should reject invalid usernames', () => { request.agent(app) .post('/cs3099group-be-4/login') .send({...bobbyCredentials, username: 'invalid'}) - .end(function(err, res) { + .end((err, res) => { expect(err).to.not.be.undefined; expect(res).to.have.status(401); }); @@ -66,8 +71,8 @@ describe('authentication', () => { it('should reject invalid passwords', () => { request.agent(app) .post('/cs3099group-be-4/login') - .send({...bobbyCredentials, password: 'invalid'}) - .end(function(err, res) { + .send({...bobbyCredentials, password: 'asasasas'}) + .end((err, res) => { expect(err).to.not.be.undefined; expect(res).to.have.status(401); }); @@ -76,21 +81,80 @@ describe('authentication', () => { request.agent(app) .post('/cs3099group-be-4/login') .send({...bobbyCredentials, grant_type: 'invalid'}) - .end(function(err, res) { + .catch((reason) => { + expect(reason).to.eql('unsupported_grant_type: invalid grant type'); + }); + }); + it('should reject empty usernames', () => { + request.agent(app) + .post('/cs3099group-be-4/login') + .send({...bobbyCredentials, username: ''}) + .end((err, res) => { expect(err).to.not.be.undefined; - expect(res).to.have.status(400); + expect(res).to.have.status(401); }); }); - }) - context('valid authentication', () => { + it('should reject empty passwords', () => { + request.agent(app) + .post('/cs3099group-be-4/login') + .send({...bobbyCredentials, password: ''}) + .end((err, res) => { + expect(err).to.not.be.undefined; + expect(res).to.have.status(401); + }); + }); + it('should reject empty grant_type', () => { + request.agent(app) + .post('/cs3099group-be-4/login') + .send({...bobbyCredentials, grant_type: ''}) + .catch((reason) => { + expect(reason).to.eql('unsupported_grant_type: invalid grant type'); + }); + }); + }); + context('valid password authentication', () => { + before((done: any) => { + resetDatabase(done); + }); it('should accept correct credentials', () => { request.agent(app) .post('/cs3099group-be-4/login') .send(bobbyCredentials) - .end(function(err, res) { - expect(err).to.be.undefined; + .end((err, res) => { + expect(err).to.be.null; + expect(res).to.have.status(200); + }); + }); + }); + context('token authentication', () => { + let bobbyToken: string; + before((done: any) => { + resetDatabase(() => { + request.agent(app) + .post('/cs3099group-be-4/login') + .send(bobbyCredentials) + .end((err, res) => { + if (err) { throw err; } + bobbyToken = res.body.access_token; + done(); + }); + }); + }); + it('should reject invalid tokens', () => { + return chai.request(app).get('/') + .set('Authorization', 'Bearer ' + 'invalid') + .end((err, res) => { + expect(err).to.not.be.undefined; + expect(res).to.have.status(401); + }); + }); + it('should accept valid tokens', () => { + return chai.request(app).get('/') + .set('Authorization', 'Bearer ' + bobbyToken) + .end((err, res) => { + expect(err).to.be.null; expect(res).to.have.status(200); }); }); - }) + }); }); diff --git a/test/integration/index.routes.test.ts b/test/integration/index.routes.test.ts index 9ddd1de..3a5d5ea 100644 --- a/test/integration/index.routes.test.ts +++ b/test/integration/index.routes.test.ts @@ -30,7 +30,7 @@ const userCredentials: Object = { let token: string; -async function addUser(done: any) { +async function startDatabase(done: any) { await seq.authenticate(); await seq.sync({ force: true @@ -47,7 +47,7 @@ async function addUser(done: any) { // Add user before(function(done: any) { - addUser(done); + startDatabase(done); }); describe('routes : index', () => { |
