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