|
| 1 | +import { rmSync } from "node:fs" |
| 2 | +import { afterEach, describe, expect, test, vi } from "vitest" |
| 3 | + |
| 4 | +const { testHome } = vi.hoisted(() => ({ |
| 5 | + testHome: "/tmp/opensea-cli-auth-store-test", |
| 6 | +})) |
| 7 | + |
| 8 | +vi.mock("node:os", () => ({ homedir: () => testHome })) |
| 9 | + |
| 10 | +import { |
| 11 | + listTokens, |
| 12 | + loadCurrentToken, |
| 13 | + loadToken, |
| 14 | + saveToken, |
| 15 | +} from "../../src/auth/store.js" |
| 16 | + |
| 17 | +function jwt(payload: Record<string, unknown>): string { |
| 18 | + const encoded = Buffer.from(JSON.stringify(payload)) |
| 19 | + .toString("base64url") |
| 20 | + .replace(/=+$/, "") |
| 21 | + return `header.${encoded}.signature` |
| 22 | +} |
| 23 | + |
| 24 | +afterEach(() => { |
| 25 | + rmSync(testHome, { recursive: true, force: true }) |
| 26 | +}) |
| 27 | + |
| 28 | +describe("auth store", () => { |
| 29 | + test("derives missing stored scopes from the OpenSea JWT claim", () => { |
| 30 | + const token = { |
| 31 | + accessToken: jwt({ |
| 32 | + opensea_scopes: |
| 33 | + "write:orders read:eligibility read:rewards unknown:scope", |
| 34 | + }), |
| 35 | + refreshToken: "refresh", |
| 36 | + expiresAt: "2030-01-01T00:00:00.000Z", |
| 37 | + scopes: [], |
| 38 | + address: "0xAbC", |
| 39 | + } |
| 40 | + saveToken(token) |
| 41 | + |
| 42 | + expect(loadCurrentToken()?.scopes).toEqual([ |
| 43 | + "read:eligibility", |
| 44 | + "write:orders", |
| 45 | + ]) |
| 46 | + expect(loadToken("0xabc")?.scopes).toEqual([ |
| 47 | + "read:eligibility", |
| 48 | + "write:orders", |
| 49 | + ]) |
| 50 | + expect(listTokens()[0]?.scopes).toEqual([ |
| 51 | + "read:eligibility", |
| 52 | + "write:orders", |
| 53 | + ]) |
| 54 | + }) |
| 55 | + |
| 56 | + test("preserves a non-empty stored scope list", () => { |
| 57 | + saveToken({ |
| 58 | + accessToken: jwt({ opensea_scopes: "write:orders" }), |
| 59 | + refreshToken: "refresh", |
| 60 | + expiresAt: "2030-01-01T00:00:00.000Z", |
| 61 | + scopes: ["read:favorites"], |
| 62 | + address: "0xabc", |
| 63 | + }) |
| 64 | + |
| 65 | + expect(loadCurrentToken()?.scopes).toEqual(["read:favorites"]) |
| 66 | + }) |
| 67 | + |
| 68 | + test("lists mixed stored and derived scope states", () => { |
| 69 | + saveToken({ |
| 70 | + accessToken: jwt({ opensea_scopes: "write:orders" }), |
| 71 | + refreshToken: "refresh-1", |
| 72 | + expiresAt: "2030-01-01T00:00:00.000Z", |
| 73 | + scopes: [], |
| 74 | + address: "0xabc", |
| 75 | + }) |
| 76 | + saveToken({ |
| 77 | + accessToken: jwt({ opensea_scopes: "write:orders" }), |
| 78 | + refreshToken: "refresh-2", |
| 79 | + expiresAt: "2030-01-01T00:00:00.000Z", |
| 80 | + scopes: ["read:favorites"], |
| 81 | + address: "0xdef", |
| 82 | + }) |
| 83 | + |
| 84 | + expect(listTokens().map(token => token.scopes)).toEqual([ |
| 85 | + ["write:orders"], |
| 86 | + ["read:favorites"], |
| 87 | + ]) |
| 88 | + }) |
| 89 | + |
| 90 | + test("leaves scopes empty for a malformed access token", () => { |
| 91 | + saveToken({ |
| 92 | + accessToken: "not-a-jwt", |
| 93 | + refreshToken: "refresh", |
| 94 | + expiresAt: "2030-01-01T00:00:00.000Z", |
| 95 | + scopes: [], |
| 96 | + address: "0xabc", |
| 97 | + }) |
| 98 | + |
| 99 | + expect(loadCurrentToken()?.scopes).toEqual([]) |
| 100 | + }) |
| 101 | +}) |
0 commit comments