aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorjw2552018-03-04 16:06:18 +0000
committerjw2552018-03-04 16:06:18 +0000
commit46f6e6c7bf1d69d1b3257f6cc186326a57c2b51d (patch)
treeebf45cb9030c23a0c88c8335050093dd02128ef8 /test
parentnow compiles. also added stubs for database testing (diff)
downloadmedicine-46f6e6c7bf1d69d1b3257f6cc186326a57c2b51d.tar.gz
medicine-46f6e6c7bf1d69d1b3257f6cc186326a57c2b51d.zip

started authenticating

Diffstat (limited to 'test')
-rw-r--r--test/integration/index.routes.test.ts94
-rw-r--r--test/integration/protocol.routes.test.ts4
2 files changed, 73 insertions, 25 deletions
diff --git a/test/integration/index.routes.test.ts b/test/integration/index.routes.test.ts
index 7728db8..31af6cf 100644
--- a/test/integration/index.routes.test.ts
+++ b/test/integration/index.routes.test.ts
@@ -6,6 +6,9 @@
import * as chai from 'chai';
const expect: Chai.ExpectStatic = chai.expect;
const should = chai.should();
+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
@@ -18,34 +21,79 @@ import 'mocha';
import * as App from '../../src/app';
let app = App.TestApp();
+const userCredentials = {
+ username: 'bobby', //'admin',
+ password: 'pass', //'eprprJacR0hBpmWvs5IDJZTnjRAY2gM3tSm0b1af',
+ grant_type: 'password'
+}
+let authenticatedUser = request.agent(app);
+let token: JSON;
+
+// add user
+before(async function(done){
+ const newUser: User = new User({
+ username: 'bobby',
+ password: 'pass',
+ userGroups: []
+ });
+ await newUser.save();
+ done();
+})
+
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 return json', () => {
- return chai.request(app).get('/')
- .then((res: ChaiHttp.Response) => {
- expect(res.type).to.eql('application/json');
+ it('should return 401 without authentication', () => {
+ chai.request(app).get('/')
+ .catch(function (err: any) {
+ return expect(err).to.have.status(401);
});
});
-
- 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!');
+
+ context('with authentication', () => {
+ 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 + " }");
+ done();
+ });
});
- });
-
- 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 200', () => {
+ return chai.request(app).get('/')
+ .set('Authorization', JSON.stringify(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/');
+ // });
+ // });
+ })
+
});
+
}); \ No newline at end of file
diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts
index e60cd81..6e71232 100644
--- a/test/integration/protocol.routes.test.ts
+++ b/test/integration/protocol.routes.test.ts
@@ -195,7 +195,7 @@ describe('routes : protocol', () => {
['get', '/projects/:project_name/files_by_id/:id', null, 200]
];
- describe('GET/POST all valid routes', () => {
+ describe('Access all valid routes', () => {
forEach(completeProtocol).it(
'%s %s should be json',
(method: string, path: string, temp: Template, res_code: number) => {
@@ -221,7 +221,7 @@ describe('routes : protocol', () => {
});;
});
forEach(completeProtocol).it(
- '%s %s should conform to the standard response protocol',
+ '%s %s should conform to the general response protocol',
(method: string, path: string, temp: Template, res_code: number) => {
const request: ChaiHttp.Request = method === 'get'
? chai.request(app).get(base + path)