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 { IntegrationImpl } 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, { type: IntegrationImpl.SpotifyClientCredentials, details: {}, secretDetails: {} }, 400); await helpers.createIntegration(req, { name: "A", details: {}, secretDetails: {} }, 400); await helpers.createIntegration(req, { name: "A", type: IntegrationImpl.SpotifyClientCredentials, secretDetails: {} }, 400); await helpers.createIntegration(req, { name: "A", type: IntegrationImpl.SpotifyClientCredentials, details: {}, }, 400); await helpers.createIntegration(req, { name: "A", type: "NonexistentType", details: {}, secretDetails: {} }, 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: IntegrationImpl.SpotifyClientCredentials, details: {}, secretDetails: {} }, 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: IntegrationImpl.SpotifyClientCredentials, details: {}, secretDetails: {} }, 200, { id: 1 }); await helpers.modifyIntegration(req, 1, { name: "B", type: IntegrationImpl.SpotifyClientCredentials, details: { secret: 'cat' }, secretDetails: {} }, 200); await helpers.checkIntegration(req, 1, 200, { name: "B", type: IntegrationImpl.SpotifyClientCredentials, 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: IntegrationImpl.SpotifyClientCredentials, details: {}, secretDetails: {} }, 200, { id: 1 }); await helpers.modifyIntegration(req, 1, { name: "B", type: "UnknownType", details: {}, secretDetails: {} }, 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: IntegrationImpl.SpotifyClientCredentials, details: {}, secretDetails: {} }, 200, { id: 1 }); await helpers.checkIntegration(req, 1, 200, { name: "A", type: IntegrationImpl.SpotifyClientCredentials, details: {} }) await helpers.deleteIntegration(req, 1, 200); await helpers.checkIntegration(req, 1, 404); } finally { req.close(); agent.close(); done(); } }); }); describe('GET /integration list 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: IntegrationImpl.SpotifyClientCredentials, details: {}, secretDetails: {} }, 200, { id: 1 }); await helpers.createIntegration(req, { name: "B", type: IntegrationImpl.SpotifyClientCredentials, details: {}, secretDetails: {} }, 200, { id: 2 }); await helpers.createIntegration(req, { name: "C", type: IntegrationImpl.SpotifyClientCredentials, details: {}, secretDetails: {} }, 200, { id: 3 }); await helpers.listIntegrations(req, 200, [ { id: 1, name: "A", type: IntegrationImpl.SpotifyClientCredentials, details: {} }, { id: 2, name: "B", type: IntegrationImpl.SpotifyClientCredentials, details: {} }, { id: 3, name: "C", type: IntegrationImpl.SpotifyClientCredentials, details: {} }, ]); } finally { req.close(); agent.close(); done(); } }); });