|
|
|
@ -4,114 +4,128 @@ 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(); |
|
|
|
|
SetupApp(app, await helpers.initTestDB(), ''); |
|
|
|
|
return app; |
|
|
|
|
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', done => { |
|
|
|
|
init().then((app) => { |
|
|
|
|
chai |
|
|
|
|
.request(app) |
|
|
|
|
.post('/song') |
|
|
|
|
.send({}) |
|
|
|
|
.then((res) => { |
|
|
|
|
expect(res).to.have.status(400); |
|
|
|
|
done(); |
|
|
|
|
}); |
|
|
|
|
}) |
|
|
|
|
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', done => { |
|
|
|
|
init().then(async(app) => { |
|
|
|
|
chai |
|
|
|
|
.request(app) |
|
|
|
|
.post('/song') |
|
|
|
|
.send({ |
|
|
|
|
title: "MySong" |
|
|
|
|
}) |
|
|
|
|
.then((res) => { |
|
|
|
|
expect(res).to.have.status(200); |
|
|
|
|
expect(res.body).to.deep.equal({ |
|
|
|
|
id: 1 |
|
|
|
|
}); |
|
|
|
|
done(); |
|
|
|
|
}); |
|
|
|
|
}) |
|
|
|
|
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', done => { |
|
|
|
|
init().then(async (app) => { |
|
|
|
|
chai |
|
|
|
|
.request(app) |
|
|
|
|
.post('/song') |
|
|
|
|
.send({ |
|
|
|
|
title: "MySong", |
|
|
|
|
artistIds: [1] |
|
|
|
|
}) |
|
|
|
|
.then((res) => { |
|
|
|
|
expect(res).to.have.status(400); |
|
|
|
|
done(); |
|
|
|
|
}); |
|
|
|
|
}) |
|
|
|
|
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', done => { |
|
|
|
|
init().then((app) => { |
|
|
|
|
var req = chai.request(app).keepOpen(); |
|
|
|
|
helpers.createArtist(req, { name: "MyArtist" }, 200, { id: 1 }) |
|
|
|
|
.then(() => helpers.createSong(req, { title: "MySong", artistIds: [ 1 ] }, 200, { id: 1 }) ) |
|
|
|
|
.then(req.close) |
|
|
|
|
.then(done); |
|
|
|
|
}); |
|
|
|
|
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', done => { |
|
|
|
|
init().then((app) => { |
|
|
|
|
var req = chai.request(app).keepOpen(); |
|
|
|
|
helpers.createArtist(req, { name: "Artist1" }, 200, { id: 1 }) |
|
|
|
|
.then(() => helpers.createArtist(req, { name: "Artist2" }, 200, { id: 2 }) ) |
|
|
|
|
.then(() => helpers.createSong(req, { title: "MySong", artistIds: [1, 2] }, 200, { id: 1 }) ) |
|
|
|
|
.then(req.close) |
|
|
|
|
.then(done); |
|
|
|
|
}); |
|
|
|
|
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', done => { |
|
|
|
|
init().then((app) => { |
|
|
|
|
var req = chai.request(app).keepOpen(); |
|
|
|
|
helpers.createArtist(req, { name: "Artist1" }, 200, { id: 1 }) |
|
|
|
|
.then(() => helpers.createSong(req, { title: "MySong", artistIds: [1, 2] }, 400) ) |
|
|
|
|
.then(req.close) |
|
|
|
|
.then(done); |
|
|
|
|
}); |
|
|
|
|
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', done => { |
|
|
|
|
init().then((app) => { |
|
|
|
|
var req = chai.request(app).keepOpen(); |
|
|
|
|
helpers.createTag(req, { name: "Root" }, 200, { id: 1 }) |
|
|
|
|
.then(() => helpers.createTag(req, { name: "Leaf", parentId: 1 }, 200, { id: 2 })) |
|
|
|
|
.then(() => helpers.createSong(req, { title: "Song", tagIds: [ 1, 2 ] }, 200, { id: 1 })) |
|
|
|
|
.then(() => helpers.checkSong(req, 1, 200, { title: "Song", storeLinks: [], tagIds: [ 1, 2 ], albumIds: [], artistIds: [] })) |
|
|
|
|
.then(req.close) |
|
|
|
|
.then(done); |
|
|
|
|
}); |
|
|
|
|
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(); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|