feat: add /oauth/callback and /api/oauth/poll relay endpoints
Build and Push Image / build (push) Failing after 17s
Build and Push Image / build (push) Failing after 17s
This commit is contained in:
1 parent
10650eee47
commit
ce459f69b1
3 files changed
+253
-5
No files matched your search
@@ -0,0 +1,130 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user