prism-ad-campaigns
Version:
Prism Ad Campaigns
448 lines (343 loc) • 16 kB
JavaScript
const Migrations = require('structure-migrations')
const MockHTTPServer = require('../helpers/mock-http-server')
const ActivateAdCampaignJob = require('../../src/jobs/activate-ad-campaign')
const Queue = require('rethinkdb-job-queue')
const pluginsList = require('../helpers/plugins')
const FacebookApiModel = require('../../src/models/facebook-api-model')
const TestAPI = require('../helpers/test-api')
const OrgTestAPI = require('structure-organizations/test/helpers/test-api').default
const AppTestAPI = require('structure-applications/test/helpers/test-api').default
const DigitalAssetTestAPI = require('structure-digital-assets/test/helpers/test-api').default
const AdAccountTestAPI = require('prism-ad-accounts/test/helpers/test-api')
const server = new MockHTTPServer()
const testApi = new TestAPI(server)
const orgTestApi = new OrgTestAPI(server)
const appTestApi = new AppTestAPI(server)
const digitalAssetTestApi = new DigitalAssetTestAPI(server)
const adAccountTestApi = new AdAccountTestAPI(server)
/** @test {PrismAdCampaigns} */
describe('Routes', function() {
before(function() {
this.timeout(10000)
this.q = new Queue()
this.sandbox = sinon.sandbox.create()
this.queueSpy = this.sandbox.spy()
this.sandbox.stub(ActivateAdCampaignJob.prototype, 'queue', this.queueSpy)
this.api = {
createCampaign: sinon.sandbox.stub().returns({id: 1234}),
createAdImage: sinon.sandbox.stub().returns({images: {bytes: {hash: 'abcde'}}}),
createLinkAdCreative: sinon.sandbox.stub().returns({id: 1234}),
createAdSet: sinon.sandbox.stub().returns({id: 1234}),
createAd: sinon.sandbox.stub().returns({id: 1234}),
}
this.sandbox.stub(FacebookApiModel.prototype, 'getApi').callsFake(() => {
return this.api
})
this.migration = new Migrations({
db: 'test',
plugins: pluginsList
})
return this.migration.process()
})
beforeEach(async function() {
const orgRes = await orgTestApi.create({
title: 'Test Org'
})
this.orgId = orgRes.body.pkg.id
const appRes = await appTestApi.create(this.orgId, {
title: 'Test App',
})
this.appId = appRes.body.pkg.id
const daRes = await digitalAssetTestApi.getByUrl(
this.orgId,
this.appId,
'https://upload.wikimedia.org/wikipedia/commons/thumb/0/0b/Cat_poster_1.jpg/1280px-Cat_poster_1.jpg'
)
this.daId = daRes.body.pkg.id
const adAcctRes = await adAccountTestApi.create(this.orgId, this.appId, {
title: 'Test Ad Account',
facebookAdAccountId: '12345',
facebookCustomConversionId: '12345',
})
this.adAcctId = adAcctRes.body.pkg.id
})
after(function() {
this.sandbox.restore()
})
afterEach(function() {
this.queueSpy.reset()
this.api.createCampaign.resetHistory()
this.api.createAdImage.resetHistory()
this.api.createLinkAdCreative.resetHistory()
this.api.createAdSet.resetHistory()
this.api.createAd.resetHistory()
return this.migration.purge()
})
it('should not create an ad campaign; missing title', async function() {
const res = await testApi.create(this.orgId, this.appId, {
})
expect(res.body.err.code).to.equal('TITLE_MISSING')
expect(res.body.err.message).to.equal('A title is required')
expect(res.body.status).to.equal(404)
})
it('should not create an ad campaign; missing link', async function() {
const res = await testApi.create(this.orgId, this.appId, {
title: 'Test Ad Campaign',
})
expect(res.body.err.code).to.equal('LINK_MISSING')
expect(res.body.err.message).to.equal('A link is required')
expect(res.body.status).to.equal(404)
})
it('should not create an ad campaign; missing headlines', async function() {
const res = await testApi.create(this.orgId, this.appId, {
title: 'Test Ad Campaign',
link: 'http://www.distractify.com/trending/2018/01/15/Z2t28iy/optical-illusion-reddit',
})
expect(res.body.err.code).to.equal('HEADLINES_MISSING')
expect(res.body.err.message).to.equal('Headlines are required')
expect(res.body.status).to.equal(404)
})
it('should not create an ad campaign; missing texts', async function() {
const res = await testApi.create(this.orgId, this.appId, {
title: 'Test Ad Campaign',
link: 'http://www.distractify.com/trending/2018/01/15/Z2t28iy/optical-illusion-reddit',
headlines: ['Headline 1'],
})
expect(res.body.err.code).to.equal('TEXTS_MISSING')
expect(res.body.err.message).to.equal('Texts are required')
expect(res.body.status).to.equal(404)
})
it('should not create an ad campaign; missing digitalAssetIds', async function() {
const res = await testApi.create(this.orgId, this.appId, {
title: 'Test Ad Campaign',
link: 'http://www.distractify.com/trending/2018/01/15/Z2t28iy/optical-illusion-reddit',
headlines: ['Headline 1'],
texts: ['Text 1'],
})
expect(res.body.err.code).to.equal('DIGITALASSETIDS_MISSING')
expect(res.body.err.message).to.equal('Digital asset ids are required')
expect(res.body.status).to.equal(404)
})
it('should create an ad campaign', async function() {
const res = await testApi.create(this.orgId, this.appId, {
title: 'Test Ad Campaign',
link: 'http://www.distractify.com/trending/2018/01/15/Z2t28iy/optical-illusion-reddit',
headlines: ['Headline 1'],
texts: ['Text 1'],
digitalAssetIds: [this.daId],
adAccountId: this.adAcctId
})
expect(res.body.status).to.equal(201)
expect(res.body.pkg.organizationId).to.equal(this.orgId)
expect(res.body.pkg.applicationId).to.equal(this.appId)
expect(res.body.pkg.status).to.equal('draft')
expect(res.body.pkg.title).to.equal('Test Ad Campaign')
expect(res.body.pkg.link).to.equal('http://www.distractify.com/trending/2018/01/15/Z2t28iy/optical-illusion-reddit')
expect(res.body.pkg.headlines).to.deep.equal(['Headline 1'])
expect(res.body.pkg.texts).to.deep.equal(['Text 1'])
expect(res.body.pkg.digitalAssetIds).to.deep.equal([this.daId])
})
it('should fetch existing ad campaign if creating with same link', async function() {
const res = await testApi.create(this.orgId, this.appId, {
title: 'Test Ad Campaign',
link: 'http://www.distractify.com/trending/2018/01/15/Z2t28iy/optical-illusion-reddit',
headlines: ['Headline 1'],
texts: ['Text 1'],
digitalAssetIds: [this.daId],
})
expect(res.body.status).to.equal(201)
const res2 = await testApi.create(this.orgId, this.appId, {
title: 'Test Ad Campaign',
link: 'http://www.distractify.com/trending/2018/01/15/Z2t28iy/optical-illusion-reddit',
headlines: ['Headline 1'],
texts: ['Text 1'],
digitalAssetIds: [this.daId],
})
expect(res2.body.status).to.equal(201)
expect(res2.body.pkg.id).to.equal(res.body.pkg.id)
expect(res2.body.pkg.organizationId).to.equal(this.orgId)
expect(res2.body.pkg.applicationId).to.equal(this.appId)
expect(res2.body.pkg.status).to.equal('draft')
expect(res2.body.pkg.title).to.equal('Test Ad Campaign')
expect(res2.body.pkg.link).to.equal('http://www.distractify.com/trending/2018/01/15/Z2t28iy/optical-illusion-reddit')
expect(res2.body.pkg.headlines).to.deep.equal(['Headline 1'])
expect(res2.body.pkg.texts).to.deep.equal(['Text 1'])
expect(res2.body.pkg.digitalAssetIds).to.deep.equal([this.daId])
})
it('should clone ad campaign', async function() {
const res = await testApi.create(this.orgId, this.appId, {
title: 'Test Ad Campaign',
link: 'http://www.distractify.com/trending/2018/01/15/Z2t28iy/optical-illusion-reddit',
headlines: ['Headline 1'],
texts: ['Text 1'],
digitalAssetIds: [this.daId],
})
expect(res.body.status).to.equal(201)
const res2 = await testApi.create(this.orgId, this.appId, {
title: 'Test Ad Campaign',
link: 'http://www.distractify.com/trending/2018/01/15/Z2t28iy/optical-illusion-reddit',
headlines: ['Headline 1'],
texts: ['Text 1'],
digitalAssetIds: [this.daId],
clone: true
})
expect(res2.body.status).to.equal(201)
expect(res2.body.pkg.id).to.not.equal(res.body.pkg.id)
expect(res2.body.pkg.organizationId).to.equal(this.orgId)
expect(res2.body.pkg.applicationId).to.equal(this.appId)
expect(res2.body.pkg.status).to.equal('draft')
expect(res2.body.pkg.title).to.equal('Test Ad Campaign')
expect(res2.body.pkg.link).to.equal('http://www.distractify.com/trending/2018/01/15/Z2t28iy/optical-illusion-reddit')
expect(res2.body.pkg.headlines).to.deep.equal(['Headline 1'])
expect(res2.body.pkg.texts).to.deep.equal(['Text 1'])
expect(res2.body.pkg.digitalAssetIds).to.deep.equal([this.daId])
expect(res2.body.pkg.clone).to.be.undefined
})
it('should update an ad campaign', async function() {
const res = await testApi.create(this.orgId, this.appId, {
title: 'Test Ad Campaign',
link: 'http://www.distractify.com/trending/2018/01/15/Z2t28iy/optical-illusion-reddit',
headlines: ['Headline 1'],
texts: ['Text 1'],
digitalAssetIds: [this.daId],
adAccountId: this.adAcctId
})
expect(res.body.status).to.equal(201)
const res2 = await digitalAssetTestApi.getByUrl(
this.orgId,
this.appId,
'https://upload.wikimedia.org/wikipedia/commons/6/6e/Golde33443.jpg'
)
const daId2 = res2.body.pkg.id
const res3 = await testApi.update(this.orgId, this.appId, res.body.pkg.id, {
title: 'Updated Test Ad Campaign',
link: 'http://www.distractify.com/updated',
headlines: ['Headline 1', 'Headline 2'],
texts: ['Text 1', 'Text 2'],
digitalAssetIds: [this.daId, daId2],
})
expect(res3.body.status).to.equal(200)
expect(res3.body.pkg.organizationId).to.equal(this.orgId)
expect(res3.body.pkg.applicationId).to.equal(this.appId)
expect(res3.body.pkg.status).to.equal('draft')
expect(res3.body.pkg.title).to.equal('Updated Test Ad Campaign')
expect(res3.body.pkg.link).to.equal('http://www.distractify.com/updated')
expect(res3.body.pkg.headlines).to.deep.equal(['Headline 1', 'Headline 2'])
expect(res3.body.pkg.texts).to.deep.equal(['Text 1', 'Text 2'])
expect(res3.body.pkg.digitalAssetIds).to.deep.equal([this.daId, daId2])
})
it('should not update an active ad campaign', async function() {
const res = await testApi.create(this.orgId, this.appId, {
title: 'Test Ad Campaign',
link: 'http://www.distractify.com/trending/2018/01/15/Z2t28iy/optical-illusion-reddit',
headlines: ['Headline 1'],
texts: ['Text 1'],
digitalAssetIds: [this.daId],
adAccountId: this.adAcctId,
})
expect(res.body.status).to.equal(201)
const res2 = await testApi.update(this.orgId, this.appId, res.body.pkg.id, {
status: 'active'
})
expect(res2.body.status).to.equal(200)
expect(res2.body.pkg.status).to.equal('active')
const res3 = await testApi.update(this.orgId, this.appId, res.body.pkg.id, {
title: 'Updated Test Ad Campaign',
})
expect(res3.body.err.code).to.equal('CANT_UPDATE_ACTIVE_AD_CAMPAIGN')
expect(res3.body.err.message).to.equal("Can't update active ad campaign")
expect(res3.body.status).to.equal(400)
})
it('should get all ad campaigns', async function() {
const res = await testApi.create(this.orgId, this.appId, {
title: 'Test Ad Campaign',
link: 'http://www.distractify.com/trending/2018/01/15/Z2t28iy/optical-illusion-reddit',
headlines: ['Headline 1'],
texts: ['Text 1'],
digitalAssetIds: [this.daId],
adAccountId: this.adAcctId,
})
expect(res.body.status).to.equal(201)
const res2 = await testApi.getAll(this.orgId, this.appId)
expect(res2.body.status).to.equal(200)
const pkg = res2.body.pkg
expect(pkg.pagination.pages).to.equal(1)
expect(pkg.pagination.current).to.equal(1)
expect(pkg.pagination.total).to.equal(1)
expect(pkg.pagination.next).to.be.false
expect(pkg.pagination.previous).to.be.false
expect(pkg['ad-campaigns'].length).to.equal(1)
expect(pkg['ad-campaigns'][0].organizationId).to.equal(this.orgId)
expect(pkg['ad-campaigns'][0].applicationId).to.equal(this.appId)
expect(pkg['ad-campaigns'][0].status).to.equal('draft')
expect(pkg['ad-campaigns'][0].title).to.equal('Test Ad Campaign')
expect(pkg['ad-campaigns'][0].link).to.equal('http://www.distractify.com/trending/2018/01/15/Z2t28iy/optical-illusion-reddit')
expect(pkg['ad-campaigns'][0].headlines).to.deep.equal(['Headline 1'])
expect(pkg['ad-campaigns'][0].texts).to.deep.equal(['Text 1'])
expect(pkg['ad-campaigns'][0].adAccountId).to.equal(this.adAcctId)
expect(pkg['ad-campaigns'][0].adAccount.id).to.equal(this.adAcctId)
expect(pkg['ad-campaigns'][0].adAccount.title).to.equal('Test Ad Account')
})
it('should get an ad campaign', async function() {
const res = await testApi.create(this.orgId, this.appId, {
title: 'Test Ad Campaign',
link: 'http://www.distractify.com/trending/2018/01/15/Z2t28iy/optical-illusion-reddit',
headlines: ['Headline 1'],
texts: ['Text 1'],
digitalAssetIds: [this.daId],
adAccountId: this.adAcctId,
})
expect(res.body.status).to.equal(201)
const res2 = await testApi.get(this.orgId, this.appId, res.body.pkg.id)
expect(res2.body.status).to.equal(200)
expect(res2.body.pkg.organizationId).to.equal(this.orgId)
expect(res2.body.pkg.applicationId).to.equal(this.appId)
expect(res2.body.pkg.status).to.equal('draft')
expect(res2.body.pkg.title).to.equal('Test Ad Campaign')
expect(res2.body.pkg.link).to.equal('http://www.distractify.com/trending/2018/01/15/Z2t28iy/optical-illusion-reddit')
expect(res2.body.pkg.headlines).to.deep.equal(['Headline 1'])
expect(res2.body.pkg.texts).to.deep.equal(['Text 1'])
expect(res2.body.pkg.adAccountId).to.equal(this.adAcctId)
expect(res2.body.pkg.adAccount.id).to.equal(this.adAcctId)
expect(res2.body.pkg.adAccount.title).to.equal('Test Ad Account')
})
it('should not activate an ad campaign; missing adAccountId', async function() {
const res = await testApi.create(this.orgId, this.appId, {
title: 'Test Ad Campaign',
link: 'http://www.distractify.com/trending/2018/01/15/Z2t28iy/optical-illusion-reddit',
headlines: ['Headline 1', 'Headline 2'],
texts: ['Text 1', 'Text 2'],
digitalAssetIds: [this.daId],
})
const adCampaign = res.body.pkg
expect(res.body.status).to.equal(201)
const res2 = await testApi.update(this.orgId, this.appId, adCampaign.id, {
status: 'active'
})
expect(res2.body.err.code).to.equal('ADACCOUNTID_MISSING')
expect(res2.body.err.message).to.equal('An adAccountId is required')
expect(res2.body.status).to.equal(400)
})
it('should activate an ad campaign', async function() {
this.timeout(30000)
const res = await testApi.create(this.orgId, this.appId, {
title: 'Test Ad Campaign',
link: 'http://www.distractify.com/trending/2018/01/15/Z2t28iy/optical-illusion-reddit',
headlines: ['Headline 1', 'Headline 2'],
texts: ['Text 1', 'Text 2'],
digitalAssetIds: [this.daId],
adAccountId: this.adAcctId
})
const adCampaign = res.body.pkg
expect(res.body.status).to.equal(201)
const res2 = await testApi.update(this.orgId, this.appId, adCampaign.id, {
status: 'active'
})
expect(res2.body.status).to.equal(200)
expect(res2.body.pkg.status).to.equal('active')
expect(this.queueSpy).to.have.been.called.once
expect(this.queueSpy.args[0][0].organizationId).to.equal(this.orgId)
expect(this.queueSpy.args[0][0].applicationId).to.equal(this.appId)
expect(this.queueSpy.args[0][0].id).to.equal(adCampaign.id)
})
})