UNPKG

paradigm-core

Version:
170 lines (133 loc) 4.4 kB
const { Migrations, OrganizationsPlugin, ApplicationsPlugin, UsersPlugin, TestHelpers: {MockHTTPServer}, } = require('structure-core') const pluginsList = require('../helpers/plugins') const TestAPI = require('../helpers/test-api') const DocumentsPlugin = require('../../../documents') const server = new MockHTTPServer(pluginsList) const testApi = new TestAPI(server) const orgTestApi = new OrganizationsPlugin.TestAPI(server) const appTestApi = new ApplicationsPlugin.TestAPI(server) const userTestApi = new UsersPlugin.TestAPI(server) const docTestApi = new DocumentsPlugin.TestAPIDocuments(server) describe('Search Routes', function() { before(function() { this.migration = new Migrations({ db: 'test', plugins: pluginsList }) return this.migration.process() }) beforeEach(async function() { const orgRes = await orgTestApi.create({ title: 'work it' }) this.orgId = orgRes.body.pkg.id const appRes = await appTestApi.create(this.orgId, { desc: '', title: 'App 1', host: 'app1.com' }) this.appId = appRes.body.pkg.id const app2Res = await appTestApi.create(this.orgId, { desc: '', title: 'App 2', host: 'app2.com' }) this.app2Id = app2Res.body.pkg.id const userRes = await userTestApi.create(this.orgId, this.appId, { organizationId: this.orgId, username: 'tom3', email: 'voldemort3@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0' }) this.userId = userRes.body.pkg.id }) afterEach(function() { return this.migration.purge() }) it('should search for document by title', async function() { await docTestApi.create(this.orgId, this.appId, { mode: 'slideshow', slug: 'harry-potters-best-kept-secret', categoryIds: [], organizationId: this.orgId, title: 'Harry Potters best kept secret', userId: this.userId }) const docRes = await docTestApi.create(this.orgId, this.appId, { mode: 'slideshow', slug: '26-diaries', categoryIds: [], organizationId: this.orgId, title: '26 diaries that Harry Potter never read', userId: this.userId }) const docId = docRes.body.pkg.id const res = await testApi.search(this.orgId, this.appId, { title: 'diaries' }) expect(res.body.status).to.equal(200) expect(res.body.pkg.length).to.equal(1) expect(res.body.pkg[0].id).to.equal(docId) }) it('should only return documents of correct application', async function() { await docTestApi.create(this.orgId, this.app2Id, { mode: 'slideshow', slug: '27-diaries', categoryIds: [], organizationId: this.orgId, title: '27 diaries that Harry Potter never read', userId: this.userId }) const docRes = await docTestApi.create(this.orgId, this.appId, { mode: 'slideshow', slug: '26-diaries', categoryIds: [], organizationId: this.orgId, title: '26 diaries that Harry Potter never read', userId: this.userId }) const docId = docRes.body.pkg.id const res = await testApi.search(this.orgId, this.appId, { title: 'diaries' }) expect(res.body.status).to.equal(200) expect(res.body.pkg.length).to.equal(1) expect(res.body.pkg[0].id).to.equal(docId) }) it('should return a maximum of 20 matches', async function() { for (let i=0; i < 21; i++) { await docTestApi.create(this.orgId, this.appId, { mode: 'slideshow', slug: `${i}-diaries`, categoryIds: [], organizationId: this.orgId, title: `${i} diaries that Harry Potter never read`, userId: this.userId }) } const res = await testApi.search(this.orgId, this.appId, { title: 'diaries' }) expect(res.body.status).to.equal(200) expect(res.body.pkg.length).to.equal(20) }) it('should search with empty string if title not provided', async function() { await docTestApi.create(this.orgId, this.appId, { mode: 'slideshow', slug: '26-diaries', categoryIds: [], organizationId: this.orgId, title: '26 diaries that Harry Potter never read', userId: this.userId }) const res = await testApi.search(this.orgId, this.appId) expect(res.body.status).to.equal(200) expect(res.body.pkg.length).to.equal(1) }) })