Skip to content

Instantly share code, notes, and snippets.

@i-van
Last active March 21, 2016 17:01
Show Gist options
  • Save i-van/e4bda249809c439df751 to your computer and use it in GitHub Desktop.
Save i-van/e4bda249809c439df751 to your computer and use it in GitHub Desktop.
'use strict';
let request = require('supertest')
, expect = require('chai').expect
, server = require('../../../api')
, Campaign = require('../../../app/models/campaign')
, AuthService = require('../../../app/services/auth');
describe('app/controllers/campaign', function() {
let token;
before(async function() {
token = await AuthService.generateToken('000000000000000000000001');
});
describe('list', function() {
before(function(done) {
Campaign.create({ externalId: '123-456-789' }, done);
});
after(function(done) {
Campaign.remove(done);
});
it('returns list of campaigns', function(done) {
request(server)
.get('/campaigns?limit=5')
.set('authentication', token)
.expect(function(res) {
let response = res.body
, campaign = response.data[0];
expect(response.data).to.have.length(1);
expect(response.totalItems).to.equal(1);
expect(response.currentPage).to.equal(1);
expect(response.itemsPerPage).to.equal(5);
expect(campaign._id).to.exist;
expect(campaign.created).to.exist;
expect(campaign.updated).to.exist;
expect(campaign.externalId).to.equal('123-456-789');
expect(campaign.settings.desiredCV).to.equal(0.05);
expect(campaign.settings.reliability).to.equal(0.95);
})
.expect(200)
.end(done);
});
});
describe('update', function() {
let id = '000000000000000000000001';
before(function (done) {
Campaign.create({ _id: id, externalId: '123-456-789', data: { name: 'Campaign Name' } }, done);
});
after(function(done) {
Campaign.remove(done);
});
it('updates campaign', function(done) {
request(server)
.put('/campaigns/' + id)
.set('authentication', token)
.field('settings.reliability', 0.98)
.field('settings.desiredCV', 0.02)
.field('settings.whitelistingThreshold', 0.05)
.field('settings.expectedROI', 2)
.field('settings.revenuePerConversion', 1.5)
.field('settings.biddingStrategy', Campaign.BIDDING_STRATEGY_AGGRESSIVE)
.expect(function(res) {
let campaign = res.body;
expect(campaign._id).to.exist;
expect(campaign.created).to.exist;
expect(campaign.updated).to.exist;
expect(campaign.externalId).to.equal('123-456-789');
expect(campaign.settings.reliability).to.equal(0.98);
expect(campaign.settings.desiredCV).to.equal(0.02);
expect(campaign.settings.whitelistingThreshold).to.equal(0.05);
expect(campaign.settings.expectedROI).to.equal(2);
expect(campaign.settings.revenuePerConversion).to.equal(1.5);
expect(campaign.settings.biddingStrategy).to.equal(Campaign.BIDDING_STRATEGY_AGGRESSIVE);
})
.expect(200)
.end(done);
});
it('does not update campaign with incorrect settings', function(done) {
request(server)
.put('/campaigns/' + id)
.set('authentication', token)
.field('settings.desiredCV', 1.5)
.field('settings.reliability', -2)
.expect(function(res) {
let response = res.body;
expect(response.message).to.equal('ValidationError');
expect(response.errors).to.have.length(7);
})
.expect(400)
.end(done);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment