Files
Docwell/Web-App/internal/spa/preview_test.go
T
tajniak81andClaude Opus 5.5 9d674e9699
CI / go (Media) (push) Successful in 14m33s
CI / go (Server) (push) Failing after 23m30s
CI / go (Web-App) (push) Canceled after 0s
CI / frontend (push) Failing after 9m30s
CI / desktop (ubuntu-latest) (push) Successful in 1h21m24s
CI / mobile (push) Failing after 4m16s
CI / docker (push) Successful in 1h1m25s
Docs / build (push) Successful in 14m59s
Docs / deploy (push) Skipped
CI / desktop (macos-latest) (push) Canceled after 0s
CI / desktop (windows-latest) (push) Canceled after 0s
Add invite links with approval, sharing switches, page views and unused-file cleanup
Phase G of docs/AFFINE.md (ADR-17):

- Invite links: one per workspace (member or guest, optional end date,
  optional admin approval) on a new /join page. Requests are approved
  (optionally as another role) or declined from workspace settings;
  join_request, join_approved and join_declined notifications reach the
  inbox, phones and the email digest. Workspace admins decide, else the
  organization's admins, else superadmins. Signup honours ?redirect=.
- Workspace switches: allowSharing (off makes every share link answer 404
  until it is back on) and urlPreview (chat apps may show private pages'
  titles). docwell-web writes title and Open Graph tags for known
  link-preview bots, asking docwell-server via DOCWELL_PREVIEW_API_URL;
  search engines are left out.
- Page analytics: page_views counts opens per page, person and day from
  the existing visit call and share-link reads; editors see totals, a
  daily chart and recent viewers in the Info panel. analytics.prune
  drops rows after 400 days.
- Unused files: attachments whose id appears in no page's Yjs state,
  page icon or comment (older than a day) are listed and deleted from
  workspace settings.
- Migration, schema snapshot, OpenAPI, generated clients, translations
  in five languages, phone inbox, compose files and user docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 15:59:55 +01:00

90 lines
3.0 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package spa
import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"sync/atomic"
"testing"
"testing/fstest"
)
func TestEndpoint(t *testing.T) {
cases := map[string]string{
"/p/abc123": "/api/v1/previews/pages/abc123",
"/share/tok_-9": "/api/v1/previews/share/tok_-9",
"/share/tok/p/page1": "/api/v1/previews/share/tok?page=page1",
"/p/abc/r/db/row": "",
"/settings/account": "",
"/p/../../admin": "",
"/share/tok/p/a'b": "",
"/share/tok/extra/path": "",
}
for in, want := range cases {
if got := endpoint(in); got != want {
t.Errorf("endpoint(%q) = %q, want %q", in, got, want)
}
}
}
func TestBotsGetPreviewMetaTags(t *testing.T) {
var calls atomic.Int32
api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls.Add(1)
switch r.URL.Path {
case "/api/v1/previews/pages/p1":
w.Write([]byte(`{"title":"Q4 <plan>","description":"","site":"Acme"}`))
case "/api/v1/previews/share/tok":
w.Write([]byte(`{"title":"Handbook","description":"How we \"work\"","site":"Acme"}`))
default:
http.Error(w, `{"error":"not_found"}`, http.StatusNotFound)
}
}))
defer api.Close()
u, _ := url.Parse(api.URL)
files := fstest.MapFS{"index.html": {Data: []byte("<!doctype html><html><head><title>Docwell</title></head><body></body></html>")}}
h := Handler(Options{Files: files, PreviewAPI: u})
fetch := func(path, ua string) string {
req := httptest.NewRequest("GET", path, nil)
req.Header.Set("User-Agent", ua)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
return rec.Body.String()
}
body := fetch("/p/p1", "Slackbot-LinkExpanding 1.0 (+https://api.slack.com/robots)")
if !strings.Contains(body, `<meta property="og:title" content="Q4 &lt;plan&gt;">`) ||
!strings.Contains(body, `<title>Q4 &lt;plan&gt;</title>`) || strings.Contains(body, "<title>Docwell</title>") {
t.Errorf("bot page = %s", body)
}
body = fetch("/share/tok", "Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)")
if !strings.Contains(body, `og:description" content="How we &#34;work&#34;"`) {
t.Errorf("bot share = %s", body)
}
// People's browsers get the plain shell and cause no lookup.
before := calls.Load()
body = fetch("/p/p1", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/142.0 Safari/537.36")
if strings.Contains(body, "og:title") || calls.Load() != before {
t.Errorf("browser got a preview: %s", body)
}
// Search engines too: they must not learn private titles.
if body = fetch("/p/p1", "Mozilla/5.0 (compatible; Googlebot/2.1)"); strings.Contains(body, "og:title") {
t.Errorf("search engine got a preview: %s", body)
}
// No preview (URL previews off): the plain shell.
if body = fetch("/p/other", "Slackbot 1.0"); strings.Contains(body, "og:title") {
t.Errorf("unknown page got a preview: %s", body)
}
// Answers are cached for a minute.
before = calls.Load()
fetch("/p/p1", "Slackbot 1.0")
if calls.Load() != before {
t.Error("second lookup was not cached")
}
}