Skip to content

Commit 3da8b2a

Browse files
committed
test(utils): add rate limit test case to superuser login
1 parent 8f996a7 commit 3da8b2a

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

test/utils/get-superuser-token.e2e-spec.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assert, beforeAll, describe, expect, it } from "vitest";
1+
import { assert, beforeAll, describe, expect, it, vi } from "vitest";
22
import { getSuperuserToken } from "../../src/utils/get-superuser-token";
33
import { checkE2eConnection } from "../_mocks/check-e2e-connection";
44
import { createLoaderContext } from "../_mocks/create-loader-context";
@@ -16,6 +16,7 @@ describe("getSuperuserToken", () => {
1616
email: "invalid",
1717
password: "invalid"
1818
});
19+
1920
expect(result).toBeUndefined();
2021
});
2122

@@ -42,6 +43,37 @@ describe("getSuperuserToken", () => {
4243
options.url,
4344
options.superuserCredentials
4445
);
46+
4547
expect(result).toBeDefined();
4648
});
49+
50+
it("should retry on rate limit error", async () => {
51+
assert(options.superuserCredentials, "Superuser credentials are not set.");
52+
assert(
53+
!("impersonateToken" in options.superuserCredentials),
54+
"Impersonate token should not be used in tests."
55+
);
56+
57+
vi.useFakeTimers({
58+
toFake: ["setTimeout"]
59+
});
60+
vi.spyOn(global, "fetch").mockResolvedValueOnce(
61+
new Response(undefined, { status: 429 })
62+
);
63+
64+
const promise = getSuperuserToken(
65+
options.url,
66+
options.superuserCredentials
67+
);
68+
69+
// Fast-forward time to speed up retries
70+
await vi.runAllTimersAsync();
71+
72+
const result = await promise;
73+
74+
expect(result).toBeDefined();
75+
expect(global.fetch).toHaveBeenCalledTimes(2);
76+
77+
vi.useRealTimers();
78+
});
4779
});

0 commit comments

Comments
 (0)