Fix existing tests to work with login.

pull/31/head
Sander Vocke 5 years ago
parent ba65bcf97f
commit c87d995a09
  1. 126
      server/test/integration/flows/AlbumFlow.js
  2. 122
      server/test/integration/flows/ArtistFlow.js
  3. 67
      server/test/integration/flows/QueryFlow.js
  4. 164
      server/test/integration/flows/SongFlow.js
  5. 104
      server/test/integration/flows/TagFlow.js
  6. 2
      server/test/integration/flows/helpers.js

@ -4,92 +4,100 @@ const express = require('express');
import { SetupApp } from '../../../app'; import { SetupApp } from '../../../app';
import { expect } from 'chai'; import { expect } from 'chai';
import * as helpers from './helpers'; import * as helpers from './helpers';
import { sha512 } from 'js-sha512';
async function init() { async function init() {
chai.use(chaiHttp); chai.use(chaiHttp);
const app = express(); const app = express();
SetupApp(app, await helpers.initTestDB(), ''); const knex = await helpers.initTestDB();
return app;
// 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', () => { describe('POST /album with no name', () => {
it('should fail', done => { it('should fail', async done => {
init().then((app) => { let agent = await init();
chai let req = agent.keepOpen();
.request(app) try {
.post('/album') await helpers.createAlbum(req, {}, 400);
.send({}) } finally {
.then((res) => { req.close();
expect(res).to.have.status(400); agent.close();
done(); done();
}); }
});
}); });
}); });
describe('POST /album with a correct request', () => { describe('POST /album with a correct request', () => {
it('should succeed', done => { it('should succeed', async done => {
init().then((app) => { let agent = await init();
chai let req = agent.keepOpen();
.request(app) try {
.post('/album') await helpers.createAlbum(req, { name: "MyAlbum" }, 200, { id: 1 });
.send({ } finally {
name: "MyAlbum" req.close();
}) agent.close();
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.deep.equal({
id: 1
});
done(); done();
}); }
});
}); });
}); });
describe('PUT /album on nonexistent album', () => { describe('PUT /album on nonexistent album', () => {
it('should fail', done => { it('should fail', async done => {
init().then((app) => { let agent = await init();
chai let req = agent.keepOpen();
.request(app) try {
.put('/album/1') await helpers.modifyAlbum(req, 1, { id: 1, name: "NewAlbumName" }, 400);
.send({ } finally {
id: 1, req.close();
name: "NewAlbumName" agent.close();
})
.then((res) => {
expect(res).to.have.status(400);
done(); done();
}) }
})
}); });
}); });
describe('PUT /album with an existing album', () => { describe('PUT /album with an existing album', () => {
it('should succeed', done => { it('should succeed', async done => {
init().then((app) => { let agent = await init();
var req = chai.request(app).keepOpen(); let req = agent.keepOpen();
helpers.createAlbum(req, { name: "MyAlbum" }, 200, { id: 1 }) try {
.then(() => helpers.modifyAlbum(req, 1, { name: "MyNewAlbum" }, 200)) await helpers.createAlbum(req, { name: "MyAlbum" }, 200, { id: 1 });
.then(() => helpers.checkAlbum(req, 1, 200, { name: "MyNewAlbum", storeLinks: [], tagIds: [], songIds: [], artistIds: [] })) await helpers.modifyAlbum(req, 1, { name: "MyNewAlbum" }, 200);
.then(req.close) await helpers.checkAlbum(req, 1, 200, { name: "MyNewAlbum", storeLinks: [], tagIds: [], songIds: [], artistIds: [] });
.then(done); } finally {
}); req.close();
agent.close();
done();
}
}); });
}); });
describe('POST /album with tags', () => { describe('POST /album with tags', () => {
it('should succeed', done => { it('should succeed', async done => {
init().then((app) => { let agent = await init();
var req = chai.request(app).keepOpen(); let req = agent.keepOpen();
helpers.createTag(req, { name: "Root" }, 200, { id: 1 }) try {
.then(() => helpers.createTag(req, { name: "Leaf", parentId: 1 }, 200, { id: 2 })) await helpers.createTag(req, { name: "Root" }, 200, { id: 1 })
.then(() => helpers.createAlbum(req, { name: "MyAlbum", tagIds: [ 1, 2 ] }, 200, { id: 1 })) await helpers.createTag(req, { name: "Leaf", parentId: 1 }, 200, { id: 2 })
.then(() => helpers.checkAlbum(req, 1, 200, { name: "MyAlbum", storeLinks: [], tagIds: [ 1, 2 ], songIds: [], artistIds: [] })) await helpers.createAlbum(req, { name: "MyAlbum", tagIds: [1, 2] }, 200, { id: 1 })
.then(req.close) await helpers.checkAlbum(req, 1, 200, { name: "MyAlbum", storeLinks: [], tagIds: [1, 2], songIds: [], artistIds: [] })
.then(done); } finally {
}); req.close();
agent.close();
done();
}
}); });
}); });

