Skip to content

Commit

Permalink
feat: add campaign http router
Browse files Browse the repository at this point in the history
  • Loading branch information
lykmapipo committed Jun 16, 2019
1 parent 236f23d commit 65a0d3c
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const common = require('./lib/common');
const Message = require('./lib/message.model');
const messageRouter = require('./lib/message.http.router');
const Campaign = require('./lib/campaign.model');
const campaignRouter = require('./lib/campaign.http.router');


/* constants */
Expand Down Expand Up @@ -38,6 +39,10 @@ function postman() {
postman.Campaign = Campaign;


/* export postman message http router */
postman.campaignRouter = campaignRouter;


/* export postman message model */
postman.Message = Message;

Expand Down
2 changes: 2 additions & 0 deletions lib/campaign.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const {
SCHEMA_OPTIONS
} = require('@lykmapipo/mongoose-common');
const actions = require('mongoose-rest-actions');
const exportable = require('@lykmapipo/mongoose-exportable');


/* constants */
Expand Down Expand Up @@ -324,6 +325,7 @@ CampaignSchema.statics.AUDIENCES = AUDIENCES;
*------------------------------------------------------------------------------
*/
CampaignSchema.plugin(actions);
CampaignSchema.plugin(exportable);


/* export campaign model */
Expand Down
129 changes: 129 additions & 0 deletions test/integration/campaign.http.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
'use strict';

const {
clear: clearHttp,
testRouter,
} = require('@lykmapipo/express-test-helpers');
const {
clear: clearDatabase,
expect
} = require('@lykmapipo/mongoose-test-helpers');
const { Campaign, campaignRouter } = require('../..');

describe('Campaign Rest API', () => {
const campaign = Campaign.fake();

const options = {
pathSingle: '/campaigns/:id',
pathList: '/campaigns',
pathSchema: '/campaigns/schema/',
pathExport: '/campaigns/export/',
};

before(() => clearHttp());

before(done => clearDatabase(done));

it('should handle HTTP POST on /campaigns', done => {
const { testPost } = testRouter(options, campaignRouter);
testPost({ ...campaign.toObject() })
.expect(201)
.expect('Content-Type', /json/)
.end((error, { body }) => {
expect(error).to.not.exist;
expect(body).to.exist;
const created = new Campaign(body);
expect(created._id).to.exist.and.be.eql(campaign._id);
expect(created.form).to.exist.and.be.eql(campaign.form);
done(error, body);
});
});

it('should handle HTTP GET on /campaigns', done => {
const { testGet } = testRouter(options, campaignRouter);
testGet()
.expect(200)
.expect('Content-Type', /json/)
.end((error, { body }) => {
expect(error).to.not.exist;
expect(body).to.exist;
expect(body.data).to.exist;
expect(body.total).to.exist;
expect(body.limit).to.exist;
expect(body.skip).to.exist;
expect(body.page).to.exist;
expect(body.pages).to.exist;
expect(body.lastModified).to.exist;
done(error, body);
});
});

it('should handle GET /campaigns/schema', done => {
const { testGetSchema } = testRouter(options, campaignRouter);
testGetSchema().expect(200, done);
});

it('should handle GET /campaigns/export', done => {
const { testGetExport } = testRouter(options, campaignRouter);
testGetExport()
.expect('Content-Type', 'text/csv; charset=utf-8')
.expect(({ headers }) => {
expect(headers['content-disposition']).to.exist;
})
.expect(200, done);
});

it('should handle HTTP GET on /campaigns/:id', done => {
const { testGet } = testRouter(options, campaignRouter);
const params = { id: campaign._id.toString() };
testGet(params)
.expect(200)
.expect('Content-Type', /json/)
.end((error, { body }) => {
expect(error).to.not.exist;
expect(body).to.exist;
const found = new Campaign(body);
expect(found._id).to.exist.and.be.eql(campaign._id);
expect(found.form).to.exist.and.be.eql(campaign.form);
done(error, body);
});
});

it('should handle HTTP PATCH on /campaigns/:id', done => {
const { testPatch } = testRouter(options, campaignRouter);
const { subject } = campaign.fakeOnly('subject');
const params = { id: campaign._id.toString() };
testPatch(params, { subject })
.expect('Content-Type', /json/)
.expect(200, done);
});

it('should handle HTTP PUT on /campaigns/:id', done => {
const { testPut } = testRouter(options, campaignRouter);
const { subject } = campaign.fakeOnly('subject');
const params = { id: campaign._id.toString() };
testPut(params, { subject })
.expect('Content-Type', /json/)
.expect(200, done);
});

it('should handle HTTP DELETE on /campaigns/:id', done => {
const { testDelete } = testRouter(options, campaignRouter);
const params = { id: campaign._id.toString() };
testDelete(params)
.expect(200)
.expect('Content-Type', /json/)
.end((error, { body }) => {
expect(error).to.not.exist;
expect(body).to.exist;
const patched = new Campaign(body);
expect(patched._id).to.exist.and.be.eql(campaign._id);
expect(patched.form).to.exist.and.be.eql(campaign.form);
done(error, body);
});
});

after(() => clearHttp());

after(done => clearDatabase(done));
});

0 comments on commit 65a0d3c

Please sign in to comment.