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'; import { IntegrationType } from '../../../../client/src/api'; 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 /integration with missing or wrong data', () => { it('should fail', async done => { let agent = await init(); let req = agent.keepOpen(); try { await helpers.createIntegration(req, { name: "A", type: IntegrationType.spotify }, 400); await helpers.createIntegration(req, { details: {}, type: IntegrationType.spotify }, 400); await helpers.createIntegration(req, { name: "A", details: {} }, 400); await helpers.createIntegration(req, { name: "A", type: "NonexistentType", details: {} }, 400); } finally { req.close(); agent.close(); done(); } }); }); describe('POST /integration with a correct request', () => { it('should succeed', async done => { let agent = await init(); let req = agent.keepOpen(); try { await helpers.createIntegration(req, { name: "A", type: IntegrationType.spotify, details: {} }, 200, { id: 1 }); } finally { req.close(); agent.close(); done(); } }); }); describe('PUT /integration with a correct request', () => { it('should succeed', async done => { let agent = await init(); let req = agent.keepOpen(); try { await helpers.createIntegration(req, { name: "A", type: IntegrationType.spotify, details: {} }, 200, { id: 1 }); await helpers.modifyIntegration(req, 1, { name: "B", type: IntegrationType.spotify, details: { secret: 'cat' } }, 200); await helpers.checkIntegration(req, 1, 200, { name: "B", type: IntegrationType.spotify, details: { secret: 'cat' } }) } finally { req.close(); agent.close(); done(); } }); }); describe('PUT /integration with wrong data', () => { it('should fail', async done => { let agent = await init(); let req = agent.keepOpen(); try { await helpers.createIntegration(req, { name: "A", type: IntegrationType.spotify, details: {} }, 200, { id: 1 }); await helpers.modifyIntegration(req, 1, { name: "B", type: "UnknownType", details: {} }, 400); } finally { req.close(); agent.close(); done(); } }); }); describe('DELETE /integration with a correct request', () => { it('should succeed', async done => { let agent = await init(); let req = agent.keepOpen(); try { await helpers.createIntegration(req, { name: "A", type: IntegrationType.spotify, details: {} }, 200, { id: 1 }); await helpers.checkIntegration(req, 1, 200, { name: "A", type: IntegrationType.spotify, details: {} }) await helpers.deleteIntegration(req, 1, 200); await helpers.checkIntegration(req, 1, 404); } finally { req.close(); agent.close(); done(); } }); });