123 lines
4.4 KiB
Go
123 lines
4.4 KiB
Go
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os/exec"
|
|
"testing"
|
|
"time"
|
|
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func TestRemoteCLIHTTPIntegration(t *testing.T) {
|
|
if _, err := exec.LookPath("python3"); err != nil {
|
|
t.Skip("python3 not installed")
|
|
}
|
|
s, _, domain, _ := editorFixture(t)
|
|
object, err := s.store.CreatePageAtPathInDomain(domain, "/ja-only")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.store.SaveRevisionForLanguage(object, "<h1>日本語のページ</h1><p>{{children}}</p>", "", "ja"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s.mux.HandleFunc("/_/auth/", s.handleAuth)
|
|
s.mux.HandleFunc("/v/raw/", s.handleRawView)
|
|
s.mux.HandleFunc("/_/api/v1/path-picker", s.handlePathPickerAPI)
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
r.Host = "localhost"
|
|
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
|
}))
|
|
defer server.Close()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
cmd := exec.CommandContext(ctx, "python3", "pages-cli/test_pages.py", "--integration")
|
|
input, _ := json.Marshal(map[string]string{"origin": server.URL})
|
|
cmd.Stdin = bytes.NewReader(input)
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("remote CLI: %v\n%s", err, out)
|
|
}
|
|
}
|
|
|
|
func TestRemoteCLILanguageForkRequiresWritePermission(t *testing.T) {
|
|
s, token, domain, object := editorFixture(t)
|
|
principal := s.mustCLIPrincipal(t, domain, token)
|
|
if err := s.store.SetPrincipalState(domain, principal, "readonly"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Changing account state revokes old sessions. Use a newly authenticated
|
|
// readonly session so this exercises write authorization, not expiration.
|
|
token, err := s.store.CreateAuthSession(domain, principal)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
body, err := json.Marshal(map[string]any{
|
|
"object_uuid": object, "body": "<h1>New language</h1>", "lang": "ja",
|
|
"content_type": "text/html", "base_revision_uuid": "", "editor_principal_uuid": principal,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
r := httptest.NewRequest("POST", "http://localhost/_/api/v1/revisions/save", bytes.NewReader(body))
|
|
r.Header.Set("Authorization", "Bearer "+token)
|
|
r.Header.Set("Content-Type", "application/json")
|
|
w := httptest.NewRecorder()
|
|
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
|
if w.Code != http.StatusForbidden {
|
|
t.Fatalf("readonly language fork: %d %s", w.Code, w.Body.String())
|
|
}
|
|
revision, err := s.store.GetRevisionMetadataForExactLanguage(object, "ja")
|
|
if err != nil || revision.UUID != "" {
|
|
t.Fatalf("denied language fork persisted: %s %v", revision.UUID, err)
|
|
}
|
|
}
|
|
|
|
func TestRemoteCLIOIDCIntegration(t *testing.T) {
|
|
if _, err := exec.LookPath("python3"); err != nil {
|
|
t.Skip("python3 not installed")
|
|
}
|
|
s, api, domain, _ := editorFixture(t)
|
|
if err := s.store.SetAuthProviderConfig(domain, store.AuthProviderConfig{Kind: "oidc", Enabled: true, Issuer: "https://id.example", ClientID: "client"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s.mux.HandleFunc("/_/auth/", s.handleAuth)
|
|
s.mux.HandleFunc("/v/raw/", s.handleRawView)
|
|
s.mux.HandleFunc("/_/api/v1/path-picker", s.handlePathPickerAPI)
|
|
principal := s.mustCLIPrincipal(t, domain, api)
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
r.Host = "localhost"
|
|
// Test-only bridge in this httptest server. The real signed OIDC callback,
|
|
// browser binding and CSRF flow are covered by TestCLIAuthOIDCFlow.
|
|
if r.URL.Path == "/__test/cli-approve" {
|
|
_ = r.ParseForm()
|
|
browser, err := s.store.CreateBrowserAuthSession(domain, principal, 0)
|
|
if err == nil {
|
|
err = s.store.BindCLIAuth(r.Context(), domain, "http://localhost", r.Form.Get("request"), browser)
|
|
}
|
|
if err == nil {
|
|
err = s.store.DecideCLIAuth(r.Context(), domain, "http://localhost", r.Form.Get("request"), browser, r.Form.Get("code"), "", true)
|
|
}
|
|
if err != nil {
|
|
http.Error(w, "synthetic approval failed", 500)
|
|
return
|
|
}
|
|
writeEventJSON(w, 200, map[string]bool{"approved": true})
|
|
return
|
|
}
|
|
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
|
}))
|
|
defer server.Close()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
cmd := exec.CommandContext(ctx, "python3", "pages-cli/test_pages.py", "--oidc-integration")
|
|
input, _ := json.Marshal(map[string]string{"origin": server.URL})
|
|
cmd.Stdin = bytes.NewReader(input)
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
t.Fatalf("CLI OIDC integration: %v %s", err, out)
|
|
}
|
|
}
|