diff options
| author | Tom Harley | 2018-03-27 19:19:16 +0100 |
|---|---|---|
| committer | Tom Harley | 2018-03-27 19:19:16 +0100 |
| commit | 150115e855e2c406b23aa569dd675597d6957570 (patch) | |
| tree | 7cb6cbbc7e7f2911596bb1f03dbcedd8b10e0e49 | |
| parent | Merge branch 'cjd24/conversion' into development (diff) | |
| parent | Centralise test configuration and introduce SQLite (diff) | |
| download | medicine-150115e855e2c406b23aa569dd675597d6957570.tar.gz medicine-150115e855e2c406b23aa569dd675597d6957570.zip | |
Merge branch ‘testing-middleware’ into development
| -rw-r--r-- | package.json | 4 | ||||
| -rw-r--r-- | src/FileRouter.ts | 2 | ||||
| -rw-r--r-- | src/app.ts | 9 | ||||
| -rw-r--r-- | src/auth.ts | 27 | ||||
| -rw-r--r-- | src/db/initPostgresDB.ts | 2 | ||||
| -rw-r--r-- | test/integration/auth.routes.test.ts | 173 | ||||
| -rw-r--r-- | test/integration/index.routes.test.ts | 79 | ||||
| -rw-r--r-- | test/integration/protocol.routes.test.ts | 167 | ||||
| -rw-r--r-- | test/test-db.ts | 41 | ||||
| -rw-r--r-- | yarn.lock | 137 |
10 files changed, 410 insertions, 231 deletions
diff --git a/package.json b/package.json index 27b034e..ea9f5b7 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "test": "mocha --reporter spec -r ts-node/register './test/**/*.test.ts'", "test:routes": "mocha --reporter spec -r ts-node/register './test/**/*.routes.test.ts'", "test:unit": "mocha --reporter spec -r ts-node/register './test/**/*.unit.test.ts'", - "test:db": "mocha --reporter spec -r ts-node/register './test/**/*database.unit.test.ts'", + "test:routes:db": "mocha --reporter spec -r ts-node/register './test/**/*database.unit.test.ts'", "test:routes:auth": "mocha --reporter spec -r ts-node/register './test/**/*auth.routes.test.ts'", "test:routes:index": "mocha --reporter spec -r ts-node/register './test/**/*index.routes.test.ts'", "test:routes:protocol": "mocha --reporter spec -r ts-node/register './test/**/*protocol.routes.test.ts'", @@ -53,6 +53,7 @@ "@types/passport": "^0.4.2", "@types/passport-local": "^1.0.32", "@types/sequelize": "^4.27.5", + "@types/sqlite3": "^3.1.1", "@types/supertest": "^2.0.4", "@types/sharp": "^0.17.6", "@types/uuid": "^3.4.3", @@ -74,6 +75,7 @@ "reflect-metadata": "^0.1.12", "sequelize": "^4.32.7", "sequelize-typescript": "^0.6.2", + "sqlite3": "^4.0.0", "supertest": "^3.0.0", "sharp": "^0.20.0", "ts-node": "^3.3.0", diff --git a/src/FileRouter.ts b/src/FileRouter.ts index f8f569d..b3b1a38 100644 --- a/src/FileRouter.ts +++ b/src/FileRouter.ts @@ -19,7 +19,7 @@ const BASE_FILE_STORAGE: string = 'TODO: figure this out'; /** * The type of an Express middleware callback. */ -type Middleware = +export type Middleware = (req: Request, res: Response, next: NextFunction) => void; /** @@ -128,8 +128,7 @@ class App { } }); if (user === null) { - next(new RequestError(404, 'user_not_found')); - return; + return next(new RequestError(404, 'user_not_found')); } else { res.locals.user = user; } @@ -176,8 +175,7 @@ class App { } }); if (fileOrNull === null) { - next(); - return; + return next(); } else { file = fileOrNull; } @@ -193,8 +191,7 @@ class App { } }); if (parentFolder === null) { - next(); - return; + return next(); } res.locals.parentFolder = parentFolder; const tFile: File | undefined = parentFolder.containedFiles.find( diff --git a/src/auth.ts b/src/auth.ts index 57c375a..5395728 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -1,5 +1,8 @@ -import { RequestError } from './errors/errorware'; +/* tslint:disable:prefer-function-over-method */ + +import { Errorware, RequestError } from './errors/errorware'; import { NextFunction, Request, Response, Router } from 'express'; +import { Middleware } from './FileRouter'; import * as jwt from 'jsonwebtoken'; import * as passport from 'passport'; import { Strategy } from 'passport-local'; @@ -7,8 +10,6 @@ import { default as Project } from './db/model/Project'; import { default as User } from './db/model/User'; import { default as UserGroup } from './db/model/UserGroup'; -import { Errorware } from './errors/errorware'; - /** * The module for system authentication. */ @@ -98,12 +99,14 @@ export class AuthRouter { */ public checkErr(req: Request, res: Response, next: NextFunction): void { if (!req.body.grant_type || !req.body.username || !req.body.password) { - next(new RequestError(400, 'invalid_request', 'Missing parameters')); + return next(new RequestError(400, 'invalid_request', 'Missing parameters')); } if (req.body.grant_type !== 'refresh_token' && req.body.grant_type !== 'password') { - next(new RequestError(400, 'unsupported_grant_type', 'invalid grant type')); + return next(new RequestError( + 400, 'unsupported_grant_type', 'invalid grant type' + )); } next(); @@ -135,7 +138,12 @@ export class AuthRouter { public init(): void { this.configStrategy(); - this.router.post('/oauth/token', this.checkErr, this.authenticate, this.genToken); + this.router.post( + '/oauth/token', + this.checkErr, + this.authenticate, + this.genToken + ); this.router.get( '/logout', (req: Request, res: Response): void => { req.logout(); } @@ -173,11 +181,12 @@ export const unauthorisedErr: Errorware = * @param res The Express http response. * @param next The next Express middleware function. */ -export const isAdmin: any = +export const isAdmin: Middleware = (req: Request, res: Response, next: NextFunction): void => { - let isAdmin: boolean = req.user.object.userGroups.some((x: UserGroup): boolean => x.name === 'admin'); + const admin: boolean = req.user.object.userGroups + .some((x: UserGroup): boolean => x.name === 'admin'); - if (!isAdmin) { + if (!admin) { next(new RequestError( 401, 'not_authorised', 'user is not authorised to perform task' diff --git a/src/db/initPostgresDB.ts b/src/db/initPostgresDB.ts index b6844d9..eb979f4 100644 --- a/src/db/initPostgresDB.ts +++ b/src/db/initPostgresDB.ts @@ -1,6 +1,6 @@ import * as Sequelize from 'sequelize'; const gres: Sequelize.Sequelize = new Sequelize( - 'postgres://admin:eprprJacR0hBpmWvs5IDJZTnjRAY2gM3tSm0b1af@postgres:5432/be4', + 'postgres://admin:eprprJacR0hBpmWvs5IDJZTnjRAY2gM3tSm0b1af@localhost:5432/be4', { operatorsAliases : false } diff --git a/test/integration/auth.routes.test.ts b/test/integration/auth.routes.test.ts index 888883f..e3f8113 100644 --- a/test/integration/auth.routes.test.ts +++ b/test/integration/auth.routes.test.ts @@ -2,10 +2,12 @@ /* Chai assertions look silly when formatted multiline. */ /* tslint:disable:no-unused-expression */ /* Chai assertions do not require assignation. */ +/* tslint:disable:typedef */ +/* Mocha tests do not require strong typing. */ import * as chai from 'chai'; const expect: Chai.ExpectStatic = chai.expect; -import { default as seq } from '../../src/db/orm'; +import { Credentials, Database, addUser, initDB, resetDB } from '../test-db'; import { default as User } from '../../src/db/model/User'; // Chai-http must be imported this way: @@ -19,135 +21,158 @@ import 'mocha'; import * as App from '../../src/app'; const app = App.TestApp(); -const badRequestErr = - { status: 'error', - error: 'invalid_request', - error_description: 'Missing parameters' }; -const unsupportedGrantTypeErr = - { status: 'error', - error: 'unsupported_grant_type', - error_description: 'invalid grant type' } -const authorisationErr = - { status: 'error', - error: 'not_authorised', - error_description: 'user does not have correct authorisation for task' } - +let database: Database; +let mockUser: Credentials; +let token: string; -const bobbyCredentials = { - username: 'bobby', - password: 'tables', - grant_type: 'password' +const initDatabase = async() => { + database = initDB(); + await database.authenticate(); }; -let bobbyToken: string; +const populateDatabase = async() => { + await resetDB(database); + mockUser = await addUser(database); +}; -async function resetDatabase(done: any){ - await seq.sync({ - force: true +const getToken = async() => { + return chai.request(app) + .post('/cs3099group-be-4/login') + .send({ + username: mockUser.username, + password: mockUser.password, + grant_type: 'password' + }) + .then((res) => { + token = res.body.access_token; }); - const u: string = bobbyCredentials.username - const p: string = bobbyCredentials.password +}; - const newUser: User = new User({ - username: u, - password: p, - userGroups: [] - }); - await newUser.save(); - done(); -} +const badRequestErr = { + status: 'error', + error: 'invalid_request', + error_description: 'Missing parameters' +}; -async function setupDatabase(done: any) { - await seq.authenticate(); - await seq.sync({ - force: true - }); - done(); -} +const unsupportedGrantTypeErr = { + status: 'error', + error: 'unsupported_grant_type', + error_description: 'invalid grant type' +}; -// Add user -before((done: any) => { - setupDatabase(done); -}); +const authorisationErr = { + status: 'error', + error: 'not_authorised', + error_description: 'user does not have correct authorisation for task' +}; describe('authentication', () => { + before(initDatabase); context('invalid password authentication', () => { + before(populateDatabase); + it('should reject login to invalid endpoints', () => { + return chai.request(app) + .post('/') + .send({ + username: mockUser.username, + password: mockUser.password, + grant_type: 'password' + }) + .then((res) => { + expect(res).to.have.status(400); + }); + }); it('should reject invalid usernames', () => { - chai.request(app) + return chai.request(app) .post('/cs3099group-be-4/login') - .send({...bobbyCredentials, username: 'invalid'}) + .send({ + username: 'invalid', + password: mockUser.password, + grant_type: 'password' + }) .then((res) => { expect(res).to.have.status(401); }); }); it('should reject invalid passwords', () => { - chai.request(app) + return chai.request(app) .post('/cs3099group-be-4/login') - .send({...bobbyCredentials, password: 'asasasas'}) + .send({ + username: mockUser.username, + password: 'asasasas', + grant_type: 'password' + }) .then((res) => { expect(res).to.have.status(401); }); }); it('should reject invalid grant_type', () => { - chai.request(app) + return chai.request(app) .post('/cs3099group-be-4/login') - .send({...bobbyCredentials, grant_type: 'invalid'}) + .send({ + username: mockUser.username, + password: mockUser.password, + grant_type: 'invalid' + }) .catch((reason) => { expect(reason.response.body).to.eql(unsupportedGrantTypeErr); }); }); it('should reject empty usernames', () => { - chai.request(app) + return chai.request(app) .post('/cs3099group-be-4/login') - .send({...bobbyCredentials, username: ''}) + .send({ + username: '', + password: mockUser.password, + grant_type: 'password' + }) .catch((reason) => { expect(reason.response.body).to.eql(badRequestErr); }); }); it('should reject empty passwords', () => { - chai.request(app) + return chai.request(app) .post('/cs3099group-be-4/login') - .send({...bobbyCredentials, password: ''}) + .send({ + username: mockUser.username, + password: '', + grant_type: 'password' + }) .catch((reason) => { expect(reason.response.body).to.eql(badRequestErr); }); }); it('should reject empty grant_type', () => { - chai.request(app) + return chai.request(app) .post('/cs3099group-be-4/login') - .send({...bobbyCredentials, grant_type: ''}) + .send({ + username: mockUser.username, + password: mockUser.password, + grant_type: '' + }) .catch((reason) => { expect(reason.response.body).to.eql(badRequestErr); }); }); }); context('valid password authentication', () => { - before((done: any) => { - resetDatabase(done); - }); + before(populateDatabase); it('should accept correct credentials', () => { - chai.request(app) + return chai.request(app) .post('/cs3099group-be-4/login') - .send(bobbyCredentials) + .send({ + username: mockUser.username, + password: mockUser.password, + grant_type: 'password' + }) .then((res) => { expect(res).to.have.status(200); }); }); }); context('token authentication', () => { - let bobbyToken: string; - before((done: any) => { - resetDatabase(() => { - chai.request(app) - .post('/cs3099group-be-4/login') - .send(bobbyCredentials) - .end((err, res) => { - if (err) { throw err; } - bobbyToken = res.body.access_token; - done(); - }); - }); - }); + beforeEach(populateDatabase); + beforeEach(getToken); it('should reject invalid tokens', () => { return chai.request(app).get('/') .set('Authorization', 'Bearer ' + 'invalid') @@ -159,7 +184,7 @@ describe('authentication', () => { }); it('should accept valid tokens', () => { return chai.request(app).get('/') - .set('Authorization', 'Bearer ' + bobbyToken) + .set('Authorization', `Bearer ${token}`) .then((res) => { expect(res).to.have.status(200); }); diff --git a/test/integration/index.routes.test.ts b/test/integration/index.routes.test.ts index 70671d8..aa30586 100644 --- a/test/integration/index.routes.test.ts +++ b/test/integration/index.routes.test.ts @@ -2,12 +2,12 @@ /* Chai assertions look silly when formatted multiline. */ /* tslint:disable:no-unused-expression */ /* Chai assertions do not require assignation. */ +/* tslint:disable:typedef */ +/* Mocha tests do not require strong typing. */ import * as chai from 'chai'; const expect: Chai.ExpectStatic = chai.expect; -const should = chai.should(); -import * as ex from 'express'; -import { default as seq } from '../../src/db/orm'; +import { Credentials, Database, addUser, initDB, resetDB } from '../test-db'; import { default as User } from '../../src/db/model/User'; // Chai-http must be imported this way: @@ -19,66 +19,58 @@ chai.use(require('chai-http')); import 'mocha'; import * as App from '../../src/app'; -const app: ex.Express = App.TestApp(); - -const userCredentials: Object = { - username: 'bobby', - password: 'pass', - grant_type: 'password' -}; +const app = App.TestApp(); +let database: Database; +let mockUser: Credentials; let token: string; -async function startDatabase(done: any) { - await seq.authenticate(); - await seq.sync({ - force: true - }); +const initDatabase = async() => { + database = initDB(); + await database.authenticate(); +}; - const newUser: User = new User({ - username: 'bobby', - password: 'pass', - userGroups: [] - }); - await newUser.save(); - done(); -} +const populateDatabase = async() => { + await resetDB(database); + mockUser = await addUser(database); +}; -// Add user -before(function(done: any) { - startDatabase(done); -}); +const getToken = async() => { + return chai.request(app) + .post('/cs3099group-be-4/login') + .send({ + username: mockUser.username, + password: mockUser.password, + grant_type: 'password' + }) + .then((res) => { + token = res.body.access_token; + }); +}; describe('routes : index', () => { + before(initDatabase); describe('GET /', () => { + before(populateDatabase); it('should return 401 without authentication', () => { chai.request(app).get('/') - .catch(function(err: any) { + .catch((err) => { return expect(err).to.have.status(401); }); }); context('with authentication', () => { - before(function(done) { - chai.request(app) - .post('/cs3099group-be-4/login') - .send(userCredentials) - .end(function(err, res) { - if (err) { throw err; } - token = res.body.access_token; - done(); - }); - }); + beforeEach(getToken); it('should return 200', () => { return chai.request(app).get('/') - .set('Authorization', 'Bearer ' + token) + .set('Authorization', `Bearer ${token}`) .then((res: ChaiHttp.Response) => { - res.status.should.eql(200); + expect(res).to.have.status(200); }); }); it('should return json', () => { return chai.request(app).get('/') - .set('Authorization', 'Bearer ' + token) + .set('Authorization', `Bearer ${token}`) .then((res: ChaiHttp.Response) => { expect(res.type).to.eql('application/json'); }); @@ -86,7 +78,7 @@ describe('routes : index', () => { it('should return a message prop', () => { return chai.request(app).get('/') - .set('Authorization', 'Bearer ' + token) + .set('Authorization', `Bearer ${token}`) .then((res: ChaiHttp.Response) => { expect(res.body.message).to.eql('Welcome to the CS3099 BE4 server!'); }); @@ -94,7 +86,7 @@ describe('routes : index', () => { it('should return an important prop', () => { return chai.request(app).get('/') - .set('Authorization', 'Bearer ' + token) + .set('Authorization', `Bearer ${token}`) .then((res: ChaiHttp.Response) => { expect(res.body.important) .to.eql('Endpoints start from /cs3099group-be-4/'); @@ -103,5 +95,4 @@ describe('routes : index', () => { }); }); - }); diff --git a/test/integration/protocol.routes.test.ts b/test/integration/protocol.routes.test.ts index 863bec9..a08f977 100644 --- a/test/integration/protocol.routes.test.ts +++ b/test/integration/protocol.routes.test.ts @@ -1,19 +1,60 @@ +/* tslint:disable:newline-per-chained-call */ +/* Chai assertions look silly when formatted multiline. */ +/* tslint:disable:no-unused-expression */ +/* Chai assertions do not require assignation. */ +/* tslint:disable:typedef */ +/* Mocha tests do not require strong typing. */ + import * as chai from 'chai'; -import * as mocha from 'mocha'; const expect: Chai.ExpectStatic = chai.expect; +import { Credentials, Database, addUser, initDB, resetDB } from '../test-db'; +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 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: +// tslint:disable-next-line:no-require-imports import forEach = require('mocha-each'); import * as App from '../../src/app'; -import * as ex from 'express'; +const app = App.TestApp(); + +let database: Database; +let mockUser: Credentials; +let token: string; + +const initDatabase = async() => { + database = initDB(); + await database.authenticate(); +}; + +const populateDatabase = async() => { + await resetDB(database); + mockUser = await addUser(database); +}; + +const getToken = async() => { + return chai.request(app) + .post('/cs3099group-be-4/login') + .send({ + username: mockUser.username, + password: mockUser.password, + grant_type: 'password' + }) + .then((res) => { + token = res.body.access_token; + }); +}; + import { Template, alternative, array, exact, match, optional } from '../../src/matcher/options'; -import { default as seq } from '../../src/db/orm'; import { default as types } from '../../src/matcher/types'; -import { default as User } from '../../src/db/model/User'; -const app = App.TestApp(); // The general response template according to protocol const responseTemplate: Template = { @@ -36,78 +77,32 @@ const errorResponseTemplate: Template = { type MochaForEachInput = [ string, string, Template, number ]; -const bobbyCredentials = { - username: 'bobby', - password: 'tables', - grant_type: 'password' -}; - -let bobbyToken: string; - -async function resetDatabase(done: any){ - await seq.sync({ - force: true - }); - const u: string = bobbyCredentials.username - const p: string = bobbyCredentials.password - - const newUser: User = new User({ - username: u, - password: p, - userGroups: [] - }); - await newUser.save(); - done(); -} - -async function setupDatabase(done: any) { - await seq.authenticate(); - await seq.sync({ - force: true - }); - done(); -} - -// Add user -before((done: any) => { - setupDatabase(() => { - resetDatabase(() => { - chai.request(app) - .post('/cs3099group-be-4/login') - .send(bobbyCredentials) - .end((err, res) => { - if (err) { throw err; } - bobbyToken = res.body.access_token; - done(); - }); - }); - }); - -}); - describe('routes : errors', () => { - describe('GET invalid routes', function() { + before(initDatabase); + describe('GET invalid routes', () => { it('should have response code 404', () => { return chai.request(app).get('/invalid/route') - .set('Authorization', 'Bearer ' + bobbyToken) - .catch(function(err: any) { + .set('Authorization', `Bearer ${token}`) + .catch((err) => { expect(err.status).to.equal(404); }); }); - it('should conform to the error protocol', function() { + it('should conform to the error protocol', () => { return chai.request(app).get('/invalid/route') - .set('Authorization', 'Bearer ' + bobbyToken) - .catch(function(err: any) { - match(errorResponseTemplate)(err.response.body).should.be.true; + .set('Authorization', `Bearer ${token}`) + .catch((err) => { + expect(match(errorResponseTemplate)(err.response.body)).to.be.true; }); }); }); - }); describe('routes : protocol', () => { const base: string = '/cs3099group-be-4'; - // format: + + /* tslint:disable:align */ + + // Format: // <method> <route> <json-response-template> <response-code> const completeProtocol: MochaForEachInput[] = [ ['get', '/_supported_protocols_', exact({ @@ -166,7 +161,7 @@ describe('routes : protocol', () => { public_admin_metadata: types.anything, private_admin_metadata: types.anything }), 401], - // todo types.metadata + // TODO types.metadata ['get', '/users/:username', { username: types.string, privileges: array(types.string), @@ -230,55 +225,63 @@ describe('routes : protocol', () => { ['get', '/projects/:project_name/files_by_id/:id', null, 200] ]; + /* tslint:enable:align */ + + before(initDatabase); + beforeEach(populateDatabase); + beforeEach(getToken); + describe('Access all valid routes', () => { forEach(completeProtocol).it( '%s %s should be json', - (method: string, path: string, temp: Template, res_code: number) => { + (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); - request.set('Authorization', 'Bearer ' + bobbyToken); + request.set('Authorization', `Bearer ${token}`); return request.then((res: ChaiHttp.Response) => { expect(res.type).to.equal('application/json'); - }).catch(function(err: any) { - match(errorResponseTemplate)(err.response.body).should.be.true; + }).catch((err) => { + expect(match(errorResponseTemplate)(err.response.body)).to.be.true; }); }); forEach(completeProtocol).it( '%s %s should have a status %4$d', - (method: string, path: string, temp: Template, res_code: number) => { + (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); - request.set('Authorization', 'Bearer ' + bobbyToken); + request.set('Authorization', `Bearer ${token}`); return request.then((res: ChaiHttp.Response) => { - expect(res).to.have.status(res_code); - }).catch(function(err: any) { - match(errorResponseTemplate)(err.response.body).should.be.true; + expect(res).to.have.status(resCode); + }).catch((err) => { + expect(match(errorResponseTemplate)(err.response.body)).to.be.true; }); }); forEach(completeProtocol).it( '%s %s should conform to the general response protocol', - (method: string, path: string, temp: Template, res_code: number) => { + (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); - request.set('Authorization', 'Bearer ' + bobbyToken); + request.set('Authorization', `Bearer ${token}`); return request.then((res: ChaiHttp.Response) => { match(responseTemplate, JSON.parse(res.body)).shoul.be.true; - }).catch(function(err: any) { - match(errorResponseTemplate)(err.response.body).should.be.true; + }).catch((err) => { + expect(match(errorResponseTemplate)(err.response.body)).to.be.true; }); }); forEach(completeProtocol).it( 'the response for %s %s should conform to its specific response protocol', - (method: string, path: string, temp: Template, res_code: number) => { + (method: string, path: string, temp: Template, resCode: number) => { if (method === 'get' && temp != null) { - return chai.request(app).get(base + path).set('Authorization', 'Bearer ' + bobbyToken) + return chai.request(app) + .get(base + path) + .set('Authorization', `Bearer ${token}`) .then((res: ChaiHttp.Response) => { - match(temp)(JSON.parse(res.body)).should.be.true; - }).catch(function(err: any) { - match(errorResponseTemplate)(err.response.body).should.be.true; + expect(match(temp)(JSON.parse(res.body))).to.be.true; + }).catch((err) => { + expect(match(errorResponseTemplate)(err.response.body)).to.be.true; }); } else { return true; diff --git a/test/test-db.ts b/test/test-db.ts new file mode 100644 index 0000000..fe8ae4d --- /dev/null +++ b/test/test-db.ts @@ -0,0 +1,41 @@ +import { default as User } from '../src/db/model/User'; +import { default as UserGroup } from '../src/db/model/UserGroup'; + +import { Sequelize } from 'sequelize-typescript'; +import * as sqlite from 'sqlite3'; + +export type Database = Sequelize; + +export const initDB: () => Database = (): Database => { + const dataFile: string = ':memory:'; + const db: sqlite.Database = new sqlite.Database(dataFile); + const seq: Database = new Sequelize(`sqlite:${dataFile}`); + seq.addModels([`${__dirname}/../src/db/model`]); + seq.options.logging = false; + return seq; +}; + +export const resetDB: (database: Database) => Promise<void> = async( + database: Database +): Promise<void> => { + await database.sync({ + force: true + }); +}; + +export interface Credentials { + username: string; + password: string; + usergroups: UserGroup[]; +} + +export const addUser: (database?: Database) => Promise<Credentials> = async( +): Promise<Credentials> => { + const mockUser: Credentials = { + username: 'mock_user', + password: 'mock_password', + usergroups: [] + }; + await new User(mockUser).save(); + return mockUser; +}; @@ -227,6 +227,12 @@ "@types/glob" "*" "@types/node" "*" +"@types/sqlite3@^3.1.1": + version "3.1.1" + resolved "https://registry.yarnpkg.com/@types/sqlite3/-/sqlite3-3.1.1.tgz#f9b707c574e8d2f73d6c41aaa86e28b6ef85a324" + dependencies: + "@types/node" "*" + "@types/superagent@*": version "3.5.7" resolved "https://registry.yarnpkg.com/@types/superagent/-/superagent-3.5.7.tgz#c170633e7dff01425b4712bb3afac3a12be49b7b" @@ -259,6 +265,10 @@ version "1.2.29" resolved "https://registry.yarnpkg.com/@types/xml-parser/-/xml-parser-1.2.29.tgz#6ba14c2916e52baa4fd8b7cb93b10a7ca55ff9f7" +abbrev@1: + version "1.1.1" + resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.1.tgz#f8f2c887ad10bf67f634f005b6987fed3179aac8" + accepts@~1.3.4: version "1.3.5" resolved "https://registry.yarnpkg.com/accepts/-/accepts-1.3.5.tgz#eb777df6011723a3b14e8a72c0805c8e86746bd2" @@ -625,7 +635,7 @@ debug@2.6.8: dependencies: ms "2.0.0" -debug@2.6.9, debug@^2.2.0, debug@^2.6.3, debug@^2.6.9: +debug@2.6.9, debug@^2.1.2, debug@^2.2.0, debug@^2.6.3, debug@^2.6.9: version "2.6.9" resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" dependencies: @@ -683,7 +693,7 @@ destroy@~1.0.4: version "1.0.4" resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.0.4.tgz#978857442c44749e4206613e37946205826abd80" -detect-libc@^1.0.3: +detect-libc@^1.0.2, detect-libc@^1.0.3: version "1.0.3" resolved "https://registry.yarnpkg.com/detect-libc/-/detect-libc-1.0.3.tgz#fa137c4bd698edf55cd5cd02ac559f91a4c4ba9b" @@ -870,7 +880,7 @@ fs-extra@^5.0.0: jsonfile "^4.0.0" universalify "^0.1.0" -fs-minipass@^1.2.3: +fs-minipass@^1.2.3, fs-minipass@^1.2.5: version "1.2.5" resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-1.2.5.tgz#06c277218454ec288df77ada54a03b8702aacb9d" dependencies: @@ -912,7 +922,7 @@ glob@7.1.1: once "^1.3.0" path-is-absolute "^1.0.0" -glob@7.1.2, glob@^7.0.0, glob@^7.1.1: +glob@7.1.2, glob@^7.0.0, glob@^7.0.5, glob@^7.1.1: version "7.1.2" resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15" dependencies: @@ -990,10 +1000,16 @@ http-errors@1.6.2, http-errors@~1.6.2: setprototypeof "1.0.3" statuses ">= 1.3.1 < 2" -iconv-lite@0.4.19: +iconv-lite@0.4.19, iconv-lite@^0.4.4: version "0.4.19" resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.4.19.tgz#f7468f60135f5e5dad3399c0a81be9a1603a082b" +ignore-walk@^3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/ignore-walk/-/ignore-walk-3.0.1.tgz#a83e62e7d272ac0e3b551aaa82831a19b69f82f8" + dependencies: + minimatch "^3.0.4" + inflection@1.12.0: version "1.12.0" resolved "https://registry.yarnpkg.com/inflection/-/inflection-1.12.0.tgz#a200935656d6f5f6bc4dc7502e1aecb703228416" @@ -1325,10 +1341,11 @@ minimist@~0.0.1: version "0.0.10" resolved "https://registry.yarnpkg.com/minimist/-/minimist-0.0.10.tgz#de3f98543dbf96082be48ad1a0c7cda836301dcf" -minipass@^2.2.1: - version "2.2.1" - resolved "https://registry.yarnpkg.com/minipass/-/minipass-2.2.1.tgz#5ada97538b1027b4cf7213432428578cb564011f" +minipass@^2.2.1, minipass@^2.2.4: + version "2.2.4" + resolved "https://registry.yarnpkg.com/minipass/-/minipass-2.2.4.tgz#03c824d84551ec38a8d1bb5bc350a5a30a354a40" dependencies: + safe-buffer "^5.1.1" yallist "^3.0.0" minizlib@^1.1.0: @@ -1394,10 +1411,22 @@ ms@^2.0.0, ms@^2.1.1: version "2.1.1" resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.1.tgz#30a5864eb3ebb0a66f2ebe6d727af06a09d86e0a" -nan@^2.3.0, nan@^2.9.2: +nan@^2.3.0: + version "2.8.0" + resolved "https://registry.yarnpkg.com/nan/-/nan-2.8.0.tgz#ed715f3fe9de02b57a5e6252d90a96675e1f085a" + +nan@^2.9.2, nan@~2.9.2: version "2.9.2" resolved "https://registry.yarnpkg.com/nan/-/nan-2.9.2.tgz#f564d75f5f8f36a6d9456cca7a6c4fe488ab7866" +needle@^2.2.0: + version "2.2.0" + resolved "https://registry.yarnpkg.com/needle/-/needle-2.2.0.tgz#f14efc69cee1024b72c8b21c7bdf94a731dc12fa" + dependencies: + debug "^2.1.2" + iconv-lite "^0.4.4" + sax "^1.2.4" + negotiator@0.6.1: version "0.6.1" resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.1.tgz#2b327184e8992101177b28563fb5e7102acd0ca9" @@ -1408,11 +1437,44 @@ node-abi@^2.2.0: dependencies: semver "^5.4.1" +node-pre-gyp@~0.9.0: + version "0.9.0" + resolved "https://registry.yarnpkg.com/node-pre-gyp/-/node-pre-gyp-0.9.0.tgz#bdd4c3afac9b1b1ebff0a9ff3362859eb6781bb8" + dependencies: + detect-libc "^1.0.2" + mkdirp "^0.5.1" + needle "^2.2.0" + nopt "^4.0.1" + npm-packlist "^1.1.6" + npmlog "^4.0.2" + rc "^1.1.7" + rimraf "^2.6.1" + semver "^5.3.0" + tar "^4" + noop-logger@^0.1.1: version "0.1.1" resolved "https://registry.yarnpkg.com/noop-logger/-/noop-logger-0.1.1.tgz#94a2b1633c4f1317553007d8966fd0e841b6a4c2" -npmlog@^4.0.1, npmlog@^4.1.2: +nopt@^4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/nopt/-/nopt-4.0.1.tgz#d0d4685afd5415193c8c7505602d0d17cd64474d" + dependencies: + abbrev "1" + osenv "^0.1.4" + +npm-bundled@^1.0.1: + version "1.0.3" + resolved "https://registry.yarnpkg.com/npm-bundled/-/npm-bundled-1.0.3.tgz#7e71703d973af3370a9591bafe3a63aca0be2308" + +npm-packlist@^1.1.6: + version "1.1.10" + resolved "https://registry.yarnpkg.com/npm-packlist/-/npm-packlist-1.1.10.tgz#1039db9e985727e464df066f4cf0ab6ef85c398a" + dependencies: + ignore-walk "^3.0.1" + npm-bundled "^1.0.1" + +npmlog@^4.0.1, npmlog@^4.0.2, npmlog@^4.1.2: version "4.1.2" resolved "https://registry.yarnpkg.com/npmlog/-/npmlog-4.1.2.tgz#08a7f2a8bf734604779a9efa4ad5cc717abb954b" dependencies: @@ -1452,10 +1514,21 @@ optimist@^0.6.1: minimist "~0.0.1" wordwrap "~0.0.2" -os-homedir@^1.0.1: +os-homedir@^1.0.0, os-homedir@^1.0.1: version "1.0.2" resolved "https://registry.yarnpkg.com/os-homedir/-/os-homedir-1.0.2.tgz#ffbc4988336e0e833de0c168c7ef152121aa7fb3" +os-tmpdir@^1.0.0: + version "1.0.2" + resolved "https://registry.yarnpkg.com/os-tmpdir/-/os-tmpdir-1.0.2.tgz#bbe67406c79aa85c5cfec766fe5734555dfa1274" + +osenv@^0.1.4: + version "0.1.5" + resolved "https://registry.yarnpkg.com/osenv/-/osenv-0.1.5.tgz#85cdfafaeb28e8677f416e287592b5f3f49ea410" + dependencies: + os-homedir "^1.0.0" + os-tmpdir "^1.0.0" + packet-reader@0.3.1: version "0.3.1" resolved "https://registry.yarnpkg.com/packet-reader/-/packet-reader-0.3.1.tgz#cd62e60af8d7fea8a705ec4ff990871c46871f27" @@ -1672,6 +1745,15 @@ rc@^1.1.6: minimist "^1.2.0" strip-json-comments "~2.0.1" +rc@^1.1.7: + version "1.2.6" + resolved "https://registry.yarnpkg.com/rc/-/rc-1.2.6.tgz#eb18989c6d4f4f162c399f79ddd29f3835568092" + dependencies: + deep-extend "~0.4.0" + ini "~1.3.0" + minimist "^1.2.0" + strip-json-comments "~2.0.1" + readable-stream@1.0.31: version "1.0.31" resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-1.0.31.tgz#8f2502e0bc9e3b0da1b94520aabb4e2603ecafae" @@ -1726,10 +1808,20 @@ right-align@^0.1.1: dependencies: align-text "^0.1.1" -safe-buffer@5.1.1, safe-buffer@^5.0.1, safe-buffer@~5.1.0, safe-buffer@~5.1.1: +rimraf@^2.6.1: + version "2.6.2" + resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.2.tgz#2ed8150d24a16ea8651e6d6ef0f47c4158ce7a36" + dependencies: + glob "^7.0.5" + +safe-buffer@5.1.1, safe-buffer@^5.0.1, safe-buffer@^5.1.1, safe-buffer@~5.1.0, safe-buffer@~5.1.1: version "5.1.1" resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.1.tgz#893312af69b2123def71f57889001671eeb2c853" +sax@^1.2.4: + version "1.2.4" + resolved "https://registry.yarnpkg.com/sax/-/sax-1.2.4.tgz#2816234e2378bddc4e5354fab5caa895df7100d9" + semver@4.3.2: version "4.3.2" resolved "https://registry.yarnpkg.com/semver/-/semver-4.3.2.tgz#c7a07158a80bedd052355b770d82d6640f803be7" @@ -1889,6 +1981,13 @@ sprintf-js@~1.0.2: version "1.0.3" resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c" +sqlite3@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/sqlite3/-/sqlite3-4.0.0.tgz#cc0e093ab51873f50d9dfc4126fcbef15d486570" + dependencies: + nan "~2.9.2" + node-pre-gyp "~0.9.0" + ssf@~0.10.1: version "0.10.2" resolved "https://registry.yarnpkg.com/ssf/-/ssf-0.10.2.tgz#65b2b4fcdfd967bc8e8383a41349009893115976" @@ -1986,7 +2085,7 @@ supports-color@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-2.0.0.tgz#535d045ce6b6363fa40117084629995e9df324c7" -supports-color@^5.3.0: +supports-color@^5.2.0: version "5.3.0" resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.3.0.tgz#5b24ac15db80fa927cf5227a4a33fd3c4c7676c0" dependencies: @@ -2010,6 +2109,18 @@ tar-stream@^1.1.2: readable-stream "^2.0.0" xtend "^4.0.0" +tar@^4: + version "4.4.1" + resolved "https://registry.yarnpkg.com/tar/-/tar-4.4.1.tgz#b25d5a8470c976fd7a9a8a350f42c59e9fa81749" + dependencies: + chownr "^1.0.1" + fs-minipass "^1.2.5" + minipass "^2.2.4" + minizlib "^1.1.0" + mkdirp "^0.5.0" + safe-buffer "^5.1.1" + yallist "^3.0.2" + tar@^4.4.0: version "4.4.0" resolved "https://registry.yarnpkg.com/tar/-/tar-4.4.0.tgz#3aaf8c29b6b800a8215f33efb4df1c95ce2ac2f5" |
