oficina / fichas Public

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

Plain git — no account needed to clone.

Download

Download this file

1package fichas
2
3import (
4 "testing"
5
6 "amadan.net/oficina/fichas/internal/fichas/seal"
7)
8
9// "Very forgiving whether I type the protocol or not."
10func TestNormaliseURLForgives(t *testing.T) {
11 for in, want := range map[string]string{
12 "brightwater.example": "https://brightwater.example",
13 " Brightwater.Example ": "https://brightwater.example",
14 "www.brightwater.example/": "https://www.brightwater.example",
15 "https://brightwater.example": "https://brightwater.example",
16 "HTTPS://BRIGHTWATER.EXAMPLE/": "https://brightwater.example",
17 "http://brightwater.example": "http://brightwater.example",
18 "brightwater.example/pricing": "https://brightwater.example/pricing",
19 "brightwater.example:8443/x?y=1": "https://brightwater.example:8443/x?y=1",
20 "": "",
21 } {
22 got, err := normaliseURL(in)
23 if err != nil || got != want {
24 t.Errorf("normaliseURL(%q) = %q, %v; want %q", in, got, err, want)
25 }
26 }
27}
28
29// The stored value becomes a link, so anything a browser would execute or
30// that is not a web address is refused.
31func TestNormaliseURLRefuses(t *testing.T) {
32 for _, in := range []string{
33 "javascript:alert(1)", "mailto:ana@example.com", "data:text/html,x",
34 "ftp://files.example", "not a domain", "localhost", "brightwater",
35 "https://user:pass@brightwater.example", ".example", "example.",
36 "https://bright<water>.example",
37 } {
38 if got, err := normaliseURL(in); err == nil {
39 t.Errorf("normaliseURL(%q) accepted it as %q", in, got)
40 }
41 }
42}
43
44func TestDisplayAndHref(t *testing.T) {
45 if got := displayURL("https://brightwater.example/pricing/"); got != "brightwater.example/pricing" {
46 t.Errorf("displayURL = %q", got)
47 }
48 if got := safeHref("javascript:alert(1)"); got != "" {
49 t.Errorf("safeHref let a script URL through: %q", got)
50 }
51 if got := safeHref("https://brightwater.example"); got != "https://brightwater.example" {
52 t.Errorf("safeHref = %q", got)
53 }
54}
55
56// Findable addresses dedupe by host: the same company typed three ways is
57// one print.
58func TestURLPrintsMatchAcrossSpellings(t *testing.T) {
59 s := testSealer(t)
60 fd := FieldDef{Key: FieldDomain, Kind: KindURL, Tier: TierPrint}
61 a, _ := printFor(s, fd, "brightwater.example")
62 b, _ := printFor(s, fd, "https://www.Brightwater.example/")
63 c, _ := printFor(s, fd, "http://brightwater.example")
64 if a != b || b != c {
65 t.Fatal("one company's address prints differently depending on how it was typed")
66 }
67 d, _ := printFor(s, fd, "brightwater.example/other-page")
68 if a == d {
69 t.Fatal("two different pages print the same")
70 }
71}
72
73// The shipped Domain field was Text. Seed upgrades it on boot, carrying the
74// values across and logging each change — and refuses any undeclared kind
75// change as before.
76func TestSeedUpgradesDomainToAWebAddress(t *testing.T) {
77 g, s := seeded(t)
78 card, err := CreateCard(ctx(), g, s, TypeCustomer, Person(1), map[string]string{FieldName: "Brightwater Labs"})
79 if err != nil {
80 t.Fatal(err)
81 }
82 // Recreate the old world: Domain as Text, with a value typed the old way.
83 var fd FieldDef
84 g.Where("key = ?", FieldDomain).First(&fd)
85 g.Model(&FieldDef{}).Where("id = ?", fd.ID).Update("kind", KindText)
86 old := fd
87 old.Kind = KindText
88 sealed, _ := s.Seal(seal.PurposeValue, []byte("Brightwater.example/"))
89 pr, _ := printFor(s, old, "Brightwater.example/")
90 g.Create(&CardValue{CardID: card.ID, FieldDefID: fd.ID, SealedValue: sealed, Print: pr})
91
92 if err := Seed(g, s); err != nil {
93 t.Fatalf("Seed refused the declared upgrade: %v", err)
94 }
95 g.Where("key = ?", FieldDomain).First(&fd)
96 if fd.Kind != KindURL {
97 t.Fatalf("Domain is still %s", fd.Kind)
98 }
99 var v CardValue
100 g.Where("card_id = ? AND field_def_id = ?", card.ID, fd.ID).First(&v)
101 raw, _ := s.Open(seal.PurposeValue, v.SealedValue)
102 if string(raw) != "https://brightwater.example" {
103 t.Fatalf("the upgraded value is %q", raw)
104 }
105 want, _ := printFor(s, fd, "brightwater.example")
106 if v.Print != want {
107 t.Fatal("the value's print was not recomputed under the new kind")
108 }
109 ev := events(t, g, ObjectCard, card.ID)
110 last := ev[len(ev)-1]
111 if last.Kind != EventUpdated || last.ActorID != nil || last.ActorReason != "field kind upgrade" {
112 t.Fatalf("the carried-across value was not logged as the app's own change: %+v", last)
113 }
114 // And it is idempotent: a second boot changes nothing.
115 n := len(ev)
116 if err := Seed(g, s); err != nil {
117 t.Fatalf("second Seed: %v", err)
118 }
119 if len(events(t, g, ObjectCard, card.ID)) != n {
120 t.Fatal("a second boot upgraded again")
121 }
122}
123