aboutsummaryrefslogtreecommitdiff
path: root/test/integration/protocol.routes.test.ts
diff options
context:
space:
mode:
authorcjd242018-04-19 20:48:19 +0100
committercjd242018-04-19 20:48:19 +0100
commit92e23d37f843e49eda01a4d0ea7ed8005efffdb2 (patch)
tree7c08678dfce9bf82eaa4f71f5e23182f8515d386 /test/integration/protocol.routes.test.ts
parentAdded a write of the base teir JSON object, made the images actually write to... (diff)
parentMerge branch 'trh/gets' into cjd24/conversion (diff)
downloadmedicine-92e23d37f843e49eda01a4d0ea7ed8005efffdb2.tar.gz
medicine-92e23d37f843e49eda01a4d0ea7ed8005efffdb2.zip

Merge branch ‘cjd24/conversion’ of https://gitlab.cs.st-andrews.ac.uk/cs3099group-be-4/project-code into cjd24/conversion

Diffstat (limited to 'test/integration/protocol.routes.test.ts')
-rw-r--r--test/integration/protocol.routes.test.ts380
1 files changed, 224 insertions, 156 deletions
diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts
index 18ee1e3..1e8bf63 100644
--- a/test/integration/protocol.routes.test.ts
+++ b/test/integration/protocol.routes.test.ts
@@ -7,8 +7,10 @@
import * as chai from 'chai';
const expect: Chai.ExpectStatic = chai.expect;
-import { Credentials, Database, addUser, initDB, resetDB } from '../test-db';
+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
@@ -28,6 +30,8 @@ const app = App.TestApp();
let database: Database;
let mockUser: Credentials;
let token: string;
+let mockProj: Projec;
+let mockFile: Filee;
const initDatabase = async() => {
database = initDB();
@@ -37,6 +41,8 @@ const initDatabase = async() => {
const populateDatabase = async() => {
await resetDB(database);
mockUser = await addUser(database, true);
+ mockProj = await addProjec(database, mockUser);
+ mockFile = await addFilee(database, mockUser);
};
const getToken = async() => {
@@ -52,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
@@ -77,156 +82,138 @@ 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;
- });
- });
- });
-});
+const base: string = '/cs3099group-be-4';
-describe('routes : protocol', () => {
- const base: string = '/cs3099group-be-4';
+/* tslint:disable:align */
- /* tslint:disable:align */
-
- // Format:
- // <method> <route> <json-response-template> <response-code>
- const completeProtocol: MochaForEachInput[] = [
- ['get', '/_supported_protocols_', exact({
- supported: array(types.string),
- required: array(types.string)
- }), 200],
- ['get', '/log', types.array({
- component: types.string,
- level: alternative([
- 'info',
- 'security',
- 'warning',
- 'error',
- 'critical'
- ]),
- value: types.anything,
- username: types.string,
- timestamp: types.string
- }), 500],
- ['post', '/log', null, 500],
- ['get', '/properties', types.array({
- id: types.string,
- display: optional(match({
- category: types.string,
- group: types.string,
- display_name: types.string,
- description: types.string
- })),
- read_only: types.boolean,
- type: alternative([
- 'string',
- 'integer',
- 'boolean'
- ]),
- value: alternative([
- types.string,
- types.integer,
- types.boolean
- ])
- }), 200],
- ['post', '/properties', null, 200],
- ['get', '/user_privileges', array({
- privilege: types.string,
- description: types.string,
- internal: types.boolean
- }), 200],
- ['get', '/users', array({
- username: types.string,
- privileges: array(types.string),
- projects: array({
- project_name: types.string,
- access_level: types.string
- }),
- public_user_metadata: types.anything,
- private_user_metadata: types.anything,
- public_admin_metadata: types.anything,
- private_admin_metadata: types.anything
- }), 401],
- // TODO types.metadata
- ['get', '/users/:username', {
- username: types.string,
- privileges: array(types.string),
- projects: array({
- project_name: types.string,
- access_level: types.string
- }),
- public_user_metadata: types.anything,
- private_user_metadata: types.anything,
- public_admin_metadata: types.anything,
- private_admin_metadata: types.anything
- }, 401],
- ['post', '/users/:username', null, 401],
- ['get', '/users/:username/properties', {
- data: optional(types.anything)
- }, 401],
- ['get', '/current_user', {
- username: types.string,
- privileges: array(types.string),
- projects: array({
- project_name: types.string,
- access_level: types.string
- }),
- public_user_metadata: types.anything,
- private_user_metadata: types.anything,
- public_admin_metadata: types.anything,
- private_admin_metadata: types.anything
- }, 200],
- ['post', '/current_user', null, 200],
- ['get', '/project_roles', array({
- role: types.string,
- description: types.string,
- internal: types.boolean
- }), 200],
- ['get', '/projects', array({
+// Format:
+// <method> <route> <json-response-template> <response-code>
+const completeProtocol: MochaForEachInput[] = [
+ ['get', '/_supported_protocols_', {
+ supported: array(types.string),
+ required: array(types.string)
+ }, 200],
+ ['get', '/log', array({
+ component: types.string,
+ level: alternative([
+ 'info',
+ 'security',
+ 'warning',
+ 'error',
+ 'critical'
+ ]),
+ value: types.anything,
+ username: types.string,
+ timestamp: types.string
+ }), 200],
+ ['post', '/log', [{
+ component: 'tesssst',
+ level: 'info',
+ value: 'sdasdasdasdasdasd'
+ }], 200],
+ ['get', '/properties', array({
+ id: types.string,
+ display: optional(match({
+ category: types.string,
+ group: types.string,
+ display_name: types.string,
+ description: types.string
+ })),
+ read_only: types.boolean,
+ type: alternative([
+ 'string',
+ 'integer',
+ 'boolean'
+ ]),
+ value: alternative([
+ types.string,
+ types.integer,
+ types.boolean
+ ])
+ }), 200],
+ ['post', '/properties?action=update', null, 200],
+ ['get', '/user_privileges', array({
+ privilege: types.string,
+ description: types.string,
+ internal: types.boolean
+ }), 200],
+ ['get', '/users', array({
+ username: types.string,
+ privileges: types.array(types.string),
+ projects: types.array({
project_name: types.string,
- users: array({
- username: types.string,
- access_level: types.string
- }),
- public_metadata: types.anything,
- private_metadata: optional(types.anything),
- admin_metadata: optional(types.anything)
- }), 200],
- ['get', '/projects/:project_name', {
+ access_level: types.string
+ }),
+ public_user_metadata: types.anything,
+ private_user_metadata: types.anything,
+ public_admin_metadata: types.anything,
+ private_admin_metadata: types.anything
+ }), 200],
+ // TODO types.metadata
+ ['get', '/users/mock_user', {
+ username: types.string,
+ privileges: array(types.string),
+ projects: array({
project_name: types.string,
- users: array({
- username: types.string,
- access_level: types.string
- }),
- public_metadata: types.anything,
- private_metadata: types.anything,
- admin_metadata: optional(types.anything)
- }, 200],
- ['post', '/projects/:project_name', null, 200],
- ['get', '/projects/:project_name/properties', {
- data: optional(types.anything)
- }, 200],
- ['get', '/projects/:project_name/files/:path', null, 200],
- ['post', '/projects/:project_name/files/:id', null, 200],
- ['get', '/projects/:project_name/files_by_id/:id', null, 200]
- ];
+ access_level: types.string
+ }),
+ public_user_metadata: types.anything,
+ private_user_metadata: types.anything,
+ public_admin_metadata: types.anything,
+ private_admin_metadata: types.anything
+ }, 200],
+ ['post', '/users/new_user', null, 200],
+ ['get', '/users/mock_user/properties', {
+ data: optional(types.anything)
+ }, 200],
+ ['get', '/current_user', {
+ username: types.string,
+ privileges: types.array(types.string),
+ projects: types.array({
+ project_name: types.string,
+ access_level: types.string
+ }),
+ public_user_metadata: types.anything,
+ private_user_metadata: types.anything,
+ public_admin_metadata: types.anything,
+ private_admin_metadata: types.anything
+ }, 200],
+ ['post', '/current_user', null, 200],
+ ['get', '/project_roles', array({
+ role: types.string,
+ description: types.string,
+ internal: types.boolean
+ }), 200],
+ ['get', '/projects', array({
+ project_name: types.string,
+ users: types.array({
+ username: types.string,
+ access_level: types.string
+ }),
+ public_metadata: types.anything,
+ private_metadata: optional(types.anything),
+ admin_metadata: optional(types.anything)
+ }), 200],
+ ['get', '/projects/mocky', {
+ project_name: types.string,
+ users: array({
+ username: types.string,
+ access_level: types.string
+ }),
+ public_metadata: types.anything,
+ private_metadata: types.anything,
+ admin_metadata: optional(types.anything)
+ }, 200],
+ ['post', '/projects/mocky', null, 200],
+ ['get', '/projects/mocky/properties', null, 200],
+ ['get', '/projects/mocky/files/example/path', null, 200],
+ ['post', '/projects/mocky/files/some_file?action=overwrite', {}, 200],
+ ['get', '/projects/mocky/files_by_id/file1', null, 200]
+];
+
+/* tslint:enable:align */
- /* tslint:enable:align */
+describe('routes : protocol', () => {
before(initDatabase);
beforeEach(populateDatabase);
@@ -238,7 +225,8 @@ describe('routes : protocol', () => {
(method: string, path: string, temp: Template, resCode: number) => {
const request: ChaiHttp.Request = method === 'get'
? chai.request(app).get(base + path)
- : chai.request(app).post(base + path);
+ : chai.request(app).post(base + path)
+ .set('content-type', 'application/json').send(temp);
request.set('Authorization', `Bearer ${token}`);
return request.then((res: ChaiHttp.Response) => {
expect(res.type).to.equal('application/json');
@@ -247,38 +235,118 @@ 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);
+ : chai.request(app).post(base + path).send(temp);
+ /*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);
});
});
forEach(completeProtocol).it(
- '%s %s should conform to the general response protocol',
+ '%s %s should conform to the general response template',
(method: string, path: string, temp: Template, resCode: number) => {
const request: ChaiHttp.Request = method === 'get'
? chai.request(app).get(base + path)
- : chai.request(app).post(base + path);
+ : chai.request(app).post(base + path).send(temp);;
request.set('Authorization', `Bearer ${token}`);
return request.then((res: ChaiHttp.Response) => {
- match(responseTemplate, JSON.parse(res.body)).shoul.be.true;
+ if (!(match(responseTemplate)(res.body))) {
+ console.log(`${method} ${path} ******************`);
+ console.log(res.body);
+ console.log("^^^^^^ received || template vvvvvvvv")
+ console.log(responseTemplate);
+ console.log('--------------------------------')
+ }
+ expect(match(responseTemplate)(res.body)).to.be.true;
});
});
forEach(completeProtocol).it(
- 'the response for %s %s should conform to its specific response protocol',
+ '%s %s should conform to its specific response template',
(method: string, path: string, temp: Template, resCode: number) => {
if (method === 'get' && temp != null) {
return chai.request(app)
.get(base + path)
.set('Authorization', `Bearer ${token}`)
.then((res: ChaiHttp.Response) => {
- expect(match(temp)(JSON.parse(res.body))).to.be.true;
+ if (!(match(temp)(res.body.data))) {
+ console.log(`${method} ${path} ******************`);
+ console.log(res.body.data);
+ console.log("^^^^^^ received || template vvvvvvvv")
+ console.log(temp);
+ console.log('--------------------------------')
+ }
+ expect(match(temp)(res.body.data)).to.be.true;
});
} else {
return true;
}
});
});
+
+ describe('test edge cases', () => {
+ before(initDatabase);
+ beforeEach(populateDatabase);
+ beforeEach(getToken);
+
+ 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('POST invalid routes', () => {
+ // Should invalid routes also possibly be 401?
+ it.skip('should have response code 404', () => {
+ return chai.request(app).post('/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).post('/invalid/route')
+ .set('Authorization', `Bearer ${token}`)
+ .catch((err) => {
+ expect(match(errorResponseTemplate)(err.response.body)).to.be.true;
+ });
+ });
+ });
+/*
+ describe('access valid routes with invalid parameters', () => {
+ forEach(completeProtocol.filter((item, index, arr) => {
+ return (item[0] == 'post');
+ })).it(
+ '%s %s with null body should throw error',
+ (method: string, path: string, temp: Template, resCode: number) => {
+ const request: any = chai.request(app).post(base + path);
+ request.set('Authorization', `Bearer ${token}`)
+ .then((res: any) => {
+ expect(res).to.have.status(400);
+ }).catch((reason: any) => {
+ expect(reason.response.body).to.eql("errererere");
+ });
+ });
+ });*/
+ });
});