2020-05-21 20:05:47 +03:00
|
|
|
const path = require('path');
|
|
|
|
const should = require('should');
|
|
|
|
const supertest = require('supertest');
|
|
|
|
const sinon = require('sinon');
|
|
|
|
const testUtils = require('../../utils');
|
|
|
|
const localUtils = require('./utils');
|
2020-05-27 20:47:53 +03:00
|
|
|
const config = require('../../../core/shared/config');
|
2020-05-21 20:05:47 +03:00
|
|
|
const labs = require('../../../core/server/services/labs');
|
|
|
|
|
|
|
|
const ghost = testUtils.startGhost;
|
|
|
|
|
|
|
|
let request;
|
|
|
|
|
|
|
|
describe('Members API', function () {
|
|
|
|
before(function () {
|
|
|
|
sinon.stub(labs, 'isSet').withArgs('members').returns(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function () {
|
|
|
|
sinon.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
before(function () {
|
|
|
|
return ghost()
|
|
|
|
.then(function () {
|
|
|
|
request = supertest.agent(config.get('url'));
|
|
|
|
})
|
|
|
|
.then(function () {
|
2020-05-21 20:44:35 +03:00
|
|
|
return localUtils.doAuth(request, 'members');
|
2020-05-21 20:05:47 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can browse', function () {
|
|
|
|
return request
|
|
|
|
.get(localUtils.API.getApiQuery('members/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200)
|
|
|
|
.then((res) => {
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.exist(jsonResponse.members);
|
2020-06-12 14:12:10 +03:00
|
|
|
jsonResponse.members.should.have.length(3);
|
2020-05-21 20:05:47 +03:00
|
|
|
localUtils.API.checkResponse(jsonResponse.members[0], 'member', 'stripe');
|
|
|
|
|
|
|
|
testUtils.API.isISO8601(jsonResponse.members[0].created_at).should.be.true();
|
|
|
|
jsonResponse.members[0].created_at.should.be.an.instanceof(String);
|
|
|
|
|
|
|
|
jsonResponse.meta.pagination.should.have.property('page', 1);
|
|
|
|
jsonResponse.meta.pagination.should.have.property('limit', 15);
|
|
|
|
jsonResponse.meta.pagination.should.have.property('pages', 1);
|
2020-06-12 14:12:10 +03:00
|
|
|
jsonResponse.meta.pagination.should.have.property('total', 3);
|
2020-05-21 20:05:47 +03:00
|
|
|
jsonResponse.meta.pagination.should.have.property('next', null);
|
|
|
|
jsonResponse.meta.pagination.should.have.property('prev', null);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-21 20:44:35 +03:00
|
|
|
it('Can browse with filter', function () {
|
|
|
|
return request
|
|
|
|
.get(localUtils.API.getApiQuery('members/?filter=label:label-1'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200)
|
|
|
|
.then((res) => {
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.exist(jsonResponse.members);
|
|
|
|
jsonResponse.members.should.have.length(1);
|
|
|
|
localUtils.API.checkResponse(jsonResponse, 'members');
|
|
|
|
localUtils.API.checkResponse(jsonResponse.members[0], 'member', 'stripe');
|
|
|
|
localUtils.API.checkResponse(jsonResponse.meta.pagination, 'pagination');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-28 12:14:02 +03:00
|
|
|
it('Can browse with search', function () {
|
|
|
|
return request
|
|
|
|
.get(localUtils.API.getApiQuery('members/?search=member1'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200)
|
|
|
|
.then((res) => {
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.exist(jsonResponse.members);
|
|
|
|
jsonResponse.members.should.have.length(1);
|
|
|
|
jsonResponse.members[0].email.should.equal('member1@test.com');
|
|
|
|
localUtils.API.checkResponse(jsonResponse, 'members');
|
|
|
|
localUtils.API.checkResponse(jsonResponse.members[0], 'member', 'stripe');
|
|
|
|
localUtils.API.checkResponse(jsonResponse.meta.pagination, 'pagination');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-12 14:12:10 +03:00
|
|
|
it('Can browse with paid', function () {
|
|
|
|
return request
|
|
|
|
.get(localUtils.API.getApiQuery('members/?paid=true'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200)
|
|
|
|
.then((res) => {
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.exist(jsonResponse.members);
|
|
|
|
jsonResponse.members.should.have.length(1);
|
|
|
|
jsonResponse.members[0].email.should.equal('paid@test.com');
|
|
|
|
localUtils.API.checkResponse(jsonResponse, 'members');
|
|
|
|
localUtils.API.checkResponse(jsonResponse.members[0], 'member', 'stripe');
|
|
|
|
localUtils.API.checkResponse(jsonResponse.meta.pagination, 'pagination');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-21 20:05:47 +03:00
|
|
|
it('Can read', function () {
|
|
|
|
return request
|
|
|
|
.get(localUtils.API.getApiQuery(`members/${testUtils.DataGenerator.Content.members[0].id}/`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200)
|
|
|
|
.then((res) => {
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.exist(jsonResponse.members);
|
|
|
|
jsonResponse.members.should.have.length(1);
|
|
|
|
localUtils.API.checkResponse(jsonResponse.members[0], 'member', 'stripe');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can add', function () {
|
|
|
|
const member = {
|
|
|
|
name: 'test',
|
2020-08-24 10:33:49 +03:00
|
|
|
email: 'memberTestAdd@test.com',
|
|
|
|
note: 'test note',
|
|
|
|
subscribed: false,
|
|
|
|
labels: ['test-label']
|
2020-05-21 20:05:47 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return request
|
|
|
|
.post(localUtils.API.getApiQuery(`members/`))
|
|
|
|
.send({members: [member]})
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(201)
|
|
|
|
.then((res) => {
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.exist(jsonResponse.members);
|
|
|
|
jsonResponse.members.should.have.length(1);
|
|
|
|
|
|
|
|
jsonResponse.members[0].name.should.equal(member.name);
|
|
|
|
jsonResponse.members[0].email.should.equal(member.email);
|
2020-08-24 10:33:49 +03:00
|
|
|
jsonResponse.members[0].note.should.equal(member.note);
|
|
|
|
jsonResponse.members[0].subscribed.should.equal(member.subscribed);
|
|
|
|
testUtils.API.isISO8601(jsonResponse.members[0].created_at).should.be.true();
|
|
|
|
|
|
|
|
jsonResponse.members[0].labels.length.should.equal(1);
|
|
|
|
jsonResponse.members[0].labels[0].name.should.equal('test-label');
|
2020-05-21 20:05:47 +03:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return request
|
|
|
|
.post(localUtils.API.getApiQuery(`members/`))
|
|
|
|
.send({members: [member]})
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(422);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can edit by id', function () {
|
|
|
|
const memberToChange = {
|
|
|
|
name: 'change me',
|
2020-08-24 10:33:49 +03:00
|
|
|
email: 'member2Change@test.com',
|
|
|
|
note: 'initial note',
|
|
|
|
subscribed: true
|
2020-05-21 20:05:47 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const memberChanged = {
|
|
|
|
name: 'changed',
|
2020-08-24 10:33:49 +03:00
|
|
|
email: 'cantChangeMe@test.com',
|
|
|
|
note: 'edited note',
|
|
|
|
subscribed: false
|
2020-05-21 20:05:47 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return request
|
|
|
|
.post(localUtils.API.getApiQuery(`members/`))
|
|
|
|
.send({members: [memberToChange]})
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(201)
|
|
|
|
.then((res) => {
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.exist(jsonResponse.members);
|
|
|
|
jsonResponse.members.should.have.length(1);
|
|
|
|
|
|
|
|
return jsonResponse.members[0];
|
|
|
|
})
|
|
|
|
.then((newMember) => {
|
|
|
|
return request
|
|
|
|
.put(localUtils.API.getApiQuery(`members/${newMember.id}/`))
|
|
|
|
.send({members: [memberChanged]})
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200)
|
|
|
|
.then((res) => {
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.exist(jsonResponse.members);
|
|
|
|
jsonResponse.members.should.have.length(1);
|
|
|
|
localUtils.API.checkResponse(jsonResponse.members[0], 'member', 'stripe');
|
|
|
|
jsonResponse.members[0].name.should.equal(memberChanged.name);
|
|
|
|
jsonResponse.members[0].email.should.equal(memberChanged.email);
|
|
|
|
jsonResponse.members[0].email.should.not.equal(memberToChange.email);
|
2020-08-24 10:33:49 +03:00
|
|
|
jsonResponse.members[0].note.should.equal(memberChanged.note);
|
|
|
|
jsonResponse.members[0].subscribed.should.equal(memberChanged.subscribed);
|
2020-05-21 20:05:47 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can destroy', function () {
|
|
|
|
const member = {
|
|
|
|
name: 'test',
|
|
|
|
email: 'memberTestDestroy@test.com'
|
|
|
|
};
|
|
|
|
|
|
|
|
return request
|
|
|
|
.post(localUtils.API.getApiQuery(`members/`))
|
|
|
|
.send({members: [member]})
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(201)
|
|
|
|
.then((res) => {
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.exist(jsonResponse.members);
|
|
|
|
|
|
|
|
return jsonResponse.members[0];
|
|
|
|
})
|
|
|
|
.then((newMember) => {
|
|
|
|
return request
|
|
|
|
.delete(localUtils.API.getApiQuery(`members/${newMember.id}`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(204)
|
|
|
|
.then(() => newMember);
|
|
|
|
})
|
|
|
|
.then((newMember) => {
|
|
|
|
return request
|
|
|
|
.get(localUtils.API.getApiQuery(`members/${newMember.id}/`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-12 07:33:45 +03:00
|
|
|
it('Can validate import data', function () {
|
|
|
|
const member = {
|
|
|
|
name: 'test',
|
|
|
|
email: 'memberTestAdd@test.com'
|
|
|
|
};
|
|
|
|
|
|
|
|
return request
|
2020-06-12 14:30:41 +03:00
|
|
|
.post(localUtils.API.getApiQuery(`members/upload/validate`))
|
2020-06-12 07:33:45 +03:00
|
|
|
.send({members: [member]})
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200)
|
|
|
|
.then((res) => {
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.not.exist(jsonResponse.members);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Fails to validate import data when stripe_customer_id is present but Stripe is not connected', function () {
|
|
|
|
const member = {
|
|
|
|
name: 'test',
|
|
|
|
email: 'memberTestAdd@test.com',
|
|
|
|
stripe_customer_id: 'cus_XXXXX'
|
|
|
|
};
|
|
|
|
|
|
|
|
return request
|
2020-06-12 14:30:41 +03:00
|
|
|
.post(localUtils.API.getApiQuery(`members/upload/validate`))
|
2020-06-12 07:33:45 +03:00
|
|
|
.send({members: [member]})
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(422)
|
|
|
|
.then((res) => {
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.exist(jsonResponse.errors);
|
2020-07-01 08:42:58 +03:00
|
|
|
jsonResponse.errors[0].message.should.match(/Missing Stripe connection/i);
|
2020-06-12 07:33:45 +03:00
|
|
|
jsonResponse.errors[0].context.should.match(/no Stripe account connected/i);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-21 20:05:47 +03:00
|
|
|
it('Can export CSV', function () {
|
|
|
|
return request
|
2020-06-16 09:08:50 +03:00
|
|
|
.get(localUtils.API.getApiQuery(`members/upload/`))
|
2020-05-21 20:05:47 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /text\/csv/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200)
|
|
|
|
.then((res) => {
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
res.headers['content-disposition'].should.match(/Attachment;\sfilename="members/);
|
|
|
|
res.text.should.match(/id,email,name,note,subscribed_to_emails,complimentary_plan,stripe_customer_id,created_at,deleted_at/);
|
|
|
|
res.text.should.match(/member1@test.com/);
|
|
|
|
res.text.should.match(/Mr Egg/);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can import CSV', function () {
|
|
|
|
return request
|
2020-06-16 09:08:50 +03:00
|
|
|
.post(localUtils.API.getApiQuery(`members/upload/`))
|
2020-05-21 20:05:47 +03:00
|
|
|
.attach('membersfile', path.join(__dirname, '/../../utils/fixtures/csv/valid-members-import.csv'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(201)
|
|
|
|
.then((res) => {
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.exist(jsonResponse.meta);
|
|
|
|
should.exist(jsonResponse.meta.stats);
|
|
|
|
|
2020-06-12 10:59:36 +03:00
|
|
|
jsonResponse.meta.stats.imported.count.should.equal(2);
|
|
|
|
jsonResponse.meta.stats.invalid.count.should.equal(0);
|
2020-07-22 19:49:41 +03:00
|
|
|
jsonResponse.meta.import_label.name.should.match(/^Import \d{4}-\d{2}-\d{2} \d{2}:\d{2}$/);
|
2020-07-26 19:32:18 +03:00
|
|
|
|
|
|
|
return jsonResponse.meta.import_label;
|
|
|
|
}).then((importLabel) => {
|
|
|
|
// check that members had the auto-generated label attached
|
|
|
|
return request.get(localUtils.API.getApiQuery(`members/?filter=label:${importLabel.slug}`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200)
|
|
|
|
.then((res) => {
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.exist(jsonResponse.members);
|
|
|
|
jsonResponse.members.should.have.length(2);
|
2020-08-24 10:33:49 +03:00
|
|
|
|
|
|
|
const importedMember1 = jsonResponse.members.find(m => m.email === 'jbloggs@example.com');
|
|
|
|
should.exist(importedMember1);
|
|
|
|
importedMember1.name.should.equal('joe');
|
|
|
|
should(importedMember1.note).equal(null);
|
|
|
|
importedMember1.subscribed.should.equal(true);
|
|
|
|
importedMember1.labels.length.should.equal(1);
|
|
|
|
testUtils.API.isISO8601(importedMember1.created_at).should.be.true();
|
|
|
|
importedMember1.comped.should.equal(false);
|
|
|
|
importedMember1.stripe.should.not.be.undefined();
|
|
|
|
importedMember1.stripe.subscriptions.length.should.equal(0);
|
|
|
|
|
|
|
|
const importedMember2 = jsonResponse.members.find(m => m.email === 'test@example.com');
|
|
|
|
should.exist(importedMember2);
|
|
|
|
importedMember2.name.should.equal('test');
|
|
|
|
should(importedMember2.note).equal('test note');
|
|
|
|
importedMember2.subscribed.should.equal(false);
|
|
|
|
importedMember2.labels.length.should.equal(2);
|
|
|
|
testUtils.API.isISO8601(importedMember2.created_at).should.be.true();
|
|
|
|
importedMember2.created_at.should.equal('1991-10-02T20:30:31.000Z');
|
|
|
|
importedMember2.comped.should.equal(false);
|
|
|
|
importedMember2.stripe.should.not.be.undefined();
|
|
|
|
importedMember2.stripe.subscriptions.length.should.equal(0);
|
2020-07-26 19:32:18 +03:00
|
|
|
});
|
2020-05-21 20:05:47 +03:00
|
|
|
});
|
|
|
|
});
|
2020-05-26 12:38:42 +03:00
|
|
|
|
|
|
|
it('Can fetch stats', function () {
|
|
|
|
return request
|
|
|
|
.get(localUtils.API.getApiQuery('members/stats/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200)
|
|
|
|
.then((res) => {
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.exist(jsonResponse.total);
|
|
|
|
should.exist(jsonResponse.total_in_range);
|
|
|
|
should.exist(jsonResponse.total_on_date);
|
|
|
|
should.exist(jsonResponse.new_today);
|
|
|
|
|
2020-06-12 14:12:10 +03:00
|
|
|
// 3 from fixtures, 2 from above posts, 2 from above import
|
|
|
|
jsonResponse.total.should.equal(7);
|
2020-05-26 12:38:42 +03:00
|
|
|
});
|
|
|
|
});
|
2020-05-21 20:05:47 +03:00
|
|
|
});
|