| 1 | package docs |
| 2 | |
| 3 | import ( |
| 4 | "archive/zip" |
| 5 | "bytes" |
| 6 | "context" |
| 7 | "encoding/base64" |
| 8 | "errors" |
| 9 | "io" |
| 10 | "log/slog" |
| 11 | "mime/multipart" |
| 12 | "net/http" |
| 13 | "net/http/httptest" |
| 14 | "os" |
| 15 | "path/filepath" |
| 16 | "strings" |
| 17 | "testing" |
| 18 | |
| 19 | "amadan.net/oficina/estilo.git/googleimport" |
| 20 | "amadan.net/rastrillo/rastrillo" |
| 21 | ) |
| 22 | |
| 23 | // The fixtures here are SYNTHESIZED .docx files, not real Google exports. |
| 24 | |
| 25 | // importMarker is text that must never appear at rest: in the database, |
| 26 | // its WAL, or the log. |
| 27 | const importMarker = "Zanzibar-quokka" |
| 28 | |
| 29 | // importName is a filename that must never reach the log. |
| 30 | const importName = "Secret board minutes.docx" |
| 31 | |
| 32 | const wordNS = `xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"` |
| 33 | |
| 34 | func wordDoc(t *testing.T, body string) []byte { |
| 35 | t.Helper() |
| 36 | var buf bytes.Buffer |
| 37 | z := zip.NewWriter(&buf) |
| 38 | w, err := z.Create("word/document.xml") |
| 39 | if err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | io.WriteString(w, `<w:document `+wordNS+`><w:body>`+body+`</w:body></w:document>`) |
| 43 | if err := z.Close(); err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | return buf.Bytes() |
| 47 | } |
| 48 | |
| 49 | // nestedDoc has text in a link and a table, a bold-off run, and a |
| 50 | // suggestion, so a report has lines and the text has places to hide. |
| 51 | func nestedDoc(t *testing.T) []byte { |
| 52 | return wordDoc(t, `<w:p><w:pPr><w:pStyle w:val="Heading1"/></w:pPr><w:r><w:t>Minutes</w:t></w:r></w:p>`+ |
| 53 | `<w:p><w:hyperlink r:id="rId1"><w:r><w:t>`+importMarker+` link</w:t></w:r></w:hyperlink></w:p>`+ |
| 54 | `<w:tbl><w:tr><w:tc><w:p><w:r><w:t>cell text</w:t></w:r></w:p></w:tc></w:tr></w:tbl>`+ |
| 55 | `<w:p><w:r><w:rPr><w:b w:val="0"/></w:rPr><w:t>not bold</w:t></w:r></w:p>`) |
| 56 | } |
| 57 | |
| 58 | // sealedApp is an app on a database whose content sealing is activated, |
| 59 | // as activatedContentApp makes one, with the caller's Config. |
| 60 | func sealedApp(t *testing.T, cfg Config) (*App, string) { |
| 61 | t.Helper() |
| 62 | key := base64.StdEncoding.EncodeToString(bytes.Repeat([]byte{73}, 32)) |
| 63 | cfg.DBPath = filepath.Join(t.TempDir(), "docs.db") |
| 64 | cfg.Env = "development" |
| 65 | cfg.ContentKey = key |
| 66 | app, err := New(cfg) |
| 67 | if err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | if err := app.Close(); err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | cipher, err := newContentCipher(key, "production") |
| 74 | if err != nil { |
| 75 | t.Fatal(err) |
| 76 | } |
| 77 | if err := MaintainContent(context.Background(), ContentMaintenanceConfig{DBPath: cfg.DBPath, ContentKey: key, ExpectedFingerprint: cipher.fingerprint}); err != nil { |
| 78 | t.Fatal(err) |
| 79 | } |
| 80 | app, err = New(cfg) |
| 81 | if err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | t.Cleanup(func() { app.Close() }) |
| 85 | return app, cfg.DBPath |
| 86 | } |
| 87 | |
| 88 | // signedIn caches one session per app and address: signing in afresh |
| 89 | // for every request runs into the sign-in rate limit. |
| 90 | var signedIn = map[*App]map[string][]*http.Cookie{} |
| 91 | |
| 92 | // asSignedIn is app.TestRequest without a fresh sign-in each time. |
| 93 | func asSignedIn(t *testing.T, app *App, who, method, path string, body io.Reader) *http.Request { |
| 94 | t.Helper() |
| 95 | if signedIn[app] == nil { |
| 96 | signedIn[app] = map[string][]*http.Cookie{} |
| 97 | t.Cleanup(func() { delete(signedIn, app) }) |
| 98 | } |
| 99 | cookies, ok := signedIn[app][who] |
| 100 | if !ok { |
| 101 | cookies = app.TestRequest(t, who, method, path, nil).Cookies() |
| 102 | signedIn[app][who] = cookies |
| 103 | } |
| 104 | req := httptest.NewRequest(method, path, body) |
| 105 | for _, c := range cookies { |
| 106 | req.AddCookie(c) |
| 107 | } |
| 108 | if method != http.MethodGet { |
| 109 | req.Header.Set("Sec-Fetch-Site", "same-origin") |
| 110 | } |
| 111 | return req |
| 112 | } |
| 113 | |
| 114 | func importRequest(t *testing.T, app *App, who, token, name string, file []byte) *http.Request { |
| 115 | t.Helper() |
| 116 | var body bytes.Buffer |
| 117 | mw := multipart.NewWriter(&body) |
| 118 | if token != "" { |
| 119 | mw.WriteField("creation_token", token) |
| 120 | } |
| 121 | if file != nil { |
| 122 | w, err := mw.CreateFormFile("file", name) |
| 123 | if err != nil { |
| 124 | t.Fatal(err) |
| 125 | } |
| 126 | w.Write(file) |
| 127 | } |
| 128 | mw.Close() |
| 129 | req := asSignedIn(t, app, who, http.MethodPost, "/import", &body) |
| 130 | req.Header.Set("Content-Type", mw.FormDataContentType()) |
| 131 | return req |
| 132 | } |
| 133 | |
| 134 | func countRows(t *testing.T, app *App, table string) int64 { |
| 135 | t.Helper() |
| 136 | var n int64 |
| 137 | if err := app.store.db.Table(table).Count(&n).Error; err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | return n |
| 141 | } |
| 142 | |
| 143 | func TestImportCreatesASealedEditableDocumentWithItsReport(t *testing.T) { |
| 144 | var logs bytes.Buffer |
| 145 | app, dbPath := sealedApp(t, Config{Logger: slog.New(slog.NewTextHandler(&logs, nil))}) |
| 146 | const who = "owner@example.com" |
| 147 | const token = "1b0f8a52-6f0e-4a7e-9a0c-2f7f8d7f0a11" |
| 148 | |
| 149 | rec := serve(app, importRequest(t, app, who, token, importName, nestedDoc(t))) |
| 150 | if rec.Code != http.StatusSeeOther { |
| 151 | t.Fatalf("POST /import: %d %s", rec.Code, rec.Body.String()) |
| 152 | } |
| 153 | loc := rec.Header().Get("Location") |
| 154 | sqid, ok := strings.CutSuffix(strings.TrimPrefix(loc, "/d/"), "?imported=1") |
| 155 | if !ok || sqid == "" { |
| 156 | t.Fatalf("Location %q", loc) |
| 157 | } |
| 158 | |
| 159 | // The same submission again, after a lost answer, is the same document. |
| 160 | again := serve(app, importRequest(t, app, who, token, importName, nestedDoc(t))) |
| 161 | if again.Code != http.StatusSeeOther || again.Header().Get("Location") != loc { |
| 162 | t.Fatalf("a retried import: %d %q, want the first document", again.Code, again.Header().Get("Location")) |
| 163 | } |
| 164 | if n := countRows(t, app, "documents"); n != 1 { |
| 165 | t.Fatalf("%d documents after a retried import, want 1", n) |
| 166 | } |
| 167 | |
| 168 | ctx := context.Background() |
| 169 | owner := app.TestPerson(t, who) |
| 170 | doc, err := app.store.Get(ctx, owner, sqid) |
| 171 | if err != nil { |
| 172 | t.Fatal(err) |
| 173 | } |
| 174 | if doc.Title != "Secret board minutes" { |
| 175 | t.Errorf("title %q", doc.Title) |
| 176 | } |
| 177 | text, err := app.store.Text(ctx, doc) |
| 178 | if err != nil { |
| 179 | t.Fatal(err) |
| 180 | } |
| 181 | for _, want := range []string{"Minutes", importMarker + " link", "cell text", "not bold"} { |
| 182 | if !strings.Contains(text, want) { |
| 183 | t.Errorf("the document has no %q: %q", want, text) |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // Sealed at rest: the update, the report, and neither in the clear |
| 188 | // in the database or its WAL. |
| 189 | var upd DocUpdate |
| 190 | if err := app.store.db.Where("doc_id = ?", doc.ID).First(&upd).Error; err != nil { |
| 191 | t.Fatal(err) |
| 192 | } |
| 193 | var rep DocImportReport |
| 194 | if err := app.store.db.Where("doc_id = ?", doc.ID).First(&rep).Error; err != nil { |
| 195 | t.Fatal(err) |
| 196 | } |
| 197 | if upd.Seq != 1 || upd.ContentEncoding != contentEncoding || rep.ContentEncoding != contentEncoding { |
| 198 | t.Fatalf("update seq %d encoding %d, report encoding %d", upd.Seq, upd.ContentEncoding, rep.ContentEncoding) |
| 199 | } |
| 200 | for _, f := range []string{dbPath, dbPath + "-wal"} { |
| 201 | b, err := os.ReadFile(f) |
| 202 | if err != nil && !errors.Is(err, os.ErrNotExist) { |
| 203 | t.Fatal(err) |
| 204 | } |
| 205 | for _, s := range []string{importMarker, "table_flattened", "link_text_kept"} { |
| 206 | if bytes.Contains(b, []byte(s)) { |
| 207 | t.Errorf("%s holds %q in the clear", filepath.Base(f), s) |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | // A report's ciphertext does not open as an update of the same |
| 212 | // document: its own kind byte. |
| 213 | probe := DocUpdate{Seq: 0, UpdateBin: append([]byte(nil), rep.ReportBin...), ContentEncoding: contentEncoding} |
| 214 | if err := app.store.content.openUpdate(doc.GUID, &probe); err == nil { |
| 215 | t.Error("a sealed report opened as an update") |
| 216 | } |
| 217 | |
| 218 | // Editable: an ordinary update applies on top. |
| 219 | if rec := serve(app, asSignedIn(t, app, who, http.MethodPost, "/d/"+sqid+"/updates", bytes.NewReader(validUpdate(t)))); rec.Code != http.StatusNoContent { |
| 220 | t.Fatalf("POST update to the import: %d", rec.Code) |
| 221 | } |
| 222 | |
| 223 | // The report shows once on arrival, and reopens from the bar. |
| 224 | arrival := serve(app, asSignedIn(t, app, who, http.MethodGet, loc, nil)).Body.String() |
| 225 | for _, want := range []string{"<dialog docs-dialog docs-import-arrival open>", "<section docs-import-report", "docs.import.report.table_flattened", "/d/" + sqid + "/import-report"} { |
| 226 | if !strings.Contains(arrival, want) { |
| 227 | t.Errorf("the arrival page has no %q", want) |
| 228 | } |
| 229 | } |
| 230 | plain := serve(app, asSignedIn(t, app, who, http.MethodGet, "/d/"+sqid, nil)).Body.String() |
| 231 | if strings.Contains(plain, "<dialog docs-dialog docs-import-arrival") || !strings.Contains(plain, "/d/"+sqid+"/import-report") { |
| 232 | t.Error("the report should show only on arrival, and its link always") |
| 233 | } |
| 234 | page := serve(app, asSignedIn(t, app, who, http.MethodGet, "/d/"+sqid+"/import-report", nil)) |
| 235 | if page.Code != http.StatusOK || !strings.Contains(page.Body.String(), "docs.import.report.link_text_kept") || !strings.Contains(page.Body.String(), "docs.import.report.incomplete") { |
| 236 | t.Fatalf("the report page: %d", page.Code) |
| 237 | } |
| 238 | if rec := serve(app, asSignedIn(t, app, "stranger@example.com", http.MethodGet, "/d/"+sqid+"/import-report", nil)); rec.Code != http.StatusOK { |
| 239 | t.Errorf("an admitted member reading a team import report: %d, want 200", rec.Code) |
| 240 | } |
| 241 | if err := app.store.SetVisibility(ctx, owner, sqid, VisibilityPrivate); err != nil { |
| 242 | t.Fatal(err) |
| 243 | } |
| 244 | if rec := serve(app, asSignedIn(t, app, "stranger@example.com", http.MethodGet, "/d/"+sqid+"/import-report", nil)); rec.Code != http.StatusNotFound { |
| 245 | t.Errorf("a stranger reading the report: %d, want 404", rec.Code) |
| 246 | } |
| 247 | created := createDocFor(t, app, who) |
| 248 | if rec := serve(app, asSignedIn(t, app, who, http.MethodGet, "/d/"+created+"/import-report", nil)); rec.Code != http.StatusNotFound { |
| 249 | t.Errorf("the report of a document never imported: %d, want 404", rec.Code) |
| 250 | } |
| 251 | |
| 252 | if strings.Contains(logs.String(), importMarker) || strings.Contains(logs.String(), "Secret board") { |
| 253 | t.Error("the log carries the file's content or name") |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | func createDocFor(t *testing.T, app *App, who string) string { |
| 258 | t.Helper() |
| 259 | req := asSignedIn(t, app, who, http.MethodPost, "/documents", strings.NewReader("title=Typed")) |
| 260 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 261 | rec := serve(app, req) |
| 262 | if rec.Code != http.StatusSeeOther { |
| 263 | t.Fatalf("POST /documents: %d", rec.Code) |
| 264 | } |
| 265 | return strings.TrimPrefix(rec.Header().Get("Location"), "/d/") |
| 266 | } |
| 267 | |
| 268 | // Every refusal creates nothing, says why in words, and logs no name or |
| 269 | // content. |
| 270 | func TestImportRefusalsCreateNothing(t *testing.T) { |
| 271 | var logs bytes.Buffer |
| 272 | app, _ := sealedApp(t, Config{Logger: slog.New(slog.NewTextHandler(&logs, nil))}) |
| 273 | const who = "owner@example.com" |
| 274 | damaged := nestedDoc(t) |
| 275 | damaged = append(damaged[:len(damaged)/2:len(damaged)/2], bytes.Repeat([]byte{0}, 64)...) |
| 276 | for _, c := range []struct { |
| 277 | name string |
| 278 | file []byte |
| 279 | fname string |
| 280 | status int |
| 281 | says string |
| 282 | }{ |
| 283 | {"no file", nil, "", http.StatusBadRequest, "docs.import.error.nofile"}, |
| 284 | {"a PDF", []byte("%PDF-1.7 " + importMarker), "minutes.pdf", http.StatusUnsupportedMediaType, "docs.import.error.notword"}, |
| 285 | {"a PDF named .docx", []byte("%PDF-1.7 " + importMarker), importName, http.StatusUnsupportedMediaType, "docs.import.error.notword"}, |
| 286 | {"a damaged archive", damaged, importName, http.StatusUnprocessableEntity, "docs.import.error.notword"}, |
| 287 | {"broken XML", wordDoc(t, `<w:p><w:r><w:t>`+importMarker), importName, http.StatusUnprocessableEntity, "docs.import.error.damaged"}, |
| 288 | {"no text", wordDoc(t, `<w:p/><w:p><w:r><w:drawing/></w:r></w:p>`), importName, http.StatusUnprocessableEntity, "docs.import.error.empty"}, |
| 289 | {"too large", append([]byte("PK\x03\x04"), bytes.Repeat([]byte("x"), googleimport.MaxExportBytes)...), importName, http.StatusRequestEntityTooLarge, "docs.import.error.toolarge"}, |
| 290 | } { |
| 291 | rec := serve(app, importRequest(t, app, who, "", c.fname, c.file)) |
| 292 | body := rec.Body.String() |
| 293 | if rec.Code != c.status || !strings.Contains(body, "docs-import-error") || !strings.Contains(body, c.says) { |
| 294 | t.Errorf("%s: %d, want %d saying %q", c.name, rec.Code, c.status, c.says) |
| 295 | } |
| 296 | if strings.Contains(body, importMarker) { |
| 297 | t.Errorf("%s: the page quotes the file", c.name) |
| 298 | } |
| 299 | } |
| 300 | for _, table := range []string{"documents", "doc_updates", "doc_import_reports"} { |
| 301 | if n := countRows(t, app, table); n != 0 { |
| 302 | t.Errorf("%d rows in %s after refusals", n, table) |
| 303 | } |
| 304 | } |
| 305 | if strings.Contains(logs.String(), importMarker) || strings.Contains(logs.String(), "Secret board") { |
| 306 | t.Error("the log carries the file's content or name") |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | // New content on this instance is sealed or it is not written: until |
| 311 | // sealing is activated, the page says so and an import is refused. |
| 312 | // Existing legacy documents are untouched by this. |
| 313 | func TestImportIsRefusedUntilContentSealingIsActivated(t *testing.T) { |
| 314 | app, err := NewForTest(t) |
| 315 | if err != nil { |
| 316 | t.Fatal(err) |
| 317 | } |
| 318 | const who = "owner@example.com" |
| 319 | get := serve(app, asSignedIn(t, app, who, http.MethodGet, "/import", nil)) |
| 320 | if get.Code != http.StatusServiceUnavailable || !strings.Contains(get.Body.String(), "docs.import.error.unsealed") { |
| 321 | t.Errorf("GET /import unsealed: %d", get.Code) |
| 322 | } |
| 323 | post := serve(app, importRequest(t, app, who, "", importName, nestedDoc(t))) |
| 324 | if post.Code != http.StatusServiceUnavailable || !strings.Contains(post.Body.String(), "docs.import.error.unsealed") { |
| 325 | t.Errorf("POST /import unsealed: %d", post.Code) |
| 326 | } |
| 327 | if _, err := app.store.CreateImported(context.Background(), app.TestPerson(t, who), "x", "", &imported{}); !errors.Is(err, errImportUnsealed) { |
| 328 | t.Errorf("CreateImported unsealed: %v", err) |
| 329 | } |
| 330 | for _, table := range []string{"documents", "doc_updates", "doc_import_reports"} { |
| 331 | if n := countRows(t, app, table); n != 0 { |
| 332 | t.Errorf("%d rows in %s", n, table) |
| 333 | } |
| 334 | } |
| 335 | // The ordinary legacy path still writes. |
| 336 | createDocFor(t, app, who) |
| 337 | } |
| 338 | |
| 339 | // A guest, admitted by a grant to one document, does not import any more |
| 340 | // than they create. |
| 341 | func TestAGuestCannotImport(t *testing.T) { |
| 342 | app, _ := sealedApp(t, Config{AllowedAddresses: "alice@example.com"}) |
| 343 | sqid := createDocFor(t, app, "alice@example.com") |
| 344 | share := asSignedIn(t, app, "alice@example.com", http.MethodPost, "/d/"+sqid+"/share", strings.NewReader("address=bob@example.com&role=writer")) |
| 345 | share.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 346 | if rec := serve(app, share); rec.Code != http.StatusSeeOther { |
| 347 | t.Fatalf("share: %d", rec.Code) |
| 348 | } |
| 349 | if rec := serve(app, asSignedIn(t, app, "bob@example.com", http.MethodGet, "/import", nil)); rec.Code != http.StatusForbidden { |
| 350 | t.Errorf("guest GET /import: %d, want 403", rec.Code) |
| 351 | } |
| 352 | if rec := serve(app, importRequest(t, app, "bob@example.com", "", importName, nestedDoc(t))); rec.Code != http.StatusForbidden { |
| 353 | t.Errorf("guest POST /import: %d, want 403", rec.Code) |
| 354 | } |
| 355 | if n := countRows(t, app, "documents"); n != 1 { |
| 356 | t.Errorf("%d documents, want alice's one", n) |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | // A cross-site POST is refused by the same origin check as every other |
| 361 | // form, before anything is read. |
| 362 | func TestImportRefusesACrossSitePost(t *testing.T) { |
| 363 | app, _ := sealedApp(t, Config{}) |
| 364 | req := importRequest(t, app, "owner@example.com", "", importName, nestedDoc(t)) |
| 365 | req.Header.Set("Sec-Fetch-Site", "cross-site") |
| 366 | if rec := serve(app, req); rec.Code != http.StatusForbidden { |
| 367 | t.Errorf("cross-site import: %d, want 403", rec.Code) |
| 368 | } |
| 369 | if n := countRows(t, app, "documents"); n != 0 { |
| 370 | t.Errorf("%d documents", n) |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | var testGoogle = googleimport.Config{ |
| 375 | ClientID: "1234567890-abcdefghijklmnop.apps.googleusercontent.com", |
| 376 | APIKey: "AIzaSyTESTONLYabcdefghijklmnopqrstuvw", |
| 377 | AppID: "1234567890", |
| 378 | } |
| 379 | |
| 380 | // Served through rastrillo's production middleware, as the binary serves |
| 381 | // it: the import page alone gains Google's origins, keeps every |
| 382 | // directive that matters, sets no COOP, and with Google off is the app's |
| 383 | // own policy with no Google anywhere. |
| 384 | func TestImportPagePolicy(t *testing.T) { |
| 385 | for _, on := range []bool{false, true} { |
| 386 | cfg := Config{} |
| 387 | if on { |
| 388 | cfg.GoogleImport = testGoogle |
| 389 | } |
| 390 | app, _ := sealedApp(t, cfg) |
| 391 | served, done, err := rastrillo.Handler(ServeOptions(rastrillo.Options{}, app)) |
| 392 | if err != nil { |
| 393 | t.Fatal(err) |
| 394 | } |
| 395 | if done != nil { |
| 396 | defer done() |
| 397 | } |
| 398 | page := func(req *http.Request) *httptest.ResponseRecorder { |
| 399 | req.Header.Set("Accept", "text/html") |
| 400 | rec := httptest.NewRecorder() |
| 401 | served.ServeHTTP(rec, req) |
| 402 | return rec |
| 403 | } |
| 404 | for _, req := range []*http.Request{ |
| 405 | asSignedIn(t, app, "owner@example.com", http.MethodGet, "/import", nil), |
| 406 | importRequest(t, app, "owner@example.com", "", "notes.pdf", []byte("%PDF")), |
| 407 | } { |
| 408 | rec := page(req) |
| 409 | policies := rec.Header().Values("Content-Security-Policy") |
| 410 | body := rec.Body.String() |
| 411 | if len(policies) != 1 { |
| 412 | t.Fatalf("google %v %s: %d policies", on, req.Method, len(policies)) |
| 413 | } |
| 414 | if rec.Header().Get("Cross-Origin-Opener-Policy") != "" { |
| 415 | t.Errorf("google %v %s: a COOP header", on, req.Method) |
| 416 | } |
| 417 | if rec.Header().Get("Cache-Control") != "no-store" { |
| 418 | t.Errorf("google %v %s: Cache-Control %q", on, req.Method, rec.Header().Get("Cache-Control")) |
| 419 | } |
| 420 | policy := policies[0] |
| 421 | for _, d := range []string{"form-action 'self'", "frame-ancestors 'none'", "base-uri 'self'"} { |
| 422 | if !strings.Contains(policy, d) { |
| 423 | t.Errorf("google %v %s: the policy lost %q", on, req.Method, d) |
| 424 | } |
| 425 | } |
| 426 | if on { |
| 427 | want, _ := googleimport.Policy(ContentSecurityPolicy) |
| 428 | if policy != want || !strings.Contains(body, "estilo-google-import") || !strings.Contains(body, "/static/estilo/google-import.") { |
| 429 | t.Errorf("google on %s: policy %q, chooser rendered %v", req.Method, policy, strings.Contains(body, "estilo-google-import")) |
| 430 | } |
| 431 | } else if policy != ContentSecurityPolicy || strings.Contains(body, "estilo-google-import") || strings.Contains(body, "google") { |
| 432 | t.Errorf("google off %s: policy %q, or Google on the page", req.Method, policy) |
| 433 | } |
| 434 | } |
| 435 | // Elsewhere the policy is the app's own either way. |
| 436 | list := page(asSignedIn(t, app, "owner@example.com", http.MethodGet, "/", nil)) |
| 437 | if got := list.Header().Get("Content-Security-Policy"); got != ContentSecurityPolicy { |
| 438 | t.Errorf("google %v: the list's policy is %q", on, got) |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | func TestPartialGoogleConfigurationRefusesToStart(t *testing.T) { |
| 444 | for _, cfg := range []googleimport.Config{ |
| 445 | {ClientID: testGoogle.ClientID}, |
| 446 | {ClientID: testGoogle.ClientID, APIKey: testGoogle.APIKey}, |
| 447 | {ClientID: "not a client id", APIKey: testGoogle.APIKey, AppID: testGoogle.AppID}, |
| 448 | {ClientID: testGoogle.ClientID, APIKey: testGoogle.APIKey, AppID: "my-project"}, |
| 449 | } { |
| 450 | if _, err := NewForTestWith(t, Config{GoogleImport: cfg}); err == nil { |
| 451 | t.Errorf("%+v: started", cfg) |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | func TestImportTitleFromAFilename(t *testing.T) { |
| 457 | for in, want := range map[string]string{ |
| 458 | "Board minutes.docx": "Board minutes", |
| 459 | `C:\Users\x\Q3 plan.DOCX`: "Q3 plan", |
| 460 | "../../etc/passwd.docx": "passwd", |
| 461 | " spaced .docx": "spaced", |
| 462 | "tab\there.docx": "tabhere", |
| 463 | ".docx": "", |
| 464 | strings.Repeat("é", 600) + ".docx": strings.Repeat("é", 512), |
| 465 | } { |
| 466 | if got := importTitle(in); got != want { |
| 467 | t.Errorf("importTitle(%q) = %q, want %q", in, got, want) |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |