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.
 
 
 
 

102 lines
3.2 KiB

const chai = require('chai');
const chaiHttp = require('chai-http');
const express = require('express');
import { SetupApp } from '../../../app';
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 /artist with no name', () => {
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', 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', 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', 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', 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();
}
});
});