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// The shipped Email and Phone fields were Text. Seed upgrades both on boot;
10// their values and their prints do not move (a print under Email or Phone
11// is the print Text gave the same value), so duplicate-finding keeps
12// matching what was stored before.
13func TestSeedUpgradesEmailAndPhone(t *testing.T) {
14 g, s := seeded(t)
15 card, err := CreateCard(ctx(), g, s, TypeCustomer, Person(1), map[string]string{FieldName: "Brightwater Labs"})
16 if err != nil {
17 t.Fatal(err)
18 }
19 // Legacy-shaped: Text took anything, internal spaces included, and
20 // the print must be exactly the one Text gave it.
21 stored := map[string]string{FieldEmail: "Ana @ Brightwater.example", FieldPhone: "+353 1 555 0142"}
22 prints := map[string]string{}
23 for key, value := range stored {
24 var fd FieldDef
25 g.Where("key = ?", key).First(&fd)
26 g.Model(&FieldDef{}).Where("id = ?", fd.ID).Update("kind", KindText)
27 old := fd
28 old.Kind = KindText
29 sealed, _ := s.Seal(seal.PurposeValue, []byte(value))
30 pr, _ := printFor(s, old, value)
31 prints[key] = pr
32 g.Create(&CardValue{CardID: card.ID, FieldDefID: fd.ID, SealedValue: sealed, Print: pr})
33 }
34 if err := Seed(g, s); err != nil {
35 t.Fatalf("Seed refused the declared upgrades: %v", err)
36 }
37 for key, want := range map[string]string{FieldEmail: KindEmail, FieldPhone: KindPhone} {
38 var fd FieldDef
39 g.Where("key = ?", key).First(&fd)
40 if fd.Kind != want {
41 t.Errorf("%s is %s, want %s", key, fd.Kind, want)
42 }
43 var v CardValue
44 g.Where("card_id = ? AND field_def_id = ?", card.ID, fd.ID).First(&v)
45 raw, _ := s.Open(seal.PurposeValue, v.SealedValue)
46 if string(raw) != stored[key] {
47 t.Errorf("%s's stored value moved: %q", key, raw)
48 }
49 if v.Print != prints[key] {
50 t.Errorf("%s's print moved, so duplicates stored before would no longer match", key)
51 }
52 }
53}
54
55// Email and Phone are forgiving but not anything-goes; the domain of an
56// address is stored in lower case, and a phone keeps its spacing tidied.
57func TestEmailAndPhoneAreCheckedAndTidied(t *testing.T) {
58 g, s := seeded(t)
59 for name, v := range map[string]map[string]string{
60 "an address with no @": {FieldEmail: "ana.example.com"},
61 "an address with space": {FieldEmail: "ana @example.com"},
62 "an address, no domain": {FieldEmail: "ana@localhost"},
63 "a phone with letters": {FieldPhone: "call me"},
64 "a phone too short": {FieldPhone: "123"},
65 } {
66 v[FieldName] = "Cnoc Teoranta"
67 fe, ok := IsFormError(func() error { _, err := CreateCard(ctx(), g, s, TypeCustomer, Person(1), v); return err }())
68 if !ok {
69 t.Errorf("%s: accepted", name)
70 continue
71 }
72 for k := range v {
73 if k != FieldName && fe[k] == "" {
74 t.Errorf("%s: no message beside %s", name, k)
75 }
76 }
77 }
78 card, err := CreateCard(ctx(), g, s, TypeCustomer, Person(1), map[string]string{
79 FieldName: "Cnoc Teoranta", FieldEmail: " Ana.Ni@Cnoc.Example ", FieldPhone: "+353 1 555 0100",
80 })
81 if err != nil {
82 t.Fatalf("forgiving values refused: %v", err)
83 }
84 view, err := LoadCard(ctx(), g, s, card.ID)
85 if err != nil {
86 t.Fatal(err)
87 }
88 if got := view.Field(FieldEmail).Value; got != "Ana.Ni@cnoc.example" {
89 t.Errorf("email stored as %q", got)
90 }
91 if got := view.Field(FieldPhone).Value; got != "+353 1 555 0100" {
92 t.Errorf("phone stored as %q", got)
93 }
94 if mailHref(view.Field(FieldEmail).Value) != "mailto:Ana.Ni@cnoc.example" || telHref(view.Field(FieldPhone).Value) != "tel:+35315550100" {
95 t.Errorf("the card does not link them")
96 }
97 if mailHref("ana@example.com?subject=hi") != "" {
98 t.Error("an email value with mailto: parameters is linked")
99 }
100}
101
102// An email value cannot carry mailto: parameters, a fragment, encoding or
103// any whitespace into the card's link.
104func TestAnEmailIsOnlyAnAddress(t *testing.T) {
105 for _, v := range []string{
106 "ana@example.com?subject=hi&body=pay+now", "ana@example.com#x", "ana&bob@example.com",
107 "ana%40x@example.com", "ana/x@example.com", "\"ana\"@example.com", "<ana@example.com>",
108 "ana\n@example.com", "ana @example.com", "ana@exa mple.com",
109 } {
110 if looksLikeAnEmail(v) {
111 t.Errorf("%q accepted as an email address", v)
112 }
113 }
114 for _, v := range []string{"ana@example.com", "ana.ni+fichas@cnoc.example", "o'brien@example.ie"} {
115 if !looksLikeAnEmail(v) {
116 t.Errorf("%q refused", v)
117 }
118 }
119}
120
121// Email and Phone were Text, and a Docs contract could carry them. After
122// the upgrade they still can, as text, which is all Docs knows.
123func TestEmailAndPhoneStillGoToAContract(t *testing.T) {
124 for kind, want := range map[string]string{KindText: KindText, KindEmail: KindText, KindPhone: KindText, KindDate: KindDate} {
125 if got, ok := releaseKind(kind); !ok || got != want {
126 t.Errorf("%s releases as %q (%v), want %q", kind, got, ok, want)
127 }
128 }
129 for _, kind := range []string{KindEnum, KindNumber, KindLongText, KindURL} {
130 if _, ok := releaseKind(kind); ok {
131 t.Errorf("%s may now go to a contract; it never could", kind)
132 }
133 }
134}
135