aboutsummaryrefslogtreecommitdiff
path: root/test/integration
diff options
context:
space:
mode:
authorjw2552018-03-05 17:57:19 +0000
committerjw2552018-03-05 17:57:19 +0000
commitdfa461025cece1c17d70f0763848ab434a62ed8d (patch)
tree15d78b6dbd21f23a9197301bf9ec6274111d6231 /test/integration
parentfixed authentication and DB initialisation (diff)
downloadmedicine-dfa461025cece1c17d70f0763848ab434a62ed8d.tar.gz
medicine-dfa461025cece1c17d70f0763848ab434a62ed8d.zip

authentication tests in progress

Diffstat (limited to 'test/integration')
-rw-r--r--test/integration/auth.routes.test.ts88
-rw-r--r--test/integration/index.routes.test.ts5
2 files changed, 73 insertions, 20 deletions
diff --git a/test/integration/auth.routes.test.ts b/test/integration/auth.routes.test.ts
index 4eeb1b2..f8265a9 100644
--- a/test/integration/auth.routes.test.ts
+++ b/test/integration/auth.routes.test.ts
@@ -5,6 +5,9 @@
import * as chai from 'chai';
const expect: Chai.ExpectStatic = chai.expect;
+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
@@ -17,26 +20,77 @@ import 'mocha';
import * as App from '../../src/app';
const app = App.TestApp();
-describe('authentication', () => {
- it('should be json', () => {
- return chai.request(app).get('/')
- .then((res: ChaiHttp.Response) => {
- expect(res.type).to.eql('application/json');
- });
- });
+const bobbyCredentials = {
+ username: 'bobby',
+ password: 'tables',
+ grant_type: 'password'
+};
- 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!');
- });
+let bobbyToken: string;
+
+async function addUser(credentials: any, done: any) {
+ await seq.authenticate();
+ await seq.sync({
+ force: true
});
- 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/');
- });
+ const u: string = credentials.username
+ const p: string = credentials.password
+ console.log(u + " " + p)
+
+ const newUser: User = new User({
+ username: u,
+ password: p,
+ userGroups: []
});
+ await newUser.save();
+ done();
+}
+// Add user
+before(function(done: any) {
+ addUser(bobbyCredentials, done);
+});
+
+describe('authentication', () => {
+ context('invalid authentication', () => {
+ it('should reject invalid usernames', () => {
+ request.agent(app)
+ .post('/cs3099group-be-4/login')
+ .send({...bobbyCredentials, username: 'invalid'})
+ .end(function(err, res) {
+ expect(err).to.not.be.undefined;
+ expect(res).to.have.status(401);
+ });
+ });
+ it('should reject invalid passwords', () => {
+ request.agent(app)
+ .post('/cs3099group-be-4/login')
+ .send({...bobbyCredentials, password: 'invalid'})
+ .end(function(err, res) {
+ expect(err).to.not.be.undefined;
+ expect(res).to.have.status(401);
+ });
+ });
+ it('should reject invalid grant_type', () => {
+ request.agent(app)
+ .post('/cs3099group-be-4/login')
+ .send({...bobbyCredentials, grant_type: 'invalid'})
+ .end(function(err, res) {
+ expect(err).to.not.be.undefined;
+ expect(res).to.have.status(400);
+ });
+ });
+ })
+ context('valid authentication', () => {
+ it('should accept correct credentials', () => {
+ request.agent(app)
+ .post('/cs3099group-be-4/login')
+ .send(bobbyCredentials)
+ .end(function(err, res) {
+ expect(err).to.be.undefined;
+ expect(res).to.have.status(200);
+ });
+ });
+ })
});
diff --git a/test/integration/index.routes.test.ts b/test/integration/index.routes.test.ts
index ff687c7..9ddd1de 100644
--- a/test/integration/index.routes.test.ts
+++ b/test/integration/index.routes.test.ts
@@ -28,7 +28,6 @@ const userCredentials: Object = {
grant_type: 'password'
};
-const authenticatedUser = request.agent(app);
let token: string;
async function addUser(done: any) {
@@ -62,8 +61,8 @@ describe('routes : index', () => {
context('with authentication', () => {
before(function(done) {
- authenticatedUser
- .post('/cs3099group-be-4/login') // cs3099group-be-4/login
+ request.agent(app)
+ .post('/cs3099group-be-4/login')
.send(userCredentials)
.end(function(err, res) {
if (err) { throw err; }