| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func env(m map[string]string) func(string) string { |
| 9 | return func(k string) string { return m[k] } |
| 10 | } |
| 11 | |
| 12 | func valid() map[string]string { |
| 13 | return map[string]string{ |
| 14 | "BASE_ORIGIN": "https://base.example.com", |
| 15 | "BASE_OWNER": "Owner@Example.com", |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | func TestLoadRequiresOriginAndOwner(t *testing.T) { |
| 20 | for _, name := range []string{"BASE_ORIGIN", "BASE_OWNER"} { |
| 21 | m := valid() |
| 22 | delete(m, name) |
| 23 | _, err := Load(env(m)) |
| 24 | if err == nil || !strings.Contains(err.Error(), name) { |
| 25 | t.Errorf("without %s: err = %v, want one naming it", name, err) |
| 26 | } |
| 27 | } |
| 28 | cfg, err := Load(env(valid())) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | if cfg.Owner != "owner@example.com" || cfg.Slug != "base" || cfg.Timezone.String() != "Europe/Dublin" { |
| 33 | t.Errorf("defaults wrong: %+v", cfg) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func TestLoadRefusesLoggedMailOffLoopback(t *testing.T) { |
| 38 | m := valid() |
| 39 | m["BASE_ALLOW_LOGGED_MAIL"] = "1" |
| 40 | if _, err := Load(env(m)); err == nil { |
| 41 | t.Fatal("logged mail on a public origin was accepted") |
| 42 | } |
| 43 | m["BASE_ORIGIN"] = "http://127.0.0.1:8830" |
| 44 | cfg, err := Load(env(m)) |
| 45 | if err != nil || !cfg.AllowLoggedMail { |
| 46 | t.Fatalf("loopback: cfg=%+v err=%v", cfg, err) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func TestSlugMakesLegalHosts(t *testing.T) { |
| 51 | for slug, ok := range map[string]bool{ |
| 52 | "acme": true, |
| 53 | "a1-b2": true, |
| 54 | "nineteen-chars-long": true, |
| 55 | "twenty-chars-longer1": false, |
| 56 | "Acme": false, |
| 57 | "-acme": false, |
| 58 | "acme-": false, |
| 59 | "ac_me": false, |
| 60 | "ac.me": false, |
| 61 | } { |
| 62 | m := valid() |
| 63 | m["BASE_SLUG"] = slug |
| 64 | _, err := Load(env(m)) |
| 65 | if (err == nil) != ok { |
| 66 | t.Errorf("slug %q: err = %v, want ok=%v", slug, err, ok) |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func TestLoadRefusesIPOrigins(t *testing.T) { |
| 72 | for _, origin := range []string{"https://203.0.113.1", "https://127.0.0.1", "https://[::1]:8443", "http://203.0.113.1"} { |
| 73 | m := valid() |
| 74 | m["BASE_ORIGIN"] = origin |
| 75 | if _, err := Load(env(m)); err == nil { |
| 76 | t.Errorf("origin %q was accepted", origin) |
| 77 | } |
| 78 | } |
| 79 | m := valid() |
| 80 | m["BASE_ORIGIN"] = "http://127.0.0.1:8830" |
| 81 | if _, err := Load(env(m)); err != nil { |
| 82 | t.Errorf("loopback http refused: %v", err) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // Finding 10: with installs configured, Base's origin and every app origin |
| 87 | // it would hand out must be ones the assertion package accepts, so a bad |
| 88 | // setting fails at boot rather than at the first handoff. HTTP on loopback |
| 89 | // stays allowed for local runs. |
| 90 | func TestLoadChecksOriginsAgainstTheAssertionRules(t *testing.T) { |
| 91 | cases := []struct { |
| 92 | name, origin, domain string |
| 93 | ok bool |
| 94 | }{ |
| 95 | {"https hostnames", "https://base.example.com", "apps.example.com", true}, |
| 96 | {"a port other than 443", "https://base.example.com:8443", "apps.example.com:8443", true}, |
| 97 | {"loopback http for local runs", "http://127.0.0.1:8830", "apps.example.com", true}, |
| 98 | {"explicit :443 on Base", "https://base.example.com:443", "apps.example.com", false}, |
| 99 | {"explicit :443 on apps", "https://base.example.com", "apps.example.com:443", false}, |
| 100 | {"apps domain is an IP", "https://base.example.com", "203.0.113.7", false}, |
| 101 | {"apps domain numeric suffix", "https://base.example.com", "apps.127", false}, |
| 102 | {"apps domain bad label", "https://base.example.com", "apps_x.example.com", false}, |
| 103 | {"apps domain with a path", "https://base.example.com", "apps.example.com/x", false}, |
| 104 | {"apps domain empty label", "https://base.example.com", "apps..example.com", false}, |
| 105 | {"Base hostname with underscore", "https://base_x.example.com", "apps.example.com", false}, |
| 106 | } |
| 107 | for _, tc := range cases { |
| 108 | t.Run(tc.name, func(t *testing.T) { |
| 109 | m := valid() |
| 110 | m["BASE_ORIGIN"] = tc.origin |
| 111 | m["BASE_APPS_DOMAIN"] = tc.domain |
| 112 | _, err := Load(env(m)) |
| 113 | if (err == nil) != tc.ok { |
| 114 | t.Errorf("origin %q, apps domain %q: err = %v, want ok=%v", tc.origin, tc.domain, err, tc.ok) |
| 115 | } |
| 116 | }) |
| 117 | } |
| 118 | // Without an apps domain nothing is installed, and the checks wait. |
| 119 | m := valid() |
| 120 | m["BASE_ORIGIN"] = "https://base.example.com:443" |
| 121 | if _, err := Load(env(m)); err != nil { |
| 122 | t.Errorf("no installs configured: %v", err) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | func TestEquipoOrigin(t *testing.T) { |
| 127 | cfg, err := Load(env(valid())) |
| 128 | if err != nil || cfg.EquipoOrigin != "" { |
| 129 | t.Fatalf("unset: %q, %v; want chat off", cfg.EquipoOrigin, err) |
| 130 | } |
| 131 | for raw, want := range map[string]string{ |
| 132 | "https://equipo.bds.oncarlos.com": "https://equipo.bds.oncarlos.com", |
| 133 | "https://equipo.bds.oncarlos.com/": "https://equipo.bds.oncarlos.com", |
| 134 | "http://127.0.0.1:9000": "http://127.0.0.1:9000", |
| 135 | "http://equipo.example.com": "", |
| 136 | "https://equipo.example.com/chat": "", |
| 137 | "https://user@equipo.example.com": "", |
| 138 | "equipo.example.com": "", |
| 139 | } { |
| 140 | m := valid() |
| 141 | m["BASE_EQUIPO_ORIGIN"] = raw |
| 142 | cfg, err := Load(env(m)) |
| 143 | switch { |
| 144 | case want == "" && err == nil: |
| 145 | t.Errorf("%q accepted as %q", raw, cfg.EquipoOrigin) |
| 146 | case want == "" && !strings.Contains(err.Error(), "BASE_EQUIPO_ORIGIN"): |
| 147 | t.Errorf("%q: error %v does not name the setting", raw, err) |
| 148 | case want != "" && (err != nil || cfg.EquipoOrigin != want): |
| 149 | t.Errorf("%q: got %q, %v; want %q", raw, cfg.EquipoOrigin, err, want) |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | func TestOficinaApps(t *testing.T) { |
| 155 | cfg, err := Load(env(valid())) |
| 156 | if err != nil || cfg.OficinaApps != nil { |
| 157 | t.Fatalf("unset: %+v, %v; want no rail", cfg.OficinaApps, err) |
| 158 | } |
| 159 | |
| 160 | // Good: in the setting's order, names from Base, a trailing / dropped, |
| 161 | // spaces and a trailing comma tolerated. |
| 162 | m := valid() |
| 163 | m["BASE_OFICINA_APPS"] = " fichas=https://fichas.oficina.works, calendar=https://calendar.oficina.works/ ,equipo=https://equipo.bds.oncarlos.com," |
| 164 | cfg, err = Load(env(m)) |
| 165 | if err != nil { |
| 166 | t.Fatal(err) |
| 167 | } |
| 168 | want := []OficinaApp{ |
| 169 | {ID: "fichas", Name: "Fichas", Origin: "https://fichas.oficina.works"}, |
| 170 | {ID: "calendar", Name: "Calendar", Origin: "https://calendar.oficina.works"}, |
| 171 | {ID: "equipo", Name: "Equipo", Origin: "https://equipo.bds.oncarlos.com"}, |
| 172 | } |
| 173 | if len(cfg.OficinaApps) != len(want) { |
| 174 | t.Fatalf("got %+v, want %+v", cfg.OficinaApps, want) |
| 175 | } |
| 176 | for i := range want { |
| 177 | if cfg.OficinaApps[i] != want[i] { |
| 178 | t.Errorf("app %d = %+v, want %+v", i, cfg.OficinaApps[i], want[i]) |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // Every known id, with the name Base gives it. |
| 183 | names := map[string]string{"calendar": "Calendar", "docs": "Docs", "sheets": "Sheets", "slides": "Slides", |
| 184 | "memoria": "Memoria", "calls": "Calls", "fichas": "Fichas", "equipo": "Equipo"} |
| 185 | var all []string |
| 186 | for _, id := range OficinaIDs() { |
| 187 | all = append(all, id+"=https://"+id+".oficina.works") |
| 188 | } |
| 189 | m["BASE_OFICINA_APPS"] = strings.Join(all, ",") |
| 190 | if cfg, err = Load(env(m)); err != nil || len(cfg.OficinaApps) != len(names) { |
| 191 | t.Fatalf("every known app: %+v, %v", cfg.OficinaApps, err) |
| 192 | } |
| 193 | for _, a := range cfg.OficinaApps { |
| 194 | if names[a.ID] != a.Name { |
| 195 | t.Errorf("%s is named %q, want %q", a.ID, a.Name, names[a.ID]) |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | // Empty: no rail. |
| 200 | for _, raw := range []string{"", " ", ","} { |
| 201 | m["BASE_OFICINA_APPS"] = raw |
| 202 | if cfg, err = Load(env(m)); err != nil || len(cfg.OficinaApps) != 0 { |
| 203 | t.Errorf("%q: %+v, %v; want no apps", raw, cfg.OficinaApps, err) |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // Refused at boot, naming the setting and what is wrong. |
| 208 | for raw, says := range map[string]string{ |
| 209 | "mail=https://mail.oficina.works": "mail", |
| 210 | "Calendar=https://calendar.oficina.works": "Calendar", |
| 211 | "calendar=http://calendar.oficina.works": "https", |
| 212 | "calendar=https://calendar.oficina.works/today": "https", |
| 213 | "calendar=https://user@calendar.oficina.works": "https", |
| 214 | "calendar=https://calendar.oficina.works?x=1": "https", |
| 215 | "calendar=https://203.0.113.7": "https", |
| 216 | "calendar=https://localhost": "https", |
| 217 | "calendar=https://calendar.oficina.works:8443": "https", |
| 218 | "calendar=calendar.oficina.works": "https", |
| 219 | "calendar=": "https", |
| 220 | "calendar": "calendar", |
| 221 | "https://calendar.oficina.works": "https://calendar.oficina.works", |
| 222 | "calendar=https://calendar.oficina.works,calendar=https://c.example.com": "twice", |
| 223 | } { |
| 224 | m["BASE_OFICINA_APPS"] = raw |
| 225 | _, err := Load(env(m)) |
| 226 | if err == nil { |
| 227 | t.Errorf("%q accepted", raw) |
| 228 | continue |
| 229 | } |
| 230 | if !strings.Contains(err.Error(), "BASE_OFICINA_APPS") || !strings.Contains(err.Error(), says) { |
| 231 | t.Errorf("%q: error %q does not name the setting and %q", raw, err, says) |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |