//go:build browser package googleimport import ( "fmt" "html/template" "io" "net/http" "net/http/httptest" "os" "os/exec" "strings" "testing" "github.com/carlosframework/rastrillo/ui" ) // TestChooserBrowser exercises the real module and multipart submission under // production-like headers. Google responses are controlled; this is not proof // of live Google consent, which needs the deployment's public configuration. func TestChooserBrowser(t *testing.T) { tmpl := template.Must(template.New("root").Parse(Template())) mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Security-Policy", baseline) if r.URL.Path != "/" { http.NotFound(w, r) return } if r.URL.Query().Get("disabled") != "1" { if err := Headers(w, baseline); err != nil { t.Error(err) http.Error(w, "policy", 500) return } } w.Header().Set("Content-Type", "text/html; charset=utf-8") fmt.Fprint(w, `
`) cfg := validConfig if r.URL.Query().Get("disabled") == "1" { cfg = Config{} } if err := tmpl.ExecuteTemplate(w, "estilo-google-import", View{Config: cfg, Kind: Docs, FormID: "upload", InputID: "file", Labels: Text("en")}); err != nil { t.Error(err) } if cfg.Enabled() { fmt.Fprintf(w, ``, JSName()) } fmt.Fprint(w, ``) }) mux.HandleFunc("/"+JSName(), func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/javascript") w.Write(JS()) }) mux.HandleFunc("/tokens.css", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/css") w.Write(ui.TokensCSS()) }) mux.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) { b, err := io.ReadAll(io.LimitReader(r.Body, 1<<20)) if err != nil { t.Error(err) } if strings.Contains(string(b), "test-google-bearer") || strings.Contains(r.URL.String(), "test-google-bearer") { t.Error("Google token reached app") } if !strings.Contains(string(b), "keep-this-token") || !strings.Contains(string(b), `filename="Example.docx"`) || !strings.Contains(string(b), "fixture-export-bytes") { t.Errorf("wrong multipart upload: %q", b) } fmt.Fprint(w, "Imported") }) server := httptest.NewServer(mux) defer server.Close() cmd := exec.Command("node", "google-import.browser.mjs", server.URL) cmd.Env = os.Environ() if out, err := cmd.CombinedOutput(); err != nil { t.Fatalf("browser: %v\n%s", err, out) } }