|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 2 | +import { refreshToken } from "."; |
| 3 | +import { SessionManager, StorageKeys } from "../../sessionManager"; |
| 4 | +import * as tokenUtils from "."; |
| 5 | + |
| 6 | +describe("refreshToken", () => { |
| 7 | + const mockDomain = "https://example.com"; |
| 8 | + const mockClientId = "test-client-id"; |
| 9 | + const mockRefreshTokenValue = "mock-refresh-token"; |
| 10 | + const mockStorage: SessionManager = { |
| 11 | + getSessionItem: vi.fn(), |
| 12 | + setSessionItem: vi.fn(), |
| 13 | + removeSessionItem: vi.fn(), |
| 14 | + destroySession: vi.fn(), |
| 15 | + }; |
| 16 | + |
| 17 | + beforeEach(() => { |
| 18 | + vi.resetAllMocks(); |
| 19 | + vi.spyOn(tokenUtils, "getDecodedToken").mockResolvedValue(null); |
| 20 | + vi.spyOn(tokenUtils, "getActiveStorage").mockResolvedValue(mockStorage); |
| 21 | + // vi.spyOn(Utils, 'sanatizeURL').mockImplementation((url) => url); |
| 22 | + global.fetch = vi.fn(); |
| 23 | + vi.spyOn(console, "error").mockImplementation(() => {}); |
| 24 | + }); |
| 25 | + |
| 26 | + afterEach(() => { |
| 27 | + vi.restoreAllMocks(); |
| 28 | + }); |
| 29 | + |
| 30 | + it("should return false if domain is not provided", async () => { |
| 31 | + const result = await refreshToken(undefined, mockClientId); |
| 32 | + expect(result).toBe(false); |
| 33 | + expect(console.error).toHaveBeenCalledWith( |
| 34 | + "Domain is required for token refresh", |
| 35 | + ); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should return false if clientId is not provided", async () => { |
| 39 | + const result = await refreshToken(mockDomain, undefined); |
| 40 | + expect(result).toBe(false); |
| 41 | + expect(console.error).toHaveBeenCalledWith( |
| 42 | + "Client ID is required for token refresh", |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + it("should return false if no refresh token is found", async () => { |
| 47 | + // mockStorage.getSessionItem.mockResolvedValue(null); |
| 48 | + const result = await refreshToken(mockDomain, mockClientId); |
| 49 | + expect(result).toBe(false); |
| 50 | + expect(console.error).toHaveBeenCalledWith("No refresh token found"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should return false if the fetch request fails", async () => { |
| 54 | + mockStorage.getSessionItem = vi |
| 55 | + .fn() |
| 56 | + .mockResolvedValue(mockRefreshTokenValue); |
| 57 | + vi.mocked(global.fetch).mockRejectedValue(new Error("Network error")); |
| 58 | + const result = await refreshToken(mockDomain, mockClientId); |
| 59 | + expect(result).toBe(false); |
| 60 | + expect(console.error).toHaveBeenCalledWith( |
| 61 | + "Error refreshing token:", |
| 62 | + expect.any(Error), |
| 63 | + ); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should return false if the response is not ok", async () => { |
| 67 | + mockStorage.getSessionItem = vi |
| 68 | + .fn() |
| 69 | + .mockResolvedValue(mockRefreshTokenValue); |
| 70 | + vi.mocked(global.fetch).mockResolvedValue({ ok: false } as Response); |
| 71 | + const result = await refreshToken(mockDomain, mockClientId); |
| 72 | + expect(result).toBe(false); |
| 73 | + expect(console.error).toHaveBeenCalledWith("Failed to refresh token"); |
| 74 | + }); |
| 75 | + |
| 76 | + it("should return false if the response does not contain an access token", async () => { |
| 77 | + mockStorage.getSessionItem = vi |
| 78 | + .fn() |
| 79 | + .mockResolvedValue(mockRefreshTokenValue); |
| 80 | + vi.mocked(global.fetch).mockResolvedValue({ |
| 81 | + ok: true, |
| 82 | + json: () => Promise.resolve({}), |
| 83 | + } as Response); |
| 84 | + const result = await refreshToken(mockDomain, mockClientId); |
| 85 | + expect(result).toBe(false); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should return true and update tokens if the refresh is successful", async () => { |
| 89 | + const mockResponse = { |
| 90 | + access_token: "new-access-token", |
| 91 | + id_token: "new-id-token", |
| 92 | + refresh_token: "new-refresh-token", |
| 93 | + }; |
| 94 | + mockStorage.getSessionItem = vi |
| 95 | + .fn() |
| 96 | + .mockResolvedValue(mockRefreshTokenValue); |
| 97 | + vi.mocked(global.fetch).mockResolvedValue({ |
| 98 | + ok: true, |
| 99 | + json: () => Promise.resolve(mockResponse), |
| 100 | + } as Response); |
| 101 | + |
| 102 | + const result = await refreshToken(mockDomain, mockClientId); |
| 103 | + |
| 104 | + expect(result).toBe(true); |
| 105 | + expect(mockStorage.setSessionItem).toHaveBeenCalledWith( |
| 106 | + StorageKeys.accessToken, |
| 107 | + "new-access-token", |
| 108 | + ); |
| 109 | + expect(mockStorage.setSessionItem).toHaveBeenCalledWith( |
| 110 | + StorageKeys.idToken, |
| 111 | + "new-id-token", |
| 112 | + ); |
| 113 | + expect(mockStorage.setSessionItem).toHaveBeenCalledWith( |
| 114 | + StorageKeys.refreshToken, |
| 115 | + "new-refresh-token", |
| 116 | + ); |
| 117 | + }); |
| 118 | + |
| 119 | + it("should use sanatizeURL for the domain", async () => { |
| 120 | + mockStorage.getSessionItem = vi |
| 121 | + .fn() |
| 122 | + .mockResolvedValue(mockRefreshTokenValue); |
| 123 | + vi.mocked(global.fetch).mockResolvedValue({ |
| 124 | + ok: true, |
| 125 | + json: () => Promise.resolve({ access_token: "new-token" }), |
| 126 | + } as Response); |
| 127 | + |
| 128 | + await refreshToken("https://example.com/", mockClientId); |
| 129 | + |
| 130 | + expect(global.fetch).toHaveBeenCalledWith( |
| 131 | + expect.stringContaining(`https://example.com/oauth2/token`), |
| 132 | + expect.any(Object), |
| 133 | + ); |
| 134 | + }); |
| 135 | +}); |
0 commit comments