paradigm-facebook-ia
Version:
Paradigm Facebook Instant Articles
316 lines (245 loc) • 8.95 kB
JavaScript
const Migrations = require('structure-migrations')
const MockHTTPServer = require('../helpers/mock-http-server')
const pluginsList = require('../helpers/plugins')
const OrgTestAPI = require('structure-organizations/test/helpers/test-api').default
const AppTestAPI = require('structure-applications/test/helpers/test-api').default
const UserTestAPI = require('structure-users/test/helpers/test-api').default
const DocTestAPI = require('paradigm-documents/test/helpers/test-api-documents')
const FacebookInstantArticleModel = require('../../src/models/facebook-instant-article')
const FacebookIAAPI = require('../../src/lib/facebook-ia-api')
const server = new MockHTTPServer()
const orgTestApi = new OrgTestAPI(server)
const appTestApi = new AppTestAPI(server)
const userTestApi = new UserTestAPI(server)
const docTestApi = new DocTestAPI(server)
/** @test {FacebookInstantArticleModel} */
describe('FacebookInstantArticleModel', function() {
before(function() {
this.timeout(10000)
this.migration = new Migrations({
db: 'test',
items: {tables: [{
action: 'create',
table: 'digital_assets',
indexes: [
'applicationId',
'updatedAt'
]
}]},
plugins: pluginsList
})
this.sandbox = sinon.sandbox.create()
this.createArticleSpy = this.sandbox.spy()
this.getArticleImportStatusSpy = this.sandbox.spy()
this.deleteArticleSpy = this.sandbox.spy()
this.sandbox.stub(FacebookIAAPI.prototype, 'createArticle', (...args) => {
this.createArticleSpy(...args)
return {id: '1234'}
}) // surely there's a better way to do this :|
this.sandbox.stub(FacebookIAAPI.prototype, 'getArticleImportStatus', (...args) => {
this.getArticleImportStatusSpy(...args)
return {id: '1234'}
})
this.sandbox.stub(FacebookIAAPI.prototype, 'deleteArticle', (...args) => {
this.deleteArticleSpy(...args)
return {success: true}
})
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',
facebook: {},
})
this.appId = appRes.body.pkg.id
const userRes = await userTestApi.create(this.orgId, this.appId, {
organizationId: this.orgId,
username: 'jsmith',
firstName: 'John',
lastName: 'Smith',
email: 'jsmith@example.com',
password: 'c4ntf1ndmyc4v3br0',
timezone: 'Europe/Madrid'
})
this.user = userRes.body.pkg
})
afterEach(function() {
this.createArticleSpy.reset()
this.getArticleImportStatusSpy.reset()
this.deleteArticleSpy.reset()
return this.migration.purge()
})
after(function() {
this.sandbox.restore()
})
beforeEach(async function() {
const docRes = await docTestApi.create(this.orgId, this.appId, {
title: 'Basic Article',
slug: 'basic-article',
fields: [
{
type: 'text',
props: {
value: '<p>Text 1</p>'
}
},
{
type: 'text',
props: {
value: '<p>Text 2</p>'
}
}
],
categoryIds: [],
status: 'published',
organizationId: this.orgId,
userId: this.user.id
})
this.doc = docRes.body.pkg
})
/** @test {FacebookInstantArticleModel#create} */
it('should create an article', async function() {
this.timeout(30000)
const facebookInstantArticleModel = new FacebookInstantArticleModel({
organizationId: this.orgId,
applicationId: this.appId,
logger: this.logger
})
const fbia = await facebookInstantArticleModel.createOrUpdateForDocument(this.doc.id)
expect(this.createArticleSpy).to.have.been.calledOnce
expect(this.deleteArticleSpy).to.not.have.been.calledOnce
expect(fbia.articleImportId).to.equal('1234')
expect(fbia.importStatus).to.equal('importing')
})
/** @test {FacebookInstantArticleModel#update} */
it('should not update an unchanged article', async function() {
this.timeout(30000)
const facebookInstantArticleModel = new FacebookInstantArticleModel({
organizationId: this.orgId,
applicationId: this.appId,
logger: this.logger
})
const fbia = await facebookInstantArticleModel.createOrUpdateForDocument(this.doc.id)
await facebookInstantArticleModel.updateById(fbia.id, {
importStatus: 'success',
articleId: '5678'
})
const fbia2 = await facebookInstantArticleModel.createOrUpdateForDocument(this.doc.id)
expect(fbia2.importStatus).to.equal('success')
expect(fbia2.articleId).to.equal('5678')
expect(this.createArticleSpy).to.have.been.calledOnce
expect(this.deleteArticleSpy.called).to.be.false
})
/** @test {FacebookInstantArticleModel#update} */
it('should update a changed article', async function() {
this.timeout(30000)
const facebookInstantArticleModel = new FacebookInstantArticleModel({
organizationId: this.orgId,
applicationId: this.appId,
logger: this.logger
})
const fbia = await facebookInstantArticleModel.createOrUpdateForDocument(this.doc.id)
await facebookInstantArticleModel.updateById(fbia.id, {
importStatus: 'success',
articleId: '5678'
})
await docTestApi.update(this.orgId, this.appId, this.doc.id, {
title: 'Basic Article',
slug: 'basic-article',
fields: [
{
type: 'text',
props: {
value: '<p>Text 1</p>'
}
},
{
type: 'text',
props: {
value: '<p>Text 2</p>'
}
},
{
type: 'text',
props: {
value: '<p>Text 3</p>'
}
}
],
})
const fbia2 = await facebookInstantArticleModel.createOrUpdateForDocument(this.doc.id)
expect(fbia2.importStatus).to.equal('importing')
expect(fbia2.articleId).to.be.null
expect(fbia2.articleImportId).to.equal('1234')
expect(this.createArticleSpy).to.have.been.calledTwice
expect(this.deleteArticleSpy.called).to.be.false
})
/** @test {FacebookInstantArticleModel#update} */
it('should update a changed article with a new permalink', async function() {
this.timeout(30000)
const facebookInstantArticleModel = new FacebookInstantArticleModel({
organizationId: this.orgId,
applicationId: this.appId,
logger: this.logger
})
const fbia = await facebookInstantArticleModel.createOrUpdateForDocument(this.doc.id)
await facebookInstantArticleModel.updateById(fbia.id, {
importStatus: 'success',
articleId: '5678'
})
await docTestApi.update(this.orgId, this.appId, this.doc.id, {
title: 'Basic Article Updated',
slug: 'basic-article-updated'
})
const fbia2 = await facebookInstantArticleModel.createOrUpdateForDocument(this.doc.id)
expect(fbia2.importStatus).to.equal('importing')
expect(fbia2.articleId).to.be.null
expect(fbia2.articleImportId).to.equal('1234')
expect(this.createArticleSpy).to.have.been.calledTwice
expect(this.deleteArticleSpy).to.have.been.calledOnce
})
/** @test {FacebookInstantArticleModel#delete} */
it('should error on deleting article that hasnt imported yet', function(done) {
(async () => {
this.timeout(30000)
const facebookInstantArticleModel = new FacebookInstantArticleModel({
organizationId: this.orgId,
applicationId: this.appId,
logger: this.logger
})
await facebookInstantArticleModel.createOrUpdateForDocument(this.doc.id)
try {
await facebookInstantArticleModel.deleteForDocument(this.doc.id)
} catch(e) {
expect(e).to.equal(`Article not finished importing, can't delete yet`)
done()
}
expect(this.createArticleSpy.called).to.be.false
expect(this.deleteArticleSpy.called).to.be.false
})()
})
/** @test {FacebookInstantArticleModel#delete} */
it('should delete a basic article', async function() {
this.timeout(30000)
const facebookInstantArticleModel = new FacebookInstantArticleModel({
organizationId: this.orgId,
applicationId: this.appId,
logger: this.logger
})
const fbia = await facebookInstantArticleModel.createOrUpdateForDocument(this.doc.id)
const fbia2 = await facebookInstantArticleModel.updateById(fbia.id, {
importStatus: 'success',
articleId: '5678'
})
await facebookInstantArticleModel.deleteForDocument(this.doc.id)
expect(this.createArticleSpy).to.have.been.calledOnce
expect(this.deleteArticleSpy).to.have.been.calledOnce
expect(this.deleteArticleSpy.args[0][0]).to.equal(fbia2.articleId)
})
})