-
Notifications
You must be signed in to change notification settings - Fork 46
/
billing.spec.ts
359 lines (330 loc) · 10.4 KB
/
billing.spec.ts
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import * as Bluebird from 'bluebird';
import * as _fs from 'fs';
// TODO: change this to type-only import once TS 3.8 gets released
// tslint:disable-next-line:import-blacklist
import * as _ from 'lodash';
import * as m from 'mochainon';
import {
balena,
BalenaSdk,
givenLoggedInUser,
IS_BROWSER,
loginPaidUser,
} from '../setup';
const { expect } = m.chai;
describe('Billing Model', function () {
describe('Free Account', function () {
givenLoggedInUser(beforeEach);
describe('balena.models.billing.getAccount()', () =>
it('should not return a billing account info object', function () {
const promise = balena.models.billing.getAccount();
return expect(promise).to.be.rejected.then(function (error) {
expect(error).to.have.property('code', 'BalenaRequestError');
expect(error).to.have.property('statusCode', 404);
expect(error)
.to.have.property('message')
.that.contains('Billing Account was not found.');
});
}));
describe('balena.models.billing.getPlan()', () =>
it('should return a free tier billing plan object', () =>
balena.models.billing.getPlan().then((plan) =>
expect(plan).to.deep.match({
title: 'Free',
name: 'Free plan',
code: 'free',
tier: 'free',
addOns: [],
billing: {
currency: 'USD',
charges: [
{
itemType: 'plan',
name: 'Free plan',
code: 'free',
unitCostCents: '0',
quantity: '1',
},
{
itemType: 'support',
name: 'Community support',
code: 'community',
unitCostCents: '0',
quantity: '1',
},
],
totalCostCents: '0',
},
support: {
title: 'Community',
name: 'Community support',
},
}),
)));
describe('balena.models.billing.getBillingInfo()', () =>
it('should return a free tier billing info object', function () {
const promise = balena.models.billing.getBillingInfo();
return expect(promise).to.become({});
}));
describe('balena.models.billing.updateBillingInfo()', function () {
it('should throw when no parameters are provided', function () {
const promise = (balena.models.billing.updateBillingInfo as any)();
return expect(promise).to.be.rejected.then((error) =>
expect(error).to.have.property('statusCode', 400),
);
});
it('should throw when an empty parameter object is provided', function () {
const promise = balena.models.billing.updateBillingInfo({} as any);
return expect(promise).to.be.rejected.then((error) =>
expect(error).to.have.property('statusCode', 400),
);
});
it('should throw when the token_id is empty', function () {
const promise = balena.models.billing.updateBillingInfo({
token_id: '',
});
return expect(promise).to.be.rejected.then((error) =>
expect(error).to.have.property('statusCode', 400),
);
});
});
describe('balena.models.billing.getInvoices()', () =>
it('should return no invoices', function () {
const promise = balena.models.billing.getInvoices();
return expect(promise).to.become([]);
}));
describe('balena.models.billing.downloadInvoice()', function () {
before(function () {
return balena.models.billing
.getInvoices()
.then((invoices) => {
this.firstInvoiceNumber = invoices?.[0]?.invoice_number;
})
.catch(_.noop);
});
it('should not be able to download any invoice', function () {
expect(this.firstInvoiceNumber).to.be.a('undefined');
const promise = balena.models.billing.downloadInvoice(
'anyinvoicenumber',
);
return expect(promise).to.be.rejected;
});
it('should throw when an invoice number is not provided', function () {
const promise = (balena.models.billing.downloadInvoice as any)();
return expect(promise).to.be.rejected;
});
it('should throw when an empty string invoice number is provided', function () {
const promise = balena.models.billing.downloadInvoice('');
return expect(promise).to.be.rejected;
});
it('should throw when trying to retrieve an non-existing invoice', function () {
const promise = balena.models.billing.downloadInvoice('notfound');
return expect(promise).to.be.rejected;
});
it('should not return an invoice of a different user', function () {
const promise = balena.models.billing.downloadInvoice('1000');
return expect(promise).to.be.rejected;
});
});
});
describe('Paid Account', function () {
let hasActiveBillingAccount = false;
const givenABillingAccountIt = (
description: string,
testFn: (...args: any[]) => any,
) => {
const $it = hasActiveBillingAccount ? it : it.skip;
$it(description, testFn);
};
before(() =>
loginPaidUser()
.then(() => balena.models.billing.getAccount())
.then(
(accountInfo) =>
(hasActiveBillingAccount =
(accountInfo != null ? accountInfo.account_state : undefined) ===
'active'),
)
.catch(_.noop),
);
describe('balena.models.billing.getAccount()', () =>
givenABillingAccountIt(
'should return a paid tier billing account info object',
function () {
const promise = balena.models.billing.getAccount();
return expect(promise).to.become({
account_state: 'active',
address: {
address1: 'One London Wall',
address2: '6th Floor',
city: 'London',
country: 'GB',
phone: '6970000000',
state: 'Greater London',
zip: 'EC2Y 5EB',
},
cc_emails: 'testdev-cc@nomail.com',
company_name: 'Resin.io',
first_name: 'John',
last_name: 'Doe',
vat_number: '',
});
},
));
describe('balena.models.billing.getPlan()', () =>
givenABillingAccountIt(
'should return a paid tier billing plan object',
() =>
balena.models.billing.getPlan().then(function (plan) {
expect(plan).to.deep.match({
title: 'Team member',
name: 'Team member plan',
code: 'free',
tier: 'free',
addOns: [],
addonPlan: {
addOns: [],
billing: {
charges: [],
currency: 'USD',
totalCostCents: '0',
},
code: 'addons',
currentPeriodEndDate: '',
uuid: '',
},
billing: {
currency: 'USD',
charges: [
{
itemType: 'plan',
name: 'Team member plan',
code: 'free',
unitCostCents: '0',
quantity: '1',
},
{
itemType: 'support',
name: 'Standard support',
code: 'core',
unitCostCents: '0',
quantity: '1',
},
],
totalCostCents: '0',
},
support: {
title: 'Standard',
name: 'Standard support',
},
});
expect(plan)
.to.have.property('currentPeriodEndDate')
.that.is.a('string');
}),
));
describe('balena.models.billing.getBillingInfo()', () =>
givenABillingAccountIt('should return a billing info object', () =>
balena.models.billing.getBillingInfo().then(function (billingInfo) {
expect(billingInfo).to.not.be.null;
// this is for local tests
if ((billingInfo as BalenaSdk.CardBillingInfo).card_type === 'Visa') {
expect(billingInfo).to.deep.equal({
first_name: 'John',
last_name: 'Doe',
company: 'Resin.io',
vat_number: 'GBUK00000000000',
address1: 'One London Wall',
address2: '6th floor',
city: 'London',
state: 'Greater London',
zip: 'EC2Y 5EB',
country: 'GB',
phone: '6970000000',
card_type: 'Visa',
last_four: '1111',
type: 'credit_card',
first_one: '4',
year: '2018',
month: '8',
full_name: 'John Doe',
});
} else {
expect(billingInfo).to.deep.equal({});
}
}),
));
describe('balena.models.billing.getInvoices()', () =>
givenABillingAccountIt('should return an array of invoice objects', () =>
balena.models.billing.getInvoices().then(function (invoices) {
expect(Array.isArray(invoices)).to.be.true;
expect(invoices.length).to.not.equal(0);
const invoice = invoices[0];
expect(invoice).to.have.property('closed_at').that.is.a('string');
expect(invoice).to.have.property('created_at').that.is.a('string');
expect(invoice).to.have.property('due_on').that.is.a('string');
expect(invoice)
.to.have.property('invoice_number')
.that.is.a('string');
expect(invoice).to.have.property('uuid').that.is.a('string');
expect(invoice).to.have.property('currency', 'USD');
expect(invoice).to.have.property('total_in_cents', '0');
expect(invoice).to.have.property('subtotal_in_cents', '0');
expect(invoice).to.have.property('state', 'paid');
}),
));
describe('balena.models.billing.downloadInvoice()', function () {
before(function () {
return balena.models.billing
.getInvoices()
.then((invoices) => {
this.firstInvoiceNumber = invoices?.[0]?.invoice_number;
})
.catch(_.noop);
});
if (IS_BROWSER) {
givenABillingAccountIt(
'should be able to download an invoice on the browser',
function () {
return balena.models.billing
.downloadInvoice(this.firstInvoiceNumber)
.then(function (result: Blob) {
expect(result).to.be.an.instanceof(Blob);
expect(result.size).to.not.equal(0);
expect(result.type).to.equal('application/pdf');
});
},
);
}
if (!IS_BROWSER) {
const rindle = require('rindle');
const tmp = require('tmp');
const fs = Bluebird.promisifyAll(require('fs')) as typeof _fs & {
statAsync: (
path: _fs.PathLike,
) => Bluebird<ReturnType<typeof _fs.statSync>>;
unlinkAsync: (
path: _fs.PathLike,
) => Bluebird<ReturnType<typeof _fs.unlinkSync>>;
};
return givenABillingAccountIt(
'should be able to download an invoice on node',
function () {
return balena.models.billing
.downloadInvoice(this.firstInvoiceNumber)
.then(function (result) {
const stream = result as Exclude<typeof result, Blob>;
expect(stream.mime).to.equal('application/pdf');
const tmpFile = tmp.tmpNameSync();
return rindle
.wait(stream.pipe(fs.createWriteStream(tmpFile)))
.then(() => fs.statAsync(tmpFile))
.then((stat: _fs.Stats) => expect(stat.size).to.not.equal(0))
.finally(() => fs.unlinkAsync(tmpFile));
});
},
);
}
});
});
});