| 1 | //go:build browser |
| 2 | |
| 3 | package docs_test |
| 4 | |
| 5 | import ( |
| 6 | "archive/zip" |
| 7 | "bytes" |
| 8 | "context" |
| 9 | "crypto/sha256" |
| 10 | "encoding/base64" |
| 11 | "encoding/json" |
| 12 | "fmt" |
| 13 | "io" |
| 14 | "log/slog" |
| 15 | "net/http" |
| 16 | "net/http/httptest" |
| 17 | "os" |
| 18 | "os/exec" |
| 19 | "path/filepath" |
| 20 | "strings" |
| 21 | "testing" |
| 22 | "time" |
| 23 | |
| 24 | "amadan.net/oficina/estilo.git/googleimport" |
| 25 | "amadan.net/rastrillo/rastrillo" |
| 26 | |
| 27 | "github.com/carlosframework/docs/internal/docs" |
| 28 | ) |
| 29 | |
| 30 | // Controlled Google responses exercise the actual app routes, middleware, |
| 31 | // converter, report and editor. The OOXML below is synthesized, not a real |
| 32 | // Google export; this does not certify real account consent or export. |
| 33 | func TestGoogleImportBrowserCreatesAnEditableDocument(t *testing.T) { |
| 34 | server := httptest.NewUnstartedServer(nil) |
| 35 | origin := "http://" + server.Listener.Addr().String() |
| 36 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 37 | key := bytes.Repeat([]byte{113}, 32) // synthetic fixture key, never deployment configuration |
| 38 | encoded := base64.StdEncoding.EncodeToString(key) |
| 39 | dbPath := filepath.Join(t.TempDir(), "docs.db") |
| 40 | if err := os.WriteFile(dbPath, nil, 0600); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | if err := docs.MaintainContent(context.Background(), docs.ContentMaintenanceConfig{ |
| 44 | DBPath: dbPath, ContentKey: encoded, ExpectedFingerprint: fmt.Sprintf("%x", sha256.Sum256(key)), Logger: logger, |
| 45 | }); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | app, err := docs.NewForTestWith(t, docs.Config{DBPath: dbPath, ContentKey: encoded, Origin: origin, Logger: logger, GoogleImport: googleimport.Config{ |
| 49 | ClientID: "123456789-client.apps.googleusercontent.com", APIKey: "AIzaPublicBrowserKey123456789", AppID: "123456789", |
| 50 | }}) |
| 51 | if err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | served, done, err := rastrillo.Handler(docs.ServeOptions(rastrillo.Options{}, app)) |
| 55 | if err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | if done != nil { |
| 59 | t.Cleanup(func() { _ = done() }) |
| 60 | } |
| 61 | server.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 62 | if r.Method == http.MethodPost && r.URL.Path == "/import" { |
| 63 | body, err := io.ReadAll(io.LimitReader(r.Body, 11<<20)) |
| 64 | if err != nil { |
| 65 | t.Error(err) |
| 66 | } |
| 67 | _ = r.Body.Close() |
| 68 | r.Body = io.NopCloser(bytes.NewReader(body)) |
| 69 | if bytes.Contains(body, []byte("controlled-google-bearer")) || strings.Contains(r.Header.Get("Authorization"), "controlled-google-bearer") || strings.Contains(r.URL.String(), "controlled-google-bearer") { |
| 70 | t.Error("Google token reached Docs") |
| 71 | } |
| 72 | } |
| 73 | served.ServeHTTP(w, r) |
| 74 | }) |
| 75 | server.Start() |
| 76 | t.Cleanup(server.Close) |
| 77 | config, err := json.Marshal(map[string]any{ |
| 78 | "origin": origin, "cookies": signInCookies(t, app, "import-browser@example.com"), |
| 79 | "fixture": googleBrowserDOCX(t), |
| 80 | }) |
| 81 | if err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) |
| 85 | defer cancel() |
| 86 | cmd := exec.CommandContext(ctx, "node", "google-import.mjs") |
| 87 | cmd.Dir = "../../fixtures/engines" |
| 88 | cmd.Env = append(os.Environ(), "DOCS_GOOGLE_IMPORT_TEST="+string(config)) |
| 89 | if out, err := cmd.CombinedOutput(); err != nil { |
| 90 | t.Fatalf("Google import browser flow: %v\n%s", err, out) |
| 91 | } |
| 92 | for _, suffix := range []string{"", "-wal"} { |
| 93 | data, err := os.ReadFile(dbPath + suffix) |
| 94 | if suffix == "-wal" && os.IsNotExist(err) { |
| 95 | continue |
| 96 | } |
| 97 | if err != nil { |
| 98 | t.Fatal(err) |
| 99 | } |
| 100 | for _, content := range []string{"linked words", "Table cell", "Edited after import.", "table_flattened"} { |
| 101 | if bytes.Contains(data, []byte(content)) { |
| 102 | t.Errorf("imported content or report persisted unsealed in database%s", suffix) |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func googleBrowserDOCX(t *testing.T) []byte { |
| 109 | t.Helper() |
| 110 | var buf bytes.Buffer |
| 111 | z := zip.NewWriter(&buf) |
| 112 | parts := map[string]string{ |
| 113 | "[Content_Types].xml": `<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/></Types>`, |
| 114 | "_rels/.rels": `<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/></Relationships>`, |
| 115 | "word/document.xml": `<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><w:body><w:p><w:r><w:t>Import fixture</w:t></w:r></w:p><w:p><w:hyperlink r:id="rId9"><w:r><w:t>linked words</w:t></w:r></w:hyperlink></w:p><w:p><w:r><w:rPr><w:b/></w:rPr><w:t>bold text</w:t></w:r><w:r><w:rPr><w:b w:val="0"/></w:rPr><w:t xml:space="preserve"> plain text</w:t></w:r></w:p><w:tbl><w:tr><w:tc><w:p><w:r><w:t>Table cell</w:t></w:r></w:p></w:tc></w:tr></w:tbl></w:body></w:document>`, |
| 116 | "word/_rels/document.xml.rels": `<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId9" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="https://fixture.invalid/never-fetch" TargetMode="External"/></Relationships>`, |
| 117 | } |
| 118 | for name, body := range parts { |
| 119 | w, err := z.Create(name) |
| 120 | if err != nil { |
| 121 | t.Fatal(err) |
| 122 | } |
| 123 | if _, err := io.WriteString(w, body); err != nil { |
| 124 | t.Fatal(err) |
| 125 | } |
| 126 | } |
| 127 | if err := z.Close(); err != nil { |
| 128 | t.Fatal(err) |
| 129 | } |
| 130 | return buf.Bytes() |
| 131 | } |
| 132 | |