From 77bfad0307f4e5daea0584a30b0dcf24ccc9ec42 Mon Sep 17 00:00:00 2001 From: jw255 Date: Thu, 22 Feb 2018 16:57:45 +0000 Subject: restructuring of tests --- package.json | 2 + src/app.ts | 11 +- src/matcher/options.test.ts | 163 ---------------------------- src/matcher/options.unit.test.ts | 163 ++++++++++++++++++++++++++++ src/matcher/types.test.ts | 178 ------------------------------ src/matcher/types.unit.test.ts | 178 ++++++++++++++++++++++++++++++ test/nodeserver.test.ts | 225 -------------------------------------- test/protocol.node.test.ts | 227 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 578 insertions(+), 569 deletions(-) delete mode 100644 src/matcher/options.test.ts create mode 100644 src/matcher/options.unit.test.ts delete mode 100644 src/matcher/types.test.ts create mode 100644 src/matcher/types.unit.test.ts delete mode 100644 test/nodeserver.test.ts create mode 100644 test/protocol.node.test.ts diff --git a/package.json b/package.json index 559efc7..15615c0 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,8 @@ "license": "UNLICENSED", "scripts": { "test": "mocha --reporter spec -r ts-node/register './**/*.test.ts'", + "test_unit": "mocha --reporter spec -r ts-node/register './**/*.unit.test.ts'", + "test_node": "mocha --reporter spec -r ts-node/register './**/*.node.test.ts'", "docs": "typedoc --out 'docs' --readme 'README.md' --name 'St Andrews Junior Honours 2017/18 - Team BE4' --module commonjs --target ES5 --exclude '*.test.ts' --includeDeclarations --excludeExternals 'src'", "start": "ts-node src/index.ts", "lint": "tslint --fix --force -p './tsconfig.json'", diff --git a/src/app.ts b/src/app.ts index 4f0e0d7..ef4485e 100644 --- a/src/app.ts +++ b/src/app.ts @@ -5,17 +5,19 @@ import * as logger from 'morgan'; class App { public express: ex.Express; - public logEnabled: boolean = true; + private logEnabled: boolean; - public constructor() { + public constructor(enableLog: boolean) { this.express = ex(); this.middleware(); this.mountRoutes(); + this.logEnabled = enableLog; } // Configure Express middleware. private middleware(): void { if (this.logEnabled) { + console.log("log enabled") this.express.use(logger('combined')); } this.express.use(bodyParser.json()); @@ -36,4 +38,7 @@ class App { } } -export default new App().express; +export default new App(true).express; +export function TestApp(){ + return new App(false).express; +} diff --git a/src/matcher/options.test.ts b/src/matcher/options.test.ts deleted file mode 100644 index ff5763e..0000000 --- a/src/matcher/options.test.ts +++ /dev/null @@ -1,163 +0,0 @@ -/* tslint:disable:typedef */ -// Disabled to allow quick template creation. - -/* tslint:disable:newline-per-chained-call */ -// Disabled to allow Chai- and Mocha-style test formatting. - -/* tslint:disable:no-unused-expression */ -// Disabled to allow `...to.be.true`. - -import * as chai from 'chai'; -chai.should(); - -import * as options from './options'; -import { default as types } from './types'; - -// tslint:disable-next-line:no-require-imports - -describe('Matching:', () => { - describe('Rough Matching', () => { - context('for literals', () => { - context('accepts matching', () => { - it('strings', () => { - options.match('a', 'a').should.be.true; - }); - it('numbers', () => { - options.match(3, 3).should.be.true; - }); - it('boolean values', () => { - options.match(true, true).should.be.true; - }); - it('null to null', () => { - options.match(null, null).should.be.true; - }); - }); - context('rejects', () => { - it('different strings', () => { - options.match('x', 'y').should.be.false; - }); - it('different numbers', () => { - options.match(20, 42).should.be.false; - }); - it('different boolean values', () => { - options.match(true, false).should.be.false; - }); - it('different types', () => { - options.match('55', 55).should.be.false; - }); - it('null matched with non-null values', () => { - options.match(null, false).should.be.false; - }); - it('non-null values matched with null', () => { - options.match(0, null).should.be.false; - }); - }); - }); - context('for checker functions', () => { - context('accepts', () => { - it('values that succeed the checker function', () => { - options.match(types.number, 9).should.be.true; - }); - }); - context('rejects', () => { - it('values that do not succeed the checker function', () => { - options.match(types.boolean, 'true').should.be.false; - }); - }); - }); - it('accepts basic things', () => { - const template = { - a: types.string - }; - const matchee = { - a: 'yes' - }; - options.match(template, matchee).should.be.true; - }); - it('accepts objects with superfluous properties', () => { - const template = { - a: types.number - }; - const matchee = { - a: 40, - b: 20 - }; - options.match(template, matchee).should.be.true; - }); - it('rejects objects missing some properties', () => { - const template = { - a: types.boolean, - b: types.number, - c: types.number - }; - const matchee = { - b: 5 - }; - options.match(template, matchee).should.be.false; - }); - it('accepts objects that omit not_present properties', () => { - const template = { - a: types.number, - b: types.not_present, - c: types.not_present - }; - const matchee = { - a: 4 - }; - options.match(template, matchee).should.be.true; - }); - it('rejects empty objects for non-empty templates', () => { - const template = { - a: types.boolean - }; - const matchee = { - }; - options.match(template, matchee).should.be.false; - }); - it('accepts empty objects for empty templates', () => { - const template = { - }; - const matchee = { - }; - options.match(template, matchee).should.be.true; - }); - it('accepts nested objects that match', () => { - const template = { - a: types.number, - b: { - ba: types.string, - bb: types.string - }, - c: types.boolean - }; - const matchee = { - a: 432, - b: { - ba: 'baracus', - bb: 'ATEAM' - }, - c: true, - x: false - }; - options.match(template, matchee).should.be.true; - }); - it('accepts literal matches', () => { - const template = { - a: 'literal' - }; - const matchee = { - a: 'literal' - }; - options.match(template, matchee).should.be.true; - }); - it('rejects incorrect literal matches', () => { - const template = { - g: 99 - }; - const matchee = { - g: 80 - }; - options.match(template, matchee).should.be.false; - }); - }); -}); diff --git a/src/matcher/options.unit.test.ts b/src/matcher/options.unit.test.ts new file mode 100644 index 0000000..ff5763e --- /dev/null +++ b/src/matcher/options.unit.test.ts @@ -0,0 +1,163 @@ +/* tslint:disable:typedef */ +// Disabled to allow quick template creation. + +/* tslint:disable:newline-per-chained-call */ +// Disabled to allow Chai- and Mocha-style test formatting. + +/* tslint:disable:no-unused-expression */ +// Disabled to allow `...to.be.true`. + +import * as chai from 'chai'; +chai.should(); + +import * as options from './options'; +import { default as types } from './types'; + +// tslint:disable-next-line:no-require-imports + +describe('Matching:', () => { + describe('Rough Matching', () => { + context('for literals', () => { + context('accepts matching', () => { + it('strings', () => { + options.match('a', 'a').should.be.true; + }); + it('numbers', () => { + options.match(3, 3).should.be.true; + }); + it('boolean values', () => { + options.match(true, true).should.be.true; + }); + it('null to null', () => { + options.match(null, null).should.be.true; + }); + }); + context('rejects', () => { + it('different strings', () => { + options.match('x', 'y').should.be.false; + }); + it('different numbers', () => { + options.match(20, 42).should.be.false; + }); + it('different boolean values', () => { + options.match(true, false).should.be.false; + }); + it('different types', () => { + options.match('55', 55).should.be.false; + }); + it('null matched with non-null values', () => { + options.match(null, false).should.be.false; + }); + it('non-null values matched with null', () => { + options.match(0, null).should.be.false; + }); + }); + }); + context('for checker functions', () => { + context('accepts', () => { + it('values that succeed the checker function', () => { + options.match(types.number, 9).should.be.true; + }); + }); + context('rejects', () => { + it('values that do not succeed the checker function', () => { + options.match(types.boolean, 'true').should.be.false; + }); + }); + }); + it('accepts basic things', () => { + const template = { + a: types.string + }; + const matchee = { + a: 'yes' + }; + options.match(template, matchee).should.be.true; + }); + it('accepts objects with superfluous properties', () => { + const template = { + a: types.number + }; + const matchee = { + a: 40, + b: 20 + }; + options.match(template, matchee).should.be.true; + }); + it('rejects objects missing some properties', () => { + const template = { + a: types.boolean, + b: types.number, + c: types.number + }; + const matchee = { + b: 5 + }; + options.match(template, matchee).should.be.false; + }); + it('accepts objects that omit not_present properties', () => { + const template = { + a: types.number, + b: types.not_present, + c: types.not_present + }; + const matchee = { + a: 4 + }; + options.match(template, matchee).should.be.true; + }); + it('rejects empty objects for non-empty templates', () => { + const template = { + a: types.boolean + }; + const matchee = { + }; + options.match(template, matchee).should.be.false; + }); + it('accepts empty objects for empty templates', () => { + const template = { + }; + const matchee = { + }; + options.match(template, matchee).should.be.true; + }); + it('accepts nested objects that match', () => { + const template = { + a: types.number, + b: { + ba: types.string, + bb: types.string + }, + c: types.boolean + }; + const matchee = { + a: 432, + b: { + ba: 'baracus', + bb: 'ATEAM' + }, + c: true, + x: false + }; + options.match(template, matchee).should.be.true; + }); + it('accepts literal matches', () => { + const template = { + a: 'literal' + }; + const matchee = { + a: 'literal' + }; + options.match(template, matchee).should.be.true; + }); + it('rejects incorrect literal matches', () => { + const template = { + g: 99 + }; + const matchee = { + g: 80 + }; + options.match(template, matchee).should.be.false; + }); + }); +}); diff --git a/src/matcher/types.test.ts b/src/matcher/types.test.ts deleted file mode 100644 index 398064c..0000000 --- a/src/matcher/types.test.ts +++ /dev/null @@ -1,178 +0,0 @@ -/* tslint:disable:no-unused-expression */ -// Disabled to allow `...to.be.true`. - -import * as chai from 'chai'; -chai.should(); - -import { default as types } from './types'; - -describe('Type Checking:', () => { - for (const x of [ - { name: 'Object', func: types.object }, - { name: 'Number', func: types.number }, - { name: 'Integer', func: types.integer }, - { name: 'String', func: types.string }, - { name: 'Boolean', func: types.boolean }, - { name: 'Array', func: types.array } - ]) { - describe(`${x.name} Checking`, () => { - it('rejects null', () => { - x.func(null).should.be.false; - }); - it('rejects undefined', () => { - x.func(undefined).should.be.false; - }); - }); - } - - describe('Presence Checking', () => { - it('accepts non-undefined values', () => { - types.anything(1).should.be.true; - types.anything('string').should.be.true; - types.anything(false).should.be.true; - }); - it('accepts null', () => { - types.anything(null).should.be.true; - }); - it('rejects undefined', () => { - types.anything(undefined).should.be.false; - }); - it('rejects objects without the named property', () => { - const x: { [key: string]: number } = {}; - /* There's no way to pick a key that definitely won't be - /* on the object */ - types.anything(x["hopefully this isn't a property"]) - .should.be.false; - }); - }); - - describe('Absence Checking', () => { - it('accepts undefined', () => { - types.not_present(undefined).should.be.true; - }); - }); - - describe('Number Checking', () => { - it('accepts positive integers', () => { - types.number(4).should.be.true; - }); - it('accepts negative integers', () => { - types.number(-10).should.be.true; - }); - it('accepts floats', () => { - types.number(3.14).should.be.true; - }); - it('accepts zero', () => { - types.number(0).should.be.true; - }); - it('accepts boxed numbers', () => { - // tslint:disable-next-line:no-construct - types.number(new Number(20)).should.be.true; - }); - it('rejects strings', () => { - types.number('haha').should.be.false; - }); - it('rejects objects', () => { - types.number(new Object()).should.be.false; - }); - it('rejects arrays', () => { - types.number([1, 2, 3]).should.be.false; - }); - it('rejects things that look like numbers', () => { - types.number('1234').should.be.false; - types.number('-3.14').should.be.false; - }); - it('accepts NaN', () => { - types.number(NaN).should.be.true; - }); - it('accepts Infinity', () => { - types.number(Infinity).should.be.true; - }); - }); - - describe('Integer Checking', () => { - it('rejects non-integer numbers', () => { - types.integer(3.14).should.be.false; - }); - it('rejects NaN', () => { - types.integer(NaN).should.be.false; - }); - it('rejects Infinity', () => { - types.integer(Infinity).should.be.false; - }); - }); - - describe('String Checking', () => { - it('accepts basic primitive strings', () => { - types.string('string!').should.be.true; - }); - it('accepts strings of numbers', () => { - types.string('1234').should.be.true; - }); - it('accepts the empty string', () => { - types.string('').should.be.true; - }); - it('accepts non-primitive string objects', () => { - // tslint:disable-next-line:no-construct - types.string(new String('String!')).should.be.true; - }); - it('rejects numbers', () => { - types.string(5).should.be.false; - // tslint:disable-next-line:no-construct - types.string(new Number(28)).should.be.false; - }); - it('rejects objects', () => { - types.string(new Object()).should.be.false; - }); - it('rejects arrays', () => { - types.string(['ah', 'ha']).should.be.false; - }); - }); - - describe('Boolean Checking', () => { - it('accepts true and false', () => { - types.boolean(true).should.be.true; - types.boolean(false).should.be.true; - }); - it('rejects numbers', () => { - types.boolean(2).should.be.false; - }); - it('rejects strings', () => { - types.boolean('not boolean').should.be.false; - }); - it('rejects strings of boolean values', () => { - types.boolean('true').should.be.false; - }); - it('rejects objects', () => { - types.boolean(new Object()).should.be.false; - }); - it('rejects arrays', () => { - types.boolean([true, true, false]).should.be.false; - }); - it('rejects falsy values', () => { - types.boolean(0).should.be.false; - }); - }); - - describe('Array Checking', () => { - it('accepts arrays', () => { - types.array([1, 'two', false]).should.be.true; - }); - it('accepts empty arrays', () => { - types.array([]).should.be.true; - types.array(new Array()).should.be.true; - }); - it('rejects strings', () => { - types.array('not an array').should.be.false; - }); - it('rejects strings that look like arrays', () => { - types.array('[1, 2, false]').should.be.false; - }); - it('rejects numbers', () => { - types.array(8).should.be.false; - }); - it('rejects objects', () => { - types.array(new Object()).should.be.false; - }); - }); -}); diff --git a/src/matcher/types.unit.test.ts b/src/matcher/types.unit.test.ts new file mode 100644 index 0000000..398064c --- /dev/null +++ b/src/matcher/types.unit.test.ts @@ -0,0 +1,178 @@ +/* tslint:disable:no-unused-expression */ +// Disabled to allow `...to.be.true`. + +import * as chai from 'chai'; +chai.should(); + +import { default as types } from './types'; + +describe('Type Checking:', () => { + for (const x of [ + { name: 'Object', func: types.object }, + { name: 'Number', func: types.number }, + { name: 'Integer', func: types.integer }, + { name: 'String', func: types.string }, + { name: 'Boolean', func: types.boolean }, + { name: 'Array', func: types.array } + ]) { + describe(`${x.name} Checking`, () => { + it('rejects null', () => { + x.func(null).should.be.false; + }); + it('rejects undefined', () => { + x.func(undefined).should.be.false; + }); + }); + } + + describe('Presence Checking', () => { + it('accepts non-undefined values', () => { + types.anything(1).should.be.true; + types.anything('string').should.be.true; + types.anything(false).should.be.true; + }); + it('accepts null', () => { + types.anything(null).should.be.true; + }); + it('rejects undefined', () => { + types.anything(undefined).should.be.false; + }); + it('rejects objects without the named property', () => { + const x: { [key: string]: number } = {}; + /* There's no way to pick a key that definitely won't be + /* on the object */ + types.anything(x["hopefully this isn't a property"]) + .should.be.false; + }); + }); + + describe('Absence Checking', () => { + it('accepts undefined', () => { + types.not_present(undefined).should.be.true; + }); + }); + + describe('Number Checking', () => { + it('accepts positive integers', () => { + types.number(4).should.be.true; + }); + it('accepts negative integers', () => { + types.number(-10).should.be.true; + }); + it('accepts floats', () => { + types.number(3.14).should.be.true; + }); + it('accepts zero', () => { + types.number(0).should.be.true; + }); + it('accepts boxed numbers', () => { + // tslint:disable-next-line:no-construct + types.number(new Number(20)).should.be.true; + }); + it('rejects strings', () => { + types.number('haha').should.be.false; + }); + it('rejects objects', () => { + types.number(new Object()).should.be.false; + }); + it('rejects arrays', () => { + types.number([1, 2, 3]).should.be.false; + }); + it('rejects things that look like numbers', () => { + types.number('1234').should.be.false; + types.number('-3.14').should.be.false; + }); + it('accepts NaN', () => { + types.number(NaN).should.be.true; + }); + it('accepts Infinity', () => { + types.number(Infinity).should.be.true; + }); + }); + + describe('Integer Checking', () => { + it('rejects non-integer numbers', () => { + types.integer(3.14).should.be.false; + }); + it('rejects NaN', () => { + types.integer(NaN).should.be.false; + }); + it('rejects Infinity', () => { + types.integer(Infinity).should.be.false; + }); + }); + + describe('String Checking', () => { + it('accepts basic primitive strings', () => { + types.string('string!').should.be.true; + }); + it('accepts strings of numbers', () => { + types.string('1234').should.be.true; + }); + it('accepts the empty string', () => { + types.string('').should.be.true; + }); + it('accepts non-primitive string objects', () => { + // tslint:disable-next-line:no-construct + types.string(new String('String!')).should.be.true; + }); + it('rejects numbers', () => { + types.string(5).should.be.false; + // tslint:disable-next-line:no-construct + types.string(new Number(28)).should.be.false; + }); + it('rejects objects', () => { + types.string(new Object()).should.be.false; + }); + it('rejects arrays', () => { + types.string(['ah', 'ha']).should.be.false; + }); + }); + + describe('Boolean Checking', () => { + it('accepts true and false', () => { + types.boolean(true).should.be.true; + types.boolean(false).should.be.true; + }); + it('rejects numbers', () => { + types.boolean(2).should.be.false; + }); + it('rejects strings', () => { + types.boolean('not boolean').should.be.false; + }); + it('rejects strings of boolean values', () => { + types.boolean('true').should.be.false; + }); + it('rejects objects', () => { + types.boolean(new Object()).should.be.false; + }); + it('rejects arrays', () => { + types.boolean([true, true, false]).should.be.false; + }); + it('rejects falsy values', () => { + types.boolean(0).should.be.false; + }); + }); + + describe('Array Checking', () => { + it('accepts arrays', () => { + types.array([1, 'two', false]).should.be.true; + }); + it('accepts empty arrays', () => { + types.array([]).should.be.true; + types.array(new Array()).should.be.true; + }); + it('rejects strings', () => { + types.array('not an array').should.be.false; + }); + it('rejects strings that look like arrays', () => { + types.array('[1, 2, false]').should.be.false; + }); + it('rejects numbers', () => { + types.array(8).should.be.false; + }); + it('rejects objects', () => { + types.array(new Object()).should.be.false; + }); + }); +}); diff --git a/test/nodeserver.test.ts b/test/nodeserver.test.ts deleted file mode 100644 index a15eee4..0000000 --- a/test/nodeserver.test.ts +++ /dev/null @@ -1,225 +0,0 @@ -/* tslint:disable:newline-per-chained-call */ -/* Chai assertions look silly when formatted multiline. */ -/* tslint:disable:no-unused-expression */ -/* Chai assertions do not require assignation. */ - -import * as chai from 'chai'; -const expect: Chai.ExpectStatic = chai.expect; - -// Chai-http must be imported this way: -// tslint:disable-next-line:no-var-requires no-require-imports -chai.use(require('chai-http')); - -// Mocha must be imported as a side-effect library: -// tslint:disable-next-line:no-import-side-effect -import 'mocha'; - -// Mocha-each must be imported using a require call: -// tslint:disable-next-line:no-require-imports -import forEach = require('mocha-each'); - -import { Template, alternative, array, exact, match, optional - } from '../src/matcher/options'; -import { default as types } from '../src/matcher/types'; - -import app from '../src/app'; - -const responseTemplate: Template = { - status: types.string, - data: optional(types.anything), - error: optional(types.string), - error_description: optional(types.string), - user_message: optional(types.string), - error_data: optional(types.anything) -}; - -describe('baseRoute', () => { - it('should be json', () => { - return chai.request(app).get('/') - .then((res: ChaiHttp.Response) => { - expect(res.type).to.eql('application/json'); - }); - }); - - 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!'); - }); - }); - 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/'); - }); - }); - -}); - -type MochaForEachInput = [ string, string, Template ]; - -describe('General protocol', () => { - const completeProtocol: MochaForEachInput[] = [ - ['get', '/_supported_protocols_', exact({ - supported: array(types.string), - required: array(types.string) - })], - ['get', '/log', types.array({ - component: types.string, - level: alternative([ - 'info', - 'security', - 'warning', - 'error', - 'critical' - ]), - value: types.anything, - username: types.string, - timestamp: types.string - })], - ['post', '/log', null], - ['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 - ]) - })], - ['post', '/properties', null], - ['get', '/user_privileges', array({ - privilege: types.string, - description: types.string, - internal: types.boolean - })], - ['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 - })], - // 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 - }], - ['post', '/users/:username', null], - ['get', '/users/:username/properties', { - data: optional(types.anything) - }], - ['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 - }], - ['post', '/current_user', null], - ['get', '/project_roles', array({ - role: types.string, - description: types.string, - internal: types.boolean - })], - ['get', '/projects', 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) - })], - ['get', '/projects/:project_name', { - 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) - }], - ['post', '/projects/:project_name', null], - ['get', '/projects/:project_name/properties', { - data: optional(types.anything) - }], - ['get', '/projects/:project_name/files/:path', null], - ['post', '/projects/:project_name/files/:id', null], - ['get', '/projects/:project_name/files_by_id/:id', null] - ]; - - describe('Checking complete Protocol', () => { - forEach(completeProtocol).it( - '%s %s should be json', - (method: string, path: string, temp: Template) => { - const request: ChaiHttp.Request = method === 'get' - ? chai.request(app).get(path) - : chai.request(app).post(path); - request.then((res: ChaiHttp.Response) => { - expect(res.type).to.equal('application/json'); - }); - }); - forEach(completeProtocol).it( - '%s %s should have a status 200', - (method: string, path: string, temp: Template) => { - const request: ChaiHttp.Request = method === 'get' - ? chai.request(app).get(path) - : chai.request(app).post(path); - request.then((res: ChaiHttp.Response) => { - expect(res).to.have.status(200); - }); - }); - forEach(completeProtocol).it( - '%s %s should conform to standard protocol', - (method: string, path: string, temp: Template) => { - const request: ChaiHttp.Request = method === 'get' - ? chai.request(app).get(path) - : chai.request(app).post(path); - request.then((res: ChaiHttp.Response) => { - match(responseTemplate, JSON.parse(res.body)).shoul.be.true; - }); - }); - forEach(completeProtocol).it( - 'the response for %s %s should comply with its own protocol', - (method: string, path: string, temp: Template) => { - if (method === 'get' && temp != null) { - return chai.request(app).get(path) - .then((res: ChaiHttp.Response) => { - match(temp)(JSON.parse(res.body)).should.be.true; - }); - } - }); - }); -}); diff --git a/test/protocol.node.test.ts b/test/protocol.node.test.ts new file mode 100644 index 0000000..1c89b9e --- /dev/null +++ b/test/protocol.node.test.ts @@ -0,0 +1,227 @@ +/* tslint:disable:newline-per-chained-call */ +/* Chai assertions look silly when formatted multiline. */ +/* tslint:disable:no-unused-expression */ +/* Chai assertions do not require assignation. */ + +import * as chai from 'chai'; +const expect: Chai.ExpectStatic = chai.expect; + +// Chai-http must be imported this way: +// tslint:disable-next-line:no-var-requires no-require-imports +chai.use(require('chai-http')); + +// Mocha must be imported as a side-effect library: +// tslint:disable-next-line:no-import-side-effect +import 'mocha'; + +// Mocha-each must be imported using a require call: +// tslint:disable-next-line:no-require-imports +import forEach = require('mocha-each'); + +import { Template, alternative, array, exact, match, optional + } from '../src/matcher/options'; +import { default as types } from '../src/matcher/types'; + +import * as App from '../src/app'; +let app = App.TestApp(); + + +const responseTemplate: Template = { + status: types.string, + data: optional(types.anything), + error: optional(types.string), + error_description: optional(types.string), + user_message: optional(types.string), + error_data: optional(types.anything) +}; + +describe('baseRoute', () => { + it('should be json', () => { + return chai.request(app).get('/') + .then((res: ChaiHttp.Response) => { + expect(res.type).to.eql('application/json'); + }); + }); + + 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!'); + }); + }); + 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/'); + }); + }); + +}); + +type MochaForEachInput = [ string, string, Template ]; + +describe('General protocol', () => { + const completeProtocol: MochaForEachInput[] = [ + ['get', '/_supported_protocols_', exact({ + supported: array(types.string), + required: array(types.string) + })], + ['get', '/log', types.array({ + component: types.string, + level: alternative([ + 'info', + 'security', + 'warning', + 'error', + 'critical' + ]), + value: types.anything, + username: types.string, + timestamp: types.string + })], + ['post', '/log', null], + ['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 + ]) + })], + ['post', '/properties', null], + ['get', '/user_privileges', array({ + privilege: types.string, + description: types.string, + internal: types.boolean + })], + ['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 + })], + // 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 + }], + ['post', '/users/:username', null], + ['get', '/users/:username/properties', { + data: optional(types.anything) + }], + ['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 + }], + ['post', '/current_user', null], + ['get', '/project_roles', array({ + role: types.string, + description: types.string, + internal: types.boolean + })], + ['get', '/projects', 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) + })], + ['get', '/projects/:project_name', { + 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) + }], + ['post', '/projects/:project_name', null], + ['get', '/projects/:project_name/properties', { + data: optional(types.anything) + }], + ['get', '/projects/:project_name/files/:path', null], + ['post', '/projects/:project_name/files/:id', null], + ['get', '/projects/:project_name/files_by_id/:id', null] + ]; + + describe('Checking complete Protocol', () => { + forEach(completeProtocol).it( + '%s %s should be json', + (method: string, path: string, temp: Template) => { + const request: ChaiHttp.Request = method === 'get' + ? chai.request(app).get(path) + : chai.request(app).post(path); + request.then((res: ChaiHttp.Response) => { + expect(res.type).to.equal('application/json'); + }); + }); + forEach(completeProtocol).it( + '%s %s should have a status 200', + (method: string, path: string, temp: Template) => { + const request: ChaiHttp.Request = method === 'get' + ? chai.request(app).get(path) + : chai.request(app).post(path); + request.then((res: ChaiHttp.Response) => { + expect(res).to.have.status(200); + }); + }); + forEach(completeProtocol).it( + '%s %s should conform to standard protocol', + (method: string, path: string, temp: Template) => { + const request: ChaiHttp.Request = method === 'get' + ? chai.request(app).get(path) + : chai.request(app).post(path); + request.then((res: ChaiHttp.Response) => { + match(responseTemplate, JSON.parse(res.body)).shoul.be.true; + }); + }); + forEach(completeProtocol).it( + 'the response for %s %s should comply with its own protocol', + (method: string, path: string, temp: Template) => { + if (method === 'get' && temp != null) { + return chai.request(app).get(path) + .then((res: ChaiHttp.Response) => { + match(temp)(JSON.parse(res.body)).should.be.true; + }); + } + }); + }); +}); -- cgit v1.2.1 From f66a9fa7f132f41b2e40029f0fb535521c3fb716 Mon Sep 17 00:00:00 2001 From: jw255 Date: Sat, 24 Feb 2018 17:13:43 +0000 Subject: restructuring of tests + yarn install script --- coverage/base.css | 212 +++ coverage/index.html | 158 ++ coverage/prettify.css | 1 + coverage/prettify.js | 1 + coverage/project-code/coverage/index.html | 106 ++ coverage/project-code/coverage/prettify.js.html | 68 + coverage/project-code/coverage/sorter.js.html | 539 ++++++ coverage/project-code/index.html | 93 + coverage/project-code/karma.conf.js.html | 101 ++ coverage/project-code/src/FileRouter.ts.html | 1769 ++++++++++++++++++++ coverage/project-code/src/app.ts.html | 197 +++ coverage/project-code/src/db/index.html | 106 ++ .../project-code/src/db/initPostgresDB.ts.html | 110 ++ .../src/db/model/ContributorGroup.ts.html | 227 +++ coverage/project-code/src/db/model/File.ts.html | 206 +++ coverage/project-code/src/db/model/Project.ts.html | 290 ++++ coverage/project-code/src/db/model/User.ts.html | 299 ++++ .../project-code/src/db/model/UserGroup.ts.html | 221 +++ .../src/db/model/UserJoinsProject.ts.html | 146 ++ coverage/project-code/src/db/model/index.html | 158 ++ coverage/project-code/src/db/orm.ts.html | 101 ++ coverage/project-code/src/index.html | 93 + coverage/project-code/src/index.ts.html | 227 +++ coverage/project-code/src/matcher/index.html | 106 ++ coverage/project-code/src/matcher/options.ts.html | 362 ++++ .../src/matcher/options.unit.test.ts.html | 554 ++++++ coverage/project-code/src/matcher/types.ts.html | 302 ++++ .../src/matcher/types.unit.test.ts.html | 599 +++++++ coverage/project-code/src/serverConfig.ts.html | 473 ++++++ coverage/project-code/test/hm.test.ts.html | 113 ++ coverage/project-code/test/index.html | 93 + .../project-code/test/protocol.node.test.ts.html | 746 +++++++++ coverage/sort-arrow-sprite.png | Bin 0 -> 209 bytes coverage/sorter.js | 158 ++ getmeyarn.sh | 30 + package.json | 6 +- src/matcher/options.unit.test.ts | 163 -- src/matcher/types.unit.test.ts | 178 -- test/hm.test.ts | 16 - test/integration/auth.routes.test.ts | 42 + test/integration/index.routes.test.ts | 42 + test/integration/protocol.routes.test.ts | 204 +++ test/mocha.opts | 4 + test/protocol.node.test.ts | 227 --- test/unit/options.unit.test.ts | 163 ++ test/unit/types.unit.test.ts | 178 ++ yarn.lock | 259 ++- 47 files changed, 9730 insertions(+), 717 deletions(-) create mode 100644 coverage/base.css create mode 100644 coverage/index.html create mode 100644 coverage/prettify.css create mode 100644 coverage/prettify.js create mode 100644 coverage/project-code/coverage/index.html create mode 100644 coverage/project-code/coverage/prettify.js.html create mode 100644 coverage/project-code/coverage/sorter.js.html create mode 100644 coverage/project-code/index.html create mode 100644 coverage/project-code/karma.conf.js.html create mode 100644 coverage/project-code/src/FileRouter.ts.html create mode 100644 coverage/project-code/src/app.ts.html create mode 100644 coverage/project-code/src/db/index.html create mode 100644 coverage/project-code/src/db/initPostgresDB.ts.html create mode 100644 coverage/project-code/src/db/model/ContributorGroup.ts.html create mode 100644 coverage/project-code/src/db/model/File.ts.html create mode 100644 coverage/project-code/src/db/model/Project.ts.html create mode 100644 coverage/project-code/src/db/model/User.ts.html create mode 100644 coverage/project-code/src/db/model/UserGroup.ts.html create mode 100644 coverage/project-code/src/db/model/UserJoinsProject.ts.html create mode 100644 coverage/project-code/src/db/model/index.html create mode 100644 coverage/project-code/src/db/orm.ts.html create mode 100644 coverage/project-code/src/index.html create mode 100644 coverage/project-code/src/index.ts.html create mode 100644 coverage/project-code/src/matcher/index.html create mode 100644 coverage/project-code/src/matcher/options.ts.html create mode 100644 coverage/project-code/src/matcher/options.unit.test.ts.html create mode 100644 coverage/project-code/src/matcher/types.ts.html create mode 100644 coverage/project-code/src/matcher/types.unit.test.ts.html create mode 100644 coverage/project-code/src/serverConfig.ts.html create mode 100644 coverage/project-code/test/hm.test.ts.html create mode 100644 coverage/project-code/test/index.html create mode 100644 coverage/project-code/test/protocol.node.test.ts.html create mode 100644 coverage/sort-arrow-sprite.png create mode 100644 coverage/sorter.js create mode 100755 getmeyarn.sh delete mode 100644 src/matcher/options.unit.test.ts delete mode 100644 src/matcher/types.unit.test.ts delete mode 100644 test/hm.test.ts create mode 100644 test/integration/auth.routes.test.ts create mode 100644 test/integration/index.routes.test.ts create mode 100644 test/integration/protocol.routes.test.ts create mode 100644 test/mocha.opts delete mode 100644 test/protocol.node.test.ts create mode 100644 test/unit/options.unit.test.ts create mode 100644 test/unit/types.unit.test.ts diff --git a/coverage/base.css b/coverage/base.css new file mode 100644 index 0000000..417c7ad --- /dev/null +++ b/coverage/base.css @@ -0,0 +1,212 @@ +body, html { + margin:0; padding: 0; + height: 100%; +} +body { + font-family: Helvetica Neue, Helvetica, Arial; + font-size: 14px; + color:#333; +} +.small { font-size: 12px; } +*, *:after, *:before { + -webkit-box-sizing:border-box; + -moz-box-sizing:border-box; + box-sizing:border-box; + } +h1 { font-size: 20px; margin: 0;} +h2 { font-size: 14px; } +pre { + font: 12px/1.4 Consolas, "Liberation Mono", Menlo, Courier, monospace; + margin: 0; + padding: 0; + -moz-tab-size: 2; + -o-tab-size: 2; + tab-size: 2; +} +a { color:#0074D9; text-decoration:none; } +a:hover { text-decoration:underline; } +.strong { font-weight: bold; } +.space-top1 { padding: 10px 0 0 0; } +.pad2y { padding: 20px 0; } +.pad1y { padding: 10px 0; } +.pad2x { padding: 0 20px; } +.pad2 { padding: 20px; } +.pad1 { padding: 10px; } +.space-left2 { padding-left:55px; } +.space-right2 { padding-right:20px; } +.center { text-align:center; } +.clearfix { display:block; } +.clearfix:after { + content:''; + display:block; + height:0; + clear:both; + visibility:hidden; + } +.fl { float: left; } +@media only screen and (max-width:640px) { + .col3 { width:100%; max-width:100%; } + .hide-mobile { display:none!important; } +} + +.quiet { + color: #7f7f7f; + color: rgba(0,0,0,0.5); +} +.quiet a { opacity: 0.7; } + +.fraction { + font-family: Consolas, 'Liberation Mono', Menlo, Courier, monospace; + font-size: 10px; + color: #555; + background: #E8E8E8; + padding: 4px 5px; + border-radius: 3px; + vertical-align: middle; +} + +div.path a:link, div.path a:visited { color: #333; } +table.coverage { + border-collapse: collapse; + margin: 10px 0 0 0; + padding: 0; +} + +table.coverage td { + margin: 0; + padding: 0; + vertical-align: top; +} +table.coverage td.line-count { + text-align: right; + padding: 0 5px 0 20px; +} +table.coverage td.line-coverage { + text-align: right; + padding-right: 10px; + min-width:20px; +} + +table.coverage td span.cline-any { + display: inline-block; + padding: 0 5px; + width: 100%; +} +.missing-if-branch { + display: inline-block; + margin-right: 5px; + border-radius: 3px; + position: relative; + padding: 0 4px; + background: #333; + color: yellow; +} + +.skip-if-branch { + display: none; + margin-right: 10px; + position: relative; + padding: 0 4px; + background: #ccc; + color: white; +} +.missing-if-branch .typ, .skip-if-branch .typ { + color: inherit !important; +} +.coverage-summary { + border-collapse: collapse; + width: 100%; +} +.coverage-summary tr { border-bottom: 1px solid #bbb; } +.keyline-all { border: 1px solid #ddd; } +.coverage-summary td, .coverage-summary th { padding: 10px; } +.coverage-summary tbody { border: 1px solid #bbb; } +.coverage-summary td { border-right: 1px solid #bbb; } +.coverage-summary td:last-child { border-right: none; } +.coverage-summary th { + text-align: left; + font-weight: normal; + white-space: nowrap; +} +.coverage-summary th.file { border-right: none !important; } +.coverage-summary th.pct { } +.coverage-summary th.pic, +.coverage-summary th.abs, +.coverage-summary td.pct, +.coverage-summary td.abs { text-align: right; } +.coverage-summary td.file { white-space: nowrap; } +.coverage-summary td.pic { min-width: 120px !important; } +.coverage-summary tfoot td { } + +.coverage-summary .sorter { + height: 10px; + width: 7px; + display: inline-block; + margin-left: 0.5em; + background: url(sort-arrow-sprite.png) no-repeat scroll 0 0 transparent; +} +.coverage-summary .sorted .sorter { + background-position: 0 -20px; +} +.coverage-summary .sorted-desc .sorter { + background-position: 0 -10px; +} +.status-line { height: 10px; } +/* dark red */ +.red.solid, .status-line.low, .low .cover-fill { background:#C21F39 } +.low .chart { border:1px solid #C21F39 } +/* medium red */ +.cstat-no, .fstat-no, .cbranch-no, .cbranch-no { background:#F6C6CE } +/* light red */ +.low, .cline-no { background:#FCE1E5 } +/* light green */ +.high, .cline-yes { background:rgb(230,245,208) } +/* medium green */ +.cstat-yes { background:rgb(161,215,106) } +/* dark green */ +.status-line.high, .high .cover-fill { background:rgb(77,146,33) } +.high .chart { border:1px solid rgb(77,146,33) } + + +.medium .chart { border:1px solid #666; } +.medium .cover-fill { background: #666; } + +.cbranch-no { background: yellow !important; color: #111; } + +.cstat-skip { background: #ddd; color: #111; } +.fstat-skip { background: #ddd; color: #111 !important; } +.cbranch-skip { background: #ddd !important; color: #111; } + +span.cline-neutral { background: #eaeaea; } +.medium { background: #eaeaea; } + +.cover-fill, .cover-empty { + display:inline-block; + height: 12px; +} +.chart { + line-height: 0; +} +.cover-empty { + background: white; +} +.cover-full { + border-right: none !important; +} +pre.prettyprint { + border: none !important; + padding: 0 !important; + margin: 0 !important; +} +.com { color: #999 !important; } +.ignore-none { color: #999; font-weight: normal; } + +.wrapper { + min-height: 100%; + height: auto !important; + height: 100%; + margin: 0 auto -48px; +} +.footer, .push { + height: 48px; +} diff --git a/coverage/index.html b/coverage/index.html new file mode 100644 index 0000000..10b3d1c --- /dev/null +++ b/coverage/index.html @@ -0,0 +1,158 @@ + + +
+| File | ++ | Statements | ++ | Branches | ++ | Functions | ++ | Lines | ++ |
|---|---|---|---|---|---|---|---|---|---|
| project-code | +0% | +0/2 | +100% | +0/0 | +0% | +0/1 | +0% | +0/2 | +|
| project-code/coverage | +0% | +0/579 | +0% | +0/306 | +0% | +0/46 | +0% | +0/78 | +|
| project-code/src | +0% | +0/16 | +0% | +0/4 | +0% | +0/2 | +0% | +0/16 | +|
| project-code/src/db | +0% | +0/3 | +100% | +0/0 | +0% | +0/2 | +0% | +0/3 | +|
| project-code/src/matcher | +0% | +0/169 | +100% | +0/0 | +0% | +0/79 | +0% | +0/169 | +|
| project-code/test | +0% | +0/3 | +100% | +0/0 | +0% | +0/2 | +0% | +0/3 | +