UNPKG

paradigm-core

Version:
214 lines (165 loc) 5.03 kB
const Highwinds = require('highwinds') const { r, Migrations, Cache: {cache}, OrganizationsPlugin, ApplicationsPlugin, TestHelpers: {MockHTTPServer}, } = require('structure-core') const pluginsList = require('../helpers/plugins') const TestAPI = require('../helpers/test-api') const server = new MockHTTPServer(pluginsList) const testApi = new TestAPI(server) const orgTestApi = new OrganizationsPlugin.TestAPI(server) const appTestApi = new ApplicationsPlugin.TestAPI(server) /** @test {ParadigmPurge} */ describe('Purge Routes', function() { before(function() { this.timeout(10000) this.sandbox = sinon.sandbox.create() this.purgeByUrl = this.sandbox.spy() this.invalidateApplication = this.sandbox.spy() this.sandbox.stub(Highwinds.prototype, 'purgeByUrl').callsFake(this.purgeByUrl) this.sandbox.stub(cache, 'invalidateApplication').callsFake(this.invalidateApplication) 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, { title: 'App 45' }) this.appId = appRes.body.pkg.id }) after(function() { this.sandbox.restore() }) afterEach(function() { this.purgeByUrl.resetHistory() this.invalidateApplication.resetHistory() return this.migration.purge() }) /** @test {ParadigmPurge#purgeAll} */ it('should purge all cdn and cache', async function() { await r .table('documents') .insert({ applicationId: this.appId, status: "published" }) const res = await testApi.purgeAll(this.orgId, this.appId, { hostname: 'static.example.com' }) expect(res.status).to.equal(201) expect(this.purgeByUrl.calledTwice).to.be.true expect(this.purgeByUrl).to.have.been.calledWith( '', { invalidateOnly: false, recursive: true } ) expect(this.invalidateApplication).to.have.been.calledWith(this.appId) const docs = await r .table('documents') .filter({ applicationId: this.appId, status: "published" }) expect(docs.length).to.equal(1) expect(docs[0].lastPurgedAt instanceof Date).to.be.true }) /** @test {ParadigmPurge#purgeAll} */ it('should purge all cdn and cache using application.host if hostname not supplied', async function() { await r .table('documents') .insert({ applicationId: this.appId, status: "published" }) const res = await testApi.purgeAll(this.orgId, this.appId) expect(res.status).to.equal(201) expect(this.purgeByUrl.calledTwice).to.be.true expect(this.purgeByUrl).to.have.been.calledWith( '', { invalidateOnly: false, recursive: true } ) expect(this.invalidateApplication).to.have.been.calledWith(this.appId) const docs = await r .table('documents') .filter({ applicationId: this.appId, status: "published" }) expect(docs.length).to.equal(1) expect(docs[0].lastPurgedAt instanceof Date).to.be.true }) /** @test {ParadigmPurge#purgeCdnAll} */ it('should purge all for cdn', async function() { await r .table('documents') .insert({ applicationId: this.appId, status: "published" }) const res = await testApi.purgeCdnAll(this.orgId, this.appId) expect(res.status).to.equal(201) expect(this.purgeByUrl).to.have.been.calledWith( '', { invalidateOnly: false, recursive: true } ) expect(this.invalidateApplication).not.to.have.been.called const docs = await r .table('documents') .filter({ applicationId: this.appId, status: "published" }) expect(docs.length).to.equal(1) expect(docs[0].lastPurgedAt instanceof Date).to.be.true }) /** @test {ParadigmPurge#purgeCdnUrl} */ it('should purge a url', async function() { const res = await testApi.purgeCdnUrl(this.orgId, this.appId, { url: 'www.example.com' }) expect(res.status).to.equal(201) expect(this.purgeByUrl).to.have.been.calledWith( 'www.example.com', { invalidateOnly: false, recursive: false } ) expect(this.invalidateApplication).not.to.have.been.called }) /** @test {ParadigmPurge#purgeCdnUrl} */ it('should purge a url recursively', async function() { const res = await testApi.purgeCdnUrl(this.orgId, this.appId, { url: 'www.example.com', recursive: true }) expect(res.status).to.equal(201) expect(this.purgeByUrl).to.have.been.calledWith( 'www.example.com', { invalidateOnly: false, recursive: true } ) expect(this.invalidateApplication).not.to.have.been.called }) })