You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
3.1 KiB
103 lines
3.1 KiB
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 /album with no name', () => { |
|
it('should fail', async done => { |
|
let agent = await init(); |
|
let req = agent.keepOpen(); |
|
try { |
|
await helpers.createAlbum(req, {}, 400); |
|
} finally { |
|
req.close(); |
|
agent.close(); |
|
done(); |
|
} |
|
}); |
|
}); |
|
|
|
describe('POST /album with a correct request', () => { |
|
it('should succeed', async done => { |
|
let agent = await init(); |
|
let req = agent.keepOpen(); |
|
try { |
|
await helpers.createAlbum(req, { name: "MyAlbum" }, 200, { id: 1 }); |
|
} finally { |
|
req.close(); |
|
agent.close(); |
|
done(); |
|
} |
|
}); |
|
}); |
|
|
|
|
|
describe('PUT /album on nonexistent album', () => { |
|
it('should fail', async done => { |
|
let agent = await init(); |
|
let req = agent.keepOpen(); |
|
try { |
|
await helpers.modifyAlbum(req, 1, { id: 1, name: "NewAlbumName" }, 400); |
|
} finally { |
|
req.close(); |
|
agent.close(); |
|
done(); |
|
} |
|
}); |
|
}); |
|
|
|
describe('PUT /album with an existing album', () => { |
|
it('should succeed', async done => { |
|
let agent = await init(); |
|
let req = agent.keepOpen(); |
|
try { |
|
await helpers.createAlbum(req, { name: "MyAlbum" }, 200, { id: 1 }); |
|
await helpers.modifyAlbum(req, 1, { name: "MyNewAlbum" }, 200); |
|
await helpers.checkAlbum(req, 1, 200, { name: "MyNewAlbum", storeLinks: [], tagIds: [], songIds: [], artistIds: [] }); |
|
} finally { |
|
req.close(); |
|
agent.close(); |
|
done(); |
|
} |
|
}); |
|
}); |
|
|
|
describe('POST /album 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.createAlbum(req, { name: "MyAlbum", tagIds: [1, 2] }, 200, { id: 1 }) |
|
await helpers.checkAlbum(req, 1, 200, { name: "MyAlbum", storeLinks: [], tagIds: [1, 2], songIds: [], artistIds: [] }) |
|
} finally { |
|
req.close(); |
|
agent.close(); |
|
done(); |
|
} |
|
}); |
|
}); |
|
|
|
|