const chai = require('chai'); const chaiHttp = require('chai-http'); const express = require('express'); import { SetupApp } from '../../../app'; import { expect } from 'chai'; import * as helpers from './helpers'; import { sha512 } from 'js-sha512'; async function init() { chai.use(chaiHttp); const app = express(); const knex = await helpers.initTestDB(); // Add test users. await knex.insert({ email: "test1@test.com", passwordHash: sha512('pass1') }).into('users'); await knex.insert({ email: "test2@test.com", passwordHash: sha512('pass2') }).into('users'); SetupApp(app, knex, ''); // Login as a test user. var agent = chai.request.agent(app); await agent .post('/login?username=' + encodeURIComponent("test1@test.com") + '&password=' + encodeURIComponent('pass1')) .send({}); return agent; } describe('POST /song with no title', () => { it('should fail', async done => { let agent = await init(); let req = agent.keepOpen(); try { await helpers.createSong(req, {}, 400); } finally { req.close(); agent.close(); done(); } }); }); describe('POST /song with only a title', () => { it('should return the first available id', async done => { let agent = await init(); let req = agent.keepOpen(); try { await helpers.createSong(req, { title: "MySong" }, 200, { id: 1 }); } finally { req.close(); agent.close(); done(); } }); }); describe('POST /song with a nonexistent artist Id', () => { it('should fail', async done => { let agent = await init(); let req = agent.keepOpen(); try { await helpers.createSong(req, { title: "MySong", artistIds: [1] }, 400); } finally { req.close(); agent.close(); done(); } }); }); describe('POST /song with an existing artist Id', () => { it('should succeed', async done => { let agent = await init(); let req = agent.keepOpen(); try { await helpers.createArtist(req, { name: "MyArtist" }, 200, { id: 1 }); await helpers.createSong(req, { title: "MySong", artistIds: [1] }, 200, { id: 1 }); } finally { req.close(); agent.close(); done(); } }); }); describe('POST /song with two existing artist Ids', () => { it('should succeed', async done => { let agent = await init(); let req = agent.keepOpen(); try { await helpers.createArtist(req, { name: "Artist1" }, 200, { id: 1 }) await helpers.createArtist(req, { name: "Artist2" }, 200, { id: 2 }) await helpers.createSong(req, { title: "MySong", artistIds: [1, 2] }, 200, { id: 1 }) } finally { req.close(); agent.close(); done(); } }); }); describe('POST /song with an existent and a nonexistent artist Id', () => { it('should fail', async done => { let agent = await init(); let req = agent.keepOpen(); try { await helpers.createArtist(req, { name: "Artist1" }, 200, { id: 1 }) await helpers.createSong(req, { title: "MySong", artistIds: [1, 2] }, 400) } finally { req.close(); agent.close(); done(); } }); }); describe('POST /song with tags', () => { it('should succeed', async done => { let agent = await init(); let req = agent.keepOpen(); try { await helpers.createTag(req, { name: "Root" }, 200, { id: 1 }) await helpers.createTag(req, { name: "Leaf", parentId: 1 }, 200, { id: 2 }) await helpers.createSong(req, { title: "Song", tagIds: [1, 2] }, 200, { id: 1 }) await helpers.checkSong(req, 1, 200, { title: "Song", storeLinks: [], tagIds: [1, 2], albumIds: [], artistIds: [] }) } finally { req.close(); agent.close(); done(); } }); });