Files
openclaw/apps/macos/Tests/OpenClawIPCTests/OpenClawOAuthStoreTests.swift

98 lines
2.9 KiB
Swift
Raw Normal View History

2025-12-14 19:10:48 +00:00
import Foundation
import Testing
2026-01-30 03:15:10 +01:00
@testable import OpenClaw
2025-12-14 19:10:48 +00:00
@Suite
2026-01-30 03:15:10 +01:00
struct OpenClawOAuthStoreTests {
2025-12-14 19:10:48 +00:00
@Test
func returnsMissingWhenFileAbsent() {
let url = FileManager().temporaryDirectory
2026-01-30 03:15:10 +01:00
.appendingPathComponent("openclaw-oauth-\(UUID().uuidString)")
2025-12-14 19:10:48 +00:00
.appendingPathComponent("oauth.json")
2026-01-30 03:15:10 +01:00
#expect(OpenClawOAuthStore.anthropicOAuthStatus(at: url) == .missingFile)
2025-12-14 19:10:48 +00:00
}
@Test
2026-01-30 03:15:10 +01:00
func usesEnvOverrideForOpenClawOAuthDir() throws {
let key = "OPENCLAW_OAUTH_DIR"
let previous = ProcessInfo.processInfo.environment[key]
defer {
if let previous {
setenv(key, previous, 1)
} else {
unsetenv(key)
}
}
let dir = FileManager().temporaryDirectory
2026-01-30 03:15:10 +01:00
.appendingPathComponent("openclaw-oauth-\(UUID().uuidString)", isDirectory: true)
setenv(key, dir.path, 1)
2026-01-30 03:15:10 +01:00
#expect(OpenClawOAuthStore.oauthDir().standardizedFileURL == dir.standardizedFileURL)
}
2025-12-14 19:10:48 +00:00
@Test
func acceptsPiFormatTokens() throws {
let url = try self.writeOAuthFile([
"anthropic": [
"type": "oauth",
"refresh": "r1",
"access": "a1",
"expires": 1_234_567_890,
],
])
2026-01-30 03:15:10 +01:00
#expect(OpenClawOAuthStore.anthropicOAuthStatus(at: url).isConnected)
2025-12-14 19:10:48 +00:00
}
@Test
func acceptsTokenKeyVariants() throws {
let url = try self.writeOAuthFile([
"anthropic": [
"type": "oauth",
"refresh_token": "r1",
"access_token": "a1",
],
])
2026-01-30 03:15:10 +01:00
#expect(OpenClawOAuthStore.anthropicOAuthStatus(at: url).isConnected)
2025-12-14 19:10:48 +00:00
}
@Test
func reportsMissingProviderEntry() throws {
let url = try self.writeOAuthFile([
"other": [
"type": "oauth",
"refresh": "r1",
"access": "a1",
],
])
2026-01-30 03:15:10 +01:00
#expect(OpenClawOAuthStore.anthropicOAuthStatus(at: url) == .missingProviderEntry)
2025-12-14 19:10:48 +00:00
}
@Test
func reportsMissingTokens() throws {
let url = try self.writeOAuthFile([
"anthropic": [
"type": "oauth",
"refresh": "",
"access": "a1",
],
])
2026-01-30 03:15:10 +01:00
#expect(OpenClawOAuthStore.anthropicOAuthStatus(at: url) == .missingTokens)
2025-12-14 19:10:48 +00:00
}
private func writeOAuthFile(_ json: [String: Any]) throws -> URL {
let dir = FileManager().temporaryDirectory
2026-01-30 03:15:10 +01:00
.appendingPathComponent("openclaw-oauth-\(UUID().uuidString)", isDirectory: true)
try FileManager().createDirectory(at: dir, withIntermediateDirectories: true)
2025-12-14 19:10:48 +00:00
let url = dir.appendingPathComponent("oauth.json")
let data = try JSONSerialization.data(withJSONObject: json, options: [.prettyPrinted, .sortedKeys])
try data.write(to: url, options: [.atomic])
return url
}
}