paradigm-core
Version:
Paradigm Core Modules
217 lines (159 loc) • 5.21 kB
JavaScript
const {
Migrations,
DigitalAssetsPlugin,
TestHelpers: {MockHTTPServer, spyOnQueue, restoreQueue},
} = require('structure-core')
const pluginsList = require('../helpers/plugins')
const purgeUrlQueue = require('../../queues/purge-url')
const server = new MockHTTPServer(pluginsList)
const daTestApi = new DigitalAssetsPlugin.TestAPI(server)
const createOrgAndApp = async function() {
// getting an organization and application Ids
var res0 = await server
.post(`/api/${process.env.API_VERSION}/organizations`)
.send({
title: 'work it'
})
const org = res0.body.pkg
const orgId = org.id
var app = await server
.post(`/api/${process.env.API_VERSION}/applications`)
.set('organizationid', orgId)
.send({
desc: '',
title: 'App 45'
})
const appId = app.body.pkg.id
return {orgId, appId}
}
describe('Pages Routes', function() {
before(function() {
this.timeout(5000)
spyOnQueue(purgeUrlQueue)
})
beforeEach(function() {
this.migration = new Migrations({
db: 'test',
plugins: pluginsList
})
return this.migration.process()
})
afterEach(function() {
purgeUrlQueue.addSpy.resetHistory()
return this.migration.purge()
})
after(function() {
restoreQueue(purgeUrlQueue)
})
it('should create a page', async function() {
const {orgId, appId} = await createOrgAndApp()
var pkg = {
title: 'Just Testing',
slug: 'just-testing'
}
var res = await server
.post(`/api/${process.env.API_VERSION}/pages`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send(pkg)
expect(res.body.status).to.equal(201)
expect(res.body.pkg.title).to.equal(pkg.title)
expect(res.body.pkg.slug).to.equal(pkg.slug)
})
it('should get a page by id', async function() {
const {orgId, appId} = await createOrgAndApp()
const daRes = await daTestApi.create(orgId, appId)
const da = daRes.body.pkg
var pkg = {
title: 'Just Testing',
slug: 'just-testing',
imageId: da.id
}
var page = await server
.post(`/api/${process.env.API_VERSION}/pages`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send(pkg)
const pageId = page.body.pkg.id
var res = await server
.get(`/api/${process.env.API_VERSION}/pages/${pageId}`)
.set('organizationid', orgId)
.set('applicationid', appId)
expect(res.body.status).to.equal(200)
expect(res.body.pkg.title).to.equal(pkg.title)
expect(res.body.pkg.slug).to.equal(pkg.slug)
expect(res.body.pkg.image.id).to.equal(da.id)
})
it('should get a page by slug', async function() {
const {orgId, appId} = await createOrgAndApp()
const daRes = await daTestApi.create(orgId, appId)
const da = daRes.body.pkg
var pkg = {
title: 'Just Testing',
slug: 'just-testing',
imageId: da.id
}
var page = await server
.post(`/api/${process.env.API_VERSION}/pages`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send(pkg)
const pageId = page.body.pkg.id
const pageSlug = page.body.pkg.slug
var res = await server
.get(`/api/${process.env.API_VERSION}/pages/slug/${pageSlug}`)
.set('organizationid', orgId)
.set('applicationid', appId)
expect(res.body.status).to.equal(200)
expect(res.body.pkg.id).to.equal(pageId)
expect(res.body.pkg.title).to.equal(pkg.title)
expect(res.body.pkg.slug).to.equal(pkg.slug)
expect(res.body.pkg.image.id).to.equal(da.id)
})
it('should get all pages', async function() {
const {orgId, appId} = await createOrgAndApp()
var pkg = {
title: 'Just Testing',
slug: 'just-testing'
}
var app = await server
.post(`/api/${process.env.API_VERSION}/pages`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send(pkg)
var res = await server
.get(`/api/${process.env.API_VERSION}/pages`)
.set('organizationid', orgId)
.set('applicationid', appId)
expect(res.body.pkg.pages.length).to.be.above(0)
expect(res.body.status).to.equal(200)
})
it('should update a page by id, then purge', async function() {
const {orgId, appId} = await createOrgAndApp()
var pkg = {
title: 'Just Testing',
slug: 'just-testing'
}
var page = await server
.post(`/api/${process.env.API_VERSION}/pages`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send(pkg)
const pageId = page.body.pkg.id
purgeUrlQueue.addSpy.resetHistory()
var res = await server
.patch(`/api/${process.env.API_VERSION}/pages/${pageId}`)
.set('organizationid', orgId)
.set('applicationid', appId)
.send({
title: 'Peekaboo'
})
var res2 = await server
.get(`/api/${process.env.API_VERSION}/pages/${pageId}`)
.set('organizationid', orgId)
.set('applicationid', appId)
expect(res2.body.status).to.equal(200)
expect(res2.body.pkg.title).to.equal('Peekaboo')
expect(purgeUrlQueue.addSpy.args[0][0].pageSlug).to.equal(pkg.slug)
})
})