import { sha512 } from "js-sha512"; import { DBDataFormat, IntegrationImpl } from "../../../client/src/api/api"; import { ReferenceDatabase } from "../reference_model/DBReferenceModel"; let chai = require('chai'); let chaiHttp = require('chai-http') chai.use(chaiHttp); let expect = chai.expect; export async function initTestDB() { // Allow different database configs - but fall back to SQLite in memory if necessary. const config = process.env.MUDBASE_DB_CONFIG ? JSON.parse(process.env.MUDBASE_DB_CONFIG) : { client: 'sqlite3', connection: ':memory:' }; console.log("Running tests with DB config: ", config); const knex = await require('knex')(config); // Undoing and doing the migrations is a test in itself. await knex.migrate.rollback(undefined, true); await knex.migrate.latest(); return knex; } export async function createTrack( req: any, props = { name: "Track" }, expectStatus: number | undefined = undefined, expectResponse = undefined ) { return await req .post('/track') .send(props) .then((res: any) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }); } export async function modifyTrack( req: any, id = 1, props = { name: "NewTrack" }, expectStatus: number | undefined = undefined, ) { await req .put('/track/' + id) .send(props) .then((res: any) => { expectStatus && expect(res).to.have.status(expectStatus); return res; }); } export async function deleteTrack( req: any, id = 1, expectStatus: number | undefined = undefined, ) { return await req .delete('/track/' + id) .send() .then((res: any) => { expectStatus && expect(res).to.have.status(expectStatus); return res; }); } export async function checkTrack( req: any, id: any, expectStatus: number | undefined = undefined, expectResponse: any = undefined, ) { await req .get('/track/' + id) .then((res: any) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }) } export async function createArtist( req: any, props = { name: "Artist" }, expectStatus: number | undefined = undefined, expectResponse = undefined ) { await req .post('/artist') .send(props) .then((res: any) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }); } export async function modifyArtist( req: any, id = 1, props = { name: "NewArtist" }, expectStatus: number | undefined = undefined, ) { await req .put('/artist/' + id) .send(props) .then((res: any) => { expectStatus && expect(res).to.have.status(expectStatus); return res; }); } export async function checkArtist( req: any, id: any, expectStatus: number | undefined = undefined, expectResponse: any = undefined, ) { await req .get('/artist/' + id) .then((res: any) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }) } export async function createTag( req: any, props = { name: "Tag" }, expectStatus: number | undefined = undefined, expectResponse = undefined ) { await req .post('/tag') .send(props) .then((res: any) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }); } export async function modifyTag( req: any, id = 1, props = { name: "NewTag" }, expectStatus: number | undefined = undefined, ) { await req .put('/tag/' + id) .send(props) .then((res: any) => { expectStatus && expect(res).to.have.status(expectStatus); return res; }); } export async function checkTag( req: any, id: any, expectStatus: number | undefined = undefined, expectResponse: any = undefined, ) { await req .get('/tag/' + id) .then((res: any) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }) } export async function createAlbum( req: any, props = { name: "Album" }, expectStatus: number | undefined = undefined, expectResponse = undefined ) { await req .post('/album') .send(props) .then((res: any) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }); } export async function modifyAlbum( req: any, id = 1, props = { name: "NewAlbum" }, expectStatus: number | undefined = undefined, ) { await req .put('/album/' + id) .send(props) .then((res: any) => { expectStatus && expect(res).to.have.status(expectStatus); return res; }); } export async function checkAlbum( req: any, id: any, expectStatus: number | undefined = undefined, expectResponse: any = undefined, ) { await req .get('/album/' + id) .then((res: any) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }) } export async function createUser( req: any, email: string, password: string, expectStatus: number | undefined = undefined, expectResponse: any = undefined, ) { const res = await req .post('/register') .send({ email: email, password: password, }); expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; } export async function login( req: any, email: string, password: string, expectStatus: number | undefined = undefined, expectResponse: any = undefined, ) { const res = await req .post('/login?username=' + encodeURIComponent(email) + '&password=' + encodeURIComponent(password)) .send({}); expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; } export async function logout( req: any, expectStatus: number | undefined = undefined, expectResponse: any = undefined, ) { const res = await req .post('/logout') .send({}); expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; } export async function createIntegration( req: any, props = { name: "Integration", type: IntegrationImpl.SpotifyClientCredentials, details: {}, secretDetails: {} }, expectStatus: number | undefined = undefined, expectResponse = undefined ) { await req .post('/integration') .send(props) .then((res: any) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }); } export async function modifyIntegration( req: any, id = 1, props = { name: "NewIntegration", type: IntegrationImpl.SpotifyClientCredentials, details: {}, secretDetails: {} }, expectStatus: number | undefined = undefined, ) { await req .put('/integration/' + id) .send(props) .then((res: any) => { expectStatus && expect(res).to.have.status(expectStatus); return res; }); } export async function checkIntegration( req: any, id: any, expectStatus: number | undefined = undefined, expectResponse: any = undefined, ) { await req .get('/integration/' + id) .then((res: any) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }) } export async function listIntegrations( req: any, expectStatus: number | undefined = undefined, expectResponse: any = undefined, ) { await req .get('/integration') .then((res: any) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }) } export async function deleteIntegration( req: any, id: any, expectStatus: number | undefined = undefined, ) { await req .delete('/integration/' + id) .then((res: any) => { expectStatus && expect(res).to.have.status(expectStatus); return res; }) } export async function getExport( req: any, expectStatus: number | undefined = undefined, expectResponse: any = undefined, ) { return await req .get('/export') .then((res: any) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }) } export async function importDB( req: any, db: DBDataFormat, expectStatus: number | undefined = undefined, expectResponse: any = undefined, ) { return await req .post('/import') .send(db) .then((res: any) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }) }