UNPKG

paradigm-core

Version:
257 lines (199 loc) 6.29 kB
const { Migrations, OrganizationsPlugin, ApplicationsPlugin, UsersPlugin, TestHelpers: {MockHTTPServer}, } = require('structure-core') const pluginsList = require('../helpers/plugins') const DocumentsPlugin = require('../../../documents') 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) 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('Sitemap Routes', function() { beforeEach(function() { this.migration = new Migrations({ db: 'test', items: {tables: [{ action: 'create', table: 'digital_assets' }]}, plugins: pluginsList }) return this.migration.process() }) afterEach(function() { return this.migration.purge() }) it('should create a sitemap', async function() { const {orgId, appId} = await createOrgAndApp() const res = await server .post(`/api/${process.env.API_VERSION}/sitemaps`) .set('organizationid', orgId) .set('applicationid', appId) .send({}) expect(res.body.status).to.equal(201) }) it('should get a published sitemaps', async function() { const {orgId, appId} = await createOrgAndApp() const channelRes = await server .post(`/api/${process.env.API_VERSION}/channels`) .set('organizationid', orgId) .set('applicationid', appId) .send({ title: 'Foo', slug: 'foo', desc: 'foo things!' }) const channel = channelRes.body.pkg const userRes = await userTestApi.create(orgId, appId, { organizationId: orgId, username: 'tom3', email: 'voldemort3@horcruxes.r.us', password: 'c4ntf1ndmyc4v3br0' }) const user = userRes.body.pkg const unpublishedDocRes = await docTestApi.create(orgId, appId, { title: 'Harry Potter Lies', slug: 'harry-potter-lies', fields: [ { body: 'Harry Potter is a dirty squib', label: 'title', type: 'text-input' }, ], categoryIds: [], organizationId: orgId, userId: user.id, }) const docRes = await docTestApi.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: [], organizationId: orgId, userId: user.id, status: 'published' }) const doc = docRes.body.pkg const sitemapRes = await server .get(`/api/${process.env.API_VERSION}/sitemaps/published`) .set('organizationid', orgId) .set('applicationid', appId) expect(sitemapRes.body.status).to.equal(200) const sitemap = sitemapRes.body.pkg expect(sitemap.urls.length).to.equal(2) expect(sitemap.urls[0]).to.deep.equal({ changefreq: 'daily', priority: 0.7, url: '/foo', description: channel.desc }) expect(sitemap.urls[1]).to.deep.equal({ changefreq: 'monthly', priority: 0.3, url: doc.permalink, description: doc.title }) }) it('should get a sitemap by Id', async function() { const {orgId, appId} = await createOrgAndApp() const res0 = await server .post(`/api/${process.env.API_VERSION}/channels`) .set('organizationid', orgId) .set('applicationid', appId) .send({ title: 'Foo', slug: 'foo', desc: 'foo things!' }) const channel = res0.body.pkg const res1 = await server .post(`/api/${process.env.API_VERSION}/sitemaps`) .set('organizationid', orgId) .set('applicationid', appId) .send({ items: [ { id: channel.id, type: 'channel' } ] }) const sitemap = res1.body.pkg const res2 = await server .get(`/api/${process.env.API_VERSION}/sitemaps/${sitemap.id}`) .set('organizationid', orgId) .set('applicationid', appId) expect(res2.body.status).to.equal(200) expect(res2.body.pkg.urls[0]).to.deep.equal({ changefreq: 'daily', priority: 0.7, url: '/foo', description: 'foo things!' }) }) it('should get all sitemaps', async function() { const {orgId, appId} = await createOrgAndApp() var app = await server .post(`/api/${process.env.API_VERSION}/sitemaps`) .set('organizationid', orgId) .set('applicationid', appId) .send({}) var res = await server .get(`/api/${process.env.API_VERSION}/sitemaps`) .set('organizationid', orgId) .set('applicationid', appId) expect(res.body.pkg.sitemaps.length).to.be.above(0) expect(res.body.status).to.equal(200) }) it.skip('should update a sitemap by Id', async function() { const {orgId, appId} = await createOrgAndApp() const res1 = await server .post(`/api/${process.env.API_VERSION}/sitemaps`) .set('organizationid', orgId) .set('applicationid', appId) .send({}) const sitemap = res1.body.pkg const res2 = await server .get(`/api/${process.env.API_VERSION}/sitemaps/${sitemap.id}`) .set('organizationid', orgId) .set('applicationid', appId) expect(res2.body.status).to.equal(200) var res = await server .patch(`/api/${process.env.API_VERSION}/sitemaps/${sitemap.id}`) .set('organizationid', orgId) .set('applicationid', appId) .send({ foo: 'bar' }) const res3 = await server .get(`/api/${process.env.API_VERSION}/sitemaps/${sitemap.id}`) .set('organizationid', orgId) .set('applicationid', appId) expect(res3.body.pkg.foo).to.equal('bar') expect(res3.body.status).to.equal(200) }) })