|
| 1 | +import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"; |
| 2 | +import { exchangeAuthCode } from "."; |
| 3 | +import { MemoryStorage, StorageKeys } from "../sessionManager"; |
| 4 | +import { setActiveStorage } from "./token"; |
| 5 | +import createFetchMock from 'vitest-fetch-mock'; |
| 6 | + |
| 7 | +const fetchMock = createFetchMock(vi); |
| 8 | + |
| 9 | +describe.only("exhangeAuthCode", () => { |
| 10 | + beforeEach(() => { |
| 11 | + fetchMock.enableMocks(); |
| 12 | + }); |
| 13 | + |
| 14 | + afterEach(() => { |
| 15 | + fetchMock.resetMocks(); |
| 16 | + }); |
| 17 | + |
| 18 | + it.only("missing state param", async () => { |
| 19 | + const urlParams = new URLSearchParams(); |
| 20 | + urlParams.append("code", "test"); |
| 21 | + |
| 22 | + const result = await exchangeAuthCode({ |
| 23 | + urlParams, |
| 24 | + domain: "http://test.kinde.com", |
| 25 | + clientId: "test", |
| 26 | + redirectURL: "http://test.kinde.com", |
| 27 | + }); |
| 28 | + |
| 29 | + expect(result).toStrictEqual({ |
| 30 | + success: false, |
| 31 | + error: "Invalid state or code", |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + it.only("missing code param", async () => { |
| 36 | + const urlParams = new URLSearchParams(); |
| 37 | + urlParams.append("state", "test"); |
| 38 | + |
| 39 | + const result = await exchangeAuthCode({ |
| 40 | + urlParams, |
| 41 | + domain: "http://test.kinde.com", |
| 42 | + clientId: "test", |
| 43 | + redirectURL: "http://test.kinde.com", |
| 44 | + }); |
| 45 | + |
| 46 | + expect(result).toStrictEqual({ |
| 47 | + success: false, |
| 48 | + error: "Invalid state or code", |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + it.only("missing active storage", async () => { |
| 53 | + const urlParams = new URLSearchParams(); |
| 54 | + urlParams.append("state", "test"); |
| 55 | + urlParams.append("code", "test"); |
| 56 | + |
| 57 | + expect(exchangeAuthCode({ |
| 58 | + urlParams, |
| 59 | + domain: "http://test.kinde.com", |
| 60 | + clientId: "test", |
| 61 | + redirectURL: "http://test.kinde.com", |
| 62 | + })).rejects.toThrowError("No active storage found"); |
| 63 | + }); |
| 64 | + |
| 65 | + it.only("state mismatch", async () => { |
| 66 | + const store = new MemoryStorage(); |
| 67 | + setActiveStorage(store); |
| 68 | + |
| 69 | + await store.setItems({ |
| 70 | + [StorageKeys.state]: "storedState", |
| 71 | + }); |
| 72 | + |
| 73 | + const urlParams = new URLSearchParams(); |
| 74 | + urlParams.append("state", "test"); |
| 75 | + urlParams.append("code", "test"); |
| 76 | + |
| 77 | + const result = await exchangeAuthCode({ |
| 78 | + urlParams, |
| 79 | + domain: "http://test.kinde.com", |
| 80 | + clientId: "test", |
| 81 | + redirectURL: "http://test.kinde.com", |
| 82 | + }) |
| 83 | + |
| 84 | + expect(result).toStrictEqual({ |
| 85 | + success: false, |
| 86 | + error: "Invalid state; supplied test, expected storedState", |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + |
| 91 | + it("should encode a simple string", async () => { |
| 92 | + const store = new MemoryStorage(); |
| 93 | + setActiveStorage(store); |
| 94 | + |
| 95 | + const state = "state"; |
| 96 | + |
| 97 | + await store.setItems({ |
| 98 | + [StorageKeys.state]: state, |
| 99 | + }); |
| 100 | + |
| 101 | + const input = "hello"; |
| 102 | + |
| 103 | + const urlParams = new URLSearchParams(); |
| 104 | + urlParams.append("code", input); |
| 105 | + urlParams.append("state", state); |
| 106 | + urlParams.append("client_id", "test"); |
| 107 | + |
| 108 | + fetchMock.mockResponseOnce(JSON.stringify({ access_token: "access_token", refresh_token: "refresh_token", id_token: "id_token" })); |
| 109 | + |
| 110 | + const result = await exchangeAuthCode({ |
| 111 | + urlParams, |
| 112 | + domain: "http://test.kinde.com", |
| 113 | + clientId: "test", |
| 114 | + redirectURL: "http://test.kinde.com", |
| 115 | + }); |
| 116 | + expect(result).toStrictEqual({ |
| 117 | + accessToken: "access_token", |
| 118 | + refreshToken: "refresh_token", |
| 119 | + idToken: "id_token", |
| 120 | + success: true, |
| 121 | + }); |
| 122 | + |
| 123 | + const postStoredState = await store.getSessionItem(StorageKeys.state); |
| 124 | + expect(postStoredState).toBeNull(); |
| 125 | + const postCodeVerifier = await store.getSessionItem(StorageKeys.codeVerifier); |
| 126 | + expect(postCodeVerifier).toBeNull(); |
| 127 | + }); |
| 128 | + |
| 129 | +}); |
0 commit comments