@ -2,85 +2,101 @@ const chai = require('chai');
const chaiHttp = require('chai-http'); const chaiHttp = require('chai-http');
const express = require('express'); const express = require('express');
import { SetupApp } from '../../../app'; import { SetupApp } from '../../../app';
import { expect } from 'chai';
import * as helpers from './helpers'; import * as helpers from './helpers';
import { sha512 } from 'js-sha512';
async function init() { async function init() {
chai.use(chaiHttp); chai.use(chaiHttp);
const app = express(); const app = express();
SetupApp(app, await helpers.initTestDB(), ''); const knex = await helpers.initTestDB();
return app;
// 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 /artist with no name', () => { describe('POST /artist with no name', () => {
it('should fail', done => { it('should fail', async done => {
init().then((app) => { let agent = await init();
chai var req = agent.keepOpen();
.request(app) try {
.post('/artist') await helpers.createArtist(req, {}, 400);
.send({}) } finally {
.then((res) => { req.close();
expect(res).to.have.status(400); agent.close();
done(); done();
}); }
});
}); });
}); });
describe('POST /artist with a correct request', () => { describe('POST /artist with a correct request', () => {
it('should succeed', done => { it('should succeed', async done => {
init().then((app) => { let agent = await init();
var req = chai.request(app).keepOpen(); var req = agent.keepOpen();
helpers.createArtist(req, { name: "MyArtist" }, 200, { id: 1 }) try {
.then(() => helpers.checkArtist(req, 1, 200, { name: "MyArtist", storeLinks: [], tagIds: [] })) await helpers.createArtist(req, { name: "MyArtist" }, 200, { id: 1 });
.then(req.close) await helpers.checkArtist(req, 1, 200, { name: "MyArtist", storeLinks: [], tagIds: [] });
.then(done); } finally {
}); req.close();
agent.close();
done();
}
}); });
}); });
describe('PUT /artist on nonexistent artist', () => { describe('PUT /artist on nonexistent artist', () => {
it('should fail', done => { it('should fail', async done => {
init().then((app) => { let agent = await init();
chai var req = agent.keepOpen();
.request(app) try {
.put('/artist/0') await helpers.modifyArtist(req, 0, { id: 0, name: "NewArtistName" }, 400)
.send({ } finally {
id: 0, req.close();
name: "NewArtistName" agent.close();
})
.then((res) => {
expect(res).to.have.status(400);
done(); done();
}) }
})
}); });
}); });
describe('PUT /artist with an existing artist', () => { describe('PUT /artist with an existing artist', () => {
it('should succeed', done => { it('should succeed', async done => {
init().then((app) => { let agent = await init();
var req = chai.request(app).keepOpen(); var req = agent.keepOpen();
helpers.createArtist(req, { name: "MyArtist" }, 200, { id: 1 }) try {
.then(() => helpers.modifyArtist(req, 1, { name: "MyNewArtist" }, 200)) await helpers.createArtist(req, { name: "MyArtist" }, 200, { id: 1 });
.then(() => helpers.checkArtist(req, 1, 200, { name: "MyNewArtist", storeLinks: [], tagIds: [] })) await helpers.modifyArtist(req, 1, { name: "MyNewArtist" }, 200);
.then(req.close) await helpers.checkArtist(req, 1, 200, { name: "MyNewArtist", storeLinks: [], tagIds: [] });
.then(done); } finally {
}); req.close();
agent.close();
done();
}
}); });
}); });
describe('POST /artist with tags', () => { describe('POST /artist with tags', () => {
it('should succeed', done => { it('should succeed', async done => {
init().then((app) => { let agent = await init();
var req = chai.request(app).keepOpen(); var req = agent.keepOpen();
helpers.createTag(req, { name: "Root" }, 200, { id: 1 }) try {
.then(() => helpers.createTag(req, { name: "Leaf", parentId: 1 }, 200, { id: 2 })) await helpers.createTag(req, { name: "Root" }, 200, { id: 1 });
.then(() => helpers.createArtist(req, { name: "MyArtist", tagIds: [ 1, 2 ] }, 200, { id: 1 })) await helpers.createTag(req, { name: "Leaf", parentId: 1 }, 200, { id: 2 });
.then(() => helpers.checkArtist(req, 1, 200, { name: "MyArtist", storeLinks: [], tagIds: [ 1, 2 ] })) await helpers.createArtist(req, { name: "MyArtist", tagIds: [1, 2] }, 200, { id: 1 });
.then(req.close) await helpers.checkArtist(req, 1, 200, { name: "MyArtist", storeLinks: [], tagIds: [1, 2] });
.then(done); } finally {
}); req.close();
agent.close();
done();
}
}); });
}); });

