|
| 1 | +import axios, { AxiosInstance } from "axios"; |
| 2 | +import MockAdapter from "axios-mock-adapter"; |
| 3 | + |
| 4 | +import SendingDomainsBaseAPI from "../../../../lib/api/SendingDomains"; |
| 5 | +import { |
| 6 | + SendingDomain, |
| 7 | + DnsRecord, |
| 8 | + SendingDomainPermissions, |
| 9 | +} from "../../../../types/api/sending-domains"; |
| 10 | + |
| 11 | +describe("lib/api/SendingDomains: ", () => { |
| 12 | + const axiosInstance: AxiosInstance = axios.create(); |
| 13 | + const mock = new MockAdapter(axiosInstance); |
| 14 | + |
| 15 | + // Add the response interceptor that returns response.data |
| 16 | + axiosInstance.interceptors.response.use((response) => response.data); |
| 17 | + |
| 18 | + const testAccountId = 100; |
| 19 | + const sendingDomainsAPI = new SendingDomainsBaseAPI( |
| 20 | + axiosInstance, |
| 21 | + testAccountId |
| 22 | + ); |
| 23 | + |
| 24 | + describe("class SendingDomainsBaseAPI(): ", () => { |
| 25 | + describe("init: ", () => { |
| 26 | + it("initializes with all necessary params.", () => { |
| 27 | + expect(sendingDomainsAPI).toHaveProperty("get"); |
| 28 | + expect(sendingDomainsAPI).toHaveProperty("getList"); |
| 29 | + expect(sendingDomainsAPI).toHaveProperty("create"); |
| 30 | + expect(sendingDomainsAPI).toHaveProperty("delete"); |
| 31 | + expect(sendingDomainsAPI).toHaveProperty("sendSetupInstructions"); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + describe("sendingDomains.getList(): ", () => { |
| 36 | + it("should get sending domains list.", async () => { |
| 37 | + const mockDnsRecords: DnsRecord[] = [ |
| 38 | + { |
| 39 | + key: "verification", |
| 40 | + domain: "ve6wza2rbpe60x7z.example.com", |
| 41 | + type: "CNAME", |
| 42 | + value: "smtp.mailtrap.live", |
| 43 | + status: "pass", |
| 44 | + name: "ve6wza2rbpe60x7z", |
| 45 | + }, |
| 46 | + { |
| 47 | + key: "spf", |
| 48 | + domain: "example.com", |
| 49 | + type: "TXT", |
| 50 | + value: "v=spf1 include:_spf.smtp.mailtrap.live ~all", |
| 51 | + status: "pass", |
| 52 | + name: "", |
| 53 | + }, |
| 54 | + ]; |
| 55 | + |
| 56 | + const mockPermissions: SendingDomainPermissions = { |
| 57 | + can_read: true, |
| 58 | + can_update: true, |
| 59 | + can_destroy: true, |
| 60 | + }; |
| 61 | + |
| 62 | + const mockSendingDomains: SendingDomain[] = [ |
| 63 | + { |
| 64 | + id: 435, |
| 65 | + domain_name: "example.com", |
| 66 | + demo: false, |
| 67 | + compliance_status: "compliant", |
| 68 | + dns_verified: true, |
| 69 | + dns_verified_at: "2024-12-26T09:40:44.161Z", |
| 70 | + dns_records: mockDnsRecords, |
| 71 | + open_tracking_enabled: true, |
| 72 | + click_tracking_enabled: true, |
| 73 | + auto_unsubscribe_link_enabled: true, |
| 74 | + custom_domain_tracking_enabled: true, |
| 75 | + health_alerts_enabled: true, |
| 76 | + critical_alerts_enabled: true, |
| 77 | + alert_recipient_email: "[email protected]", |
| 78 | + permissions: mockPermissions, |
| 79 | + }, |
| 80 | + ]; |
| 81 | + |
| 82 | + mock |
| 83 | + .onGet( |
| 84 | + `https://mailtrap.io/api/accounts/${testAccountId}/sending_domains` |
| 85 | + ) |
| 86 | + .reply(200, { data: mockSendingDomains }); |
| 87 | + |
| 88 | + const result = await sendingDomainsAPI.getList(); |
| 89 | + |
| 90 | + expect(result).toEqual({ data: mockSendingDomains }); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe("sendingDomains.get(): ", () => { |
| 95 | + it("should get a single sending domain by id.", async () => { |
| 96 | + const mockDnsRecords: DnsRecord[] = [ |
| 97 | + { |
| 98 | + key: "verification", |
| 99 | + domain: "ve6wza2rbpe60x7z.example.com", |
| 100 | + type: "CNAME", |
| 101 | + value: "smtp.mailtrap.live", |
| 102 | + status: "pass", |
| 103 | + name: "ve6wza2rbpe60x7z", |
| 104 | + }, |
| 105 | + ]; |
| 106 | + |
| 107 | + const mockPermissions: SendingDomainPermissions = { |
| 108 | + can_read: true, |
| 109 | + can_update: true, |
| 110 | + can_destroy: true, |
| 111 | + }; |
| 112 | + |
| 113 | + const mockSendingDomain: SendingDomain = { |
| 114 | + id: 999, |
| 115 | + domain_name: "example.com", |
| 116 | + demo: false, |
| 117 | + compliance_status: "compliant", |
| 118 | + dns_verified: true, |
| 119 | + dns_verified_at: "2024-12-26T09:40:44.161Z", |
| 120 | + dns_records: mockDnsRecords, |
| 121 | + open_tracking_enabled: true, |
| 122 | + click_tracking_enabled: true, |
| 123 | + auto_unsubscribe_link_enabled: true, |
| 124 | + custom_domain_tracking_enabled: true, |
| 125 | + health_alerts_enabled: true, |
| 126 | + critical_alerts_enabled: true, |
| 127 | + alert_recipient_email: "[email protected]", |
| 128 | + permissions: mockPermissions, |
| 129 | + }; |
| 130 | + |
| 131 | + mock |
| 132 | + .onGet( |
| 133 | + `https://mailtrap.io/api/accounts/${testAccountId}/sending_domains/${mockSendingDomain.id}` |
| 134 | + ) |
| 135 | + .reply(200, mockSendingDomain); |
| 136 | + |
| 137 | + const result = await sendingDomainsAPI.get(mockSendingDomain.id); |
| 138 | + |
| 139 | + expect(result).toEqual(mockSendingDomain); |
| 140 | + }); |
| 141 | + }); |
| 142 | + |
| 143 | + describe("sendingDomains.create(): ", () => { |
| 144 | + it("should create a new sending domain.", async () => { |
| 145 | + const mockDnsRecords: DnsRecord[] = [ |
| 146 | + { |
| 147 | + key: "verification", |
| 148 | + domain: "ve6wza2rbpe60x7z.newdomain.com", |
| 149 | + type: "CNAME", |
| 150 | + value: "smtp.mailtrap.live", |
| 151 | + status: "pass", |
| 152 | + name: "ve6wza2rbpe60x7z", |
| 153 | + }, |
| 154 | + ]; |
| 155 | + |
| 156 | + const mockPermissions: SendingDomainPermissions = { |
| 157 | + can_read: true, |
| 158 | + can_update: true, |
| 159 | + can_destroy: true, |
| 160 | + }; |
| 161 | + |
| 162 | + const mockSendingDomain: SendingDomain = { |
| 163 | + id: 436, |
| 164 | + domain_name: "newdomain.com", |
| 165 | + demo: false, |
| 166 | + compliance_status: "pending", |
| 167 | + dns_verified: false, |
| 168 | + dns_verified_at: "", |
| 169 | + dns_records: mockDnsRecords, |
| 170 | + open_tracking_enabled: true, |
| 171 | + click_tracking_enabled: true, |
| 172 | + auto_unsubscribe_link_enabled: true, |
| 173 | + custom_domain_tracking_enabled: true, |
| 174 | + health_alerts_enabled: true, |
| 175 | + critical_alerts_enabled: true, |
| 176 | + alert_recipient_email: "[email protected]", |
| 177 | + permissions: mockPermissions, |
| 178 | + }; |
| 179 | + |
| 180 | + const createParams = { |
| 181 | + domain_name: "newdomain.com", |
| 182 | + }; |
| 183 | + |
| 184 | + mock |
| 185 | + .onPost( |
| 186 | + `https://mailtrap.io/api/accounts/${testAccountId}/sending_domains`, |
| 187 | + { sending_domain: createParams } |
| 188 | + ) |
| 189 | + .reply(201, mockSendingDomain); |
| 190 | + |
| 191 | + const result = await sendingDomainsAPI.create(createParams); |
| 192 | + |
| 193 | + expect(result).toEqual(mockSendingDomain); |
| 194 | + }); |
| 195 | + }); |
| 196 | + |
| 197 | + describe("sendingDomains.delete(): ", () => { |
| 198 | + it("should delete a sending domain by id.", async () => { |
| 199 | + const sendingDomainId = 999; |
| 200 | + |
| 201 | + mock |
| 202 | + .onDelete( |
| 203 | + `https://mailtrap.io/api/accounts/${testAccountId}/sending_domains/${sendingDomainId}` |
| 204 | + ) |
| 205 | + .reply(204); |
| 206 | + |
| 207 | + const result = await sendingDomainsAPI.delete(sendingDomainId); |
| 208 | + |
| 209 | + expect(result).toBeUndefined(); |
| 210 | + }); |
| 211 | + }); |
| 212 | + |
| 213 | + describe("sendingDomains.sendSetupInstructions(): ", () => { |
| 214 | + it("should send setup instructions for a sending domain.", async () => { |
| 215 | + const sendingDomainId = 999; |
| 216 | + const email = "[email protected]"; |
| 217 | + |
| 218 | + mock |
| 219 | + .onPost( |
| 220 | + `https://mailtrap.io/api/accounts/${testAccountId}/sending_domains/${sendingDomainId}/send_setup_instructions` |
| 221 | + ) |
| 222 | + .reply(204); |
| 223 | + |
| 224 | + const result = await sendingDomainsAPI.sendSetupInstructions( |
| 225 | + sendingDomainId, |
| 226 | + email |
| 227 | + ); |
| 228 | + |
| 229 | + expect(result).toBeUndefined(); |
| 230 | + }); |
| 231 | + }); |
| 232 | + }); |
| 233 | +}); |
0 commit comments