aboutsummaryrefslogtreecommitdiff
path: root/test/integration
diff options
context:
space:
mode:
Diffstat (limited to 'test/integration')
-rw-r--r--test/integration/auth.routes.test.ts4
-rw-r--r--test/integration/index.routes.test.ts104
-rw-r--r--test/integration/protocol.routes.test.ts85
3 files changed, 111 insertions, 82 deletions
diff --git a/test/integration/auth.routes.test.ts b/test/integration/auth.routes.test.ts
index e0fb723..4eeb1b2 100644
--- a/test/integration/auth.routes.test.ts
+++ b/test/integration/auth.routes.test.ts
@@ -15,7 +15,7 @@ chai.use(require('chai-http'));
import 'mocha';
import * as App from '../../src/app';
-let app = App.TestApp();
+const app = App.TestApp();
describe('authentication', () => {
it('should be json', () => {
@@ -39,4 +39,4 @@ describe('authentication', () => {
});
});
-}); \ No newline at end of file
+});
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
+
+});
diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts
index 6e71232..9b152fc 100644
--- a/test/integration/protocol.routes.test.ts
+++ b/test/integration/protocol.routes.test.ts
@@ -1,5 +1,5 @@
-import * as mocha from 'mocha';
import * as chai from 'chai';
+import * as mocha from 'mocha';
const expect: Chai.ExpectStatic = chai.expect;
chai.use(require('chai-http'));
@@ -7,13 +7,16 @@ import 'mocha';
import forEach = require('mocha-each');
import request = require('supertest');
+import * as App from '../../src/app';
+import * as ex from 'express';
import { Template, alternative, array, exact, match, optional
} from '../../src/matcher/options';
+import { default as seq } from '../../src/db/orm';
import { default as types } from '../../src/matcher/types';
-import * as App from '../../src/app';
-let app = App.TestApp();
+import { default as User } from '../../src/db/model/User';
+const app = App.TestApp();
-// The general response template according to protocol
+// The general response template according to protocol
const responseTemplate: Template = {
status: types.string,
data: optional(types.anything),
@@ -34,38 +37,51 @@ const errorResponseTemplate: Template = {
type MochaForEachInput = [ string, string, Template, number ];
-const userCredentials = {
- email: 'sponge@bob.com',
- password: 'garyTheSnail'
+const userCredentials: Object = {
+ username: 'bobby',
+ password: 'pass',
+ grant_type: 'password'
+};
+
+const authenticatedUser = request.agent(app);
+let token: string;
+
+async function addUser(done: any) {
+ await seq.authenticate();
+ await seq.sync({
+ force: true
+ });
+
+ const newUser: User = new User({
+ username: 'bobby',
+ password: 'pass',
+ userGroups: []
+ });
+ await newUser.save();
+ done();
}
-
-//now let's login the user before we run any tests
-let authenticatedUser = request.agent(app);
-
-before(function(done){
- authenticatedUser
- .post('/login')
- .send(userCredentials)
- .end(function(err, res){
- expect(res.status).to.equal(200);
- done();
- });
+
+// Add user
+before(function(done: any) {
+ addUser(done);
});
describe('routes : errors', () => {
- describe('GET invalid routes', function(){
+ describe('GET invalid routes', function() {
it('should have response code 404', () => {
return chai.request(app).get('/invalid/route')
- .catch(function (err: any) {
+ .set('Authorization', 'Bearer ' + token)
+ .catch(function(err: any) {
expect(err.status).to.equal(404);
});
});
- it('should conform to the error protocol', function(){
+ it('should conform to the error protocol', function() {
return chai.request(app).get('/invalid/route')
- .catch(function (err: any) {
+ .set('Authorization', 'Bearer ' + token)
+ .catch(function(err: any) {
match(errorResponseTemplate)(err.response.body).should.be.true;
});
- })
+ });
});
});
@@ -202,11 +218,12 @@ 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);
return request.then((res: ChaiHttp.Response) => {
expect(res.type).to.equal('application/json');
- }).catch(function (err: any) {
+ }).catch(function(err: any) {
match(errorResponseTemplate)(err.response.body).should.be.true;
- });;
+ });
});
forEach(completeProtocol).it(
'%s %s should have a status %4$d',
@@ -214,11 +231,12 @@ 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);
return request.then((res: ChaiHttp.Response) => {
expect(res).to.have.status(res_code);
- }).catch(function (err: any) {
+ }).catch(function(err: any) {
match(errorResponseTemplate)(err.response.body).should.be.true;
- });;
+ });
});
forEach(completeProtocol).it(
'%s %s should conform to the general response protocol',
@@ -226,23 +244,24 @@ 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);
return request.then((res: ChaiHttp.Response) => {
match(responseTemplate, JSON.parse(res.body)).shoul.be.true;
- }).catch(function (err: any) {
+ }).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, res_code: number) => {
if (method === 'get' && temp != null) {
- return chai.request(app).get(base + path)
+ return chai.request(app).get(base + path).set('Authorization', 'Bearer ' + token)
.then((res: ChaiHttp.Response) => {
match(temp)(JSON.parse(res.body)).should.be.true;
- }).catch(function (err: any) {
+ }).catch(function(err: any) {
match(errorResponseTemplate)(err.response.body).should.be.true;
});
- }else{
+ } else {
return true;
}
});