@ -4,19 +4,32 @@ const express = require('express');
import { SetupApp } from '../../../app'; import { SetupApp } from '../../../app';
import { expect } from 'chai'; import { expect } from 'chai';
import * as helpers from './helpers'; import * as helpers from './helpers';
import { sha512 } from 'js-sha512';
async function init() { async function init() {
chai.use(chaiHttp); chai.use(chaiHttp);
const app = express(); const app = express();
SetupApp(app, await helpers.initTestDB(), '');; const knex = await helpers.initTestDB();
return app;
// 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 /query with no songs', () => { describe('POST /query with no songs', () => {
it('should give empty list', done => { it('should give empty list', async done => {
init().then((app) => { let agent = await init();
chai try {
.request(app) let res = await agent
.post('/query') .post('/query')
.send({ .send({
'query': {}, 'query': {},
@ -31,7 +44,6 @@ describe('POST /query with no songs', () => {
'ascending': true 'ascending': true
} }
}) })
.then((res) => {
expect(res).to.have.status(200); expect(res).to.have.status(200);
expect(res.body).to.deep.equal({ expect(res.body).to.deep.equal({
songs: [], songs: [],
@ -39,15 +51,15 @@ describe('POST /query with no songs', () => {
artists: [], artists: [],
albums: [], albums: [],
}); });
} finally {
agent.close();
done(); done();
}); }
})
}); });
}); });
describe('POST /query with several songs and filters', () => { describe('POST /query with several songs and filters', () => {
it('should give all correct results', done => { it('should give all correct results', async done => {
init().then((app) => {
const song1 = { const song1 = {
songId: 1, songId: 1,
title: 'Song1', title: 'Song1',
@ -252,20 +264,23 @@ describe('POST /query with several songs and filters', () => {
}); });
} }
var req = chai.request(app).keepOpen(); let agent = await init();
let req = agent.keepOpen();
helpers.createArtist(req, { name: "Artist1" }, 200) try {
.then(() => helpers.createArtist(req, { name: "Artist2" }, 200)) await helpers.createArtist(req, { name: "Artist1" }, 200);
.then(() => helpers.createSong(req, { title: "Song1", artistIds: [1] }, 200)) await helpers.createArtist(req, { name: "Artist2" }, 200);
.then(() => helpers.createSong(req, { title: "Song2", artistIds: [1] }, 200)) await helpers.createSong(req, { title: "Song1", artistIds: [1] }, 200);
.then(() => helpers.createSong(req, { title: "Song3", artistIds: [2] }, 200)) await helpers.createSong(req, { title: "Song2", artistIds: [1] }, 200);
.then(() => checkAllSongs(req)) await helpers.createSong(req, { title: "Song3", artistIds: [2] }, 200);
.then(() => checkIdIn(req)) await checkAllSongs(req);
.then(() => checkIdNotIn(req)) await checkIdIn(req);
.then(() => checkArtistIdIn(req)) await checkIdNotIn(req);
.then(() => checkOrRelation(req)) await checkArtistIdIn(req);
.then(req.close) await checkOrRelation(req);
.then(done) } finally {
}) req.close();
agent.close();
done();
}
}); });
}); });

