WeKnora deployment source with the reviewed wiki identity, summary and recovery fixes
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s

This commit is contained in:
yawan
2026-09-22 16:19:06 +08:00
commit f7fc992f57
3929 changed files with 1006607 additions and 0 deletions
+123
View File
@@ -0,0 +1,123 @@
package handler
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/Tencent/WeKnora/internal/config"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"github.com/gin-gonic/gin"
)
// stubOIDCStartUserService only implements GetOIDCAuthorizationURL; every
// other UserService call panics via the nil interface embedding. Keeps the
// test focused on the OIDCStart handler's branching logic.
type stubOIDCStartUserService struct {
interfaces.UserService
getOIDCAuthorizationURL func(ctx context.Context, redirectURI string) (*types.OIDCAuthURLResponse, error)
}
func (s *stubOIDCStartUserService) GetOIDCAuthorizationURL(ctx context.Context, redirectURI string) (*types.OIDCAuthURLResponse, error) {
return s.getOIDCAuthorizationURL(ctx, redirectURI)
}
func newOIDCStartTestRouter(h *AuthHandler) *gin.Engine {
gin.SetMode(gin.TestMode)
r := gin.New()
r.Use(errorCapture())
r.GET("/auth/oidc/start", h.OIDCStart)
return r
}
// TestOIDCStart_RedirectsToAuthProvider: on success the handler must 302
// to the IdP authorization URL and bind the nonce via cookie (CSRF/replay
// defence), exactly like /auth/oidc/url.
func TestOIDCStart_RedirectsToAuthProvider(t *testing.T) {
const authURL = "http://idp.example.com/authorize?client_id=weknora"
us := &stubOIDCStartUserService{
getOIDCAuthorizationURL: func(context.Context, string) (*types.OIDCAuthURLResponse, error) {
return &types.OIDCAuthURLResponse{
Success: true,
AuthorizationURL: authURL,
State: "state123",
Nonce: "nonce456",
}, nil
},
}
h := NewAuthHandler(&config.Config{}, us, nil, nil, nil)
r := newOIDCStartTestRouter(h)
req := httptest.NewRequest(http.MethodGet, "/auth/oidc/start", nil)
req.Host = "weknora.example.com"
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusFound {
t.Fatalf("status = %d, want 302", w.Code)
}
if loc := w.Header().Get("Location"); loc != authURL {
t.Errorf("Location = %q, want %q", loc, authURL)
}
var hasNonce bool
for _, ck := range w.Result().Cookies() {
if ck.Name == oidcNonceCookieName && ck.Value == "nonce456" {
hasNonce = true
}
}
if !hasNonce {
t.Errorf("expected %s cookie to be set with the nonce", oidcNonceCookieName)
}
}
// TestOIDCStart_BuildsCallbackURLFromRequestOrigin: redirect_uri handed to
// the IdP is derived from the request's own origin (no caller-supplied
// value needed); X-Forwarded-Proto upgrades the scheme to https.
func TestOIDCStart_BuildsCallbackURLFromRequestOrigin(t *testing.T) {
var captured string
us := &stubOIDCStartUserService{
getOIDCAuthorizationURL: func(_ context.Context, redirectURI string) (*types.OIDCAuthURLResponse, error) {
captured = redirectURI
return &types.OIDCAuthURLResponse{Success: true, AuthorizationURL: "http://idp", Nonce: "n"}, nil
},
}
h := NewAuthHandler(&config.Config{}, us, nil, nil, nil)
r := newOIDCStartTestRouter(h)
req := httptest.NewRequest(http.MethodGet, "/auth/oidc/start", nil)
req.Host = "weknora.example.com"
req.Header.Set("X-Forwarded-Proto", "https")
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
const want = "https://weknora.example.com/api/v1/auth/oidc/callback"
if captured != want {
t.Errorf("callback URL passed to IdP = %q, want %q", captured, want)
}
if w.Code != http.StatusFound {
t.Fatalf("status = %d, want 302", w.Code)
}
}
// TestOIDCStart_ServiceErrorReturnsNonRedirect: if the IdP URL cannot be
// built, never 302 — surface the error the same way /auth/oidc/url does.
func TestOIDCStart_ServiceErrorReturnsNonRedirect(t *testing.T) {
us := &stubOIDCStartUserService{
getOIDCAuthorizationURL: func(context.Context, string) (*types.OIDCAuthURLResponse, error) {
return nil, fmt.Errorf("idp unavailable")
},
}
h := NewAuthHandler(&config.Config{}, us, nil, nil, nil)
r := newOIDCStartTestRouter(h)
req := httptest.NewRequest(http.MethodGet, "/auth/oidc/start", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code == http.StatusFound {
t.Fatalf("status = 302, want non-redirect on service error")
}
}