oficina / sheets Public

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

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package sheets
4
5import (
6 "context"
7 "fmt"
8 "net/http"
9 "testing"
10 "time"
11
12 "amadan.net/rastrillo/rastrillo/harness"
13 "github.com/chromedp/chromedp"
14)
15
16// TestDocumentStylesSurviveTheProductionCSP: Sheets in Chromium behind
17// the production serving stack (productionHandler: rastrillo's security
18// headers and Sheets' CSP), standalone. A filled cell and an inked cell
19// in the scriptless table view paint differently from a plain one, the
20// table scrolls sideways in its wrapper, and the grid's Format palette
21// swatches carry their colours.
22//
23// Every one of those is a style attribute. Under rastrillo fb806a2's
24// default style-src the browser drops them all — the cell turns
25// transparent on black, the swatch loses --sheet-h, the wrapper stops
26// scrolling — while the other browser checks, which serve app.Router()
27// with no CSP at all, stay green (Astra on #46).
28func TestDocumentStylesSurviveTheProductionCSP(t *testing.T) {
29 app, cookies := signIn(t)
30 s, cells := newSheet(t, app, cookies, "Painted")
31 postCell(t, app, cookies, cells, `{"r":0,"c":0,"raw":"filled"}`)
32 postCell(t, app, cookies, cells, `{"r":0,"c":1,"raw":"inked"}`)
33 postCell(t, app, cookies, cells, `{"r":0,"c":2,"raw":"plain"}`)
34 postFormatForm(t, app, cookies, s, map[string]string{"r1": "0", "c1": "0", "r2": "0", "c2": "0", "fill": "red", "page": "1"})
35 postFormatForm(t, app, cookies, s, map[string]string{"r1": "0", "c1": "1", "r2": "0", "c2": "1", "ink": "blue", "page": "1"})
36
37 rig := harness.New(t, func(string) http.Handler { return productionHandler(t, app) })
38 run := func(what string, actions ...chromedp.Action[chromedp.Void]) {
39 t.Helper()
40 ctx, cancel := context.WithTimeout(rig.Context(), 20*time.Second)
41 defer cancel()
42 if err := chromedp.Do(ctx, actions...); err != nil {
43 t.Fatalf("%s: %v", what, err)
44 }
45 }
46 var set []chromedp.Action[chromedp.Void]
47 for _, c := range cookies {
48 set = append(set, setCookie(rig.Origin, c))
49 }
50 run("sign in", set...)
51
52 var table struct {
53 FilledBg, PlainBg, InkedInk, PlainInk, Overflow string
54 }
55 run("table view",
56 chromedp.Navigate(fmt.Sprintf("%s/sheets/%d/table", rig.Origin, s.ID)),
57 chromedp.WaitVisible(chromedp.CSS(`table[data-cell-table]`)),
58 evalInto(&table, `(() => {
59 const cell = (x) => [...document.querySelectorAll('table[data-cell-table] tbody td')].find(td => td.textContent.trim() === x);
60 const cs = (x) => getComputedStyle(cell(x));
61 return {
62 FilledBg: cs('filled').backgroundColor,
63 PlainBg: cs('plain').backgroundColor,
64 InkedInk: cs('inked').color,
65 PlainInk: cs('plain').color,
66 Overflow: getComputedStyle(document.querySelector('table[data-cell-table]').parentElement).overflowX,
67 };
68 })()`),
69 )
70 if table.FilledBg == table.PlainBg {
71 t.Errorf("a red-filled cell paints %s, the same as a plain one: its style attribute was refused", table.FilledBg)
72 }
73 if table.InkedInk == table.PlainInk {
74 t.Errorf("a blue-inked cell paints %s text, the same as a plain one: its style attribute was refused", table.InkedInk)
75 }
76 if table.Overflow != "auto" {
77 t.Errorf("the table's wrapper has overflow-x %q, want auto: wide sheets cannot scroll", table.Overflow)
78 }
79
80 var swatch string
81 run("grid palette",
82 chromedp.Navigate(fmt.Sprintf("%s/sheets/%d", rig.Origin, s.ID)),
83 chromedp.WaitVisible(chromedp.CSS(`td[data-x="0"][data-y="0"]`)),
84 evalInto(&swatch, `getComputedStyle(document.querySelector('[data-swatch][data-value="red"]')).getPropertyValue('--sheet-h').trim()`),
85 )
86 if swatch == "" {
87 t.Error("the Fill palette's red swatch has no --sheet-h: its style attribute was refused")
88 }
89}
90