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(); 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', async done => { let agent = await init(); try { let res = await agent .post('/query') .send({ 'query': {}, 'offsetsLimits': { 'songOffset': 0, 'songLimit': 10, }, 'ordering': { 'orderBy': { 'type': 'name', }, 'ascending': true }, 'responseType': 'details', }) 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: [ 'hello my', 'darling' ], 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': 'name', }, 'ascending': true }, 'responseType': 'details', }) .then((res) => { expect(res).to.have.status(200); expect(res.body).to.deep.equal({ songs: [song1, song2, 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, }, 'ordering': { 'orderBy': { 'type': 'name', }, 'ascending': true }, 'responseType': 'details', }) .then((res) => { expect(res).to.have.status(200); expect(res.body).to.deep.equal({ songs: [song1, song3], 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, }, 'ordering': { 'orderBy': { 'type': 'name', }, 'ascending': true }, 'responseType': 'details', }) .then((res) => { expect(res).to.have.status(200); expect(res.body).to.deep.equal({ songs: [song2], artists: [], tags: [], albums: [], }); }); } async function checkArtistIdIn(req) { console.log("HERE!") await req .post('/query') .send({ "query": { "prop": "artistId", "propOperator": "IN", "propOperand": [1] }, 'offsetsLimits': { 'songOffset': 0, 'songLimit': 10, }, 'ordering': { 'orderBy': { 'type': 'name', }, 'ascending': true }, 'responseType': 'details', }) .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, }, 'ordering': { 'orderBy': { 'type': 'name', }, 'ascending': true }, 'responseType': 'details', }) .then((res) => { expect(res).to.have.status(200); expect(res.body).to.deep.equal({ songs: [song1, song3], artists: [], tags: [], albums: [], }); }); } async function checkStoreLinksLike(req) { await req .post('/query') .send({ "query": { "prop": "songStoreLinks", "propOperator": "LIKE", "propOperand": 'llo m' }, 'offsetsLimits': { 'songOffset': 0, 'songLimit': 10, }, 'ordering': { 'orderBy': { 'type': 'name', }, 'ascending': true }, 'responseType': 'details', }) .then((res) => { expect(res).to.have.status(200); expect(res.body).to.deep.equal({ songs: [song1], artists: [], tags: [], albums: [], }); }); } async function checkResponseTypeIds(req) { await req .post('/query') .send({ "query": {}, 'offsetsLimits': { 'songOffset': 0, 'songLimit': 10, }, 'ordering': { 'orderBy': { 'type': 'name', }, 'ascending': true }, 'responseType': 'ids', }) .then((res) => { expect(res).to.have.status(200); expect(res.body).to.deep.equal({ songs: [song1.songId, song2.songId, song3.songId], artists: [], tags: [], albums: [], }); }); } async function checkResponseTypeCount(req) { await req .post('/query') .send({ "query": {}, 'offsetsLimits': { 'songOffset': 0, 'songLimit': 10, }, 'ordering': { 'orderBy': { 'type': 'name', }, 'ascending': true }, 'responseType': 'count', }) .then((res) => { expect(res).to.have.status(200); expect(res.body).to.deep.equal({ songs: 3, artists: 0, tags: 0, albums: 0, }); }); } 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], storeLinks: [ 'hello my', 'darling' ] }, 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); await checkStoreLinksLike(req); await checkResponseTypeCount(req); await checkResponseTypeIds(req); } finally { req.close(); agent.close(); done(); } }); });