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-09-23 14:20:22 +03:00
|
|
|
const Papa = require('papaparse');
|
2020-05-21 20:05:47 +03:00
|
|
|
|
2022-02-10 15:03:47 +03:00
|
|
|
const {mockManager} = require('../../utils/e2e-framework');
|
|
|
|
|
2022-02-11 17:02:08 +03:00
|
|
|
describe('Legacy Members API', function () {
|
2020-11-30 17:25:22 +03:00
|
|
|
let request;
|
2020-05-21 20:05:47 +03:00
|
|
|
|
2022-02-01 19:57:27 +03:00
|
|
|
beforeEach(function () {
|
2022-02-10 15:03:47 +03:00
|
|
|
mockManager.mockLabsDisabled('multipleProducts');
|
2022-02-01 19:57:27 +03:00
|
|
|
});
|
|
|
|
|
2021-08-13 16:18:57 +03:00
|
|
|
afterEach(function () {
|
2022-02-10 15:03:47 +03:00
|
|
|
mockManager.restore();
|
2020-05-21 20:05:47 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
before(async function () {
|
2021-11-18 11:55:35 +03:00
|
|
|
await localUtils.startGhost();
|
2020-11-30 17:25:22 +03:00
|
|
|
request = supertest.agent(config.get('url'));
|
2020-12-11 21:45:35 +03:00
|
|
|
await localUtils.doAuth(request, 'members', 'members:emails');
|
2020-05-21 20:05:47 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can browse', async function () {
|
|
|
|
const res = await request
|
2020-05-21 20:05:47 +03:00
|
|
|
.get(localUtils.API.getApiQuery('members/'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.exist(jsonResponse.members);
|
2021-05-10 21:32:11 +03:00
|
|
|
jsonResponse.members.should.have.length(8);
|
2021-01-28 20:25:38 +03:00
|
|
|
localUtils.API.checkResponse(jsonResponse.members[0], 'member', 'subscriptions');
|
2020-11-30 17:25:22 +03:00
|
|
|
|
|
|
|
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);
|
2021-05-10 21:32:11 +03:00
|
|
|
jsonResponse.meta.pagination.should.have.property('total', 8);
|
2020-11-30 17:25:22 +03:00
|
|
|
jsonResponse.meta.pagination.should.have.property('next', null);
|
|
|
|
jsonResponse.meta.pagination.should.have.property('prev', null);
|
2020-05-21 20:05:47 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can browse with filter', async function () {
|
|
|
|
const res = await request
|
2020-05-21 20:44:35 +03:00
|
|
|
.get(localUtils.API.getApiQuery('members/?filter=label:label-1'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
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');
|
2021-01-28 20:25:38 +03:00
|
|
|
localUtils.API.checkResponse(jsonResponse.members[0], 'member', 'subscriptions');
|
2020-11-30 17:25:22 +03:00
|
|
|
localUtils.API.checkResponse(jsonResponse.meta.pagination, 'pagination');
|
2020-05-21 20:44:35 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can browse with search', async function () {
|
|
|
|
const res = await request
|
2020-05-28 12:14:02 +03:00
|
|
|
.get(localUtils.API.getApiQuery('members/?search=member1'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
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');
|
2021-01-28 20:25:38 +03:00
|
|
|
localUtils.API.checkResponse(jsonResponse.members[0], 'member', 'subscriptions');
|
2020-11-30 17:25:22 +03:00
|
|
|
localUtils.API.checkResponse(jsonResponse.meta.pagination, 'pagination');
|
2020-05-28 12:14:02 +03:00
|
|
|
});
|
|
|
|
|
2021-01-28 19:31:02 +03:00
|
|
|
it('Can filter by paid status', async function () {
|
2020-11-30 17:25:22 +03:00
|
|
|
const res = await request
|
2021-01-28 19:31:02 +03:00
|
|
|
.get(localUtils.API.getApiQuery('members/?filter=status:paid'))
|
2020-06-12 14:12:10 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.exist(jsonResponse.members);
|
2021-05-17 16:55:23 +03:00
|
|
|
jsonResponse.members.should.have.length(5);
|
2020-11-30 17:25:22 +03:00
|
|
|
jsonResponse.members[0].email.should.equal('paid@test.com');
|
|
|
|
jsonResponse.members[1].email.should.equal('trialing@test.com');
|
|
|
|
localUtils.API.checkResponse(jsonResponse, 'members');
|
2021-01-28 20:25:38 +03:00
|
|
|
localUtils.API.checkResponse(jsonResponse.members[0], 'member', 'subscriptions');
|
2020-11-30 17:25:22 +03:00
|
|
|
localUtils.API.checkResponse(jsonResponse.meta.pagination, 'pagination');
|
2020-06-12 14:12:10 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can read', async function () {
|
|
|
|
const res = await request
|
2020-05-21 20:05:47 +03:00
|
|
|
.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)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
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);
|
2021-08-26 18:03:32 +03:00
|
|
|
localUtils.API.checkResponse(jsonResponse.members[0], 'member', ['subscriptions', 'products']);
|
2020-05-21 20:05:47 +03:00
|
|
|
});
|
|
|
|
|
2020-12-11 21:45:35 +03:00
|
|
|
it('Can read and include email_recipients', async function () {
|
|
|
|
const res = await request
|
|
|
|
.get(localUtils.API.getApiQuery(`members/${testUtils.DataGenerator.Content.members[0].id}/?include=email_recipients`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
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);
|
2021-08-26 18:03:32 +03:00
|
|
|
localUtils.API.checkResponse(jsonResponse.members[0], 'member', ['subscriptions', 'email_recipients', 'products']);
|
2020-12-11 21:45:35 +03:00
|
|
|
jsonResponse.members[0].email_recipients.length.should.equal(1);
|
|
|
|
localUtils.API.checkResponse(jsonResponse.members[0].email_recipients[0], 'email_recipient', ['email']);
|
|
|
|
localUtils.API.checkResponse(jsonResponse.members[0].email_recipients[0].email, 'email');
|
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can add', async function () {
|
2020-05-21 20:05:47 +03:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
const res = await request
|
2020-05-21 20:05:47 +03:00
|
|
|
.post(localUtils.API.getApiQuery(`members/`))
|
|
|
|
.send({members: [member]})
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
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);
|
|
|
|
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');
|
|
|
|
|
|
|
|
should.exist(res.headers.location);
|
|
|
|
res.headers.location.should.equal(`http://127.0.0.1:2369${localUtils.API.getApiQuery('members/')}${res.body.members[0].id}/`);
|
|
|
|
|
|
|
|
await 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);
|
2020-05-21 20:05:47 +03:00
|
|
|
});
|
|
|
|
|
2022-02-01 19:57:27 +03:00
|
|
|
it('Can add complimentary subscription', async function () {
|
|
|
|
const stripeService = require('../../../core/server/services/stripe');
|
|
|
|
stripeService.api._configured = true;
|
|
|
|
const fakePrice = {
|
|
|
|
id: 'price_1',
|
|
|
|
product: '',
|
|
|
|
active: true,
|
|
|
|
nickname: 'Complimentary',
|
|
|
|
unit_amount: 0,
|
|
|
|
currency: 'USD',
|
|
|
|
type: 'recurring',
|
|
|
|
recurring: {
|
|
|
|
interval: 'year'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const fakeSubscription = {
|
|
|
|
id: 'sub_1',
|
|
|
|
customer: 'cus_1',
|
|
|
|
status: 'active',
|
|
|
|
cancel_at_period_end: false,
|
|
|
|
metadata: {},
|
|
|
|
current_period_end: Date.now() / 1000,
|
|
|
|
start_date: Date.now() / 1000,
|
|
|
|
plan: fakePrice,
|
|
|
|
items: {
|
|
|
|
data: [{
|
|
|
|
price: fakePrice
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
sinon.stub(stripeService.api, 'createCustomer').callsFake(async function (data) {
|
|
|
|
return {
|
|
|
|
id: 'cus_1',
|
|
|
|
email: data.email
|
|
|
|
};
|
|
|
|
});
|
|
|
|
sinon.stub(stripeService.api, 'createPrice').resolves(fakePrice);
|
|
|
|
sinon.stub(stripeService.api, 'createSubscription').resolves(fakeSubscription);
|
|
|
|
sinon.stub(stripeService.api, 'getSubscription').resolves(fakeSubscription);
|
|
|
|
const initialMember = {
|
|
|
|
name: 'Name',
|
|
|
|
email: 'compedtest@test.com',
|
|
|
|
subscribed: true
|
|
|
|
};
|
|
|
|
|
|
|
|
const compedPayload = {
|
|
|
|
comped: true
|
|
|
|
};
|
|
|
|
|
|
|
|
const res = await request
|
|
|
|
.post(localUtils.API.getApiQuery(`members/`))
|
|
|
|
.send({members: [initialMember]})
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
should.exist(res.headers.location);
|
|
|
|
res.headers.location.should.equal(`http://127.0.0.1:2369${localUtils.API.getApiQuery('members/')}${res.body.members[0].id}/`);
|
|
|
|
|
|
|
|
const newMember = jsonResponse.members[0];
|
|
|
|
|
|
|
|
const res2 = await request
|
|
|
|
.put(localUtils.API.getApiQuery(`members/${newMember.id}/`))
|
|
|
|
.send({members: [compedPayload]})
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
should.not.exist(res2.headers['x-cache-invalidate']);
|
|
|
|
|
|
|
|
const jsonResponse2 = res2.body;
|
|
|
|
|
|
|
|
should.exist(jsonResponse2);
|
|
|
|
should.exist(jsonResponse2.members);
|
|
|
|
jsonResponse2.members.should.have.length(1);
|
|
|
|
localUtils.API.checkResponse(jsonResponse2.members[0], 'member', ['subscriptions', 'products']);
|
|
|
|
|
|
|
|
const member = jsonResponse2.members[0];
|
|
|
|
|
|
|
|
should.equal(member.status, 'comped');
|
|
|
|
should.equal(member.subscriptions.length, 1);
|
|
|
|
stripeService.api._configured = false;
|
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can edit by id', async function () {
|
2020-05-21 20:05:47 +03:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
const res = await request
|
2020-05-21 20:05:47 +03:00
|
|
|
.post(localUtils.API.getApiQuery(`members/`))
|
|
|
|
.send({members: [memberToChange]})
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
should.exist(res.headers.location);
|
|
|
|
res.headers.location.should.equal(`http://127.0.0.1:2369${localUtils.API.getApiQuery('members/')}${res.body.members[0].id}/`);
|
|
|
|
|
|
|
|
const newMember = jsonResponse.members[0];
|
|
|
|
|
|
|
|
const res2 = await 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);
|
|
|
|
|
|
|
|
should.not.exist(res2.headers['x-cache-invalidate']);
|
|
|
|
|
|
|
|
const jsonResponse2 = res2.body;
|
|
|
|
|
|
|
|
should.exist(jsonResponse2);
|
|
|
|
should.exist(jsonResponse2.members);
|
|
|
|
jsonResponse2.members.should.have.length(1);
|
2021-04-26 19:14:34 +03:00
|
|
|
localUtils.API.checkResponse(jsonResponse2.members[0], 'member', ['subscriptions', 'products']);
|
2020-11-30 17:25:22 +03:00
|
|
|
jsonResponse2.members[0].name.should.equal(memberChanged.name);
|
|
|
|
jsonResponse2.members[0].email.should.equal(memberChanged.email);
|
|
|
|
jsonResponse2.members[0].email.should.not.equal(memberToChange.email);
|
|
|
|
jsonResponse2.members[0].note.should.equal(memberChanged.note);
|
|
|
|
jsonResponse2.members[0].subscribed.should.equal(memberChanged.subscribed);
|
2020-05-21 20:05:47 +03:00
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can destroy', async function () {
|
2020-05-21 20:05:47 +03:00
|
|
|
const member = {
|
|
|
|
name: 'test',
|
|
|
|
email: 'memberTestDestroy@test.com'
|
|
|
|
};
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
const res = await request
|
2020-05-21 20:05:47 +03:00
|
|
|
.post(localUtils.API.getApiQuery(`members/`))
|
|
|
|
.send({members: [member]})
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.exist(jsonResponse.members);
|
|
|
|
|
|
|
|
const newMember = jsonResponse.members[0];
|
|
|
|
|
|
|
|
await request
|
|
|
|
.delete(localUtils.API.getApiQuery(`members/${newMember.id}`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(204);
|
|
|
|
|
|
|
|
await 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-05-21 20:05:47 +03:00
|
|
|
});
|
|
|
|
|
2020-09-23 14:20:22 +03:00
|
|
|
it('Can export CSV', async function () {
|
|
|
|
const res = await 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)
|
2020-09-23 14:20:22 +03:00
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
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/);
|
|
|
|
|
|
|
|
const csv = Papa.parse(res.text, {header: true});
|
|
|
|
should.exist(csv.data.find(row => row.name === 'Mr Egg'));
|
|
|
|
should.exist(csv.data.find(row => row.name === 'Egon Spengler'));
|
|
|
|
should.exist(csv.data.find(row => row.name === 'Ray Stantz'));
|
|
|
|
should.exist(csv.data.find(row => row.email === 'member2@test.com'));
|
2020-05-21 20:05:47 +03:00
|
|
|
});
|
|
|
|
|
2020-09-23 13:46:08 +03:00
|
|
|
it('Can export a filtered CSV', async function () {
|
|
|
|
const res = await request
|
|
|
|
.get(localUtils.API.getApiQuery(`members/upload/?search=Egg`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /text\/csv/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
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/);
|
|
|
|
|
|
|
|
const csv = Papa.parse(res.text, {header: true});
|
|
|
|
should.exist(csv.data.find(row => row.name === 'Mr Egg'));
|
|
|
|
should.not.exist(csv.data.find(row => row.name === 'Egon Spengler'));
|
|
|
|
should.not.exist(csv.data.find(row => row.name === 'Ray Stantz'));
|
|
|
|
should.not.exist(csv.data.find(row => row.email === 'member2@test.com'));
|
|
|
|
});
|
|
|
|
|
2020-11-30 17:25:22 +03:00
|
|
|
it('Can import CSV', async function () {
|
|
|
|
const res = await 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)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(201);
|
|
|
|
|
|
|
|
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-12-09 19:15:53 +03:00
|
|
|
jsonResponse.meta.stats.imported.should.equal(2);
|
|
|
|
jsonResponse.meta.stats.invalid.length.should.equal(0);
|
2020-11-30 17:25:22 +03:00
|
|
|
jsonResponse.meta.import_label.name.should.match(/^Import \d{4}-\d{2}-\d{2} \d{2}:\d{2}$/);
|
|
|
|
|
|
|
|
const importLabel = jsonResponse.meta.import_label;
|
|
|
|
|
|
|
|
// check that members had the auto-generated label attached
|
|
|
|
const res2 = await 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);
|
|
|
|
|
|
|
|
const jsonResponse2 = res2.body;
|
|
|
|
should.exist(jsonResponse2);
|
|
|
|
should.exist(jsonResponse2.members);
|
|
|
|
jsonResponse2.members.should.have.length(2);
|
|
|
|
|
|
|
|
const importedMember1 = jsonResponse2.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();
|
2021-06-03 20:28:14 +03:00
|
|
|
importedMember1.comped.should.equal(false);
|
2021-01-28 20:25:38 +03:00
|
|
|
importedMember1.subscriptions.should.not.be.undefined();
|
|
|
|
importedMember1.subscriptions.length.should.equal(0);
|
2020-11-30 17:25:22 +03:00
|
|
|
|
|
|
|
const importedMember2 = jsonResponse2.members.find(m => m.email === 'test@example.com');
|
|
|
|
should.exist(importedMember2);
|
|
|
|
importedMember2.name.should.equal('test');
|
|
|
|
should(importedMember2.note).equal('test note');
|
2022-01-11 17:44:12 +03:00
|
|
|
importedMember2.subscribed.should.equal(false);
|
2020-11-30 17:25:22 +03:00
|
|
|
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');
|
2021-06-03 20:28:14 +03:00
|
|
|
importedMember2.comped.should.equal(false);
|
2021-01-28 20:25:38 +03:00
|
|
|
importedMember2.subscriptions.should.not.be.undefined();
|
|
|
|
importedMember2.subscriptions.length.should.equal(0);
|
2020-05-21 20:05:47 +03:00
|
|
|
});
|
2020-05-26 12:38:42 +03:00
|
|
|
|
2021-03-29 10:37:01 +03:00
|
|
|
it('Can fetch member counts stats', async function () {
|
2020-11-30 17:25:22 +03:00
|
|
|
const res = await request
|
2021-03-29 10:37:01 +03:00
|
|
|
.get(localUtils.API.getApiQuery('members/stats/count/'))
|
2020-05-26 12:38:42 +03:00
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
2020-11-30 17:25:22 +03:00
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.exist(jsonResponse.total);
|
2021-03-29 10:37:01 +03:00
|
|
|
should.exist(jsonResponse.resource);
|
|
|
|
should.exist(jsonResponse.data);
|
|
|
|
const data = jsonResponse.data;
|
|
|
|
// 2 from above posts, 2 from above import
|
2021-07-16 14:06:46 +03:00
|
|
|
data[data.length - 1].free.should.equal(4);
|
|
|
|
data[data.length - 1].paid.should.equal(0);
|
2022-02-01 19:57:27 +03:00
|
|
|
// 1 from the comped test
|
|
|
|
data[data.length - 1].comped.should.equal(1);
|
2020-05-26 12:38:42 +03:00
|
|
|
});
|
2021-04-08 14:03:45 +03:00
|
|
|
|
|
|
|
it('Can import CSV and bulk destroy via auto-added label', function () {
|
|
|
|
// HACK: mock dates otherwise we'll often get unexpected members appearing
|
|
|
|
// from previous tests with the same import label due to auto-generated
|
|
|
|
// import labels only including minutes
|
|
|
|
sinon.stub(Date, 'now').returns(new Date('2021-03-30T17:21:00.000Z'));
|
|
|
|
|
|
|
|
// import our dummy data for deletion
|
|
|
|
return request
|
|
|
|
.post(localUtils.API.getApiQuery(`members/upload/`))
|
|
|
|
.attach('membersfile', path.join(__dirname, '/../../utils/fixtures/csv/valid-members-for-bulk-delete.csv'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.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);
|
|
|
|
should.exist(jsonResponse.meta.import_label);
|
|
|
|
|
|
|
|
jsonResponse.meta.stats.imported.should.equal(8);
|
|
|
|
|
|
|
|
return jsonResponse.meta.import_label;
|
|
|
|
})
|
|
|
|
.then((importLabel) => {
|
|
|
|
// check that the import worked by checking browse response with filter
|
|
|
|
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) => {
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.exist(jsonResponse.members);
|
|
|
|
jsonResponse.members.should.have.length(8);
|
|
|
|
})
|
|
|
|
.then(() => importLabel);
|
|
|
|
})
|
|
|
|
.then((importLabel) => {
|
|
|
|
// perform the bulk delete
|
|
|
|
return request
|
|
|
|
.del(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) => {
|
|
|
|
should.not.exist(res.headers['x-cache-invalidate']);
|
|
|
|
const jsonResponse = res.body;
|
|
|
|
should.exist(jsonResponse);
|
|
|
|
should.exist(jsonResponse.meta);
|
|
|
|
should.exist(jsonResponse.meta.stats);
|
|
|
|
should.exist(jsonResponse.meta.stats.successful);
|
|
|
|
should.equal(jsonResponse.meta.stats.successful, 8);
|
|
|
|
})
|
|
|
|
.then(() => importLabel);
|
|
|
|
})
|
|
|
|
.then((importLabel) => {
|
|
|
|
// check that the bulk delete worked by checking browse response with filter
|
|
|
|
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(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-08-13 16:18:57 +03:00
|
|
|
|
|
|
|
it('Can bulk unsubscribe members with filter', async function () {
|
|
|
|
// import our dummy data for deletion
|
|
|
|
await request
|
|
|
|
.post(localUtils.API.getApiQuery(`members/upload/`))
|
|
|
|
.attach('membersfile', path.join(__dirname, '/../../utils/fixtures/csv/members-for-bulk-unsubscribe.csv'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private);
|
|
|
|
|
|
|
|
const browseResponse = await request
|
|
|
|
.get(localUtils.API.getApiQuery('members/?filter=label:bulk-unsubscribe-test'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
browseResponse.body.members.should.have.length(8);
|
|
|
|
const allMembersSubscribed = browseResponse.body.members.every((member) => {
|
|
|
|
return member.subscribed;
|
|
|
|
});
|
|
|
|
|
|
|
|
should.ok(allMembersSubscribed);
|
|
|
|
|
|
|
|
const bulkUnsubscribeResponse = await request
|
|
|
|
.put(localUtils.API.getApiQuery('members/bulk/?filter=label:bulk-unsubscribe-test'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({
|
2021-10-01 15:21:14 +03:00
|
|
|
bulk: {
|
|
|
|
action: 'unsubscribe'
|
|
|
|
}
|
2021-08-13 16:18:57 +03:00
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
should.exist(bulkUnsubscribeResponse.body.bulk);
|
|
|
|
should.exist(bulkUnsubscribeResponse.body.bulk.meta);
|
|
|
|
should.exist(bulkUnsubscribeResponse.body.bulk.meta.stats);
|
|
|
|
should.exist(bulkUnsubscribeResponse.body.bulk.meta.stats.successful);
|
|
|
|
should.equal(bulkUnsubscribeResponse.body.bulk.meta.stats.successful, 8);
|
|
|
|
|
|
|
|
const postUnsubscribeBrowseResponse = await request
|
|
|
|
.get(localUtils.API.getApiQuery('members/?filter=label:bulk-unsubscribe-test'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
postUnsubscribeBrowseResponse.body.members.should.have.length(8);
|
|
|
|
const allMembersUnsubscribed = postUnsubscribeBrowseResponse.body.members.every((member) => {
|
|
|
|
return !member.subscribed;
|
|
|
|
});
|
|
|
|
|
|
|
|
should.ok(allMembersUnsubscribed);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Can bulk add and remove labels to members with filter', async function () {
|
|
|
|
// import our dummy data for deletion
|
|
|
|
await request
|
|
|
|
.post(localUtils.API.getApiQuery('members/upload/'))
|
|
|
|
.attach('membersfile', path.join(__dirname, '/../../utils/fixtures/csv/members-for-bulk-add-labels.csv'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private);
|
|
|
|
|
|
|
|
const newLabelResponse = await request
|
|
|
|
.post(localUtils.API.getApiQuery('labels'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({
|
|
|
|
labels: [{
|
|
|
|
name: 'Awesome Label For Testing Bulk Add'
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
|
|
|
|
const labelToAdd = newLabelResponse.body.labels[0];
|
|
|
|
|
|
|
|
const bulkAddLabelResponse = await request
|
|
|
|
.put(localUtils.API.getApiQuery('members/bulk/?filter=label:bulk-add-labels-test'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({
|
2021-10-01 15:21:14 +03:00
|
|
|
bulk: {
|
|
|
|
action: 'addLabel',
|
|
|
|
meta: {
|
|
|
|
label: labelToAdd
|
|
|
|
}
|
2021-08-13 16:18:57 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
should.exist(bulkAddLabelResponse.body.bulk);
|
|
|
|
should.exist(bulkAddLabelResponse.body.bulk.meta);
|
|
|
|
should.exist(bulkAddLabelResponse.body.bulk.meta.stats);
|
|
|
|
should.exist(bulkAddLabelResponse.body.bulk.meta.stats.successful);
|
|
|
|
should.equal(bulkAddLabelResponse.body.bulk.meta.stats.successful, 8);
|
|
|
|
|
|
|
|
const postLabelAddBrowseResponse = await request
|
|
|
|
.get(localUtils.API.getApiQuery(`members/?filter=label:${labelToAdd.slug}`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
postLabelAddBrowseResponse.body.members.should.have.length(8);
|
|
|
|
|
|
|
|
const labelToRemove = newLabelResponse.body.labels[0];
|
|
|
|
|
|
|
|
const bulkRemoveLabelResponse = await request
|
|
|
|
.put(localUtils.API.getApiQuery('members/bulk/?filter=label:bulk-add-labels-test'))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.send({
|
2021-10-01 15:21:14 +03:00
|
|
|
bulk: {
|
|
|
|
action: 'removeLabel',
|
|
|
|
meta: {
|
|
|
|
label: labelToRemove
|
|
|
|
}
|
2021-08-13 16:18:57 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
should.exist(bulkRemoveLabelResponse.body.bulk);
|
|
|
|
should.exist(bulkRemoveLabelResponse.body.bulk.meta);
|
|
|
|
should.exist(bulkRemoveLabelResponse.body.bulk.meta.stats);
|
|
|
|
should.exist(bulkRemoveLabelResponse.body.bulk.meta.stats.successful);
|
|
|
|
should.equal(bulkRemoveLabelResponse.body.bulk.meta.stats.successful, 8);
|
|
|
|
|
|
|
|
const postLabelRemoveBrowseResponse = await request
|
|
|
|
.get(localUtils.API.getApiQuery(`members/?filter=label:${labelToRemove.slug}`))
|
|
|
|
.set('Origin', config.get('url'))
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect('Cache-Control', testUtils.cacheRules.private)
|
|
|
|
.expect(200);
|
|
|
|
|
|
|
|
postLabelRemoveBrowseResponse.body.members.should.have.length(0);
|
|
|
|
});
|
2020-05-21 20:05:47 +03:00
|
|
|
});
|