aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/integration/auth.routes.test.ts22
-rw-r--r--test/integration/protocol.routes.test.ts91
-rw-r--r--test/test-db.ts4
-rw-r--r--test/unit/options.unit.test.ts27
-rw-r--r--test/unit/types.unit.test.ts1
5 files changed, 94 insertions, 51 deletions
diff --git a/test/integration/auth.routes.test.ts b/test/integration/auth.routes.test.ts
index af3f696..29c0cfc 100644
--- a/test/integration/auth.routes.test.ts
+++ b/test/integration/auth.routes.test.ts
@@ -65,13 +65,15 @@ const authorisationErr = {
error_description: 'user does not have correct authorisation for task'
};
+const base: string = '/cs3099group-be-4';
+
describe('authentication', () => {
before(initDatabase);
context('invalid password authentication', () => {
before(populateDatabase);
it('should reject login to invalid endpoints', () => {
return chai.request(app)
- .post('/')
+ .post('/invalid endpoint')
.send({
username: mockUser.username,
password: mockUser.password,
@@ -86,7 +88,7 @@ describe('authentication', () => {
});
it('should reject invalid usernames', () => {
return chai.request(app)
- .post('/cs3099group-be-4/oauth/token')
+ .post(`${base}/oauth/token`)
.send({
username: 'invalid',
password: mockUser.password,
@@ -97,7 +99,7 @@ describe('authentication', () => {
});
it('should reject invalid passwords', () => {
return chai.request(app)
- .post('/cs3099group-be-4/oauth/token')
+ .post(`${base}/oauth/token`)
.send({
username: mockUser.username,
password: 'asasasas',
@@ -108,7 +110,7 @@ describe('authentication', () => {
});
it('should reject invalid grant_type', () => {
return chai.request(app)
- .post('/cs3099group-be-4/oauth/token')
+ .post(`${base}/oauth/token`)
.send({
username: mockUser.username,
password: mockUser.password,
@@ -119,7 +121,7 @@ describe('authentication', () => {
});
it('should reject empty usernames', () => {
return chai.request(app)
- .post('/cs3099group-be-4/oauth/token')
+ .post(`${base}/oauth/token`)
.send({
username: '',
password: mockUser.password,
@@ -130,7 +132,7 @@ describe('authentication', () => {
});
it('should reject empty passwords', () => {
return chai.request(app)
- .post('/cs3099group-be-4/oauth/token')
+ .post(`${base}/oauth/token`)
.send({
username: mockUser.username,
password: '',
@@ -141,7 +143,7 @@ describe('authentication', () => {
});
it('should reject empty grant_type', () => {
return chai.request(app)
- .post('/cs3099group-be-4/oauth/token')
+ .post(`${base}/oauth/token`)
.send({
username: mockUser.username,
password: mockUser.password,
@@ -155,7 +157,7 @@ describe('authentication', () => {
before(populateDatabase);
it('should accept correct credentials', () => {
return chai.request(app)
- .post('/cs3099group-be-4/oauth/token')
+ .post(`${base}/oauth/token`)
.send({
username: mockUser.username,
password: mockUser.password,
@@ -169,7 +171,7 @@ describe('authentication', () => {
beforeEach(populateDatabase);
beforeEach(getToken);
it('should reject invalid tokens', () => {
- return chai.request(app).get('/')
+ return chai.request(app).get(`${base}/users`)
.set('Authorization', 'Bearer ' + 'invalid')
.then((res) => {
expect(res).to.have.status(401);
@@ -178,7 +180,7 @@ describe('authentication', () => {
});
});
it('should accept valid tokens', () => {
- return chai.request(app).get('/')
+ return chai.request(app).get(`${base}/_supported_protocols_`)
.set('Authorization', `Bearer ${token}`)
.then((res) => {
expect(res).to.have.status(200);
diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts
index ebe18c2..ad9b892 100644
--- a/test/integration/protocol.routes.test.ts
+++ b/test/integration/protocol.routes.test.ts
@@ -10,6 +10,7 @@ const expect: Chai.ExpectStatic = chai.expect;
import { Credentials, Database, addUser, initDB, resetDB,
Projec, addProjec, Filee, addFilee } from '../test-db';
import { default as User } from '../../src/db/model/User';
+import * as fs from 'fs';
// Chai-http must be imported this way:
// tslint:disable-next-line:no-var-requires no-require-imports
@@ -57,8 +58,7 @@ const getToken = async() => {
});
};
-import { Template, alternative, array, exact, match, optional
- } from '../../src/matcher/options';
+import options, { Template, alternative, array, exact, match, optional }from '../../src/matcher/options';
import { default as types } from '../../src/matcher/types';
// The general response template according to protocol
@@ -82,27 +82,6 @@ const errorResponseTemplate: Template = {
type MochaForEachInput = [ string, string, Template, number ];
-describe('routes : errors', () => {
- before(initDatabase);
- describe('GET invalid routes', () => {
- // Should invalid routes also possibly be 401?
- it.skip('should have response code 404', () => {
- return chai.request(app).get('/invalid/route')
- .set('Authorization', `Bearer ${token}`)
- .catch((err) => {
- expect(err.status).to.equal(404);
- });
- });
- it('should conform to the error protocol', () => {
- return chai.request(app).get('/invalid/route')
- .set('Authorization', `Bearer ${token}`)
- .catch((err) => {
- expect(match(errorResponseTemplate)(err.response.body)).to.be.true;
- });
- });
- });
-});
-
describe('routes : protocol', () => {
const base: string = '/cs3099group-be-4';
@@ -111,11 +90,11 @@ describe('routes : protocol', () => {
// Format:
// <method> <route> <json-response-template> <response-code>
const completeProtocol: MochaForEachInput[] = [
- ['get', '/_supported_protocols_', exact({
+ ['get', '/_supported_protocols_', {
supported: array(types.string),
required: array(types.string)
- }), 200],
- ['get', '/log', types.array({
+ }, 200],
+ ['get', '/log', array({
component: types.string,
level: alternative([
'info',
@@ -133,7 +112,7 @@ describe('routes : protocol', () => {
level: 'info',
value: 'sdasdasdasdasdasd'
}], 200],
- ['get', '/properties', types.array({
+ ['get', '/properties', array({
id: types.string,
display: optional(match({
category: types.string,
@@ -153,7 +132,7 @@ describe('routes : protocol', () => {
types.boolean
])
}), 200],
- ['post', '/properties', null, 200],
+ ['post', '/properties?action=update', null, 200],
['get', '/user_privileges', array({
privilege: types.string,
description: types.string,
@@ -161,8 +140,8 @@ describe('routes : protocol', () => {
}), 200],
['get', '/users', array({
username: types.string,
- privileges: array(types.string),
- projects: array({
+ privileges: types.array(types.string),
+ projects: types.array({
project_name: types.string,
access_level: types.string
}),
@@ -190,8 +169,8 @@ describe('routes : protocol', () => {
}, 200],
['get', '/current_user', {
username: types.string,
- privileges: array(types.string),
- projects: array({
+ privileges: types.array(types.string),
+ projects: types.array({
project_name: types.string,
access_level: types.string
}),
@@ -208,7 +187,7 @@ describe('routes : protocol', () => {
}), 200],
['get', '/projects', array({
project_name: types.string,
- users: array({
+ users: types.array({
username: types.string,
access_level: types.string
}),
@@ -227,11 +206,9 @@ describe('routes : protocol', () => {
admin_metadata: optional(types.anything)
}, 200],
['post', '/projects/mocky', null, 200],
- ['get', '/projects/mocky/properties', {
- data: optional(types.anything)
- }, 200],
+ ['get', '/projects/mocky/properties', null, 200],
['get', '/projects/mocky/files/example/path', null, 200],
- ['post', '/projects/mocky/files/mockyfile2', null, 200],
+ ['post', '/projects/mocky/files/some_file?action=overwrite', {}, 200],
['get', '/projects/mocky/files_by_id/file1', null, 200]
];
@@ -257,11 +234,19 @@ describe('routes : protocol', () => {
forEach(completeProtocol).it(
'%s %s should have a status %4$d',
(method: string, path: string, temp: Template, resCode: number) => {
- const request: ChaiHttp.Request = method === 'get'
+ const request: any = method === 'get'
? chai.request(app).get(base + path)
: chai.request(app).post(base + path);
+ /*if (path.includes('/projects/mocky/files/some_file')) {
+ fs.createReadStream('test/mockUI/ui.html').pipe(
+ request);
+ console.log("tasdasdas")
+ }*/
request.set('Authorization', `Bearer ${token}`);
return request.then((res: ChaiHttp.Response) => {
+ if (path.includes('/projects/mocky/files/some_file')) {
+ console.log("something")
+ }
expect(res).to.have.status(resCode);
});
});
@@ -273,6 +258,7 @@ describe('routes : protocol', () => {
: chai.request(app).post(base + path);
request.set('Authorization', `Bearer ${token}`);
return request.then((res: ChaiHttp.Response) => {
+
expect(match(responseTemplate)(res.body)).to.be.true;
});
});
@@ -284,11 +270,38 @@ describe('routes : protocol', () => {
.get(base + path)
.set('Authorization', `Bearer ${token}`)
.then((res: ChaiHttp.Response) => {
- expect(match(temp)(res.body)).to.be.true;
+ if(!match(temp)(res.body.data)){
+ console.log("******" + method + " - " + path + "*****8")
+ console.log(res.body)
+ console.log(temp);
+ console.log("*************************************9")
+ }
+ expect(match(temp)(res.body.data)).to.be.true;
});
} else {
return true;
}
});
});
+
+ describe('test edge cases', () => {
+ before(initDatabase);
+ describe('GET invalid routes', () => {
+ // Should invalid routes also possibly be 401?
+ it.skip('should have response code 404', () => {
+ return chai.request(app).get('/invalid/route')
+ .set('Authorization', `Bearer ${token}`)
+ .catch((err) => {
+ expect(err.status).to.equal(404);
+ });
+ });
+ it('should conform to the error protocol', () => {
+ return chai.request(app).get('/invalid/route')
+ .set('Authorization', `Bearer ${token}`)
+ .catch((err) => {
+ expect(match(errorResponseTemplate)(err.response.body)).to.be.true;
+ });
+ });
+ });
+ });
});
diff --git a/test/test-db.ts b/test/test-db.ts
index 40ae438..ef980c5 100644
--- a/test/test-db.ts
+++ b/test/test-db.ts
@@ -112,9 +112,9 @@ export const addFilee: (database: Database, user: Credentials) =>
path: 'example/path',
uuid: 'file1',
creator: user,
- type: "generic"
+ type: 'generic'
};
await new File(mockFile).save();
return mockFile;
-}; \ No newline at end of file
+};
diff --git a/test/unit/options.unit.test.ts b/test/unit/options.unit.test.ts
index 1925835..a4c879a 100644
--- a/test/unit/options.unit.test.ts
+++ b/test/unit/options.unit.test.ts
@@ -159,5 +159,32 @@ describe('Matching:', () => {
};
options.match(template, matchee).should.be.false;
});
+ it('accepts optional matches', () => {
+ const template = {
+ a: options.optional(types.array),
+ b: options.optional(types.anything)
+ };
+ const matchee = {
+ b: []
+ };
+ options.match(template)(matchee).should.be.true;
+ });
+ it('big bad unit test', () => {
+ const matchee = { status: 'success',
+ data: [ { privilege: 'admin', description: 'null', internal: false },
+ { privilege: 'logging',
+ description: 'Allows Post to Log',
+ internal: false } ] };
+ const template = {
+ status: types.string,
+ data: options.optional(types.anything),
+ error: options.optional(types.string),
+ error_description: options.optional(types.string),
+ user_message: options.optional(types.string),
+ error_data: options.optional(types.anything)
+ };
+
+ options.match(template)(matchee).should.be.true;
+ });
});
});
diff --git a/test/unit/types.unit.test.ts b/test/unit/types.unit.test.ts
index ad0f99c..f18d5a1 100644
--- a/test/unit/types.unit.test.ts
+++ b/test/unit/types.unit.test.ts
@@ -30,6 +30,7 @@ describe('Type Checking:', () => {
types.anything(1).should.be.true;
types.anything('string').should.be.true;
types.anything(false).should.be.true;
+ types.anything([]).should.be.true;
});
it('accepts null', () => {
types.anything(null).should.be.true;