|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest" |
| 2 | + |
| 3 | +const { loadCurrentToken, saveToken, oauthRefresh, OpenSeaOAuth } = vi.hoisted( |
| 4 | + () => ({ |
| 5 | + loadCurrentToken: vi.fn(), |
| 6 | + saveToken: vi.fn(), |
| 7 | + oauthRefresh: vi.fn(), |
| 8 | + OpenSeaOAuth: vi.fn(() => ({ refresh: oauthRefresh })), |
| 9 | + }), |
| 10 | +) |
| 11 | + |
| 12 | +vi.mock("../../src/auth/store.js", () => ({ |
| 13 | + clearTokens: vi.fn(), |
| 14 | + listTokens: vi.fn(() => []), |
| 15 | + loadCurrentToken, |
| 16 | + removeToken: vi.fn(), |
| 17 | + saveToken, |
| 18 | +})) |
| 19 | + |
| 20 | +vi.mock("@opensea/sdk", async importOriginal => { |
| 21 | + const actual = await importOriginal<typeof import("@opensea/sdk")>() |
| 22 | + return { ...actual, OpenSeaOAuth } |
| 23 | +}) |
| 24 | + |
| 25 | +import { authCommand } from "../../src/commands/auth.js" |
| 26 | +import { createCommandTestContext } from "../mocks.js" |
| 27 | + |
| 28 | +const storedToken = { |
| 29 | + accessToken: "old-access", |
| 30 | + refreshToken: "old-refresh", |
| 31 | + expiresAt: "2030-01-01T00:00:00.000Z", |
| 32 | + scopes: ["read:eligibility"], |
| 33 | + address: "0xabc", |
| 34 | + authMethod: "oauth" as const, |
| 35 | +} |
| 36 | + |
| 37 | +describe("auth refresh", () => { |
| 38 | + beforeEach(() => { |
| 39 | + vi.spyOn(console, "log").mockImplementation(() => {}) |
| 40 | + loadCurrentToken.mockReturnValue(storedToken) |
| 41 | + }) |
| 42 | + |
| 43 | + afterEach(() => { |
| 44 | + vi.clearAllMocks() |
| 45 | + vi.restoreAllMocks() |
| 46 | + delete process.env.OPENSEA_OAUTH_CLIENT_ID |
| 47 | + }) |
| 48 | + |
| 49 | + it("refreshes OAuth sessions through OIDC discovery", async () => { |
| 50 | + oauthRefresh.mockResolvedValue({ |
| 51 | + accessToken: "new-access", |
| 52 | + refreshToken: "new-refresh", |
| 53 | + expiresAt: new Date("2031-01-01T00:00:00.000Z"), |
| 54 | + scopes: ["read:eligibility", "write:orders"], |
| 55 | + }) |
| 56 | + const ctx = createCommandTestContext() |
| 57 | + const cmd = authCommand( |
| 58 | + () => undefined, |
| 59 | + ctx.getFormat, |
| 60 | + () => "https://auth.example.com", |
| 61 | + ) |
| 62 | + |
| 63 | + await cmd.parseAsync(["refresh", "--client-id", "public-client"], { |
| 64 | + from: "user", |
| 65 | + }) |
| 66 | + |
| 67 | + expect(OpenSeaOAuth).toHaveBeenCalledWith({ |
| 68 | + clientId: "public-client", |
| 69 | + issuer: "https://auth.example.com", |
| 70 | + }) |
| 71 | + expect(oauthRefresh).toHaveBeenCalledWith("old-refresh") |
| 72 | + expect(saveToken).toHaveBeenCalledWith({ |
| 73 | + accessToken: "new-access", |
| 74 | + refreshToken: "new-refresh", |
| 75 | + expiresAt: "2031-01-01T00:00:00.000Z", |
| 76 | + scopes: ["read:eligibility", "write:orders"], |
| 77 | + address: "0xabc", |
| 78 | + authMethod: "oauth", |
| 79 | + }) |
| 80 | + }) |
| 81 | + |
| 82 | + it("keeps the previous refresh token when rotation omits one", async () => { |
| 83 | + oauthRefresh.mockResolvedValue({ |
| 84 | + accessToken: "new-access", |
| 85 | + expiresAt: new Date("2031-01-01T00:00:00.000Z"), |
| 86 | + scopes: ["read:eligibility"], |
| 87 | + }) |
| 88 | + const ctx = createCommandTestContext() |
| 89 | + |
| 90 | + await authCommand(() => undefined, ctx.getFormat).parseAsync(["refresh"], { |
| 91 | + from: "user", |
| 92 | + }) |
| 93 | + |
| 94 | + expect(saveToken).toHaveBeenCalledWith( |
| 95 | + expect.objectContaining({ refreshToken: "old-refresh" }), |
| 96 | + ) |
| 97 | + }) |
| 98 | + |
| 99 | + it("keeps the previous refresh token when rotation returns an empty one", async () => { |
| 100 | + oauthRefresh.mockResolvedValue({ |
| 101 | + accessToken: "new-access", |
| 102 | + refreshToken: "", |
| 103 | + expiresAt: new Date("2031-01-01T00:00:00.000Z"), |
| 104 | + scopes: ["read:eligibility"], |
| 105 | + }) |
| 106 | + const ctx = createCommandTestContext() |
| 107 | + |
| 108 | + await authCommand(() => undefined, ctx.getFormat).parseAsync(["refresh"], { |
| 109 | + from: "user", |
| 110 | + }) |
| 111 | + |
| 112 | + expect(saveToken).toHaveBeenCalledWith( |
| 113 | + expect.objectContaining({ refreshToken: "old-refresh" }), |
| 114 | + ) |
| 115 | + }) |
| 116 | + |
| 117 | + it("rejects pre-release sessions without an auth method", async () => { |
| 118 | + loadCurrentToken.mockReturnValue({ |
| 119 | + ...storedToken, |
| 120 | + authMethod: undefined, |
| 121 | + }) |
| 122 | + const ctx = createCommandTestContext() |
| 123 | + |
| 124 | + await expect( |
| 125 | + authCommand(() => undefined, ctx.getFormat).parseAsync(["refresh"], { |
| 126 | + from: "user", |
| 127 | + }), |
| 128 | + ).rejects.toThrow( |
| 129 | + "Stored auth token has no auth method. Run `opensea login` again.", |
| 130 | + ) |
| 131 | + expect(oauthRefresh).not.toHaveBeenCalled() |
| 132 | + }) |
| 133 | + |
| 134 | + it("refreshes SIWE sessions through the SIWE endpoint", async () => { |
| 135 | + loadCurrentToken.mockReturnValue({ ...storedToken, authMethod: "siwe" }) |
| 136 | + const fetchSpy = vi.spyOn(globalThis, "fetch").mockResolvedValue( |
| 137 | + new Response( |
| 138 | + JSON.stringify({ |
| 139 | + access_token: "siwe-access", |
| 140 | + refresh_token: "siwe-refresh", |
| 141 | + expires_in: 3600, |
| 142 | + scopes: ["read:eligibility"], |
| 143 | + }), |
| 144 | + { status: 200, headers: { "Content-Type": "application/json" } }, |
| 145 | + ), |
| 146 | + ) |
| 147 | + const ctx = createCommandTestContext() |
| 148 | + |
| 149 | + await authCommand( |
| 150 | + () => undefined, |
| 151 | + ctx.getFormat, |
| 152 | + () => "https://auth.example.com", |
| 153 | + ).parseAsync(["refresh"], { from: "user" }) |
| 154 | + |
| 155 | + expect(fetchSpy).toHaveBeenCalledWith( |
| 156 | + "https://auth.example.com/api/refresh", |
| 157 | + expect.any(Object), |
| 158 | + ) |
| 159 | + expect(OpenSeaOAuth).not.toHaveBeenCalled() |
| 160 | + expect(saveToken).toHaveBeenCalledWith( |
| 161 | + expect.objectContaining({ authMethod: "siwe" }), |
| 162 | + ) |
| 163 | + }) |
| 164 | + |
| 165 | + it("surfaces OAuth token endpoint failures", async () => { |
| 166 | + oauthRefresh.mockRejectedValue(new Error("Token request failed (400)")) |
| 167 | + const ctx = createCommandTestContext() |
| 168 | + |
| 169 | + await expect( |
| 170 | + authCommand(() => undefined, ctx.getFormat).parseAsync(["refresh"], { |
| 171 | + from: "user", |
| 172 | + }), |
| 173 | + ).rejects.toThrow("Token request failed (400)") |
| 174 | + }) |
| 175 | + |
| 176 | + it("rejects stored sessions without a refresh token", async () => { |
| 177 | + loadCurrentToken.mockReturnValue({ ...storedToken, refreshToken: "" }) |
| 178 | + const ctx = createCommandTestContext() |
| 179 | + |
| 180 | + await expect( |
| 181 | + authCommand(() => undefined, ctx.getFormat).parseAsync(["refresh"], { |
| 182 | + from: "user", |
| 183 | + }), |
| 184 | + ).rejects.toThrow("Stored auth token has no refresh token") |
| 185 | + }) |
| 186 | +}) |
0 commit comments