import { expect } from "chai"; import { sha512 } from "js-sha512"; import { IntegrationImpl } from "../../../../client/src/api"; 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 createSong( req, props = { title: "Song" }, expectStatus = undefined, expectResponse = undefined ) { await req .post('/song') .send(props) .then((res) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }); } export async function modifySong( req, id = 1, props = { name: "NewSong" }, expectStatus = undefined, ) { await req .put('/song/' + id) .send(props) .then((res) => { expectStatus && expect(res).to.have.status(expectStatus); return res; }); } export async function checkSong( req, id, expectStatus = undefined, expectResponse = undefined, ) { await req .get('/song/' + id) .then((res) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }) } export async function createArtist( req, props = { name: "Artist" }, expectStatus = undefined, expectResponse = undefined ) { await req .post('/artist') .send(props) .then((res) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }); } export async function modifyArtist( req, id = 1, props = { name: "NewArtist" }, expectStatus = undefined, ) { await req .put('/artist/' + id) .send(props) .then((res) => { expectStatus && expect(res).to.have.status(expectStatus); return res; }); } export async function checkArtist( req, id, expectStatus = undefined, expectResponse = undefined, ) { await req .get('/artist/' + id) .then((res) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }) } export async function createTag( req, props = { name: "Tag" }, expectStatus = undefined, expectResponse = undefined ) { await req .post('/tag') .send(props) .then((res) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }); } export async function modifyTag( req, id = 1, props = { name: "NewTag" }, expectStatus = undefined, ) { await req .put('/tag/' + id) .send(props) .then((res) => { expectStatus && expect(res).to.have.status(expectStatus); return res; }); } export async function checkTag( req, id, expectStatus = undefined, expectResponse = undefined, ) { await req .get('/tag/' + id) .then((res) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }) } export async function createAlbum( req, props = { name: "Album" }, expectStatus = undefined, expectResponse = undefined ) { await req .post('/album') .send(props) .then((res) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }); } export async function modifyAlbum( req, id = 1, props = { name: "NewAlbum" }, expectStatus = undefined, ) { await req .put('/album/' + id) .send(props) .then((res) => { expectStatus && expect(res).to.have.status(expectStatus); return res; }); } export async function checkAlbum( req, id, expectStatus = undefined, expectResponse = undefined, ) { await req .get('/album/' + id) .then((res) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }) } export async function createUser( req, email, password, expectStatus = undefined, expectResponse = 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, email, password, expectStatus = undefined, expectResponse = 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, expectStatus = undefined, expectResponse = 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, props = { name: "Integration", type: IntegrationImpl.SpotifyClientCredentials, details: {}, secretDetails: {} }, expectStatus = undefined, expectResponse = undefined ) { await req .post('/integration') .send(props) .then((res) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }); } export async function modifyIntegration( req, id = 1, props = { name: "NewIntegration", type: IntegrationImpl.SpotifyClientCredentials, details: {}, secretDetails: {} }, expectStatus = undefined, ) { await req .put('/integration/' + id) .send(props) .then((res) => { expectStatus && expect(res).to.have.status(expectStatus); return res; }); } export async function checkIntegration( req, id, expectStatus = undefined, expectResponse = undefined, ) { await req .get('/integration/' + id) .then((res) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }) } export async function listIntegrations( req, expectStatus = undefined, expectResponse = undefined, ) { await req .get('/integration') .then((res) => { expectStatus && expect(res).to.have.status(expectStatus); expectResponse && expect(res.body).to.deep.equal(expectResponse); return res; }) } export async function deleteIntegration( req, id, expectStatus = undefined, ) { await req .delete('/integration/' + id) .then((res) => { expectStatus && expect(res).to.have.status(expectStatus); return res; }) }