-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathgraphql.cancelSubscription.test.js
170 lines (133 loc) · 5.45 KB
/
graphql.cancelSubscription.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import sinon from 'sinon';
import nodemailer from 'nodemailer';
import { expect } from 'chai';
import config from 'config';
import * as utils from '../test/utils';
import models from '../server/models';
const ordersData = utils.data('orders');
const cancelSubscriptionQuery = `
mutation cancelSubscription($id: Int!) {
cancelSubscription(id: $id) {
id
isSubscriptionActive
}
}
`;
describe('graphql.cancelSubscriptions.test.js', () => {
let collective, user, user2, paymentMethod, sandbox;
before(() => {
sandbox = sinon.sandbox.create();
});
after(() => sandbox.restore());
beforeEach(() => utils.resetTestDB());
beforeEach(() => models.User.createUserWithCollective(utils.data('user1')).tap((u => user = u)));
beforeEach(() => models.User.createUserWithCollective(utils.data('user2'))
.tap((u => user2 = u)));
beforeEach(() => models.Collective.create(utils.data('collective1')).tap((g => collective = g)));
beforeEach(() => collective.addHost(user.collective));
// create stripe account
beforeEach(() => {
models.ConnectedAccount.create({
service: 'stripe',
token: 'sktest_123',
CollectiveId: user.CollectiveId
})
});
// Create a paymentMethod.
beforeEach(() => models.PaymentMethod.create(utils.data('paymentMethod2')).tap(c => paymentMethod = c));
afterEach(() => {
utils.clearbitStubAfterEach(sandbox);
});
/**
* Cancel subscription
*/
describe('#cancel', () => {
const subscription = utils.data('subscription1');
let order, nm;
// create a fake nodemailer transport
beforeEach(() => {
config.mailgun.user = 'xxxxx';
config.mailgun.password = 'password';
nm = nodemailer.createTransport({
name: 'testsend',
service: 'Mailgun',
sendMail (data, callback) {
callback();
},
logger: false
});
sinon.stub(nodemailer, 'createTransport', () => nm);
});
// stub the transport
beforeEach(() => sinon.stub(nm, 'sendMail', (object, cb) => cb(null, object)));
afterEach(() => nm.sendMail.restore());
afterEach(() => {
config.mailgun.user = '';
config.mailgun.password = '';
nodemailer.createTransport.restore();
});
beforeEach(() => {
return models.Subscription.create(subscription)
.then(sub => models.Order.create({
...ordersData[0],
CreatedByUserId: user.id,
FromCollectiveId: user.CollectiveId,
CollectiveId: collective.id,
PaymentMethodId: paymentMethod.id,
SubscriptionId: sub.id
}))
.tap(d => order = d)
.catch()
});
it('fails if if no authorization provided', async () => {
const res = await utils.graphqlQuery(cancelSubscriptionQuery, { id: order.id});
expect(res.errors).to.exist;
expect(res.errors[0].message).to.equal('You need to be logged in to cancel a subscription');
});
it('fails if the subscription does not exist', async () => {
const res = await utils.graphqlQuery(cancelSubscriptionQuery, { id: 2}, user);
expect(res.errors).to.exist;
expect(res.errors[0].message).to.equal('Subscription not found');
});
it('fails if user isn\'t an admin of the collective' , async () => {
const res = await utils.graphqlQuery(cancelSubscriptionQuery, { id: order.id}, user2);
expect(res.errors).to.exist;
expect(res.errors[0].message).to.equal('You don\'t have permission to cancel this subscription');
});
it('fails if the subscription is already canceled', async () => {
const order2 = await models.Subscription.create(Object.assign({}, subscription, {isActive: false, deactivatedAt: new Date()}))
.then(sub => models.Order.create({
...ordersData[0],
CreatedByUserId: user.id,
FromCollectiveId: user.CollectiveId,
CollectiveId: collective.id,
PaymentMethodId: paymentMethod.id,
SubscriptionId: sub.id
}))
const res = await utils.graphqlQuery(cancelSubscriptionQuery, { id: order2.id}, user);
expect(res.errors).to.exist;
expect(res.errors[0].message).to.equal('Subscription already canceled')
})
it('succeeds in canceling the subscription', async () => {
const res = await utils.graphqlQuery(cancelSubscriptionQuery, {id:order.id}, user);
expect(res.errors).to.not.exist;
const orders = await models.Order.findAll({ include: [{ model: models.Subscription}]})
// check that subscription is updated in database
expect(orders[0].Subscription.isActive).to.equal(false);
expect(orders[0].Subscription.deactivatedAt).to.not.equal(null);
// check that activity is created
const activity = await models.Activity.findOne({where: {type: 'subscription.canceled'}});
expect(activity).to.be.defined;
expect(activity.CollectiveId).to.be.equal(collective.id);
expect(activity.UserId).to.be.equal(user.id);
expect(activity.data.subscription.id).to.be.equal(order.SubscriptionId);
expect(activity.data.collective.id).to.be.equal(collective.id);
expect(activity.data.user.id).to.be.equal(user.id);
// confirm that email went out
const { subject, html, cc } = nm.sendMail.lastCall.args[0];
expect(subject).to.contain('Subscription canceled to Scouts');
expect(html).to.contain('month has been canceled');
expect(cc).to.equal(`info@${collective.slug}.opencollective.com`);
});
});
});