UNPKG

prism-ad-campaigns

Version:
235 lines (170 loc) 7.03 kB
const r = require('structure-driver') const Migrations = require('structure-migrations') const MockHTTPServer = require('../helpers/mock-http-server') 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 Logger = require('structure-logger') const AdCampaignModel = require('../../src/models/ad-campaign') 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('Models', function() { before(function() { this.timeout(10000) this.logger = new Logger() this.q = new Queue() this.sandbox = sinon.sandbox.create() this.api = { createCampaign: sinon.sandbox.stub().returns({id: 'abc'}), createAdImage: sinon.sandbox.stub().returns({images: {bytes: {hash: 'def'}}}), createLinkAdCreative: sinon.sandbox.stub().returns({id: 'ghi'}), createAdSet: sinon.sandbox.stub().returns({id: 'jkl'}), createAd: sinon.sandbox.stub().returns({id: 'lmn'}), } this.getApi = 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.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 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 adCampaignModel = new AdCampaignModel({ organizationId: this.orgId, applicationId: this.appId, logger: this.logger }) const res2 = await adCampaignModel.activate(adCampaign.id) expect(res2.status).to.equal('active') const res3 = await testApi.get(this.orgId, this.appId, adCampaign.id) expect(res3.body.status).to.equal(200) expect(res2.status).to.equal('active') const adCreatives = await r .table('link_ad_campaigns_ad_creatives') .getAll(adCampaign.id, {index: 'adCampaignId'}) expect(adCreatives.length).to.equal(4) const adSets = await r .table('link_ad_campaigns_ad_sets') .getAll(adCampaign.id, {index: 'adCampaignId'}) expect(adSets.length).to.equal(1) for (const adSet of adSets) { const ads = await r .table('link_ad_sets_ads') .getAll(adSet.adSetId, {index: 'adSetId'}) expect(ads.length).to.equal(4) } expect(this.api.createCampaign.callCount).to.equal(1) expect(this.api.createAdImage.callCount).to.equal(1) expect(this.api.createLinkAdCreative.callCount).to.equal(4) for (const argList of this.api.createLinkAdCreative.args) { expect(argList[0].urlTags).to.equal('utm_source=kyle_all_con&utm_campaign=jkl') } expect(this.api.createAdSet.callCount).to.equal(1) expect(this.api.createAd.callCount).to.equal(4) expect(this.getApi.callCount).to.equal(11) for (const argList of this.getApi.args) { expect(argList).to.deep.equal([this.adAcctId]) } }) it('should not activate a campaign twice if activate called again', 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 adCampaignModel = new AdCampaignModel({ organizationId: this.orgId, applicationId: this.appId, logger: this.logger }) const res2 = await adCampaignModel.activate(adCampaign.id) expect(res2.status).to.equal('active') expect(res2.id).to.equal(adCampaign.id) const res3 = await adCampaignModel.activate(adCampaign.id) expect(res3.id).to.equal(adCampaign.id) const res4 = await testApi.get(this.orgId, this.appId, adCampaign.id) expect(res4.body.status).to.equal(200) expect(res4.body.pkg.status).to.equal('active') const adCreatives = await r .table('link_ad_campaigns_ad_creatives') .getAll(adCampaign.id, {index: 'adCampaignId'}) expect(adCreatives.length).to.equal(4) const adSets = await r .table('link_ad_campaigns_ad_sets') .getAll(adCampaign.id, {index: 'adCampaignId'}) expect(adSets.length).to.equal(1) for (const adSet of adSets) { const ads = await r .table('link_ad_sets_ads') .getAll(adSet.adSetId, {index: 'adSetId'}) expect(ads.length).to.equal(4) } expect(this.api.createCampaign.callCount).to.equal(1) expect(this.api.createAdImage.callCount).to.equal(1) expect(this.api.createLinkAdCreative.callCount).to.equal(4) expect(this.api.createAdSet.callCount).to.equal(1) expect(this.api.createAd.callCount).to.equal(4) }) })