@ -4,114 +4,128 @@ const express = require('express');
import { SetupApp } from '../../../app'; import { SetupApp } from '../../../app';
import { expect } from 'chai'; import { expect } from 'chai';
import * as helpers from './helpers'; import * as helpers from './helpers';
import { sha512 } from 'js-sha512';
async function init() { async function init() {
chai.use(chaiHttp); chai.use(chaiHttp);
const app = express(); const app = express();
SetupApp(app, await helpers.initTestDB(), ''); const knex = await helpers.initTestDB();
return app;
// 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', () => { describe('POST /song with no title', () => {
it('should fail', done => { it('should fail', async done => {
init().then((app) => { let agent = await init();
chai let req = agent.keepOpen();
.request(app) try {
.post('/song') await helpers.createSong(req, {}, 400);
.send({}) } finally {
.then((res) => { req.close();
expect(res).to.have.status(400); agent.close();
done(); done();
}); }
})
}); });
}); });
describe('POST /song with only a title', () => { describe('POST /song with only a title', () => {
it('should return the first available id', done => { it('should return the first available id', async done => {
init().then(async(app) => { let agent = await init();
chai let req = agent.keepOpen();
.request(app) try {
.post('/song') await helpers.createSong(req, { title: "MySong" }, 200, { id: 1 });
.send({ } finally {
title: "MySong" req.close();
}) agent.close();
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.deep.equal({
id: 1
});
done(); done();
}); }
})
}); });
}); });
describe('POST /song with a nonexistent artist Id', () => { describe('POST /song with a nonexistent artist Id', () => {
it('should fail', done => { it('should fail', async done => {
init().then(async (app) => { let agent = await init();
chai let req = agent.keepOpen();
.request(app) try {
.post('/song') await helpers.createSong(req, { title: "MySong", artistIds: [1] }, 400);
.send({ } finally {
title: "MySong", req.close();
artistIds: [1] agent.close();
})
.then((res) => {
expect(res).to.have.status(400);
done(); done();
}); }
})
}); });
}); });
describe('POST /song with an existing artist Id', () => { describe('POST /song with an existing artist Id', () => {
it('should succeed', done => { it('should succeed', async done => {
init().then((app) => { let agent = await init();
var req = chai.request(app).keepOpen(); let req = agent.keepOpen();
helpers.createArtist(req, { name: "MyArtist" }, 200, { id: 1 }) try {
.then(() => helpers.createSong(req, { title: "MySong", artistIds: [ 1 ] }, 200, { id: 1 }) ) await helpers.createArtist(req, { name: "MyArtist" }, 200, { id: 1 });
.then(req.close) await helpers.createSong(req, { title: "MySong", artistIds: [1] }, 200, { id: 1 });
.then(done); } finally {
}); req.close();
agent.close();
done();
}
}); });
}); });
describe('POST /song with two existing artist Ids', () => { describe('POST /song with two existing artist Ids', () => {
it('should succeed', done => { it('should succeed', async done => {
init().then((app) => { let agent = await init();
var req = chai.request(app).keepOpen(); let req = agent.keepOpen();
helpers.createArtist(req, { name: "Artist1" }, 200, { id: 1 }) try {
.then(() => helpers.createArtist(req, { name: "Artist2" }, 200, { id: 2 }) ) await helpers.createArtist(req, { name: "Artist1" }, 200, { id: 1 })
.then(() => helpers.createSong(req, { title: "MySong", artistIds: [1, 2] }, 200, { id: 1 }) ) await helpers.createArtist(req, { name: "Artist2" }, 200, { id: 2 })
.then(req.close) await helpers.createSong(req, { title: "MySong", artistIds: [1, 2] }, 200, { id: 1 })
.then(done); } finally {
}); req.close();
agent.close();
done();
}
}); });
}); });
describe('POST /song with an existent and a nonexistent artist Id', () => { describe('POST /song with an existent and a nonexistent artist Id', () => {
it('should fail', done => { it('should fail', async done => {
init().then((app) => { let agent = await init();
var req = chai.request(app).keepOpen(); let req = agent.keepOpen();
helpers.createArtist(req, { name: "Artist1" }, 200, { id: 1 }) try {
.then(() => helpers.createSong(req, { title: "MySong", artistIds: [1, 2] }, 400) ) await helpers.createArtist(req, { name: "Artist1" }, 200, { id: 1 })
.then(req.close) await helpers.createSong(req, { title: "MySong", artistIds: [1, 2] }, 400)
.then(done); } finally {
}); req.close();
agent.close();
done();
}
}); });
}); });
describe('POST /song with tags', () => { describe('POST /song with tags', () => {
it('should succeed', done => { it('should succeed', async done => {
init().then((app) => { let agent = await init();
var req = chai.request(app).keepOpen(); let req = agent.keepOpen();
helpers.createTag(req, { name: "Root" }, 200, { id: 1 }) try {
.then(() => helpers.createTag(req, { name: "Leaf", parentId: 1 }, 200, { id: 2 })) await helpers.createTag(req, { name: "Root" }, 200, { id: 1 })
.then(() => helpers.createSong(req, { title: "Song", tagIds: [ 1, 2 ] }, 200, { id: 1 })) await helpers.createTag(req, { name: "Leaf", parentId: 1 }, 200, { id: 2 })
.then(() => helpers.checkSong(req, 1, 200, { title: "Song", storeLinks: [], tagIds: [ 1, 2 ], albumIds: [], artistIds: [] })) await helpers.createSong(req, { title: "Song", tagIds: [1, 2] }, 200, { id: 1 })
.then(req.close) await helpers.checkSong(req, 1, 200, { title: "Song", storeLinks: [], tagIds: [1, 2], albumIds: [], artistIds: [] })
.then(done); } finally {
}); req.close();
agent.close();
done();
}
}); });
}); });

