oficina / estilo Public

Clone
git clone https://amadan.net/oficina/estilo

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package googleimport
4
5import (
6 "fmt"
7 "html/template"
8 "io"
9 "net/http"
10 "net/http/httptest"
11 "os"
12 "os/exec"
13 "strings"
14 "testing"
15
16 "github.com/carlosframework/rastrillo/ui"
17)
18
19// TestChooserBrowser exercises the real module and multipart submission under
20// production-like headers. Google responses are controlled; this is not proof
21// of live Google consent, which needs the deployment's public configuration.
22func TestChooserBrowser(t *testing.T) {
23 tmpl := template.Must(template.New("root").Parse(Template()))
24 mux := http.NewServeMux()
25 mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
26 w.Header().Set("Content-Security-Policy", baseline)
27 if r.URL.Path != "/" {
28 http.NotFound(w, r)
29 return
30 }
31 if r.URL.Query().Get("disabled") != "1" {
32 if err := Headers(w, baseline); err != nil {
33 t.Error(err)
34 http.Error(w, "policy", 500)
35 return
36 }
37 }
38 w.Header().Set("Content-Type", "text/html; charset=utf-8")
39 fmt.Fprint(w, `<!doctype html><html><head><meta name="viewport" content="width=device-width"><link rel="stylesheet" href="/tokens.css"></head><body><form id="upload" method="post" action="/upload" enctype="multipart/form-data"><input name="creation_token" value="keep-this-token" type="hidden"><label for="file">File</label><input id="file" type="file" name="file" required><button type="submit">Import</button></form>`)
40 cfg := validConfig
41 if r.URL.Query().Get("disabled") == "1" {
42 cfg = Config{}
43 }
44 if err := tmpl.ExecuteTemplate(w, "estilo-google-import", View{Config: cfg, Kind: Docs, FormID: "upload", InputID: "file", Labels: Text("en")}); err != nil {
45 t.Error(err)
46 }
47 if cfg.Enabled() {
48 fmt.Fprintf(w, `<script type="module" src="/%s"></script>`, JSName())
49 }
50 fmt.Fprint(w, `</body></html>`)
51 })
52 mux.HandleFunc("/"+JSName(), func(w http.ResponseWriter, r *http.Request) {
53 w.Header().Set("Content-Type", "text/javascript")
54 w.Write(JS())
55 })
56 mux.HandleFunc("/tokens.css", func(w http.ResponseWriter, r *http.Request) {
57 w.Header().Set("Content-Type", "text/css")
58 w.Write(ui.TokensCSS())
59 })
60 mux.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) {
61 b, err := io.ReadAll(io.LimitReader(r.Body, 1<<20))
62 if err != nil {
63 t.Error(err)
64 }
65 if strings.Contains(string(b), "test-google-bearer") || strings.Contains(r.URL.String(), "test-google-bearer") {
66 t.Error("Google token reached app")
67 }
68 if !strings.Contains(string(b), "keep-this-token") || !strings.Contains(string(b), `filename="Example.docx"`) || !strings.Contains(string(b), "fixture-export-bytes") {
69 t.Errorf("wrong multipart upload: %q", b)
70 }
71 fmt.Fprint(w, "Imported")
72 })
73 server := httptest.NewServer(mux)
74 defer server.Close()
75 cmd := exec.Command("node", "google-import.browser.mjs", server.URL)
76 cmd.Env = os.Environ()
77 if out, err := cmd.CombinedOutput(); err != nil {
78 t.Fatalf("browser: %v\n%s", err, out)
79 }
80}
81