|
| 1 | +import {expect, sinon} from '@loopback/testlab'; |
| 2 | +import chargebee from 'chargebee'; |
| 3 | +import {ChargeBeeService} from '../../providers/sdk/chargebee/charge-bee.service'; |
| 4 | +import {StripeService} from '../../providers/sdk/stripe/stripe.service'; |
| 5 | +import {TInvoicePdf} from '../../types'; |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------- |
| 8 | +// ChargeBee Tests |
| 9 | +// ------------------------------------------------------------------------- |
| 10 | + |
| 11 | +describe('ChargeBeeService - Invoice PDF Download', () => { |
| 12 | + let service: ChargeBeeService; |
| 13 | + let sandbox: sinon.SinonSandbox; |
| 14 | + |
| 15 | + /** |
| 16 | + * Helper function to stub ChargeBee API calls. |
| 17 | + * ChargeBee SDK uses a builder pattern: chargebee.resource.action(params).request() |
| 18 | + * So each stub must return an object with a `.request` stub. |
| 19 | + */ |
| 20 | + function stubCb(returnValue: object) { |
| 21 | + // NOSONAR |
| 22 | + return { |
| 23 | + request: sinon.stub().resolves(returnValue), |
| 24 | + setIdempotencyKey: sinon.stub().returnsThis(), |
| 25 | + headers: sinon.stub().returnsThis(), |
| 26 | + }; |
| 27 | + } |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + sandbox = sinon.createSandbox(); |
| 31 | + |
| 32 | + // Stub the global chargebee.configure to prevent side effects |
| 33 | + sandbox.stub(chargebee, 'configure'); |
| 34 | + |
| 35 | + // Initialize service with test configuration |
| 36 | + service = new ChargeBeeService({ |
| 37 | + site: 'test-site', |
| 38 | + apiKey: 'test-key', |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + sandbox.restore(); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('getInvoicePdf - Happy Path', () => { |
| 47 | + it('returns PDF URL for a valid invoice', async () => { |
| 48 | + // Stub the chargebee.invoice.pdf() call |
| 49 | + const pdfStub = sandbox.stub(chargebee.invoice, 'pdf').returns( |
| 50 | + stubCb({ |
| 51 | + download: { |
| 52 | + download_url: 'https://test.chargebee.com/invoice/inv_123/pdf', |
| 53 | + expires_at: 1735689600, // 2024-12-31 |
| 54 | + }, |
| 55 | + }), |
| 56 | + ); |
| 57 | + |
| 58 | + // Call the method |
| 59 | + const result: TInvoicePdf = await service.getInvoicePdf('inv_123'); |
| 60 | + |
| 61 | + // Verify the result |
| 62 | + expect(result.invoiceId).to.equal('inv_123'); |
| 63 | + expect(result.pdfUrl).to.equal( |
| 64 | + 'https://test.chargebee.com/invoice/inv_123/pdf', |
| 65 | + ); |
| 66 | + expect(result.expiresAt).to.equal(1735689600); |
| 67 | + expect(result.generatedAt).to.be.type('number'); |
| 68 | + expect(result.generatedAt).to.be.greaterThan(0); |
| 69 | + |
| 70 | + // Verify the API was called correctly |
| 71 | + sinon.assert.calledOnce(pdfStub); |
| 72 | + sinon.assert.calledWith(pdfStub, 'inv_123'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('returns PDF URL with current timestamp', async () => { |
| 76 | + sandbox.stub(chargebee.invoice, 'pdf').returns( |
| 77 | + stubCb({ |
| 78 | + download: { |
| 79 | + download_url: 'https://test.chargebee.com/invoice/inv_456/pdf', |
| 80 | + expires_at: 1735689600, |
| 81 | + }, |
| 82 | + }), |
| 83 | + ); |
| 84 | + |
| 85 | + const before = Math.floor(Date.now() / 1000); |
| 86 | + const result = await service.getInvoicePdf('inv_456'); |
| 87 | + const after = Math.floor(Date.now() / 1000); |
| 88 | + |
| 89 | + expect(result.generatedAt).to.be.greaterThanOrEqual(before); |
| 90 | + expect(result.generatedAt).to.be.lessThanOrEqual(after); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('getInvoicePdf - Error Cases', () => { |
| 95 | + it('throws error when PDF URL is not available', async () => { |
| 96 | + // Stub to return empty download object |
| 97 | + sandbox.stub(chargebee.invoice, 'pdf').returns( |
| 98 | + stubCb({ |
| 99 | + download: {}, |
| 100 | + }), |
| 101 | + ); |
| 102 | + |
| 103 | + await expect(service.getInvoicePdf('inv_123')).to.be.rejectedWith( |
| 104 | + 'PDF URL not available for invoice inv_123. The invoice may be in an invalid state.', |
| 105 | + ); |
| 106 | + }); |
| 107 | + |
| 108 | + it('throws error when download object is missing', async () => { |
| 109 | + // Stub to return result without download |
| 110 | + sandbox.stub(chargebee.invoice, 'pdf').returns(stubCb({})); |
| 111 | + |
| 112 | + await expect(service.getInvoicePdf('inv_123')).to.be.rejectedWith( |
| 113 | + 'PDF URL not available for invoice inv_123. The invoice may be in an invalid state.', |
| 114 | + ); |
| 115 | + }); |
| 116 | + }); |
| 117 | +}); |
| 118 | + |
| 119 | +// ------------------------------------------------------------------------- |
| 120 | +// Stripe Tests |
| 121 | +// ------------------------------------------------------------------------- |
| 122 | + |
| 123 | +describe('StripeService - Invoice PDF Download', () => { |
| 124 | + let service: StripeService; |
| 125 | + let sandbox: sinon.SinonSandbox; |
| 126 | + |
| 127 | + beforeEach(() => { |
| 128 | + sandbox = sinon.createSandbox(); |
| 129 | + |
| 130 | + // Initialize service with test configuration |
| 131 | + service = new StripeService({secretKey: 'sk_test_123'}); |
| 132 | + }); |
| 133 | + |
| 134 | + afterEach(() => { |
| 135 | + sandbox.restore(); |
| 136 | + }); |
| 137 | + |
| 138 | + describe('getInvoicePdf - Error Cases', () => { |
| 139 | + it('throws error for non-existent invoice', async () => { |
| 140 | + // Mock Stripe error |
| 141 | + sandbox |
| 142 | + .stub(service['stripe'].invoices, 'retrieve') |
| 143 | + .rejects({code: 'resource_missing'}); |
| 144 | + |
| 145 | + await expect(service.getInvoicePdf('in_nonexistent')).to.be.rejectedWith( |
| 146 | + 'Invoice not found: in_nonexistent', |
| 147 | + ); |
| 148 | + }); |
| 149 | + |
| 150 | + it('throws error for other Stripe errors', async () => { |
| 151 | + // Mock generic Stripe error |
| 152 | + sandbox |
| 153 | + .stub(service['stripe'].invoices, 'retrieve') |
| 154 | + .rejects({code: 'api_error', message: 'Something went wrong'}); |
| 155 | + |
| 156 | + await expect(service.getInvoicePdf('in_error')).to.be.rejected(); |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
0 commit comments