-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathwebhooks.stripe.test.js
110 lines (90 loc) · 2.88 KB
/
webhooks.stripe.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
import {expect} from 'chai';
import request from 'supertest';
import _ from 'lodash';
import sinon from 'sinon';
import app from '../server/index';
import originalStripeMock from './mocks/stripe';
import { appStripe } from '../server/paymentProviders/stripe/gateway';
import bitcoin from '../server/paymentProviders/stripe/bitcoin';
describe('webhooks.stripe.test.js', () => {
let sandbox;
it('returns 200 if the event is not livemode in production', (done) => {
const stripeMock = _.cloneDeep(originalStripeMock);
const webhookEvent = stripeMock.webhook_source_chargeable;
const event = _.extend({}, webhookEvent, {
livemode: false
});
const env = process.env.NODE_ENV;
process.env.NODE_ENV = 'production';
request(app)
.post('/webhooks/stripe')
.send(event)
.expect(200)
.end((err) => {
expect(err).to.not.exist;
process.env.NODE_ENV = env;
done();
});
});
describe('Webhook events: ', () => {
beforeEach(() => {
sandbox = sinon.sandbox.create();
sandbox.stub(bitcoin, 'webhook', () => {
return Promise.resolve();
})
});
afterEach(() => {
sandbox.restore();
})
it('returns an error if the event does not exist', (done) => {
const stripeMock = _.cloneDeep(originalStripeMock);
stripeMock.event_payment_succeeded = {
error: {
type: 'invalid_request_error',
message: 'No such event',
param: 'id',
requestId: 'req_7Y8TeQytYKcs1k'
}
};
sandbox.stub(appStripe.events, "retrieve", () => Promise.resolve(stripeMock.event_payment_succeeded));
request(app)
.post('/webhooks/stripe')
.send({
id: 123
})
.expect(400, {
error: {
code: 400,
type: 'bad_request',
message: 'Event not found'
}
})
.end(done);
});
it('lets `source.chargeable through`', (done) => {
const stripeMock = _.cloneDeep(originalStripeMock);
sandbox.stub(appStripe.events, "retrieve", () => Promise.resolve(stripeMock.event_source_chargeable));
request(app)
.post('/webhooks/stripe')
.send(stripeMock.webhook_source_chargeable)
.expect(200)
.end(done);
});
it('returns an error if the event is `source.chargeable`', (done) => {
const stripeMock = _.cloneDeep(originalStripeMock);
stripeMock.event_source_chargeable.type = 'application_fee.created';
sandbox.stub(appStripe.events, "retrieve", () => Promise.resolve(stripeMock.event_source_chargeable));
request(app)
.post('/webhooks/stripe')
.send(stripeMock.webhook_payment_succeeded)
.expect(400, {
error: {
code: 400,
type: 'bad_request',
message: 'Wrong event type received'
}
})
.end(done);
});
});
});