UNPKG

paradigm-core

Version:
1,887 lines (1,541 loc) 80.8 kB
const moment = require('moment-timezone') const { Migrations, OrganizationsPlugin, ApplicationsPlugin, UsersPlugin, TestHelpers: {MockHTTPServer, spyOnQueue, restoreQueue}, } = require('structure-core') const pluginsList = require('../../helpers/plugins') const purgeUrlQueue = require('../../../queues/purge-url') const TestAPI = require('../../helpers/test-api-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 createOrgAndApp = async function() { const orgRes = await orgTestApi.create({ title: 'work it' }) const orgId = orgRes.body.pkg.id const appRes = await appTestApi.create(orgId, { desc: '', title: 'App 45', host: 'app45.com' }) const appId = appRes.body.pkg.id return {orgId, appId} } describe('Documents Routes', function() { before(function() { this.timeout(5000) spyOnQueue(purgeUrlQueue) this.migration = new Migrations({ db: 'test', items: {tables: [{ action: 'create', table: 'digital_assets', indexes: [ 'applicationId', 'updatedAt' ] }]}, plugins: pluginsList }) return this.migration.process() }) after(function() { restoreQueue(purgeUrlQueue) }) afterEach(function() { purgeUrlQueue.addSpy.resetHistory() return this.migration.purge() }) it('should create a document', async function() { this.timeout(3000) // No idea why this test sometimes takes more than 2 seconds, while the rest dont :O const {orgId, appId} = await createOrgAndApp() const userRes = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'tom3', email: 'voldemort3@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0' }) const user = userRes.body.pkg const categoryRes = await server .post(`/api/${process.env.API_VERSION}/categories`) .set('organizationid', orgId) .set('applicationid', appId) .send({ slug: 'diaries', title: 'Diaries' }) const category = categoryRes.body.pkg const res = await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'harry-potter-facts', fields: [ { body: 'Why Harry Potter should have used his invisibility cloak more...', label: 'title', type: 'text-input' }, ], categoryIds: [category.id], organizationId: orgId, userId: user.id }) expect(res.body.status).to.equal(201) expect(res.body.pkg.applicationId).to.equal(appId) expect(res.body.pkg.organizationId).to.equal(orgId) expect(res.body.pkg.categoryIds).to.deep.equal([category.id]) }) it('should strip join keys from pkg', async function() { const {orgId, appId} = await createOrgAndApp() const userRes = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'tom3', email: 'voldemort3@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0' }) const user = userRes.body.pkg const res = await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'harry-potter-facts', fields: [ { body: 'Why Harry Potter should have used his invisibility cloak more...', label: 'title', type: 'text-input', }, ], categories: [{slug: 'horcruxes'}], categoryIds: [], featuredImage: 'myimg', facebookImage: 'myimg', twitterImage: 'myimg', user: 'myuser', organizationId: orgId, userId: user.id }) expect(res.body.status).to.equal(201) expect(res.body.pkg.applicationId).to.equal(appId) expect(res.body.pkg.organizationId).to.equal(orgId) expect(res.body.pkg.categories).to.deep.equal([]) expect(res.body.pkg.featuredImage).to.equal(null) expect(res.body.pkg.facebookImage).to.be.undefined expect(res.body.pkg.twitterImage).to.be.undefined expect(res.body.pkg.user).to.be.undefined }) it('should not create a document; slug missing', async function() { const {orgId, appId} = await createOrgAndApp() const res = await testApi.create(orgId, appId, { title: 'Harry Potter Facts', categoryIds: [], }) expect(res.body.status).to.equal(404) expect(res.body.err.code).to.equal("SLUG_MISSING") }) it('should not create a document; slug taken', async function() { const {orgId, appId} = await createOrgAndApp() const res = await testApi.create(orgId, appId, { title: 'my title', slug: 'my-slug', categoryIds: [], }) expect(res.body.status).to.equal(201) const res2 = await testApi.create(orgId, appId, { title: 'my title', slug: 'my-slug', categoryIds: [], }) expect(res2.body.status).to.equal(400) expect(res2.body.err.code).to.equal("DOCUMENT_SLUG_INVALID") }) it('should not create a document; slug empty', async function() { const {orgId, appId} = await createOrgAndApp() const res = await testApi.create(orgId, appId, { title: '', slug: '', categoryIds: [], }) expect(res.body.status).to.equal(400) expect(res.body.err.code).to.equal("SLUG_INVALID") }) it('should not create a document; slug invalid', async function() { const {orgId, appId} = await createOrgAndApp() const res = await testApi.create(orgId, appId, { slug: [], title: 'Harry Potter Facts', categoryIds: [], }) expect(res.body.status).to.equal(400) expect(res.body.err.code).to.equal("SLUG_INVALID") }) it('should not create a document; slug invalid pattern', async function() { const {orgId, appId} = await createOrgAndApp() const res = await testApi.create(orgId, appId, { slug: 'test?woops=nope', title: 'Harry Potter Facts', categoryIds: [], }) expect(res.body.status).to.equal(400) expect(res.body.err.code).to.equal("SLUG_INVALID") }) it('should not create a document; title missing', async function() { const {orgId, appId} = await createOrgAndApp() const res = await testApi.create(orgId, appId, { slug: 'harry-potter-facts', categoryIds: [], }) expect(res.body.status).to.equal(404) expect(res.body.err.code).to.equal("TITLE_MISSING") }) it('should not create a document; title invalid', async function() { const {orgId, appId} = await createOrgAndApp() const res = await testApi.create(orgId, appId, { slug: 'harry-potter-facts', title: [], categoryIds: [], }) expect(res.body.status).to.equal(400) expect(res.body.err.code).to.equal("TITLE_INVALID") }) it('should not create a document; categoryIds missing', async function() { const {orgId, appId} = await createOrgAndApp() const res = await testApi.create(orgId, appId, { slug: 'harry-potter-facts', title: 'Harry Potter Facts', }) expect(res.body.status).to.equal(404) expect(res.body.err.code).to.equal("CATEGORYIDS_MISSING") }) it('should not create a document; categoryIds invalid', async function() { const {orgId, appId} = await createOrgAndApp() const res = await testApi.create(orgId, appId, { slug: 'harry-potter-facts', title: 'Harry Potter Facts', categoryIds: 'whatnow', }) expect(res.body.status).to.equal(400) expect(res.body.err.code).to.equal("CATEGORYIDS_INVALID") }) it('should not create a document; fields invalid', async function() { const {orgId, appId} = await createOrgAndApp() const res = await testApi.create(orgId, appId, { slug: 'harry-potter-facts', title: 'Harry Potter Facts', categoryIds: [], fields: 'whatnow' }) expect(res.body.status).to.equal(400) expect(res.body.err.code).to.equal("FIELDS_INVALID") }) it('should not create a document; invalid categoryId scope', async function() { const {orgId, appId} = await createOrgAndApp() const userRes = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'tom3', email: 'voldemort3@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0' }) const user = userRes.body.pkg const categoryRes = await server .post(`/api/${process.env.API_VERSION}/categories`) .set('organizationid', orgId) .set('applicationid', appId) .send({ slug: 'diaries', title: 'Diaries' }) const category = categoryRes.body.pkg const res = await testApi.create(orgId, '12345', { title: 'Harry Potter Facts', slug: 'harry-potter-facts', fields: [ { body: 'Why Harry Potter should have used his invisibility cloak more...', label: 'title', type: 'text-input', }, ], categoryIds: [category.id], organizationId: orgId, userId: user.id }) expect(res.body.status).to.equal(400) expect(res.body.err.code).to.equal("ID_SCOPE_INVALID") }) it('should get a document by Id', async function() { const {orgId, appId} = await createOrgAndApp() const userRes = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'tom', email: 'voldemort@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0' }) const user = userRes.body.pkg const doc = await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'harry-potter-facts', categoryIds: [], fields: [ { body: 'Why Quidditch should have still be played during the Tri Wizard Tournament', label: 'title', type: 'text-input' }, ], organizationId: orgId, userId: user.id }) var docId = doc.body.pkg.id const res = await testApi.get(orgId, appId, docId) expect(res.body.pkg.fields[0].body).to.equal('Why Quidditch should have still be played during the Tri Wizard Tournament') expect(res.body.status).to.equal(200) }) it('should get a document by short Id', async function() { const {orgId, appId} = await createOrgAndApp() const userRes = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'tom', email: 'voldemort@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0' }) const user = userRes.body.pkg const doc = await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'harry-potter-facts', categoryIds: [], fields: [ { body: 'Why Quidditch should have still be played during the Tri Wizard Tournament', label: 'title', type: 'text-input' }, ], organizationId: orgId, userId: user.id }) var docSid = doc.body.pkg.sid const res = await testApi.get(orgId, appId, docSid) expect(res.body.pkg.fields[0].body).to.equal('Why Quidditch should have still be played during the Tri Wizard Tournament') expect(res.body.status).to.equal(200) }) it('should not get a document by short Id if wrong application', async function() { const {orgId, appId} = await createOrgAndApp() const app2Res = await appTestApi.create(orgId, { desc: '', title: 'App 45', host: 'app45.com' }) const app2Id = app2Res.body.pkg.id const userRes = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'tom', email: 'voldemort@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0' }) const user = userRes.body.pkg const doc = await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'harry-potter-facts', categoryIds: [], fields: [ { body: 'Why Quidditch should have still be played during the Tri Wizard Tournament', label: 'title', type: 'text-input' }, ], organizationId: orgId, userId: user.id }) var docSid = doc.body.pkg.sid const res = await testApi.get(orgId, app2Id, docSid) expect(res.body.status).to.equal(400) expect(res.body.err.code).to.equal('DOCUMENT_NOT_FOUND') }) it('should get all documents', async function() { const {orgId, appId} = await createOrgAndApp() const userRes = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'tom', email: 'voldemort@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0' }) const user = userRes.body.pkg const docRes = await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'harry-potter-facts', categoryIds: [], fields: [ { body: 'Tom Riddle had a secret diary that even he did not know about', label: 'title', type: 'text-input' }, ], organizationId: orgId, userId: user.id }) const doc = docRes.body.pkg const res = await testApi.getAllByUser(orgId, appId, user.sid) expect(res.body.status).to.equal(200) expect(res.body.pkg.documents.length).to.equal(1) expect(res.body.pkg.documents[0].id).to.equal(doc.id) }) it('should get all documents by user sid', async function() { const {orgId, appId} = await createOrgAndApp() const userRes = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'tom', email: 'voldemort@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0' }) const user = userRes.body.pkg const docRes = await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'harry-potter-facts', categoryIds: [], fields: [ { body: 'Tom Riddle had a secret diary that even he did not know about', label: 'title', type: 'text-input' }, ], organizationId: orgId, userId: user.id }) const doc = docRes.body.pkg const res = await testApi.getAllByUser(orgId, appId, user.sid) expect(res.body.status).to.equal(200) expect(res.body.pkg.documents.length).to.equal(1) expect(res.body.pkg.documents[0].id).to.equal(doc.id) }) it('should get all documents from correct organization', async function() { let {orgId, appId} = await createOrgAndApp() const org1Id = orgId const orgAppRes = await createOrgAndApp() const org2Id = orgAppRes.orgId const userRes = await userTestApi.create(orgId, appId, { username: 'tom', email: 'voldemort@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0' }) const user = userRes.body.pkg await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'harry-potter-facts', categoryIds: [], fields: [ { body: 'Tom Riddle had a secret diary that even he did not know about', label: 'title', type: 'text-input' }, ], organizationId: org1Id, userId: user.id }) await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'harry-potter-facts', categoryIds: [], fields: [ { body: 'What you probably missed about Snape in his last moments', label: 'title', type: 'text-input' }, ], organizationId: org2Id, userId: user.id }) const res = await testApi.getAll(orgId, appId) expect(res.body.status).to.equal(200) expect(res.body.pkg.documents.length).to.equal(1) expect(res.body.pkg.documents[0].organizationId).to.equal(org1Id) }) it('should paginate documents', async function() { const {orgId, appId} = await createOrgAndApp() const userRes = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'tom', email: 'voldemort@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0' }) const user = userRes.body.pkg const categoryRes = await server .post(`/api/${process.env.API_VERSION}/categories`) .set('organizationid', orgId) .set('applicationid', appId) .send({ slug: 'diaries', title: 'Diaries' }) const category = categoryRes.body.pkg const doc1Res = await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'tom-riddle-diary', categoryIds: [category.id], fields: [ { body: 'Tom Riddle had a secret diary that even he did not know about', label: 'title', type: 'text-input' }, ], organizationId: orgId, publishedAt: new Date().toISOString(), status: 'published', userId: user.id }) const doc1 = doc1Res.body.pkg const doc2Res = await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'ginny-weasly', categoryIds: [category.id], fields: [ { body: 'Ginny Weasely read more than just diaries...', label: 'title', type: 'text-input' }, ], organizationId: orgId, publishedAt: new Date().toISOString(), status: 'published', userId: user.id }) const doc2 = doc2Res.body.pkg const res1 = await testApi.getAll(orgId, appId, { page: 1, limit: 1 }) expect(res1.body.pkg.pagination.current).to.equal(1) expect(res1.body.pkg.pagination.pages).to.equal(2) expect(res1.body.pkg.pagination.total).to.equal(2) expect(res1.body.pkg.pagination.previous).to.be.false expect(res1.body.pkg.pagination.next).to.equal(2) expect(res1.body.pkg.documents.length).to.equal(1) expect(res1.body.pkg.documents[0].id).to.equal(doc2.id) expect(res1.body.status).to.equal(200) const res2 = await testApi.getAll(orgId, appId, { page: 2, limit: 1 }) expect(res2.body.pkg.pagination.current).to.equal(2) expect(res2.body.pkg.pagination.pages).to.equal(2) expect(res2.body.pkg.pagination.total).to.equal(2) expect(res2.body.pkg.documents.length).to.equal(1) expect(res2.body.pkg.pagination.previous).to.equal(1) expect(res2.body.pkg.pagination.next).to.be.false expect(res2.body.pkg.documents.length).to.equal(1) expect(res2.body.pkg.documents[0].id).to.equal(doc1.id) expect(res2.body.status).to.equal(200) }) it('should paginate documents when filtering by status', async function() { const {orgId, appId} = await createOrgAndApp() const userRes = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'tom', email: 'voldemort@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0' }) const user = userRes.body.pkg const categoryRes = await server .post(`/api/${process.env.API_VERSION}/categories`) .set('organizationid', orgId) .set('applicationid', appId) .send({ slug: 'diaries', title: 'Diaries' }) const category = categoryRes.body.pkg const doc1Res = await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'tom-riddle-diary', categoryIds: [category.id], fields: [ { body: 'Tom Riddle had a secret diary that even he did not know about', label: 'title', type: 'text-input' }, ], organizationId: orgId, publishedAt: new Date().toISOString(), status: 'published', userId: user.id }) const doc1 = doc1Res.body.pkg const doc2Res = await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'ginny-weasly', categoryIds: [category.id], fields: [ { body: 'Ginny Weasely read more than just diaries...', label: 'title', type: 'text-input' }, ], organizationId: orgId, publishedAt: new Date().toISOString(), status: 'published', userId: user.id }) const doc2 = doc2Res.body.pkg await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'snape-scandal', categoryIds: [category.id], fields: [ { body: 'You wont believe what Snape said in Potions today', label: 'title', type: 'text-input' }, ], organizationId: orgId, publishedAt: new Date().toISOString(), status: 'draft', userId: user.id }) const res1 = await testApi.getAll(orgId, appId, { page: 1, limit: 1, status: 'published', }) expect(res1.body.pkg.pagination.current).to.equal(1) expect(res1.body.pkg.pagination.pages).to.equal(2) expect(res1.body.pkg.pagination.total).to.equal(2) expect(res1.body.pkg.pagination.previous).to.be.false expect(res1.body.pkg.pagination.next).to.equal(2) expect(res1.body.pkg.documents.length).to.equal(1) expect(res1.body.pkg.documents[0].id).to.equal(doc2.id) expect(res1.body.status).to.equal(200) const res2 = await testApi.getAll(orgId, appId, { page: 2, limit: 1, status: 'published', }) expect(res2.body.pkg.pagination.current).to.equal(2) expect(res2.body.pkg.pagination.pages).to.equal(2) expect(res2.body.pkg.pagination.total).to.equal(2) expect(res2.body.pkg.documents.length).to.equal(1) expect(res2.body.pkg.pagination.previous).to.equal(1) expect(res2.body.pkg.pagination.next).to.be.false expect(res2.body.pkg.documents.length).to.equal(1) expect(res2.body.pkg.documents[0].id).to.equal(doc1.id) expect(res2.body.status).to.equal(200) }) it('should paginate documents when filtering by user', async function() { const {orgId, appId} = await createOrgAndApp() const user1Res = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'user1', email: 'user1@example.com', password: 'password1' }) const user1 = user1Res.body.pkg const user2Res = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'user2', email: 'user2@example.com', password: 'password2' }) const user2 = user2Res.body.pkg const categoryRes = await server .post(`/api/${process.env.API_VERSION}/categories`) .set('organizationid', orgId) .set('applicationid', appId) .send({ slug: 'diaries', title: 'Diaries' }) const category = categoryRes.body.pkg const doc1Res = await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'tom-riddle-diary', categoryIds: [category.id], fields: [ { body: 'Tom Riddle had a secret diary that even he did not know about', label: 'title', type: 'text-input' }, ], organizationId: orgId, publishedAt: new Date().toISOString(), status: 'published', userId: user1.id }) const doc1 = doc1Res.body.pkg await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'snape-scandal', categoryIds: [category.id], fields: [ { body: 'You wont believe what Snape said in Potions today', label: 'title', type: 'text-input' }, ], organizationId: orgId, publishedAt: new Date().toISOString(), status: 'draft', userId: user2.id }) const doc3Res = await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'ginny-weasly', categoryIds: [category.id], fields: [ { body: 'Ginny Weasely read more than just diaries...', label: 'title', type: 'text-input' }, ], organizationId: orgId, publishedAt: new Date().toISOString(), status: 'published', userId: user1.id }) const doc3 = doc3Res.body.pkg const res1 = await testApi.getAll(orgId, appId, { page: 1, limit: 1, userId: user1.id, }) expect(res1.body.pkg.pagination.current).to.equal(1) expect(res1.body.pkg.pagination.pages).to.equal(2) expect(res1.body.pkg.pagination.total).to.equal(2) expect(res1.body.pkg.pagination.previous).to.be.false expect(res1.body.pkg.pagination.next).to.equal(2) expect(res1.body.pkg.documents.length).to.equal(1) expect(res1.body.pkg.documents[0].id).to.equal(doc3.id) expect(res1.body.status).to.equal(200) const res2 = await testApi.getAll(orgId, appId, { page: 2, limit: 1, userId: user1.id, }) expect(res2.body.pkg.pagination.current).to.equal(2) expect(res2.body.pkg.pagination.pages).to.equal(2) expect(res2.body.pkg.pagination.total).to.equal(2) expect(res2.body.pkg.documents.length).to.equal(1) expect(res2.body.pkg.pagination.previous).to.equal(1) expect(res2.body.pkg.pagination.next).to.be.false expect(res2.body.pkg.documents.length).to.equal(1) expect(res2.body.pkg.documents[0].id).to.equal(doc1.id) expect(res2.body.status).to.equal(200) }) it('should paginate documents when filtering using user endpoint', async function() { const {orgId, appId} = await createOrgAndApp() const user1Res = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'user1', email: 'user1@example.com', password: 'password1' }) const user1 = user1Res.body.pkg const user2Res = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'user2', email: 'user2@example.com', password: 'password2' }) const user2 = user2Res.body.pkg const categoryRes = await server .post(`/api/${process.env.API_VERSION}/categories`) .set('organizationid', orgId) .set('applicationid', appId) .send({ slug: 'diaries', title: 'Diaries' }) const category = categoryRes.body.pkg const doc1Res = await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'tom-riddle-diary', categoryIds: [category.id], fields: [ { body: 'Tom Riddle had a secret diary that even he did not know about', label: 'title', type: 'text-input' }, ], organizationId: orgId, publishedAt: new Date().toISOString(), status: 'published', userId: user1.id }) const doc1 = doc1Res.body.pkg await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'snape-scandal', categoryIds: [category.id], fields: [ { body: 'You wont believe what Snape said in Potions today', label: 'title', type: 'text-input' }, ], organizationId: orgId, publishedAt: new Date().toISOString(), status: 'draft', userId: user2.id }) const doc3Res = await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'ginny-weasly', categoryIds: [category.id], fields: [ { body: 'Ginny Weasely read more than just diaries...', label: 'title', type: 'text-input' }, ], organizationId: orgId, publishedAt: new Date().toISOString(), status: 'published', userId: user1.id }) const doc3 = doc3Res.body.pkg const res1 = await testApi.getAllByUser(orgId, appId, user1.sid, { page: 1, limit: 1, }) expect(res1.body.pkg.pagination.current).to.equal(1) expect(res1.body.pkg.pagination.pages).to.equal(2) expect(res1.body.pkg.pagination.total).to.equal(2) expect(res1.body.pkg.pagination.previous).to.be.false expect(res1.body.pkg.pagination.next).to.equal(2) expect(res1.body.pkg.documents.length).to.equal(1) expect(res1.body.pkg.documents[0].id).to.equal(doc3.id) expect(res1.body.status).to.equal(200) const res2 = await testApi.getAllByUser(orgId, appId, user1.sid, { page: 2, limit: 1, }) expect(res2.body.pkg.pagination.current).to.equal(2) expect(res2.body.pkg.pagination.pages).to.equal(2) expect(res2.body.pkg.pagination.total).to.equal(2) expect(res2.body.pkg.documents.length).to.equal(1) expect(res2.body.pkg.pagination.previous).to.equal(1) expect(res2.body.pkg.pagination.next).to.be.false expect(res2.body.pkg.documents.length).to.equal(1) expect(res2.body.pkg.documents[0].id).to.equal(doc1.id) expect(res2.body.status).to.equal(200) }) it('should paginate documents when filtering by title', async function() { const {orgId, appId} = await createOrgAndApp() const userRes = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'tom', email: 'voldemort@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0' }) const user = userRes.body.pkg const categoryRes = await server .post(`/api/${process.env.API_VERSION}/categories`) .set('organizationid', orgId) .set('applicationid', appId) .send({ slug: 'diaries', title: 'Diaries' }) const category = categoryRes.body.pkg await testApi.create(orgId, appId, { title: 'Harry Potter Facts 1', slug: 'tom-riddle-diary', categoryIds: [category.id], fields: [ { body: 'Tom Riddle had a secret diary that even he did not know about', label: 'title', type: 'text-input' }, ], organizationId: orgId, publishedAt: new Date().toISOString(), userId: user.id }) await testApi.create(orgId, appId, { title: 'Hunger Games Facts', slug: 'bows-and-stuff', categoryIds: [category.id], fields: [ { body: 'Learn to use a bow like Katniss.', label: 'title', type: 'text-input' }, ], organizationId: orgId, publishedAt: new Date().toISOString(), userId: user.id }) await testApi.create(orgId, appId, { title: 'Harry Potter Facts 2', slug: 'snape-scandal', categoryIds: [category.id], fields: [ { body: 'You wont believe what Snape said in Potions today', label: 'title', type: 'text-input' }, ], organizationId: orgId, publishedAt: new Date().toISOString(), userId: user.id }) const res1 = await testApi.getAll(orgId, appId, { page: 1, limit: 1, title: 'Harry Potter' }) expect(res1.body.pkg.pagination.current).to.equal(1) expect(res1.body.pkg.pagination.pages).to.equal(2) expect(res1.body.pkg.pagination.total).to.equal(2) expect(res1.body.pkg.pagination.previous).to.be.false expect(res1.body.pkg.pagination.next).to.equal(2) expect(res1.body.pkg.documents.length).to.equal(1) expect(res1.body.pkg.documents[0].title).to.equal('Harry Potter Facts 2') expect(res1.body.status).to.equal(200) const res2 = await testApi.getAll(orgId, appId, { page: 2, limit: 1, title: 'Harry Potter' }) expect(res2.body.pkg.pagination.current).to.equal(2) expect(res2.body.pkg.pagination.pages).to.equal(2) expect(res2.body.pkg.pagination.total).to.equal(2) expect(res2.body.pkg.documents.length).to.equal(1) expect(res2.body.pkg.pagination.previous).to.equal(1) expect(res2.body.pkg.pagination.next).to.be.false expect(res2.body.pkg.documents.length).to.equal(1) expect(res2.body.pkg.documents[0].title).to.equal('Harry Potter Facts 1') expect(res2.body.status).to.equal(200) }) it('should paginate documents when filtering by user and status', async function() { const {orgId, appId} = await createOrgAndApp() const user1Res = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'user1', email: 'user1@example.com', password: 'password1' }) const user1 = user1Res.body.pkg const user2Res = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'user2', email: 'user2@example.com', password: 'password2' }) const user2 = user2Res.body.pkg const categoryRes = await server .post(`/api/${process.env.API_VERSION}/categories`) .set('organizationid', orgId) .set('applicationid', appId) .send({ slug: 'diaries', title: 'Diaries' }) const category = categoryRes.body.pkg const doc1Res = await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'tom-riddle-diary', categoryIds: [category.id], fields: [ { body: 'Tom Riddle had a secret diary that even he did not know about', label: 'title', type: 'text-input' }, ], organizationId: orgId, publishedAt: new Date().toISOString(), status: 'published', userId: user1.id }) const doc1 = doc1Res.body.pkg await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'voldemort-beach-pics', categoryIds: [category.id], fields: [ { body: 'Voldemort has trimmed up for the summer', label: 'title', type: 'text-input' }, ], organizationId: orgId, publishedAt: new Date().toISOString(), status: 'draft', userId: user1.id }) await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'snape-scandal', categoryIds: [category.id], fields: [ { body: 'You wont believe what Snape said in Potions today', label: 'title', type: 'text-input' }, ], organizationId: orgId, publishedAt: new Date().toISOString(), status: 'published', userId: user2.id }) const doc4Res = await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'ginny-weasly', categoryIds: [category.id], fields: [ { body: 'Ginny Weasely read more than just diaries...', label: 'title', type: 'text-input' }, ], organizationId: orgId, publishedAt: new Date().toISOString(), status: 'published', userId: user1.id }) const doc4 = doc4Res.body.pkg const res1 = await testApi.getAll(orgId, appId, { page: 1, limit: 1, userId: user1.id, status: 'published', }) expect(res1.body.pkg.pagination.current).to.equal(1) expect(res1.body.pkg.pagination.pages).to.equal(2) expect(res1.body.pkg.pagination.total).to.equal(2) expect(res1.body.pkg.pagination.previous).to.be.false expect(res1.body.pkg.pagination.next).to.equal(2) expect(res1.body.pkg.documents.length).to.equal(1) expect(res1.body.pkg.documents[0].id).to.equal(doc4.id) expect(res1.body.status).to.equal(200) const res2 = await testApi.getAll(orgId, appId, { page: 2, limit: 1, userId: user1.id, status: 'published', }) expect(res2.body.pkg.pagination.current).to.equal(2) expect(res2.body.pkg.pagination.pages).to.equal(2) expect(res2.body.pkg.pagination.total).to.equal(2) expect(res2.body.pkg.documents.length).to.equal(1) expect(res2.body.pkg.pagination.previous).to.equal(1) expect(res2.body.pkg.pagination.next).to.be.false expect(res2.body.pkg.documents[0].id).to.equal(doc1.id) expect(res2.body.status).to.equal(200) }) it('should get all documents by categories slug', async function() { const {orgId, appId} = await createOrgAndApp() const userRes = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'tom', email: 'voldemort@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0' }) const user = userRes.body.pkg const categoryRes = await server .post(`/api/${process.env.API_VERSION}/categories`) .set('organizationid', orgId) .set('applicationid', appId) .send({ slug: 'horcruxes', title: 'Horcruxes' }) const category = categoryRes.body.pkg await testApi.create(orgId, appId, { title: 'Harry Potter Facts', categoryIds: [category.id], fields: [ { body: 'Tom Riddle had a secret diary that even he did not know about', label: 'title', type: 'text-input' }, ], organizationId: orgId, slug: 'riddle-1', userId: user.id }) const res = await testApi.getAll(orgId, appId, { categorySlugs: category.slug }) expect(res.body.pkg.documents.length).to.be.above(0) expect(res.body.status).to.equal(200) expect(res.body.pkg.documents[0].categories[0].slug).to.equal('horcruxes') }) it('should get all documents by user', async function() { const {orgId, appId} = await createOrgAndApp() const user1Res = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'user1', email: 'user1@example.com', password: 'password1' }) const user1 = user1Res.body.pkg const user2Res = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'user2', email: 'user2@example.com', password: 'password2' }) const user2 = user2Res.body.pkg await testApi.create(orgId, appId, { title: 'Doc1', categoryIds: [], fields: [ { body: 'body 1', label: 'title', type: 'text-input' }, ], organizationId: orgId, slug: 'doc-1', userId: user1.id }) await testApi.create(orgId, appId, { title: 'Doc2', categoryIds: [], fields: [ { body: 'body 2', label: 'title', type: 'text-input' }, ], organizationId: orgId, slug: 'doc-2', userId: user1.id }) await testApi.create(orgId, appId, { title: 'Doc3', categoryIds: [], fields: [ { body: 'body 3', label: 'title', type: 'text-input' }, ], organizationId: orgId, slug: 'doc-3', userId: user2.id }) const res = await testApi.getAll(orgId, appId, { userId: user1.id }) expect(res.body.status).to.equal(200) expect(res.body.pkg.documents.length).to.equal(2) expect(res.body.pkg.documents[0].user.id).to.equal(user1.id) expect(res.body.pkg.documents[1].user.id).to.equal(user1.id) }) it('should get all documents by title search term', async function() { const {orgId, appId} = await createOrgAndApp() const userRes = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'tom', email: 'voldemort@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0' }) const user = userRes.body.pkg await testApi.create(orgId, appId, { title: 'Doc1 Foo', categoryIds: [], fields: [ { body: 'body 1', label: 'title', type: 'text-input' }, ], organizationId: orgId, slug: 'doc-1', userId: user.id }) await testApi.create(orgId, appId, { title: 'Doc2 Bar', categoryIds: [], fields: [ { body: 'body 2', label: 'title', type: 'text-input' }, ], organizationId: orgId, slug: 'doc-2', userId: user.id }) await testApi.create(orgId, appId, { title: 'Doc3 Foo', categoryIds: [], fields: [ { body: 'body 3', label: 'title', type: 'text-input' }, ], organizationId: orgId, slug: 'doc-3', userId: user.id }) const res = await testApi.getAll(orgId, appId, { title: 'Foo' }) expect(res.body.status).to.equal(200) expect(res.body.pkg.documents.length).to.equal(2) expect(res.body.pkg.documents[0].title).to.equal('Doc3 Foo') expect(res.body.pkg.documents[1].title).to.equal('Doc1 Foo') }) it('should get all documents by status', async function() { const {orgId, appId} = await createOrgAndApp() const userRes = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'tom', email: 'voldemort@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0' }) const user = userRes.body.pkg await testApi.create(orgId, appId, { title: 'Doc1', categoryIds: [], fields: [ { body: 'body 1', label: 'title', type: 'text-input' }, ], organizationId: orgId, slug: 'doc-1', status: 'draft', userId: user.id }) await testApi.create(orgId, appId, { title: 'Doc2', categoryIds: [], fields: [ { body: 'body 2', label: 'title', type: 'text-input' }, ], organizationId: orgId, slug: 'doc-2', status: 'scheduled', userId: user.id }) await testApi.create(orgId, appId, { title: 'Doc3', categoryIds: [], fields: [ { body: 'body 3', label: 'title', type: 'text-input' }, ], organizationId: orgId, slug: 'doc-3', status: 'published', userId: user.id }) const res = await testApi.getAll(orgId, appId, { status: 'published' }) expect(res.body.status).to.equal(200) expect(res.body.pkg.documents.length).to.equal(1) expect(res.body.pkg.documents[0].status).to.equal('published') }) it('should get all documents by status by user', async function() { const {orgId, appId} = await createOrgAndApp() const user1Res = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'user1', email: 'user1@example.com', password: 'password1' }) const user1 = user1Res.body.pkg const user2Res = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'user2', email: 'user2@example.com', password: 'password2' }) const user2 = user2Res.body.pkg await testApi.create(orgId, appId, { title: 'Doc1', categoryIds: [], fields: [ { body: 'body 1', label: 'title', type: 'text-input' }, ], organizationId: orgId, slug: 'doc-1', status: 'draft', userId: user1.id }) await testApi.create(orgId, appId, { title: 'Doc2', categoryIds: [], fields: [ { body: 'body 2', label: 'title', type: 'text-input' }, ], organizationId: orgId, slug: 'doc-2', status: 'published', userId: user1.id }) await testApi.create(orgId, appId, { title: 'Doc3', categoryIds: [], fields: [ { body: 'body 3', label: 'title', type: 'text-input' }, ], organizationId: orgId, slug: 'doc-3', status: 'published', userId: user2.id }) const res = await testApi.getAll(orgId, appId, { userId: user1.id, status: 'published' }) expect(res.body.status).to.equal(200) expect(res.body.pkg.documents.length).to.equal(1) expect(res.body.pkg.documents[0].user.id).to.equal(user1.id) expect(res.body.pkg.documents[0].status).to.equal('published') }) it('should get no documents by categories slug', async function() { const {orgId, appId} = await createOrgAndApp() const userRes = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'tom', email: 'voldemort@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0' }) const user = userRes.body.pkg const categoryRes = await server .post(`/api/${process.env.API_VERSION}/categories`) .set('organizationid', orgId) .set('applicationid', appId) .send({ slug: 'horcruxes', title: 'Horcruxes' }) const category = categoryRes.body.pkg await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'harry-potter-facts', categoryIds: [category.id], fields: [ { body: 'Tom Riddle had a secret diary that even he did not know about', label: 'title', type: 'text-input' }, ], organizationId: orgId, userId: user.id }) const res = await testApi.getAll(orgId, appId, { categorySlugs: 'hallows', taxonomyOperator: 'and' }) expect(res.body.pkg.documents.length).to.equal(0) expect(res.body.status).to.equal(200) }) it('should get all documents by tags slug', async function() { const {orgId, appId} = await createOrgAndApp() const userRes = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'tom', email: 'voldemort@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0' }) const user = userRes.body.pkg await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'harry-potter-facts', categoryIds: [], fields: [ { body: 'Tom Riddle had a secret diary that even he did not know about', label: 'title', type: 'text-input' }, ], organizationId: orgId, tags: ['hex'], userId: user.id }) const res = await testApi.getAll(orgId, appId, { tags: 'hex' }) expect(res.body.pkg.documents.length).to.be.above(0) expect(res.body.status).to.equal(200) }) it('should get no documents by tags slug', async function() { const {orgId, appId} = await createOrgAndApp() const userRes = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'tom', email: 'voldemort@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0' }) const user = userRes.body.pkg await testApi.create(orgId, appId, { title: 'Harry Potter Facts', slug: 'harry-potter-facts', categoryIds: [], fields: [ { body: 'Tom Riddle had a secret diary that even he did not know about', label: 'title', type: 'text-input' }, ], organizationId: orgId, tags: ['hexx'], userId: user.id }) const res = await testApi.getAll(orgId, appId, { tags: 'foo', taxonomyOperator: 'and' }) expect(res.body.pkg.documents.length).to.equal(0) expect(res.body.status).to.equal(200) }) it('should get all documents by categories & tags (taxonomy operator)', async function() { const {orgId, appId} = await createOrgAndApp() const userRes = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'tom', email: 'voldemort@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0' }) const user = userRes.body.pkg const categoryRes = await server .post(`/api/${process.env.API_VERSION}/categories`) .set('organizationid', orgId) .set('applicationid', appId) .send({ slug: 'horcruxes', title: 'Horcruxes' }) const category = categoryRes.body.pkg await testApi.create(orgId, appId, { title: 'Harry Potter Facts', categoryIds: [category.id], fields: [ { body: 'Tom Riddle had a secret diary that even he did not know about', label: 'title', type: 'text-input' }, ], organizationId: orgId, slug: 'riddle-1', tags: ['horcruxes'], userId: user.id }) await testApi.create(orgId, appId, { title: 'Harry Potter Facts', categoryIds: [category.id], fields: [ { body: 'Diaries are fun', label: 'title', type: 'text-input' }, ], organizationId: orgId, slug: 'riddle-2', tags: ['fun'], userId: user.id }) const res = await testApi.getAll(orgId, appId, { categorySlugs: category.slug, tags: 'fun', taxonomyOperator: 'and' }) expect(res.body.pkg.documents.length).to.equal(1) expect(res.body.status).to.equal(200) const res2 = await testApi.getAll(orgId, appId, { categorySlugs: category.slug, tags: 'horcruxes', }) expect(res2.body.pkg.documents.length).to.equal(2) expect(res2.body.status).to.equal(200) }) it('should get all documents by categories (taxonomy operator)', async function() { const {orgId, appId} = await createOrgAndApp() const userRes = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'tom', email: 'voldemort@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0' }) const user = userRes.body.pkg const category1Res = await server .post(`/api/${process.env.API_VERSION}/categories`) .set('organizationid', orgId) .set('applicationid', appId) .send({ slug: 'cat1', title: 'Category 1' }) const category1 = category1Res.body.pkg const category2Res = await server .post(`/api/${process.env.API_VERSION}/categories`) .set('organizationid', orgId) .set('applicationid', appId) .send({ slug: 'cat2', title: 'Category 2' }) const category2 = category2Res.body.pkg await testApi.create(orgId, appId, { title: 'Harry Potter Facts', categoryIds: [category1.id], fields: [ { body: 'Tom Riddle had a secret diary that even he did not know about', label: 'title', type