import { FixtureLoader } from '../fixtures/FixtureLoader' ; import { Server } from '../src/server' ; import { JwtUtils } from '../src/security/JwtUtils' ; import { IUser } from '../../shared/models/IUser' ; import chai = require ( 'chai' ) ; import chaiHttp = require ( 'chai-http' ) ; export class TestHelper { public app = new Server ( ) . app ; public fixtureLoader = new FixtureLoader ( ) ; public baseUrl : string ; public static commonChaiSetup ( ) { chai . use ( chaiHttp ) ; return chai ; } constructor ( BASE_URL ) { this . baseUrl = BASE_URL ; } public async resetForNextTest ( ) { await this . fixtureLoader . load ( ) ; } public async basicUserGetRequest ( user , url , queryOptions ? ) { return await chai . request ( this . app ) . get ( url ) . query ( queryOptions ) . set ( 'Cookie' , `token=${ JwtUtils . generateToken ( user ) }` ) . catch ( ( err ) => err . response ) ; } public async basicUserPostRequest ( user , url , sendData ? ) { return await chai . request ( this . app ) . post ( url ) . set ( 'Cookie' , `token=${ JwtUtils . generateToken ( user ) }` ) . send ( sendData ) . catch ( ( err ) => err . response ) ; } public async basicUserPutRequest ( user , url , sendData ? ) { return await chai . request ( this . app ) . put ( url ) . set ( 'Cookie' , `token=${ JwtUtils . generateToken ( user ) }` ) . send ( sendData ) . catch ( ( err ) => err . response ) ; } public async basicUserDeleteRequest ( user , url ) { return await chai . request ( this . app ) . del ( url ) . set ( 'Cookie' , `token=${ JwtUtils . generateToken ( user ) }` ) . catch ( ( err ) => err . response ) ; } public async commonUserGetRequest ( user , urlPostfix , queryOptions ? ) { return await this . basicUserGetRequest ( user , this . baseUrl + urlPostfix , queryOptions ) ; } public async commonUserPostRequest ( user , urlPostfix , sendData ? ) { return await this . basicUserPostRequest ( user , this . baseUrl + urlPostfix , sendData ) ; } public async commonUserPutRequest ( user , urlPostfix , sendData ? ) { return await this . basicUserPutRequest ( user , this . baseUrl + urlPostfix , sendData ) ; } public async commonUserDeleteRequest ( user , urlPostfix ) { return await this . basicUserDeleteRequest ( user , this . baseUrl + urlPostfix ) ; } }