aboutsummaryrefslogtreecommitdiff
path: root/test/integration
diff options
context:
space:
mode:
authorjw2552018-03-06 16:50:03 +0000
committerjw2552018-03-06 16:50:03 +0000
commitf3491957d8599a7ba97d9cb324274df49b6fdbd0 (patch)
tree98fbc253b0f5d3f140450b339d01da1c8055b052 /test/integration
parentMerge branch 'trh/payload-checker' into testing-middleware (diff)
downloadmedicine-f3491957d8599a7ba97d9cb324274df49b6fdbd0.tar.gz
medicine-f3491957d8599a7ba97d9cb324274df49b6fdbd0.zip

authentication tests

Diffstat (limited to 'test/integration')
-rw-r--r--test/integration/auth.routes.test.ts102
-rw-r--r--test/integration/index.routes.test.ts4
2 files changed, 85 insertions, 21 deletions
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', () => {