@ -4,72 +4,84 @@ const express = require('express');
import { SetupApp } from '../../../app'; import { SetupApp } from '../../../app';
import { expect } from 'chai'; import { expect } from 'chai';
import * as helpers from './helpers'; import * as helpers from './helpers';
import { sha512 } from 'js-sha512';
async function init() { async function init() {
chai.use(chaiHttp); chai.use(chaiHttp);
const app = express(); const app = express();
SetupApp(app, await helpers.initTestDB(), ''); const knex = await helpers.initTestDB();
return app;
// 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 /tag with no name', () => { describe('POST /tag with no name', () => {
it('should fail', done => { it('should fail', async done => {
init().then((app) => { let agent = await init();
chai let req = agent.keepOpen();
.request(app) try {
.post('/tag') await helpers.createTag(req, {}, 400);
.send({}) } finally {
.then((res) => { req.close();
expect(res).to.have.status(400); agent.close();
done(); done();
}); }
});
}); });
}); });
describe('POST /tag with a correct request', () => { describe('POST /tag with a correct request', () => {
it('should succeed', done => { it('should succeed', async done => {
init().then((app) => { let agent = await init();
chai let req = agent.keepOpen();
.request(app) try {
.post('/tag') await helpers.createTag(req, { name: "MyTag" }, 200, { id: 1 });
.send({ } finally {
name: "MyTag" req.close();
}) agent.close();
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.deep.equal({
id: 1
});
done(); done();
}); }
});
}); });
}); });
describe('POST /tag with a parent', () => { describe('POST /tag with a parent', () => {
it('should succeed', done => { it('should succeed', async done => {
init().then((app) => { let agent = await init();
var req = chai.request(app).keepOpen(); let req = agent.keepOpen();
helpers.createTag(req, { name: "Tag1" }, 200, { id: 1 }) try {
.then(() => helpers.createTag(req, { name: "Tag2", parentId: 1 }, 200, { id: 2 }) ) await helpers.createTag(req, { name: "Tag1" }, 200, { id: 1 })
.then(() => helpers.checkTag(req, 2, 200, { name: "Tag2", parentId: 1 })) await helpers.createTag(req, { name: "Tag2", parentId: 1 }, 200, { id: 2 })
.then(req.close) await helpers.checkTag(req, 2, 200, { name: "Tag2", parentId: 1 })
.then(done); } finally {
}); req.close();
agent.close();
done();
}
}); });
}); });
describe('PUT /tag with a new parent', () => { describe('PUT /tag with a new parent', () => {
it('should succeed', done => { it('should succeed', async done => {
init().then((app) => { let agent = await init();
var req = chai.request(app).keepOpen(); let req = agent.keepOpen();
helpers.createTag(req, { name: "Tag1" }, 200, { id: 1 }) try {
.then(() => helpers.createTag(req, { name: "Tag2" }, 200, { id: 2 }) ) await helpers.createTag(req, { name: "Tag1" }, 200, { id: 1 })
.then(() => helpers.modifyTag(req, 2, { parentId: 1 }, 200) ) await helpers.createTag(req, { name: "Tag2" }, 200, { id: 2 })
.then(() => helpers.checkTag(req, 2, 200, { name: "Tag2", parentId: 1 })) await helpers.modifyTag(req, 2, { parentId: 1 }, 200)
.then(req.close) await helpers.checkTag(req, 2, 200, { name: "Tag2", parentId: 1 })
.then(done); } finally {
}); req.close();
agent.close();
done();
}
}); });
}); });

@ -1,4 +1,5 @@
import { expect } from "chai"; import { expect } from "chai";
import { sha512 } from "js-sha512";
export async function initTestDB() { export async function initTestDB() {
// Allow different database configs - but fall back to SQLite in memory if necessary. // Allow different database configs - but fall back to SQLite in memory if necessary.
@ -11,6 +12,7 @@ export async function initTestDB() {
// Undoing and doing the migrations is a test in itself. // Undoing and doing the migrations is a test in itself.
await knex.migrate.rollback(undefined, true); await knex.migrate.rollback(undefined, true);
await knex.migrate.latest(); await knex.migrate.latest();
return knex; return knex;
} }

Loading…
Cancel
Save