forked from nicobailon/pi-mcp-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp-auth-flow.test.ts
More file actions
145 lines (125 loc) · 4.02 KB
/
mcp-auth-flow.test.ts
File metadata and controls
145 lines (125 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* Tests for mcp-auth-flow.ts - OAuth flow using MCP SDK
*/
import { describe, it, before, after } from "node:test"
import assert from "node:assert"
import { existsSync, rmSync, mkdirSync } from "fs"
import { join } from "path"
import { tmpdir } from "os"
import { randomBytes } from "crypto"
// Set up isolated temp directory for tests
const TEST_DIR = join(tmpdir(), `mcp-oauth-test-${randomBytes(4).toString('hex')}`)
process.env.MCP_OAUTH_DIR = TEST_DIR
import {
authenticate,
completeAuth,
getAuthStatus,
removeAuth,
supportsOAuth,
initializeOAuth,
shutdownOAuth,
type AuthStatus,
} from "./mcp-auth-flow.js"
import { updateTokens, clearAllCredentials } from "./mcp-auth.js"
import type { ServerEntry } from "./types.js"
describe("mcp-auth-flow", () => {
before(() => {
// Ensure clean state
try {
if (existsSync(TEST_DIR)) {
rmSync(TEST_DIR, { recursive: true, force: true })
}
mkdirSync(TEST_DIR, { recursive: true })
} catch {
// Ignore cleanup errors
}
})
after(async () => {
// Shutdown OAuth and clean up
await shutdownOAuth()
try {
if (existsSync(TEST_DIR)) {
rmSync(TEST_DIR, { recursive: true, force: true })
}
} catch {
// Ignore cleanup errors
}
})
describe("supportsOAuth", () => {
it("should return true for OAuth HTTP server", () => {
const definition: ServerEntry = {
url: "https://api.example.com/mcp",
}
assert.strictEqual(supportsOAuth(definition), true)
})
it("should return false for bearer auth", () => {
const definition: ServerEntry = {
url: "https://api.example.com/mcp",
auth: "bearer",
}
assert.strictEqual(supportsOAuth(definition), false)
})
it("should return false for stdio server", () => {
const definition: ServerEntry = {
command: "npx",
args: ["-y", "@example/mcp-server"],
}
assert.strictEqual(supportsOAuth(definition), false)
})
it("should return false when no URL", () => {
const definition: ServerEntry = {}
assert.strictEqual(supportsOAuth(definition), false)
})
})
describe("getAuthStatus", () => {
it("should return 'not_authenticated' when no tokens", async () => {
const status = await getAuthStatus("status-test-none")
assert.strictEqual(status, "not_authenticated")
})
it("should return 'authenticated' when tokens exist and not expired", async () => {
await updateTokens("status-test-ok", {
accessToken: "token",
expiresAt: Date.now() / 1000 + 3600, // 1 hour from now
})
const status = await getAuthStatus("status-test-ok")
assert.strictEqual(status, "authenticated")
})
it("should return 'expired' when tokens are expired", async () => {
await updateTokens("status-test-expired", {
accessToken: "token",
expiresAt: Date.now() / 1000 - 3600, // 1 hour ago
})
const status = await getAuthStatus("status-test-expired")
assert.strictEqual(status, "expired")
})
})
describe("removeAuth", () => {
it("should remove all credentials", async () => {
await updateTokens("remove-test", { accessToken: "token" })
await removeAuth("remove-test")
const status = await getAuthStatus("remove-test")
assert.strictEqual(status, "not_authenticated")
})
})
describe("initializeOAuth / shutdownOAuth", () => {
it("should start callback server on initialize", async () => {
await initializeOAuth()
// If we get here without error, server started
assert.ok(true)
})
it("should stop callback server on shutdown", async () => {
await initializeOAuth()
await shutdownOAuth()
// If we get here without error, server stopped
assert.ok(true)
})
})
describe("authenticate / completeAuth", () => {
it("should throw if no server URL provided", async () => {
await assert.rejects(
async () => await authenticate("no-url-test", ""),
/Invalid URL/
)
})
})
})