trails/apps/journal/app/routes/api.well-known.trails-cool.test.ts
Ullrich Schäfer 1ae406a8aa
Implement OAuth2 PKCE auth, discovery, and mobile API client
Journal server (Phase 1.4 + 1.5):
- Add oauth_clients, oauth_codes, oauth_tokens tables to journal schema
- Implement GET /oauth/authorize with PKCE flow and login redirect
- Implement POST /oauth/token (authorization_code + refresh_token grants)
- Add validateBearerToken() + getAuthenticatedUser() middleware
- Seed trails-cool-mobile as trusted OAuth client on server startup
- Add GET /.well-known/trails-cool discovery endpoint
- Add returnTo support to login page and magic link verify
- Add @trails-cool/api workspace dependency to journal

Mobile app (Phase 1.5 + 1.6):
- Login screen with server URL input and discovery validation
- OAuth2 PKCE login via expo-web-browser with expo-crypto for Hermes
- Token storage in expo-secure-store with auto-refresh on 401
- API client with bearer token injection and typed errors
- Server URL persistence with localhost default in dev mode
- API version compatibility check on app foreground
- Log out + switch server on Profile tab
- iOS ATS exception for local networking

Tests:
- PKCE crypto verification, OAuthError, token generation
- Discovery endpoint response shape
- API version semver compatibility
- API client error types

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 00:41:40 +02:00

33 lines
1.1 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
vi.stubEnv("DOMAIN", "trails.cool");
vi.stubEnv("ORIGIN", "https://trails.cool");
describe("GET /.well-known/trails-cool", () => {
beforeEach(() => {
vi.resetModules();
});
it("returns discovery response with API version", async () => {
const { loader } = await import("./api.well-known.trails-cool.ts");
const resp = loader() as Response;
const data = await resp.json();
expect(data.apiVersion).toMatch(/^\d+\.\d+\.\d+$/);
expect(data.instanceName).toBe("trails.cool");
expect(data.apiBaseUrl).toBe("https://trails.cool/api/v1");
});
it("uses localhost defaults when env vars are unset", async () => {
vi.stubEnv("DOMAIN", "");
vi.stubEnv("ORIGIN", "");
// Re-import to pick up changed env
const mod = await import("./api.well-known.trails-cool.ts");
const resp = mod.loader() as Response;
const data = await resp.json();
// Falls back to process.env values (empty strings trigger ?? fallback)
expect(data).toHaveProperty("apiVersion");
expect(data).toHaveProperty("apiBaseUrl");
});
});