|
| 1 | +import axios from "axios"; |
| 2 | +import AxiosMockAdapter from "axios-mock-adapter"; |
| 3 | + |
| 4 | +import Billing from "../../../../lib/api/resources/Billing"; |
| 5 | +import handleSendingError from "../../../../lib/axios-logger"; |
| 6 | +import MailtrapError from "../../../../lib/MailtrapError"; |
| 7 | + |
| 8 | +import CONFIG from "../../../../config"; |
| 9 | + |
| 10 | +const { CLIENT_SETTINGS } = CONFIG; |
| 11 | +const { GENERAL_ENDPOINT } = CLIENT_SETTINGS; |
| 12 | + |
| 13 | +describe("lib/api/resources/Billing: ", () => { |
| 14 | + let mock: AxiosMockAdapter; |
| 15 | + const accountId = 100; |
| 16 | + const billingAPI = new Billing(axios, accountId); |
| 17 | + const responseData = { |
| 18 | + billing: { |
| 19 | + cycle_start: "2024-01-01T00:00:00Z", |
| 20 | + cycle_end: "2024-01-31T23:59:59Z", |
| 21 | + }, |
| 22 | + sending: { |
| 23 | + plan: { |
| 24 | + name: "Pro", |
| 25 | + }, |
| 26 | + usage: { |
| 27 | + sent_messages_count: { |
| 28 | + current: 1000, |
| 29 | + limit: 10000, |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + testing: { |
| 34 | + plan: { |
| 35 | + name: "Pro", |
| 36 | + }, |
| 37 | + usage: { |
| 38 | + sent_messages_count: { |
| 39 | + current: 500, |
| 40 | + limit: 5000, |
| 41 | + }, |
| 42 | + forwarded_messages_count: { |
| 43 | + current: 200, |
| 44 | + limit: 2000, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }; |
| 49 | + |
| 50 | + describe("class Billing(): ", () => { |
| 51 | + describe("init: ", () => { |
| 52 | + it("initializes with all necessary params.", () => { |
| 53 | + expect(billingAPI).toHaveProperty("getCurrentBillingCycleUsage"); |
| 54 | + }); |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + beforeAll(() => { |
| 59 | + /** |
| 60 | + * Init Axios interceptors for handling response.data, errors. |
| 61 | + */ |
| 62 | + axios.interceptors.response.use( |
| 63 | + (response) => response.data, |
| 64 | + handleSendingError |
| 65 | + ); |
| 66 | + mock = new AxiosMockAdapter(axios); |
| 67 | + }); |
| 68 | + |
| 69 | + afterEach(() => { |
| 70 | + mock.reset(); |
| 71 | + }); |
| 72 | + |
| 73 | + describe("getCurrentBillingCycleUsage(): ", () => { |
| 74 | + it("successfully gets billing cycle usage.", async () => { |
| 75 | + const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/billing/usage`; |
| 76 | + |
| 77 | + expect.assertions(2); |
| 78 | + |
| 79 | + mock.onGet(endpoint).reply(200, responseData); |
| 80 | + const result = await billingAPI.getCurrentBillingCycleUsage(); |
| 81 | + |
| 82 | + expect(mock.history.get[0].url).toEqual(endpoint); |
| 83 | + expect(result).toEqual(responseData); |
| 84 | + }); |
| 85 | + |
| 86 | + it("fails with error when accountId is invalid.", async () => { |
| 87 | + const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/billing/usage`; |
| 88 | + const expectedErrorMessage = "Account not found"; |
| 89 | + const statusCode = 404; |
| 90 | + |
| 91 | + expect.assertions(3); |
| 92 | + |
| 93 | + mock.onGet(endpoint).reply(statusCode, { error: expectedErrorMessage }); |
| 94 | + |
| 95 | + try { |
| 96 | + await billingAPI.getCurrentBillingCycleUsage(); |
| 97 | + } catch (error) { |
| 98 | + expect(error).toBeInstanceOf(MailtrapError); |
| 99 | + |
| 100 | + if (error instanceof MailtrapError) { |
| 101 | + expect(error.message).toEqual(expectedErrorMessage); |
| 102 | + // Verify status code is accessible via cause property |
| 103 | + // @ts-expect-error ES5 types don't know about cause property |
| 104 | + expect(error.cause?.response?.status).toEqual(statusCode); |
| 105 | + } |
| 106 | + } |
| 107 | + }); |
| 108 | + |
| 109 | + it("fails with error when billing endpoint returns 403 Forbidden.", async () => { |
| 110 | + const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/billing/usage`; |
| 111 | + const expectedErrorMessage = "Access denied"; |
| 112 | + const statusCode = 403; |
| 113 | + |
| 114 | + expect.assertions(3); |
| 115 | + |
| 116 | + mock.onGet(endpoint).reply(statusCode, { error: expectedErrorMessage }); |
| 117 | + |
| 118 | + try { |
| 119 | + await billingAPI.getCurrentBillingCycleUsage(); |
| 120 | + } catch (error) { |
| 121 | + expect(error).toBeInstanceOf(MailtrapError); |
| 122 | + |
| 123 | + if (error instanceof MailtrapError) { |
| 124 | + expect(error.message).toEqual(expectedErrorMessage); |
| 125 | + // Verify status code is accessible via cause property |
| 126 | + // @ts-expect-error ES5 types don't know about cause property |
| 127 | + expect(error.cause?.response?.status).toEqual(statusCode); |
| 128 | + } |
| 129 | + } |
| 130 | + }); |
| 131 | + |
| 132 | + it("fails with error when no error body is provided.", async () => { |
| 133 | + const endpoint = `${GENERAL_ENDPOINT}/api/accounts/${accountId}/billing/usage`; |
| 134 | + const expectedErrorMessage = "Request failed with status code 500"; |
| 135 | + const statusCode = 500; |
| 136 | + |
| 137 | + expect.assertions(3); |
| 138 | + |
| 139 | + mock.onGet(endpoint).reply(statusCode); |
| 140 | + |
| 141 | + try { |
| 142 | + await billingAPI.getCurrentBillingCycleUsage(); |
| 143 | + } catch (error) { |
| 144 | + expect(error).toBeInstanceOf(MailtrapError); |
| 145 | + |
| 146 | + if (error instanceof MailtrapError) { |
| 147 | + expect(error.message).toEqual(expectedErrorMessage); |
| 148 | + // Verify status code is accessible via cause property |
| 149 | + // @ts-expect-error ES5 types don't know about cause property |
| 150 | + expect(error.cause?.response?.status).toEqual(statusCode); |
| 151 | + } |
| 152 | + } |
| 153 | + }); |
| 154 | + }); |
| 155 | +}); |
0 commit comments