package config import ( "strings" "testing" ) func env(m map[string]string) func(string) string { return func(k string) string { return m[k] } } func valid() map[string]string { return map[string]string{ "BASE_ORIGIN": "https://base.example.com", "BASE_OWNER": "Owner@Example.com", } } func TestLoadRequiresOriginAndOwner(t *testing.T) { for _, name := range []string{"BASE_ORIGIN", "BASE_OWNER"} { m := valid() delete(m, name) _, err := Load(env(m)) if err == nil || !strings.Contains(err.Error(), name) { t.Errorf("without %s: err = %v, want one naming it", name, err) } } cfg, err := Load(env(valid())) if err != nil { t.Fatal(err) } if cfg.Owner != "owner@example.com" || cfg.Slug != "base" || cfg.Timezone.String() != "Europe/Dublin" { t.Errorf("defaults wrong: %+v", cfg) } } func TestLoadRefusesLoggedMailOffLoopback(t *testing.T) { m := valid() m["BASE_ALLOW_LOGGED_MAIL"] = "1" if _, err := Load(env(m)); err == nil { t.Fatal("logged mail on a public origin was accepted") } m["BASE_ORIGIN"] = "http://127.0.0.1:8830" cfg, err := Load(env(m)) if err != nil || !cfg.AllowLoggedMail { t.Fatalf("loopback: cfg=%+v err=%v", cfg, err) } } func TestSlugMakesLegalHosts(t *testing.T) { for slug, ok := range map[string]bool{ "acme": true, "a1-b2": true, "nineteen-chars-long": true, "twenty-chars-longer1": false, "Acme": false, "-acme": false, "acme-": false, "ac_me": false, "ac.me": false, } { m := valid() m["BASE_SLUG"] = slug _, err := Load(env(m)) if (err == nil) != ok { t.Errorf("slug %q: err = %v, want ok=%v", slug, err, ok) } } } func TestLoadRefusesIPOrigins(t *testing.T) { for _, origin := range []string{"https://203.0.113.1", "https://127.0.0.1", "https://[::1]:8443", "http://203.0.113.1"} { m := valid() m["BASE_ORIGIN"] = origin if _, err := Load(env(m)); err == nil { t.Errorf("origin %q was accepted", origin) } } m := valid() m["BASE_ORIGIN"] = "http://127.0.0.1:8830" if _, err := Load(env(m)); err != nil { t.Errorf("loopback http refused: %v", err) } } // Finding 10: with installs configured, Base's origin and every app origin // it would hand out must be ones the assertion package accepts, so a bad // setting fails at boot rather than at the first handoff. HTTP on loopback // stays allowed for local runs. func TestLoadChecksOriginsAgainstTheAssertionRules(t *testing.T) { cases := []struct { name, origin, domain string ok bool }{ {"https hostnames", "https://base.example.com", "apps.example.com", true}, {"a port other than 443", "https://base.example.com:8443", "apps.example.com:8443", true}, {"loopback http for local runs", "http://127.0.0.1:8830", "apps.example.com", true}, {"explicit :443 on Base", "https://base.example.com:443", "apps.example.com", false}, {"explicit :443 on apps", "https://base.example.com", "apps.example.com:443", false}, {"apps domain is an IP", "https://base.example.com", "203.0.113.7", false}, {"apps domain numeric suffix", "https://base.example.com", "apps.127", false}, {"apps domain bad label", "https://base.example.com", "apps_x.example.com", false}, {"apps domain with a path", "https://base.example.com", "apps.example.com/x", false}, {"apps domain empty label", "https://base.example.com", "apps..example.com", false}, {"Base hostname with underscore", "https://base_x.example.com", "apps.example.com", false}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { m := valid() m["BASE_ORIGIN"] = tc.origin m["BASE_APPS_DOMAIN"] = tc.domain _, err := Load(env(m)) if (err == nil) != tc.ok { t.Errorf("origin %q, apps domain %q: err = %v, want ok=%v", tc.origin, tc.domain, err, tc.ok) } }) } // Without an apps domain nothing is installed, and the checks wait. m := valid() m["BASE_ORIGIN"] = "https://base.example.com:443" if _, err := Load(env(m)); err != nil { t.Errorf("no installs configured: %v", err) } } func TestEquipoOrigin(t *testing.T) { cfg, err := Load(env(valid())) if err != nil || cfg.EquipoOrigin != "" { t.Fatalf("unset: %q, %v; want chat off", cfg.EquipoOrigin, err) } for raw, want := range map[string]string{ "https://equipo.bds.oncarlos.com": "https://equipo.bds.oncarlos.com", "https://equipo.bds.oncarlos.com/": "https://equipo.bds.oncarlos.com", "http://127.0.0.1:9000": "http://127.0.0.1:9000", "http://equipo.example.com": "", "https://equipo.example.com/chat": "", "https://user@equipo.example.com": "", "equipo.example.com": "", } { m := valid() m["BASE_EQUIPO_ORIGIN"] = raw cfg, err := Load(env(m)) switch { case want == "" && err == nil: t.Errorf("%q accepted as %q", raw, cfg.EquipoOrigin) case want == "" && !strings.Contains(err.Error(), "BASE_EQUIPO_ORIGIN"): t.Errorf("%q: error %v does not name the setting", raw, err) case want != "" && (err != nil || cfg.EquipoOrigin != want): t.Errorf("%q: got %q, %v; want %q", raw, cfg.EquipoOrigin, err, want) } } } func TestOficinaApps(t *testing.T) { cfg, err := Load(env(valid())) if err != nil || cfg.OficinaApps != nil { t.Fatalf("unset: %+v, %v; want no rail", cfg.OficinaApps, err) } // Good: in the setting's order, names from Base, a trailing / dropped, // spaces and a trailing comma tolerated. m := valid() m["BASE_OFICINA_APPS"] = " fichas=https://fichas.oficina.works, calendar=https://calendar.oficina.works/ ,equipo=https://equipo.bds.oncarlos.com," cfg, err = Load(env(m)) if err != nil { t.Fatal(err) } want := []OficinaApp{ {ID: "fichas", Name: "Fichas", Origin: "https://fichas.oficina.works"}, {ID: "calendar", Name: "Calendar", Origin: "https://calendar.oficina.works"}, {ID: "equipo", Name: "Equipo", Origin: "https://equipo.bds.oncarlos.com"}, } if len(cfg.OficinaApps) != len(want) { t.Fatalf("got %+v, want %+v", cfg.OficinaApps, want) } for i := range want { if cfg.OficinaApps[i] != want[i] { t.Errorf("app %d = %+v, want %+v", i, cfg.OficinaApps[i], want[i]) } } // Every known id, with the name Base gives it. names := map[string]string{"calendar": "Calendar", "docs": "Docs", "sheets": "Sheets", "slides": "Slides", "memoria": "Memoria", "calls": "Calls", "fichas": "Fichas", "equipo": "Equipo"} var all []string for _, id := range OficinaIDs() { all = append(all, id+"=https://"+id+".oficina.works") } m["BASE_OFICINA_APPS"] = strings.Join(all, ",") if cfg, err = Load(env(m)); err != nil || len(cfg.OficinaApps) != len(names) { t.Fatalf("every known app: %+v, %v", cfg.OficinaApps, err) } for _, a := range cfg.OficinaApps { if names[a.ID] != a.Name { t.Errorf("%s is named %q, want %q", a.ID, a.Name, names[a.ID]) } } // Empty: no rail. for _, raw := range []string{"", " ", ","} { m["BASE_OFICINA_APPS"] = raw if cfg, err = Load(env(m)); err != nil || len(cfg.OficinaApps) != 0 { t.Errorf("%q: %+v, %v; want no apps", raw, cfg.OficinaApps, err) } } // Refused at boot, naming the setting and what is wrong. for raw, says := range map[string]string{ "mail=https://mail.oficina.works": "mail", "Calendar=https://calendar.oficina.works": "Calendar", "calendar=http://calendar.oficina.works": "https", "calendar=https://calendar.oficina.works/today": "https", "calendar=https://user@calendar.oficina.works": "https", "calendar=https://calendar.oficina.works?x=1": "https", "calendar=https://203.0.113.7": "https", "calendar=https://localhost": "https", "calendar=https://calendar.oficina.works:8443": "https", "calendar=calendar.oficina.works": "https", "calendar=": "https", "calendar": "calendar", "https://calendar.oficina.works": "https://calendar.oficina.works", "calendar=https://calendar.oficina.works,calendar=https://c.example.com": "twice", } { m["BASE_OFICINA_APPS"] = raw _, err := Load(env(m)) if err == nil { t.Errorf("%q accepted", raw) continue } if !strings.Contains(err.Error(), "BASE_OFICINA_APPS") || !strings.Contains(err.Error(), says) { t.Errorf("%q: error %q does not name the setting and %q", raw, err, says) } } }