Fix existing tests to work with login.

pull/31/head
Sander Vocke 5 years ago
parent ba65bcf97f
commit c87d995a09
  1. 132
      server/test/integration/flows/AlbumFlow.js
  2. 126
      server/test/integration/flows/ArtistFlow.js
  3. 457
      server/test/integration/flows/QueryFlow.js
  4. 170
      server/test/integration/flows/SongFlow.js
  5. 108
      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 { 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 /album with no name', () => {
it('should fail', done => {
init().then((app) => {
chai
.request(app)
.post('/album')
.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.createAlbum(req, {}, 400);
} finally {
req.close();
agent.close();
done();
}
});
});
describe('POST /album with a correct request', () => {
it('should succeed', done => {
init().then((app) => {
chai
.request(app)
.post('/album')
.send({
name: "MyAlbum"
})
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.deep.equal({
id: 1
});
done();
});
});
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', done => {
init().then((app) => {
chai
.request(app)
.put('/album/1')
.send({
id: 1,
name: "NewAlbumName"
})
.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.modifyAlbum(req, 1, { id: 1, name: "NewAlbumName" }, 400);
} finally {
req.close();
agent.close();
done();
}
});
});
describe('PUT /album with an existing album', () => {
it('should succeed', done => {
init().then((app) => {
var req = chai.request(app).keepOpen();
helpers.createAlbum(req, { name: "MyAlbum" }, 200, { id: 1 })
.then(() => helpers.modifyAlbum(req, 1, { name: "MyNewAlbum" }, 200))
.then(() => helpers.checkAlbum(req, 1, 200, { name: "MyNewAlbum", storeLinks: [], tagIds: [], songIds: [], artistIds: [] }))
.then(req.close)
.then(done);
});
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', 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.createAlbum(req, { name: "MyAlbum", tagIds: [ 1, 2 ] }, 200, { id: 1 }))
.then(() => helpers.checkAlbum(req, 1, 200, { name: "MyAlbum", storeLinks: [], tagIds: [ 1, 2 ], songIds: [], 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.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();
}
});
});

@ -2,85 +2,101 @@ 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();
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 /artist with no name', () => {
it('should fail', done => {
init().then((app) => {
chai
.request(app)
.post('/artist')
.send({})
.then((res) => {
expect(res).to.have.status(400);
done();
});
});
it('should fail', async done => {
let agent = await init();
var req = agent.keepOpen();
try {
await helpers.createArtist(req, {}, 400);
} finally {
req.close();
agent.close();
done();
}
});
});
describe('POST /artist with a correct request', () => {
it('should succeed', done => {
init().then((app) => {
var req = chai.request(app).keepOpen();
helpers.createArtist(req, { name: "MyArtist" }, 200, { id: 1 })
.then(() => helpers.checkArtist(req, 1, 200, { name: "MyArtist", storeLinks: [], tagIds: [] }))
.then(req.close)
.then(done);
});
it('should succeed', async done => {
let agent = await init();
var req = agent.keepOpen();
try {
await helpers.createArtist(req, { name: "MyArtist" }, 200, { id: 1 });
await helpers.checkArtist(req, 1, 200, { name: "MyArtist", storeLinks: [], tagIds: [] });
} finally {
req.close();
agent.close();
done();
}
});
});
describe('PUT /artist on nonexistent artist', () => {
it('should fail', done => {
init().then((app) => {
chai
.request(app)
.put('/artist/0')
.send({
id: 0,
name: "NewArtistName"
})
.then((res) => {
expect(res).to.have.status(400);
done();
})
})
it('should fail', async done => {
let agent = await init();
var req = agent.keepOpen();
try {
await helpers.modifyArtist(req, 0, { id: 0, name: "NewArtistName" }, 400)
} finally {
req.close();
agent.close();
done();
}
});
});
describe('PUT /artist with an existing artist', () => {
it('should succeed', done => {
init().then((app) => {
var req = chai.request(app).keepOpen();
helpers.createArtist(req, { name: "MyArtist" }, 200, { id: 1 })
.then(() => helpers.modifyArtist(req, 1, { name: "MyNewArtist" }, 200))
.then(() => helpers.checkArtist(req, 1, 200, { name: "MyNewArtist", storeLinks: [], tagIds: [] }))
.then(req.close)
.then(done);
});
it('should succeed', async done => {
let agent = await init();
var req = agent.keepOpen();
try {
await helpers.createArtist(req, { name: "MyArtist" }, 200, { id: 1 });
await helpers.modifyArtist(req, 1, { name: "MyNewArtist" }, 200);
await helpers.checkArtist(req, 1, 200, { name: "MyNewArtist", storeLinks: [], tagIds: [] });
} finally {
req.close();
agent.close();
done();
}
});
});
describe('POST /artist 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.createArtist(req, { name: "MyArtist", tagIds: [ 1, 2 ] }, 200, { id: 1 }))
.then(() => helpers.checkArtist(req, 1, 200, { name: "MyArtist", storeLinks: [], tagIds: [ 1, 2 ] }))
.then(req.close)
.then(done);
});
it('should succeed', async done => {
let agent = await init();
var 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.createArtist(req, { name: "MyArtist", tagIds: [1, 2] }, 200, { id: 1 });
await helpers.checkArtist(req, 1, 200, { name: "MyArtist", storeLinks: [], tagIds: [1, 2] });
} finally {
req.close();
agent.close();
done();
}
});
});

@ -4,19 +4,32 @@ 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 /query with no songs', () => {
it('should give empty list', done => {
init().then((app) => {
chai
.request(app)
it('should give empty list', async done => {
let agent = await init();
try {
let res = await agent
.post('/query')
.send({
'query': {},
@ -31,241 +44,243 @@ describe('POST /query with no songs', () => {
'ascending': true
}
})
expect(res).to.have.status(200);
expect(res.body).to.deep.equal({
songs: [],
tags: [],
artists: [],
albums: [],
});
} finally {
agent.close();
done();
}
});
});
describe('POST /query with several songs and filters', () => {
it('should give all correct results', async done => {
const song1 = {
songId: 1,
title: 'Song1',
storeLinks: [],
artists: [
{
artistId: 1,
name: 'Artist1',
storeLinks: [],
}
],
tags: [],
albums: []
};
const song2 = {
songId: 2,
title: 'Song2',
storeLinks: [],
artists: [
{
artistId: 1,
name: 'Artist1',
storeLinks: [],
}
],
tags: [],
albums: []
};
const song3 = {
songId: 3,
title: 'Song3',
storeLinks: [],
artists: [
{
artistId: 2,
name: 'Artist2',
storeLinks: [],
}
],
tags: [],
albums: []
};
async function checkAllSongs(req) {
await req
.post('/query')
.send({
"query": {},
'offsetsLimits': {
'songOffset': 0,
'songLimit': 10,
},
'ordering': {
'orderBy': {
'type': 0,
},
'ascending': true
}
})
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.deep.equal({
songs: [],
tags: [],
songs: [song1, song2, song3],
artists: [],
tags: [],
albums: [],
});
done();
});
})
});
});
}
describe('POST /query with several songs and filters', () => {
it('should give all correct results', done => {
init().then((app) => {
const song1 = {
songId: 1,
title: 'Song1',
storeLinks: [],
artists: [
{
artistId: 1,
name: 'Artist1',
storeLinks: [],
}
],
tags: [],
albums: []
};
const song2 = {
songId: 2,
title: 'Song2',
storeLinks: [],
artists: [
{
artistId: 1,
name: 'Artist1',
storeLinks: [],
}
],
tags: [],
albums: []
};
const song3 = {
songId: 3,
title: 'Song3',
storeLinks: [],
artists: [
{
artistId: 2,
name: 'Artist2',
storeLinks: [],
}
],
tags: [],
albums: []
};
async function checkAllSongs(req) {
await req
.post('/query')
.send({
"query": {},
'offsetsLimits': {
'songOffset': 0,
'songLimit': 10,
async function checkIdIn(req) {
await req
.post('/query')
.send({
"query": {
"prop": "songId",
"propOperator": "IN",
"propOperand": [1, 3, 5]
},
'offsetsLimits': {
'songOffset': 0,
'songLimit': 10,
},
'ordering': {
'orderBy': {
'type': 0,
},
'ordering': {
'orderBy': {
'type': 0,
},
'ascending': true
}
})
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.deep.equal({
songs: [ song1, song2, song3 ],
artists: [],
tags: [],
albums: [],
});
'ascending': true
}
})
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.deep.equal({
songs: [song1, song3],
artists: [],
tags: [],
albums: [],
});
}
});
}
async function checkIdIn(req) {
await req
.post('/query')
.send({
"query": {
"prop": "songId",
"propOperator": "IN",
"propOperand": [1, 3, 5]
},
'offsetsLimits': {
'songOffset': 0,
'songLimit': 10,
async function checkIdNotIn(req) {
await req
.post('/query')
.send({
"query": {
"prop": "songId",
"propOperator": "NOTIN",
"propOperand": [1, 3, 5]
},
'offsetsLimits': {
'songOffset': 0,
'songLimit': 10,
},
'ordering': {
'orderBy': {
'type': 0,
},
'ordering': {
'orderBy': {
'type': 0,
},
'ascending': true
}
})
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.deep.equal({
songs: [ song1, song3 ],
artists: [],
tags: [],
albums: [],
});
'ascending': true
}
})
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.deep.equal({
songs: [song2],
artists: [],
tags: [],
albums: [],
});
}
});
}
async function checkIdNotIn(req) {
await req
.post('/query')
.send({
"query": {
"prop": "songId",
"propOperator": "NOTIN",
"propOperand": [1, 3, 5]
},
'offsetsLimits': {
'songOffset': 0,
'songLimit': 10,
async function checkArtistIdIn(req) {
await req
.post('/query')
.send({
"query": {
"prop": "artistId",
"propOperator": "IN",
"propOperand": [1]
},
'offsetsLimits': {
'songOffset': 0,
'songLimit': 10,
},
'ordering': {
'orderBy': {
'type': 0,
},
'ordering': {
'orderBy': {
'type': 0,
},
'ascending': true
}
})
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.deep.equal({
songs: [ song2 ],
artists: [],
tags: [],
albums: [],
});
'ascending': true
}
})
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.deep.equal({
songs: [song1, song2],
artists: [],
tags: [],
albums: [],
});
}
});
}
async function checkArtistIdIn(req) {
await req
.post('/query')
.send({
"query": {
"prop": "artistId",
"propOperator": "IN",
"propOperand": [1]
},
'offsetsLimits': {
'songOffset': 0,
'songLimit': 10,
},
'ordering': {
'orderBy': {
'type': 0,
async function checkOrRelation(req) {
await req
.post('/query')
.send({
"query": {
"childrenOperator": "OR",
"children": [
{
"prop": "artistId",
"propOperator": "IN",
"propOperand": [2]
},
'ascending': true
}
})
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.deep.equal({
songs: [ song1, song2 ],
artists: [],
tags: [],
albums: [],
});
});
}
async function checkOrRelation(req) {
await req
.post('/query')
.send({
"query": {
"childrenOperator": "OR",
"children": [
{
"prop": "artistId",
"propOperator": "IN",
"propOperand": [2]
},
{
"prop": "songId",
"propOperator": "EQ",
"propOperand": 1
}
]
},
'offsetsLimits': {
'songOffset': 0,
'songLimit': 10,
{
"prop": "songId",
"propOperator": "EQ",
"propOperand": 1
}
]
},
'offsetsLimits': {
'songOffset': 0,
'songLimit': 10,
},
'ordering': {
'orderBy': {
'type': 0,
},
'ordering': {
'orderBy': {
'type': 0,
},
'ascending': true
}
})
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.deep.equal({
songs: [ song1, song3 ],
artists: [],
tags: [],
albums: [],
});
'ascending': true
}
})
.then((res) => {
expect(res).to.have.status(200);
expect(res.body).to.deep.equal({
songs: [song1, song3],
artists: [],
tags: [],
albums: [],
});
}
var req = chai.request(app).keepOpen();
});
}
helpers.createArtist(req, { name: "Artist1" }, 200)
.then(() => helpers.createArtist(req, { name: "Artist2" }, 200))
.then(() => helpers.createSong(req, { title: "Song1", artistIds: [1] }, 200))
.then(() => helpers.createSong(req, { title: "Song2", artistIds: [1] }, 200))
.then(() => helpers.createSong(req, { title: "Song3", artistIds: [2] }, 200))
.then(() => checkAllSongs(req))
.then(() => checkIdIn(req))
.then(() => checkIdNotIn(req))
.then(() => checkArtistIdIn(req))
.then(() => checkOrRelation(req))
.then(req.close)
.then(done)
})
let agent = await init();
let req = agent.keepOpen();
try {
await helpers.createArtist(req, { name: "Artist1" }, 200);
await helpers.createArtist(req, { name: "Artist2" }, 200);
await helpers.createSong(req, { title: "Song1", artistIds: [1] }, 200);
await helpers.createSong(req, { title: "Song2", artistIds: [1] }, 200);
await helpers.createSong(req, { title: "Song3", artistIds: [2] }, 200);
await checkAllSongs(req);
await checkIdIn(req);
await checkIdNotIn(req);
await checkArtistIdIn(req);
await checkOrRelation(req);
} finally {
req.close();
agent.close();
done();
}
});
});

@ -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();
}
});
});

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

@ -1,4 +1,5 @@
import { expect } from "chai";
import { sha512 } from "js-sha512";
export async function initTestDB() {
// 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.
await knex.migrate.rollback(undefined, true);
await knex.migrate.latest();
return knex;
}

Loading…
Cancel
Save