| 1 | package sheets |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/estilo.git/googleimport" |
| 11 | ) |
| 12 | |
| 13 | var testGoogle = googleimport.Config{ |
| 14 | ClientID: "123456789-client.apps.googleusercontent.com", |
| 15 | APIKey: "AIzaPublicBrowserKey123456789", |
| 16 | AppID: "123456789", |
| 17 | } |
| 18 | |
| 19 | func TestLoadGoogleImportIsAllOrNothing(t *testing.T) { |
| 20 | set := func(id, key, app string) { |
| 21 | t.Setenv("SHEETS_GOOGLE_CLIENT_ID", id) |
| 22 | t.Setenv("SHEETS_GOOGLE_API_KEY", key) |
| 23 | t.Setenv("SHEETS_GOOGLE_APP_ID", app) |
| 24 | } |
| 25 | set("", "", "") |
| 26 | if cfg, err := LoadGoogleImport(); err != nil || cfg.Enabled() { |
| 27 | t.Errorf("none set = %+v, %v; want off", cfg, err) |
| 28 | } |
| 29 | set(testGoogle.ClientID, testGoogle.APIKey, testGoogle.AppID) |
| 30 | if cfg, err := LoadGoogleImport(); err != nil || cfg != testGoogle { |
| 31 | t.Errorf("all set = %+v, %v; want on", cfg, err) |
| 32 | } |
| 33 | set(testGoogle.ClientID, "", testGoogle.AppID) |
| 34 | if _, err := LoadGoogleImport(); err == nil { |
| 35 | t.Error("two of three set: want a refusal") |
| 36 | } |
| 37 | set(testGoogle.ClientID, testGoogle.APIKey, "not-a-project-number") |
| 38 | if _, err := LoadGoogleImport(); err == nil { |
| 39 | t.Error("malformed project number: want a refusal") |
| 40 | } else if strings.Contains(err.Error(), "not-a-project-number") || strings.Contains(err.Error(), testGoogle.APIKey) { |
| 41 | t.Errorf("refusal carries a value: %v", err) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // googleApp is a signed-in app with Google import configured. |
| 46 | func googleApp(t *testing.T) (*App, []*http.Cookie) { |
| 47 | t.Helper() |
| 48 | app, cookies := signIn(t) |
| 49 | app.google = testGoogle |
| 50 | return app, cookies |
| 51 | } |
| 52 | |
| 53 | func TestImportPageWithGoogleOffIsUnchanged(t *testing.T) { |
| 54 | app, cookies := signIn(t) |
| 55 | rec := httptest.NewRecorder() |
| 56 | productionHandler(t, app).ServeHTTP(rec, as(httptest.NewRequest(http.MethodGet, "/sheets/import", nil), cookies)) |
| 57 | if rec.Code != http.StatusOK { |
| 58 | t.Fatalf("import page = %d", rec.Code) |
| 59 | } |
| 60 | if got := rec.Header().Get("Content-Security-Policy"); got != ContentSecurityPolicy { |
| 61 | t.Errorf("policy = %q, want the app's own %q", got, ContentSecurityPolicy) |
| 62 | } |
| 63 | body := rec.Body.String() |
| 64 | for _, absent := range []string{"estilo-google-import", "google.com", googleScriptPath} { |
| 65 | if strings.Contains(body, absent) { |
| 66 | t.Errorf("page with Google off carries %q", absent) |
| 67 | } |
| 68 | } |
| 69 | if !strings.Contains(body, `id="import-form"`) || !strings.Contains(body, `id="file"`) { |
| 70 | t.Error("the upload form is missing") |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestImportPageWithGoogleOnWidensOnlyItsOwnPolicy(t *testing.T) { |
| 75 | app, cookies := googleApp(t) |
| 76 | want, err := googleimport.Policy(ContentSecurityPolicy) |
| 77 | if err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | h := productionHandler(t, app) |
| 81 | for name, req := range map[string]*http.Request{ |
| 82 | "page": httptest.NewRequest(http.MethodGet, "/sheets/import", nil), |
| 83 | "refused re-render": uploadRequest(t, "notes.txt", []byte("not a workbook")), |
| 84 | } { |
| 85 | rec := httptest.NewRecorder() |
| 86 | h.ServeHTTP(rec, as(req, cookies)) |
| 87 | if got := rec.Header().Get("Content-Security-Policy"); got != want { |
| 88 | t.Errorf("%s: policy = %q, want %q", name, got, want) |
| 89 | } |
| 90 | if n := len(rec.Header().Values("Content-Security-Policy")); n != 1 { |
| 91 | t.Errorf("%s: %d policies, want exactly one", name, n) |
| 92 | } |
| 93 | if got := rec.Header().Get("Cross-Origin-Opener-Policy"); got != "" { |
| 94 | t.Errorf("%s: COOP = %q, want none", name, got) |
| 95 | } |
| 96 | if got := rec.Header().Get("Cache-Control"); !strings.Contains(got, "no-store") { |
| 97 | t.Errorf("%s: Cache-Control = %q, want no-store", name, got) |
| 98 | } |
| 99 | body := rec.Body.String() |
| 100 | for _, present := range []string{ |
| 101 | `estilo-google-import`, `data-google-form="import-form"`, `data-google-input="file"`, |
| 102 | `data-google-kind="sheets"`, `<script type="module" src="` + googleScriptPath + `">`, |
| 103 | `id="import-form"`, `<noscript>`, |
| 104 | } { |
| 105 | if !strings.Contains(body, present) { |
| 106 | t.Errorf("%s: page lacks %q", name, present) |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // Every other page keeps the app's own policy. |
| 112 | rec := httptest.NewRecorder() |
| 113 | h.ServeHTTP(rec, as(httptest.NewRequest(http.MethodGet, "/sheets", nil), cookies)) |
| 114 | if got := rec.Header().Get("Content-Security-Policy"); got != ContentSecurityPolicy { |
| 115 | t.Errorf("sheet list policy = %q, want the app's own", got) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | func TestGoogleScriptIsServedAsImmutableJavaScript(t *testing.T) { |
| 120 | app, _ := signIn(t) |
| 121 | rec := httptest.NewRecorder() |
| 122 | productionHandler(t, app).ServeHTTP(rec, httptest.NewRequest(http.MethodGet, googleScriptPath, nil)) |
| 123 | if rec.Code != http.StatusOK { |
| 124 | t.Fatalf("script = %d", rec.Code) |
| 125 | } |
| 126 | if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/javascript") { |
| 127 | t.Errorf("Content-Type = %q", ct) |
| 128 | } |
| 129 | body, _ := io.ReadAll(rec.Body) |
| 130 | if string(body) != string(googleimport.JS()) { |
| 131 | t.Error("script body is not estilo's module") |
| 132 | } |
| 133 | } |
| 134 | |