Files
cline-multi-mirror/src/services/uri/SharedUriHandler.test.ts
T
BeeandTomás Barreiro 10cbbb2c6b feat: Cline Auth Provider (#6131)
* Add ClineAuthProvider and integrate with AuthService

- Add new ClineAuthProvider class for Cline-specific authentication
- Update AuthService to support both Firebase and Cline auth providers
- Switch default provider from Firebase to Cline
- Add dynamic auth URL fetching for Cline provider
- Update type definitions to support multiple provider types

* Add API auth URL configuration and update Cline auth flow

- Add apiAuthUrl to environment configs for all environments
- Update ClineAuthProvider to use new token exchange API endpoint
- Add shared Cline API utilities and types
- Refactor auth service to handle access tokens with expiration
- Update mock auth service and test fixtures for new auth flow

* add changeset

* wip

* refactor authentication service and improve token handling

- Add null check for auth token in ClineAccountService
- Update authorization header format to use 'workos:' prefix
- Replace hardcoded API endpoints with CLINE_API_ENDPOINT constants
- Refactor ClineAuthInfo interface to use accessToken terminology
- Remove Firebase auth provider dependency
- Simplify auth callback handling and token storage
- Improve error handling for missing authentication tokens

* refactor(auth): standardize workos token prefix handling

Centralize workos: prefix application in AuthService.getAuthToken() method instead of duplicating across multiple API call sites. This ensures consistent authentication token formatting and simplifies maintenance by having a single source of truth for token prefixing.

* Remove unused code

* update mock responses

* Set Firebase Auth Provider as default

* Update src/shared/cline/api.ts

Co-authored-by: Tomás Barreiro <52393857+BarreiroT@users.noreply.github.com>

* remove docs

* refactor(auth): replace type union with IAuthProvider interface

- Replace AvailableAuthProvider type union with IAuthProvider interface for better extensibility
- Add ServiceConfig type for provider configuration
- Remove hardcoded providerName field in favor of provider.name property
- Update method signatures to use IAuthProvider interface
- Improve error messages and code comments for clarity
- Add TODO for mock auth provider implementation

---------

Co-authored-by: Tomás Barreiro <52393857+BarreiroT@users.noreply.github.com>
2025-09-23 08:23:22 -07:00

172 lines
6.1 KiB
TypeScript

import { expect } from "chai"
import { afterEach, beforeEach, describe, it } from "mocha"
import * as sinon from "sinon"
import { WebviewProvider } from "@/core/webview"
import { ErrorService } from "../error"
import { Logger } from "../logging/Logger"
import { SharedUriHandler } from "./SharedUriHandler"
describe("SharedUriHandler", () => {
let sandbox: sinon.SinonSandbox
let handleOpenRouterCallbackStub: sinon.SinonStub
let handleAuthCallbackStub: sinon.SinonStub
beforeEach(async () => {
sandbox = sinon.createSandbox()
// Mock Logger methods to avoid HostProvider dependency
sandbox.stub(Logger, "info").returns()
sandbox.stub(Logger, "error").returns()
// Mock ErrorService to avoid telemetry dependency
const mockErrorService = {
logMessage: sandbox.stub(),
logException: sandbox.stub(),
toClineError: sandbox.stub(),
isEnabled: sandbox.stub().returns(false),
getSettings: sandbox.stub().returns({ enabled: false, hostEnabled: false }),
getProvider: sandbox.stub(),
dispose: sandbox.stub().resolves(),
}
sandbox.stub(ErrorService, "initialize").resolves(mockErrorService as any)
sandbox.stub(ErrorService, "get").returns(mockErrorService as any)
await ErrorService.initialize()
handleOpenRouterCallbackStub = sandbox.stub().resolves()
handleAuthCallbackStub = sandbox.stub().resolves()
const mockWebviewProvider = {
controller: {
handleOpenRouterCallback: handleOpenRouterCallbackStub,
handleAuthCallback: handleAuthCallbackStub,
},
} as any
sandbox.stub(WebviewProvider, "getVisibleInstance").returns(mockWebviewProvider)
})
afterEach(() => {
sandbox.restore()
})
describe("handleUri", () => {
describe("OpenRouter callback handling", () => {
it("should successfully handle OpenRouter callback with code", async () => {
const result = await SharedUriHandler.handleUri("vscode://cline.cline/openrouter?code=test123")
expect(result).to.be.true
sinon.assert.calledOnceWithExactly(handleOpenRouterCallbackStub, "test123")
})
it("should return false when OpenRouter code is missing", async () => {
const result = await SharedUriHandler.handleUri("vscode://cline.cline/openrouter")
expect(result).to.be.false
expect(handleOpenRouterCallbackStub.called).to.be.false
})
it("should handle URL with plus signs in code parameter", async () => {
const result = await SharedUriHandler.handleUri("vscode://cline.cline/openrouter?code=test+123+abc")
expect(result).to.be.true
// Plus signs in query params are preserved
sinon.assert.calledOnceWithExactly(handleOpenRouterCallbackStub, "test+123+abc")
})
})
describe("Auth callback handling", () => {
it("should successfully handle auth callback with idToken", async () => {
const result = await SharedUriHandler.handleUri("vscode://cline.cline/auth?idToken=jwt123&provider=google")
expect(result).to.be.true
sinon.assert.calledOnceWithExactly(handleAuthCallbackStub, "jwt123", "google")
})
it("should successfully handle auth callback without provider", async () => {
const result = await SharedUriHandler.handleUri("vscode://cline.cline/auth?idToken=jwt123")
expect(result).to.be.true
sinon.assert.calledOnceWithExactly(handleAuthCallbackStub, "jwt123", null)
})
it("should return false when idToken is missing", async () => {
const result = await SharedUriHandler.handleUri("vscode://cline.cline/auth?provider=google")
expect(result).to.be.false
expect(handleAuthCallbackStub.called).to.be.false
})
})
describe("Unknown path handling", () => {
it("should return false for unknown paths", async () => {
const result = await SharedUriHandler.handleUri("vscode://cline.cline/unknown?param=value")
expect(result).to.be.false
expect(handleAuthCallbackStub.called).to.be.false
expect(handleOpenRouterCallbackStub.called).to.be.false
})
})
describe("Error handling", () => {
it("should catch and log errors from controller methods", async () => {
handleOpenRouterCallbackStub.rejects(new Error("Controller error"))
const result = await SharedUriHandler.handleUri("vscode://cline.cline/openrouter?code=test123")
expect(result).to.be.false
})
it("should handle malformed URIs gracefully", async () => {
const result = await SharedUriHandler.handleUri("invalid://uri")
expect(result).to.be.false
expect(handleAuthCallbackStub.called).to.be.false
expect(handleOpenRouterCallbackStub.called).to.be.false
})
})
describe("Query parameter parsing", () => {
it("should correctly parse multiple query parameters", async () => {
const result = await SharedUriHandler.handleUri(
"vscode://cline.cline/auth?idToken=jwt123&provider=github&extra=param",
)
expect(result).to.be.true
sinon.assert.calledOnceWithExactly(handleAuthCallbackStub, "jwt123", "github")
})
it("should handle URL-encoded parameters", async () => {
const result = await SharedUriHandler.handleUri(
"vscode://cline.cline/auth?idToken=jwt%20with%20spaces&provider=google",
)
expect(result).to.be.true
// URLSearchParams should decode %20 to spaces
sinon.assert.calledOnceWithExactly(handleAuthCallbackStub, "jwt with spaces", "google")
})
it("should handle empty query string", async () => {
const result = await SharedUriHandler.handleUri("vscode://cline.cline/openrouter")
expect(result).to.be.false
expect(handleAuthCallbackStub.called).to.be.false
expect(handleOpenRouterCallbackStub.called).to.be.false
})
})
describe("Different URI schemes", () => {
it("should handle HTTP scheme URIs", async () => {
const result = await SharedUriHandler.handleUri("http://localhost:3000/openrouter?code=test123")
expect(result).to.be.true
sinon.assert.calledOnceWithExactly(handleOpenRouterCallbackStub, "test123")
})
it("should handle HTTPS scheme URIs", async () => {
const result = await SharedUriHandler.handleUri("https://example.com/auth?idToken=jwt123&provider=github")
expect(result).to.be.true
sinon.assert.calledOnceWithExactly(handleAuthCallbackStub, "jwt123", "github")
})
})
})
})