UNPKG

paradigm-core

Version:
165 lines (122 loc) 4.79 kB
const { Migrations, OrganizationsPlugin, ApplicationsPlugin, UsersPlugin, TestHelpers: {MockHTTPServer, spyOnQueue, restoreQueue}, } = require('structure-core') const DocumentsPlugin = require('../../../documents') const pluginsList = require('../helpers/plugins') const createUpdateArticleQueue = require('../../queues/create-update-article') const deleteArticleQueue = require('../../queues/delete-article') const server = new MockHTTPServer(pluginsList) const orgTestApi = new OrganizationsPlugin.TestAPI(server) const appTestApi = new ApplicationsPlugin.TestAPI(server) const userTestApi = new UsersPlugin.TestAPI(server) const docTestApi = new DocumentsPlugin.TestAPIDocuments(server) /** @test {AppleNewsPlugin Hooks} */ describe('AppleNewsPlugin Hooks', function() { before(function() { this.timeout(10000) spyOnQueue(createUpdateArticleQueue) spyOnQueue(deleteArticleQueue) this.migration = new Migrations({ db: 'test', plugins: pluginsList }) return this.migration.process() }) beforeEach(async function() { const orgRes = await orgTestApi.create({ title: 'Forbidden Children' }) this.orgId = orgRes.body.pkg.id const appRes = await appTestApi.create(this.orgId, { desc: '', title: 'App 45', host: 'app45.com' }) this.appId = appRes.body.pkg.id const userRes = await userTestApi.create(this.orgId, this.appId, { organizationId: this.orgId, username: 'tom3', email: 'voldemort3@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0', timezone: 'Europe/Madrid' }) this.userId = userRes.body.pkg.id const docRes = await docTestApi.create(this.orgId, this.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: [], organizationId: this.orgId, userId: this.userId }) this.docId = docRes.body.pkg.id }) after(function() { restoreQueue(createUpdateArticleQueue) restoreQueue(deleteArticleQueue) }) afterEach(function() { createUpdateArticleQueue.addSpy.resetHistory() deleteArticleQueue.addSpy.resetHistory() return this.migration.purge() }) /** @test {Hooks#publish} */ it('should queue create update job after publish route called', async function() { const res = await docTestApi.publish(this.orgId, this.appId, this.docId) expect(res.status).to.equal(201) expect(createUpdateArticleQueue.addSpy).to.have.been.calledOnce expect(createUpdateArticleQueue.addSpy.args[0][0].documentId).to.equal(this.docId) }) /** @test {Hooks#updatePublish} */ it('should queue create update job after doc published via update', async function() { const res = await docTestApi.update(this.orgId, this.appId, this.docId, { status: 'published' }) expect(res.status).to.equal(200) expect(createUpdateArticleQueue.addSpy).to.have.been.calledOnce expect(createUpdateArticleQueue.addSpy.args[0][0].documentId).to.equal(this.docId) }) /** @test {Hooks#unpublish} */ it('should queue delete job after doc unpublish route called', async function() { const res = await docTestApi.unpublish(this.orgId, this.appId, this.docId) expect(res.status).to.equal(201) expect(deleteArticleQueue.addSpy).to.have.been.calledOnce expect(deleteArticleQueue.addSpy.args[0][0].documentId).to.equal(this.docId) }) /** @test {Hooks#updateUnpublish} */ it('should queue delete job after doc unpublished via update', async function() { const res = await docTestApi.update(this.orgId, this.appId, this.docId, { status: 'published' }) expect(res.status).to.equal(200) deleteArticleQueue.addSpy.resetHistory() const res2 = await docTestApi.update(this.orgId, this.appId, this.docId, { status: 'deleted' }) expect(res2.status).to.equal(200) expect(deleteArticleQueue.addSpy).to.have.been.calledOnce expect(deleteArticleQueue.addSpy.args[0][0].documentId).to.equal(this.docId) }) /** @test {Hooks#delete} */ it('should queue delete job after doc delete route called', async function() { const res = await docTestApi.update(this.orgId, this.appId, this.docId, { status: 'published' }) expect(res.status).to.equal(200) deleteArticleQueue.addSpy.resetHistory() const res2 = await docTestApi.delete(this.orgId, this.appId, this.docId) expect(res2.status).to.equal(200) expect(deleteArticleQueue.addSpy).to.have.been.calledOnce expect(deleteArticleQueue.addSpy.args[0][0].documentId).to.equal(this.docId) }) })