|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const mockFetch = vi.fn(); |
| 4 | +global.fetch = mockFetch as typeof fetch; |
| 5 | + |
| 6 | +import { infomaniakClient } from "@dokploy/server/utils/dns/infomaniak"; |
| 7 | + |
| 8 | +const jsonResponse = (body: unknown, ok = true, status = 200) => |
| 9 | + ({ |
| 10 | + ok, |
| 11 | + status, |
| 12 | + json: async () => body, |
| 13 | + }) as Response; |
| 14 | + |
| 15 | +const ikSuccess = (data: unknown) => jsonResponse({ result: "success", data }); |
| 16 | + |
| 17 | +const ikError = (description: string, status = 400) => |
| 18 | + jsonResponse( |
| 19 | + { result: "error", error: { code: "not_authorized", description } }, |
| 20 | + false, |
| 21 | + status, |
| 22 | + ); |
| 23 | + |
| 24 | +const config = { |
| 25 | + providerType: "infomaniak" as const, |
| 26 | + apiToken: "ik_test_token", |
| 27 | +}; |
| 28 | + |
| 29 | +const lastCall = () => |
| 30 | + mockFetch.mock.calls.at(-1) as [string, RequestInit & { method?: string }]; |
| 31 | + |
| 32 | +const lastBody = () => JSON.parse(lastCall()[1].body as string); |
| 33 | + |
| 34 | +beforeEach(() => { |
| 35 | + mockFetch.mockReset(); |
| 36 | +}); |
| 37 | + |
| 38 | +describe("infomaniakClient.listZones", () => { |
| 39 | + it("exposes each domain product as a zone keyed by its name", async () => { |
| 40 | + mockFetch.mockResolvedValue( |
| 41 | + ikSuccess([ |
| 42 | + { id: 1, customer_name: "example.com" }, |
| 43 | + { id: 2, customer_name: "example.ch" }, |
| 44 | + ]), |
| 45 | + ); |
| 46 | + |
| 47 | + const zones = await infomaniakClient.listZones(config); |
| 48 | + |
| 49 | + expect(zones).toEqual([ |
| 50 | + { id: "example.com", name: "example.com" }, |
| 51 | + { id: "example.ch", name: "example.ch" }, |
| 52 | + ]); |
| 53 | + const [url, init] = lastCall(); |
| 54 | + expect(url).toBe( |
| 55 | + "https://api.infomaniak.com/1/product?service_name=domain", |
| 56 | + ); |
| 57 | + expect(init.headers).toMatchObject({ |
| 58 | + Authorization: "Bearer ik_test_token", |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + it("propagates the API error description", async () => { |
| 63 | + mockFetch.mockResolvedValue(ikError("Authorization required", 401)); |
| 64 | + |
| 65 | + await expect(infomaniakClient.listZones(config)).rejects.toThrow( |
| 66 | + "Authorization required", |
| 67 | + ); |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +describe("infomaniakClient.listRecords", () => { |
| 72 | + it("rebuilds the fqdn from the relative source", async () => { |
| 73 | + mockFetch.mockResolvedValue( |
| 74 | + ikSuccess([ |
| 75 | + { id: 10, type: "A", source: "app", target: "1.2.3.4", ttl: 300 }, |
| 76 | + { id: 11, type: "A", source: "", target: "5.6.7.8", ttl: 600 }, |
| 77 | + ]), |
| 78 | + ); |
| 79 | + |
| 80 | + const records = await infomaniakClient.listRecords(config, "example.com"); |
| 81 | + |
| 82 | + expect(records).toEqual([ |
| 83 | + { |
| 84 | + id: "10", |
| 85 | + type: "A", |
| 86 | + name: "app.example.com", |
| 87 | + content: "1.2.3.4", |
| 88 | + ttl: 300, |
| 89 | + }, |
| 90 | + { |
| 91 | + id: "11", |
| 92 | + type: "A", |
| 93 | + name: "example.com", |
| 94 | + content: "5.6.7.8", |
| 95 | + ttl: 600, |
| 96 | + }, |
| 97 | + ]); |
| 98 | + expect(lastCall()[0]).toBe( |
| 99 | + "https://api.infomaniak.com/2/zones/example.com/records?with=records_description", |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + it("treats an @ source as the apex", async () => { |
| 104 | + mockFetch.mockResolvedValue( |
| 105 | + ikSuccess([ |
| 106 | + { id: 12, type: "A", source: "@", target: "1.2.3.4", ttl: 300 }, |
| 107 | + ]), |
| 108 | + ); |
| 109 | + |
| 110 | + const records = await infomaniakClient.listRecords(config, "example.com"); |
| 111 | + |
| 112 | + expect(records[0]?.name).toBe("example.com"); |
| 113 | + }); |
| 114 | + |
| 115 | + it("unquotes TXT targets", async () => { |
| 116 | + mockFetch.mockResolvedValue( |
| 117 | + ikSuccess([ |
| 118 | + { |
| 119 | + id: 13, |
| 120 | + type: "TXT", |
| 121 | + source: "_acme-challenge", |
| 122 | + target: '"token-value"', |
| 123 | + ttl: 300, |
| 124 | + }, |
| 125 | + ]), |
| 126 | + ); |
| 127 | + |
| 128 | + const records = await infomaniakClient.listRecords(config, "example.com"); |
| 129 | + |
| 130 | + expect(records[0]?.content).toBe("token-value"); |
| 131 | + }); |
| 132 | + |
| 133 | + it("leaves a CAA target untouched", async () => { |
| 134 | + mockFetch.mockResolvedValue( |
| 135 | + ikSuccess([ |
| 136 | + { |
| 137 | + id: 14, |
| 138 | + type: "CAA", |
| 139 | + source: "", |
| 140 | + target: '0 issue "letsencrypt.org"', |
| 141 | + ttl: 300, |
| 142 | + }, |
| 143 | + ]), |
| 144 | + ); |
| 145 | + |
| 146 | + const records = await infomaniakClient.listRecords(config, "example.com"); |
| 147 | + |
| 148 | + expect(records[0]?.content).toBe('0 issue "letsencrypt.org"'); |
| 149 | + }); |
| 150 | +}); |
| 151 | + |
| 152 | +describe("infomaniakClient.upsertRecord", () => { |
| 153 | + it("creates the record when no matching source and type exists", async () => { |
| 154 | + mockFetch |
| 155 | + .mockResolvedValueOnce(ikSuccess([])) |
| 156 | + .mockResolvedValueOnce(ikSuccess({ id: 42 })); |
| 157 | + |
| 158 | + const result = await infomaniakClient.upsertRecord(config, { |
| 159 | + zoneId: "example.com", |
| 160 | + type: "A", |
| 161 | + name: "app.example.com", |
| 162 | + content: "1.2.3.4", |
| 163 | + ttl: 600, |
| 164 | + }); |
| 165 | + |
| 166 | + expect(result).toEqual({ id: "42" }); |
| 167 | + const [url, init] = lastCall(); |
| 168 | + expect(url).toBe("https://api.infomaniak.com/2/zones/example.com/records"); |
| 169 | + expect(init.method).toBe("POST"); |
| 170 | + expect(lastBody()).toEqual({ |
| 171 | + type: "A", |
| 172 | + source: "app", |
| 173 | + target: "1.2.3.4", |
| 174 | + ttl: 600, |
| 175 | + }); |
| 176 | + }); |
| 177 | + |
| 178 | + it("updates the existing record instead of creating a duplicate", async () => { |
| 179 | + mockFetch |
| 180 | + .mockResolvedValueOnce( |
| 181 | + ikSuccess([ |
| 182 | + { id: 7, type: "A", source: "app", target: "1.1.1.1", ttl: 300 }, |
| 183 | + ]), |
| 184 | + ) |
| 185 | + .mockResolvedValueOnce(ikSuccess({ id: 7 })); |
| 186 | + |
| 187 | + const result = await infomaniakClient.upsertRecord(config, { |
| 188 | + zoneId: "example.com", |
| 189 | + type: "A", |
| 190 | + name: "app.example.com", |
| 191 | + content: "1.2.3.4", |
| 192 | + }); |
| 193 | + |
| 194 | + expect(result).toEqual({ id: "7" }); |
| 195 | + const [url, init] = lastCall(); |
| 196 | + expect(url).toBe( |
| 197 | + "https://api.infomaniak.com/2/zones/example.com/records/7", |
| 198 | + ); |
| 199 | + expect(init.method).toBe("PUT"); |
| 200 | + expect(lastBody().ttl).toBe(300); |
| 201 | + }); |
| 202 | + |
| 203 | + it("writes an empty source for an apex record and strips the trailing dot", async () => { |
| 204 | + mockFetch |
| 205 | + .mockResolvedValueOnce(ikSuccess([])) |
| 206 | + .mockResolvedValueOnce(ikSuccess({ id: 43 })); |
| 207 | + |
| 208 | + await infomaniakClient.upsertRecord(config, { |
| 209 | + zoneId: "example.com", |
| 210 | + type: "A", |
| 211 | + name: "example.com.", |
| 212 | + content: "1.2.3.4", |
| 213 | + }); |
| 214 | + |
| 215 | + expect(lastBody().source).toBe(""); |
| 216 | + }); |
| 217 | + |
| 218 | + it("quotes a TXT target on write", async () => { |
| 219 | + mockFetch |
| 220 | + .mockResolvedValueOnce(ikSuccess([])) |
| 221 | + .mockResolvedValueOnce(ikSuccess({ id: 44 })); |
| 222 | + |
| 223 | + await infomaniakClient.upsertRecord(config, { |
| 224 | + zoneId: "example.com", |
| 225 | + type: "TXT", |
| 226 | + name: "_acme-challenge.example.com", |
| 227 | + content: "token-value", |
| 228 | + }); |
| 229 | + |
| 230 | + expect(lastBody().target).toBe('"token-value"'); |
| 231 | + }); |
| 232 | + |
| 233 | + it("does not double-quote a TXT target that is already quoted", async () => { |
| 234 | + mockFetch |
| 235 | + .mockResolvedValueOnce(ikSuccess([])) |
| 236 | + .mockResolvedValueOnce(ikSuccess({ id: 45 })); |
| 237 | + |
| 238 | + await infomaniakClient.upsertRecord(config, { |
| 239 | + zoneId: "example.com", |
| 240 | + type: "TXT", |
| 241 | + name: "_acme-challenge.example.com", |
| 242 | + content: '"token-value"', |
| 243 | + }); |
| 244 | + |
| 245 | + expect(lastBody().target).toBe('"token-value"'); |
| 246 | + }); |
| 247 | +}); |
| 248 | + |
| 249 | +describe("infomaniakClient.updateRecord", () => { |
| 250 | + it("updates the record and keeps its id", async () => { |
| 251 | + mockFetch.mockResolvedValue(ikSuccess({ id: 7 })); |
| 252 | + |
| 253 | + const result = await infomaniakClient.updateRecord( |
| 254 | + config, |
| 255 | + "example.com", |
| 256 | + "7", |
| 257 | + { |
| 258 | + type: "CNAME", |
| 259 | + name: "www.example.com", |
| 260 | + content: "example.com", |
| 261 | + ttl: 900, |
| 262 | + }, |
| 263 | + ); |
| 264 | + |
| 265 | + expect(result).toEqual({ id: "7" }); |
| 266 | + const [url, init] = lastCall(); |
| 267 | + expect(url).toBe( |
| 268 | + "https://api.infomaniak.com/2/zones/example.com/records/7", |
| 269 | + ); |
| 270 | + expect(init.method).toBe("PUT"); |
| 271 | + expect(lastBody()).toEqual({ |
| 272 | + type: "CNAME", |
| 273 | + source: "www", |
| 274 | + target: "example.com", |
| 275 | + ttl: 900, |
| 276 | + }); |
| 277 | + }); |
| 278 | + |
| 279 | + it("falls back to the default ttl when none is provided", async () => { |
| 280 | + mockFetch.mockResolvedValue(ikSuccess({ id: 7 })); |
| 281 | + |
| 282 | + await infomaniakClient.updateRecord(config, "example.com", "7", { |
| 283 | + type: "A", |
| 284 | + name: "app.example.com", |
| 285 | + content: "1.2.3.4", |
| 286 | + }); |
| 287 | + |
| 288 | + expect(lastBody().ttl).toBe(300); |
| 289 | + }); |
| 290 | +}); |
| 291 | + |
| 292 | +describe("infomaniakClient.deleteRecord", () => { |
| 293 | + it("deletes the record", async () => { |
| 294 | + mockFetch.mockResolvedValue(ikSuccess(null)); |
| 295 | + |
| 296 | + await infomaniakClient.deleteRecord(config, "example.com", "7"); |
| 297 | + |
| 298 | + const [url, init] = lastCall(); |
| 299 | + expect(url).toBe( |
| 300 | + "https://api.infomaniak.com/2/zones/example.com/records/7", |
| 301 | + ); |
| 302 | + expect(init.method).toBe("DELETE"); |
| 303 | + }); |
| 304 | + |
| 305 | + it("propagates a delete failure", async () => { |
| 306 | + mockFetch.mockResolvedValue(ikError("Record not found", 404)); |
| 307 | + |
| 308 | + await expect( |
| 309 | + infomaniakClient.deleteRecord(config, "example.com", "7"), |
| 310 | + ).rejects.toThrow("Record not found"); |
| 311 | + }); |
| 312 | +}); |
| 313 | + |
| 314 | +describe("infomaniakClient.testConnection", () => { |
| 315 | + it("resolves when the domain listing succeeds", async () => { |
| 316 | + mockFetch.mockResolvedValue(ikSuccess([])); |
| 317 | + |
| 318 | + await expect( |
| 319 | + infomaniakClient.testConnection(config), |
| 320 | + ).resolves.toBeUndefined(); |
| 321 | + }); |
| 322 | + |
| 323 | + it("rejects on an invalid token", async () => { |
| 324 | + mockFetch.mockResolvedValue(ikError("Authorization required", 401)); |
| 325 | + |
| 326 | + await expect(infomaniakClient.testConnection(config)).rejects.toThrow( |
| 327 | + "Infomaniak: request to /1/product?service_name=domain failed: Authorization required", |
| 328 | + ); |
| 329 | + }); |
| 330 | +}); |
0 commit comments