oficina / estilo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package googleimport
2
3import (
4 "bytes"
5 "html/template"
6 "net/http/httptest"
7 "os/exec"
8 "reflect"
9 "strings"
10 "testing"
11)
12
13var validConfig = Config{"123456789-client.apps.googleusercontent.com", "AIzaPublicBrowserKey123456789", "123456789"}
14
15const baseline = "default-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; frame-ancestors 'none'; base-uri 'self'; form-action 'self'"
16
17func TestConfiguration(t *testing.T) {
18 if (Config{}).Validate() != nil || (Config{}).Enabled() {
19 t.Fatal("unset must be disabled")
20 }
21 if validConfig.Validate() != nil || !validConfig.Enabled() {
22 t.Fatal("valid public config")
23 }
24 for _, c := range []Config{{ClientID: validConfig.ClientID}, {ClientID: validConfig.ClientID, APIKey: validConfig.APIKey}, {"123.foo", validConfig.APIKey, "123"}, {validConfig.ClientID, "key\nheader", "123"}, {validConfig.ClientID, validConfig.APIKey, "project-name"}} {
25 if c.Validate() == nil {
26 t.Fatal("accepted malformed or partial config")
27 }
28 }
29}
30
31func TestPolicyOnlyExpandsGoogleSources(t *testing.T) {
32 policy, err := Policy(baseline)
33 if err != nil {
34 t.Fatal(err)
35 }
36 if !strings.HasPrefix(policy, baseline+";") {
37 t.Fatalf("changed existing policy: %s", policy)
38 }
39 for _, want := range []string{"script-src 'self' https://accounts.google.com https://apis.google.com", "frame-src 'self' https://accounts.google.com https://docs.google.com", "connect-src 'self' https://accounts.google.com https://apis.google.com https://www.googleapis.com"} {
40 if !strings.Contains(policy, want) {
41 t.Fatal("missing", want)
42 }
43 }
44 if strings.Contains(policy, "unsafe-eval") || strings.Contains(policy, "*") {
45 t.Fatal("unbounded policy")
46 }
47 strict, err := Policy("default-src 'none'; script-src-elem 'none'")
48 if err != nil || !strings.Contains(strict, "script-src 'self' https://accounts.google.com") || !strings.Contains(strict, "script-src-elem 'self' https://accounts.google.com") {
49 t.Fatal("chooser's own module must be allowed", strict, err)
50 }
51 p, err := Policy(baseline + "; child-src 'none'; script-src-elem 'self'")
52 if err != nil || !strings.Contains(p, "frame-src https://accounts.google.com https://docs.google.com") || !strings.Contains(p, "script-src-elem 'self' https://accounts.google.com") {
53 t.Fatal(p, err)
54 }
55 for _, bad := range []string{"", "script-src 'self'", baseline + ", default-src *", baseline + "; DEFAULT-SRC *", baseline + "\r\n"} {
56 if _, err := Policy(bad); err == nil {
57 t.Fatal("accepted ambiguous policy")
58 }
59 }
60 w := httptest.NewRecorder()
61 w.Header().Set("Content-Security-Policy", baseline)
62 if err := Headers(w, baseline); err != nil {
63 t.Fatal(err)
64 }
65 if got := w.Header().Values("Content-Security-Policy"); len(got) != 1 || got[0] != policy {
66 t.Fatal(got)
67 }
68 if w.Header().Get("Cross-Origin-Opener-Policy") != "" || w.Header().Get("Cache-Control") != "no-store" {
69 t.Fatal(w.Header())
70 }
71 w.Header().Add("Content-Security-Policy", "default-src 'none'")
72 if Headers(w, baseline) == nil {
73 t.Fatal("multiple policies were replaced")
74 }
75}
76
77func TestTemplateAndTwelveLocales(t *testing.T) {
78 tmpl, err := template.New("root").ParseFS(Templates(), "templates/*.html")
79 if err != nil {
80 t.Fatal(err)
81 }
82 if len(messages) != 12 {
83 t.Fatal("all twelve locales required")
84 }
85 for locale, labels := range messages {
86 for i := range reflect.ValueOf(labels).NumField() {
87 if reflect.ValueOf(labels).Field(i).String() == "" {
88 t.Fatal(locale, "missing label", i)
89 }
90 }
91 var buf bytes.Buffer
92 v := View{Config: validConfig, Kind: Docs, FormID: `bad" onclick="evil()`, InputID: "file", Labels: labels}
93 if err := tmpl.ExecuteTemplate(&buf, "estilo-google-import", v); err != nil {
94 t.Fatal(err)
95 }
96 html := buf.String()
97 if strings.Contains(html, ` onclick="evil()`) || strings.Contains(html, "<script") || !strings.Contains(html, "data-google-choose-wrap hidden") || !strings.Contains(html, "<noscript>") {
98 t.Fatal(locale, html)
99 }
100 v.Config = Config{}
101 buf.Reset()
102 if err := tmpl.ExecuteTemplate(&buf, "estilo-google-import", v); err != nil || strings.TrimSpace(buf.String()) != "" {
103 t.Fatal("disabled rendered", err)
104 }
105 }
106 if Text("zz") != Text("en") || Text("pt-BR") != Text("pt") {
107 t.Fatal("locale fallback")
108 }
109 a := JS()
110 a[0] = '!'
111 if bytes.Equal(a, JS()) || !strings.HasSuffix(JSName(), ".mjs") {
112 t.Fatal("asset mutability/name")
113 }
114}
115
116func TestBrowserModuleLogic(t *testing.T) {
117 out, err := exec.Command("node", "--test", "google-import.test.mjs").CombinedOutput()
118 if err != nil {
119 t.Fatalf("node: %v\n%s", err, out)
120 }
121}
122
123// The chooser brings its own spacing (google-import.css): before, it
124// had none, and Docs and Sheets each spaced it in their own CSS.
125// chrome/rhythm_browser_test.go measures it in real engines.
126func TestTheChooserHasItsSpacing(t *testing.T) {
127 css := string(CSS())
128 for _, rule := range []string{
129 "[estilo-google-import] { display: flex; flex-direction: column; align-items: flex-start; }",
130 "[estilo-google-import] > * + [data-google-choose-wrap] { margin-block-start: var(--rst-sp-4); }",
131 "[estilo-google-import] > [data-google-status]:empty { margin-block-start: 0; }",
132 } {
133 if !strings.Contains(css, rule) {
134 t.Errorf("google-import.css lacks %q", rule)
135 }
136 }
137}
138