aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--package.json4
-rw-r--r--src/FileRouter.ts4
-rw-r--r--src/app.ts12
-rw-r--r--test/integration/auth.routes.test.ts2
-rw-r--r--test/integration/index.routes.test.ts43
-rw-r--r--test/integration/protocol.routes.test.ts73
-rw-r--r--test/mocha.opts4
8 files changed, 98 insertions, 46 deletions
diff --git a/.gitignore b/.gitignore
index daf15f6..fdd6ed3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -6,7 +6,7 @@ dist/
# LaTeX output files
*.aux
-*.log
+*.log*
*.pdf
# PostreSQL log files
diff --git a/package.json b/package.json
index ccdf7ad..26089b5 100644
--- a/package.json
+++ b/package.json
@@ -5,8 +5,8 @@
"license": "UNLICENSED",
"scripts": {
"test": "mocha --reporter spec -r ts-node/register './test/**/*.test.ts'",
- "test_routes": "mocha --reporter spec -r ts-node/register './test/**/*.routes.test.ts'",
- "test_unit": "mocha --reporter spec -r ts-node/register './test/**/*.unit.test.ts'",
+ "test:routes": "mocha --reporter spec -r ts-node/register './test/**/*.routes.test.ts'",
+ "test:unit": "mocha --reporter spec -r ts-node/register './test/**/*.unit.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/FileRouter.ts b/src/FileRouter.ts
index 34738bc..93a1e1a 100644
--- a/src/FileRouter.ts
+++ b/src/FileRouter.ts
@@ -524,8 +524,8 @@ export class FileRouter {
* Initialize the FileRouter
*/
public constructor() {
- this.router = Router();
- this.init();
+ this.router = Router();
+ this.init();
}
/**
diff --git a/src/app.ts b/src/app.ts
index ef4485e..612ba49 100644
--- a/src/app.ts
+++ b/src/app.ts
@@ -33,8 +33,18 @@ class App {
important : 'Endpoints start from /cs3099group-be-4/'
});
});
- this.express.use('/', defRouter);
+ defRouter.get('/*', (req: ex.Request, res: ex.Response): void => {
+ res.status(404)
+ .json({
+ status: 'error',
+ error : 'invalid_route',
+ error_description: 'Endpoints start from /cs3099group-be-4/\n'
+ +' Please refer to \'https://github.com/CS3099JH2017/cs3099jh/blob/master/protocols/BE01.md\''
+ + ' for further details.'
+ });
+ })
this.express.use('/cs3099group-be-4', FileRouter);
+ this.express.use('/', defRouter);
}
}
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;
}
});
});
diff --git a/test/mocha.opts b/test/mocha.opts
deleted file mode 100644
index 1782c28..0000000
--- a/test/mocha.opts
+++ /dev/null
@@ -1,4 +0,0 @@
---compilers ts-node/register
---require source-map-support/register
---full-trace
-src/**/*.test.ts \ No newline at end of file