|
| 1 | +import { ensureCurrentIpInAccessList } from "../../src/common/atlas/ensureAccessList.js"; |
| 2 | +import { ApiClientError } from "../../src/common/atlas/apiClientError.js"; |
| 3 | + |
| 4 | +describe("ensureCurrentIpInAccessList", () => { |
| 5 | + const projectId = "test-project-id"; |
| 6 | + const ip = "1.2.3.4"; |
| 7 | + let apiClient: any; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + apiClient = { |
| 11 | + getIpInfo: jest.fn().mockResolvedValue({ currentIpv4Address: ip }), |
| 12 | + createProjectIpAccessList: jest.fn().mockResolvedValue(undefined), |
| 13 | + }; |
| 14 | + }); |
| 15 | + |
| 16 | + it("adds the current IP to the access list", async () => { |
| 17 | + await expect(ensureCurrentIpInAccessList(apiClient, projectId)).resolves.not.toThrow(); |
| 18 | + expect(apiClient.getIpInfo).toHaveBeenCalled(); |
| 19 | + expect(apiClient.createProjectIpAccessList).toHaveBeenCalledWith({ |
| 20 | + params: { path: { groupId: projectId } }, |
| 21 | + body: [ |
| 22 | + { |
| 23 | + groupId: projectId, |
| 24 | + ipAddress: ip, |
| 25 | + comment: expect.any(String), |
| 26 | + }, |
| 27 | + ], |
| 28 | + }); |
| 29 | + }); |
| 30 | + |
| 31 | + it("is idempotent if the IP is already present (409 error)", async () => { |
| 32 | + apiClient.createProjectIpAccessList.mockRejectedValueOnce( |
| 33 | + Object.assign(new ApiClientError("Conflict", { status: 409, statusText: "Conflict" } as any), { |
| 34 | + response: { status: 409 }, |
| 35 | + }) |
| 36 | + ); |
| 37 | + await expect(ensureCurrentIpInAccessList(apiClient, projectId)).resolves.not.toThrow(); |
| 38 | + }); |
| 39 | + |
| 40 | + it("throws for other errors", async () => { |
| 41 | + apiClient.createProjectIpAccessList.mockRejectedValueOnce( |
| 42 | + Object.assign(new ApiClientError("Other", { status: 500, statusText: "Server Error" } as any), { |
| 43 | + response: { status: 500 }, |
| 44 | + }) |
| 45 | + ); |
| 46 | + await expect(ensureCurrentIpInAccessList(apiClient, projectId)).rejects.toThrow(); |
| 47 | + }); |
| 48 | +}); |
0 commit comments