|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { createTokenPromise } from "../../src/utils/create-token-promise"; |
| 3 | + |
| 4 | +vi.mock("../../src/utils/get-superuser-token"); |
| 5 | + |
| 6 | +describe("createTokenPromise", async () => { |
| 7 | + const superuserToken = "superuser-token"; |
| 8 | + |
| 9 | + const gst = await import("../../src/utils/get-superuser-token"); |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + gst.getSuperuserToken = vi.fn().mockResolvedValue(superuserToken); |
| 13 | + }); |
| 14 | + |
| 15 | + it("should return impersonate token when provided", async () => { |
| 16 | + const impersonateToken = "impersonate-token"; |
| 17 | + |
| 18 | + const token = await createTokenPromise({ |
| 19 | + superuserCredentials: { impersonateToken }, |
| 20 | + url: "doesnt-matter" |
| 21 | + }); |
| 22 | + |
| 23 | + expect(token).toBe(impersonateToken); |
| 24 | + expect(gst.getSuperuserToken).not.toHaveBeenCalled(); |
| 25 | + }); |
| 26 | + |
| 27 | + it("should return superuser token when email and password are provided", async () => { |
| 28 | + const token = await createTokenPromise({ |
| 29 | + superuserCredentials: { |
| 30 | + email: "doesnt-matter", |
| 31 | + password: "doesnt-matter" |
| 32 | + }, |
| 33 | + url: "doesnt-matter" |
| 34 | + }); |
| 35 | + |
| 36 | + expect(token).toBe(superuserToken); |
| 37 | + expect(gst.getSuperuserToken).toHaveBeenCalledWith("doesnt-matter", { |
| 38 | + email: "doesnt-matter", |
| 39 | + password: "doesnt-matter" |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should return undefined when no credentials are provided", async () => { |
| 44 | + const token = await createTokenPromise({ |
| 45 | + url: "doesnt-matter" |
| 46 | + }); |
| 47 | + |
| 48 | + expect(token).toBeUndefined(); |
| 49 | + expect(gst.getSuperuserToken).not.toHaveBeenCalled(); |
| 50 | + }); |
| 51 | +}); |
0 commit comments