package server import ( "encoding/json" "io" "net/http" "net/http/httptest" "path/filepath" "strings" "testing" "time" "git.site.latha.org/experiments/gitea-k3s-deployer/internal/config" "git.site.latha.org/experiments/gitea-k3s-deployer/internal/deployer" "git.site.latha.org/experiments/gitea-k3s-deployer/internal/store" ) func newTestServer(t *testing.T) *Server { t.Helper() cfg := config.Config{ GiteaURL: "https://git.site.latha.org", } st, err := store.New(filepath.Join(t.TempDir(), "state.json")) if err != nil { t.Fatalf("store.New: %v", err) } fg := &fakeGitea{} fa := &fakeApplier{} svc := deployer.NewService(cfg, st, fg, fa) return New(cfg, st, svc, fa) } func TestOAuthRelayFlow(t *testing.T) { srv := newTestServer(t) ts := httptest.NewServer(srv.Handler()) defer ts.Close() // 1. Missing state on poll -> 400 resp, err := http.Get(ts.URL + "/api/oauth/poll") if err != nil { t.Fatalf("poll missing state: %v", err) } resp.Body.Close() if resp.StatusCode != http.StatusBadRequest { t.Fatalf("expected 400 for missing state, got %d", resp.StatusCode) } // 2. Poll before callback -> 202 pending resp, err = http.Get(ts.URL + "/api/oauth/poll?state=state-123") if err != nil { t.Fatalf("poll pending: %v", err) } var pollOut map[string]string _ = json.NewDecoder(resp.Body).Decode(&pollOut) resp.Body.Close() if resp.StatusCode != http.StatusAccepted || pollOut["status"] != "pending" { t.Fatalf("expected 202 pending, got %d %v", resp.StatusCode, pollOut) } // 3. Callback missing params -> 400 resp, err = http.Get(ts.URL + "/oauth/callback?state=state-123") if err != nil { t.Fatalf("callback missing code: %v", err) } resp.Body.Close() if resp.StatusCode != http.StatusBadRequest { t.Fatalf("expected 400 for missing code, got %d", resp.StatusCode) } // 4. Callback with error -> 400 resp, err = http.Get(ts.URL + "/oauth/callback?error=access_denied&state=state-123") if err != nil { t.Fatalf("callback error: %v", err) } resp.Body.Close() if resp.StatusCode != http.StatusBadRequest { t.Fatalf("expected 400 for oauth error, got %d", resp.StatusCode) } // 5. Valid callback -> 200 HTML resp, err = http.Get(ts.URL + "/oauth/callback?state=state-123&code=auth-code-xyz") if err != nil { t.Fatalf("callback valid: %v", err) } bodyBytes, _ := io.ReadAll(resp.Body) resp.Body.Close() if resp.StatusCode != http.StatusOK { t.Fatalf("expected 200 for callback, got %d", resp.StatusCode) } if !strings.Contains(string(bodyBytes), "Authorized! You can close this tab.") { t.Fatalf("unexpected callback HTML: %s", string(bodyBytes)) } // 6. Poll after callback -> 200 ready with code resp, err = http.Get(ts.URL + "/api/oauth/poll?state=state-123") if err != nil { t.Fatalf("poll ready: %v", err) } pollOut = map[string]string{} _ = json.NewDecoder(resp.Body).Decode(&pollOut) resp.Body.Close() if resp.StatusCode != http.StatusOK || pollOut["status"] != "ready" || pollOut["code"] != "auth-code-xyz" { t.Fatalf("expected 200 ready with code, got %d %v", resp.StatusCode, pollOut) } // 7. Single-use consumption: second poll -> 202 pending resp, err = http.Get(ts.URL + "/api/oauth/poll?state=state-123") if err != nil { t.Fatalf("poll second time: %v", err) } pollOut = map[string]string{} _ = json.NewDecoder(resp.Body).Decode(&pollOut) resp.Body.Close() if resp.StatusCode != http.StatusAccepted || pollOut["status"] != "pending" { t.Fatalf("expected 202 pending after consumption, got %d %v", resp.StatusCode, pollOut) } } func TestOAuthRelayTTLExpiration(t *testing.T) { now := time.Date(2026, 11, 17, 21, 0, 0, 0, time.UTC) st := newOAuthRelayStore() st.now = func() time.Time { return now } st.Set("s1", "c1") now = now.Add(6 * time.Minute) if code, ok := st.Take("s1"); ok || code != "" { t.Fatalf("expected expired entry to be rejected, got (%q, %v)", code, ok) } } func TestAtprotoClientMetadata(t *testing.T) { srv := newTestServer(t) ts := httptest.NewServer(srv.Handler()) defer ts.Close() resp, err := http.Get(ts.URL + "/oauth/atproto-client-metadata.json") if err != nil { t.Fatalf("get client metadata: %v", err) } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { t.Fatalf("expected 200, got %d", resp.StatusCode) } if ct := resp.Header.Get("Content-Type"); !strings.HasPrefix(ct, "application/json") { t.Fatalf("expected application/json content type, got %q", ct) } body, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("read body: %v", err) } var out struct { ClientID string `json:"client_id"` RedirectURIs []string `json:"redirect_uris"` Scope string `json:"scope"` DPoPBoundAccessTokens bool `json:"dpop_bound_access_tokens"` } if err := json.Unmarshal(body, &out); err != nil { t.Fatalf("unmarshal client metadata: %v", err) } // client_id must exactly match the public URL this is served from. if out.ClientID != "https://deploy.site.latha.org/oauth/atproto-client-metadata.json" { t.Fatalf("client_id = %q", out.ClientID) } if len(out.RedirectURIs) != 1 || out.RedirectURIs[0] != "https://deploy.site.latha.org/oauth/callback" { t.Fatalf("redirect_uris = %v", out.RedirectURIs) } if !out.DPoPBoundAccessTokens { t.Fatal("dpop_bound_access_tokens must be true") } }