| 1 | package equipo |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestStagingLoggedMailBoundary(t *testing.T) { |
| 6 | for _, tc := range []struct { |
| 7 | origin string |
| 8 | allowed bool |
| 9 | }{ |
| 10 | {"https://equipo-staging-0123456789abcdef.bds.oncarlos.com", true}, |
| 11 | {"https://equipo-staging.bds.oncarlos.com", true}, |
| 12 | {"https://equipo.bds.oncarlos.com", false}, |
| 13 | {"https://equipo-staging-x.bds.oncarlos.com", false}, |
| 14 | {"https://equipo-staging-0123456789abcdef.bds.oncarlos.com.evil.test", false}, |
| 15 | {"https://equipo-staging-0123456789abcdef.other.oncarlos.com", false}, |
| 16 | {"http://equipo-staging-0123456789abcdef.bds.oncarlos.com", false}, |
| 17 | {"https://equipo-staging-0123456789abcdef.bds.oncarlos.com:444", false}, |
| 18 | {"http://127.0.0.1:4321", true}, |
| 19 | } { |
| 20 | t.Run(tc.origin, func(t *testing.T) { |
| 21 | cfg := Config{Origin: tc.origin, InstanceKey: "test", AllowLoggedMail: true} |
| 22 | if err := cfg.validate(); (err == nil) != tc.allowed { |
| 23 | t.Fatalf("allowed=%v error=%v", tc.allowed, err) |
| 24 | } |
| 25 | }) |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | // EQUIPO_ASSISTANT_HOSTS is a comma-separated list of origins, compared |
| 30 | // lowercased; unset allows none. |
| 31 | func TestAssistantHostsConfig(t *testing.T) { |
| 32 | env := map[string]string{"EQUIPO_ORIGIN": "http://127.0.0.1:1", "EQUIPO_INSTANCE_KEY": "k", "EQUIPO_ALLOW_LOGGED_MAIL": "1"} |
| 33 | cfg, err := LoadConfig(func(k string) string { return env[k] }) |
| 34 | if err != nil || len(cfg.AssistantHosts) != 0 { |
| 35 | t.Fatalf("unset %v %v", cfg.AssistantHosts, err) |
| 36 | } |
| 37 | env["EQUIPO_ASSISTANT_HOSTS"] = " https://Base.Example.com, ,http://127.0.0.1:8080 " |
| 38 | if cfg, err = LoadConfig(func(k string) string { return env[k] }); err != nil || len(cfg.AssistantHosts) != 2 || cfg.AssistantHosts[0] != "https://base.example.com" || cfg.AssistantHosts[1] != "http://127.0.0.1:8080" { |
| 39 | t.Fatalf("hosts %q %v", cfg.AssistantHosts, err) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | func TestDatabaseName(t *testing.T) { |
| 44 | env := map[string]string{"EQUIPO_ORIGIN": "http://127.0.0.1:1", "EQUIPO_INSTANCE_KEY": "k", "EQUIPO_ALLOW_LOGGED_MAIL": "1"} |
| 45 | for name, ok := range map[string]bool{"": true, "equipo-v1.db": true, "../equipo.db": false, "/data/x.db": false, ".hidden.db": false, "equipo.sqlite": false} { |
| 46 | env["EQUIPO_DATABASE"] = name |
| 47 | cfg, err := LoadConfig(func(k string) string { return env[k] }) |
| 48 | if (err == nil) != ok { |
| 49 | t.Fatalf("%q accepted=%v", name, err == nil) |
| 50 | } |
| 51 | if ok && name == "" && cfg.DatabaseName != "equipo.db" { |
| 52 | t.Fatal("default database name", cfg.DatabaseName) |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // EQUIPO_OFICINA_CONNECT_ORIGIN is off by default; set, it needs an |
| 58 | // https origin (http on loopback) and EQUIPO_OFICINA_CONNECT_CLIENT_ID, |
| 59 | // and the client's redirect is this instance's callback. |
| 60 | func TestOficinaConnectConfig(t *testing.T) { |
| 61 | env := map[string]string{"EQUIPO_ORIGIN": "https://equipo.example.com", "EQUIPO_INSTANCE_KEY": "k", "CARLOS_SMTP_HOST": "smtp.example.com", "CARLOS_SMTP_FROM": "e@example.com", "EQUIPO_OFICINA_CONNECT_CLIENT_ID": "c"} |
| 62 | get := func(k string) string { return env[k] } |
| 63 | if cfg, err := LoadConfig(get); err != nil || cfg.OficinaConnectOrigin != "" || cfg.OficinaConnectClientID != "" { |
| 64 | t.Fatalf("unset: %+v %v", cfg, err) |
| 65 | } |
| 66 | for origin, ok := range map[string]bool{"https://connect.oficina.works": true, "http://connect.oficina.works": false, "https://connect.oficina.works/x": false, "http://127.0.0.1:9": true} { |
| 67 | env["EQUIPO_OFICINA_CONNECT_ORIGIN"] = origin |
| 68 | if _, err := LoadConfig(get); (err == nil) != ok { |
| 69 | t.Errorf("%s: %v", origin, err) |
| 70 | } |
| 71 | } |
| 72 | env["EQUIPO_OFICINA_CONNECT_ORIGIN"], env["EQUIPO_OFICINA_CONNECT_CLIENT_ID"] = "https://connect.oficina.works", "" |
| 73 | if _, err := LoadConfig(get); err == nil { |
| 74 | t.Error("no client id accepted") |
| 75 | } |
| 76 | env["EQUIPO_OFICINA_CONNECT_CLIENT_ID"] = " c " |
| 77 | cfg, err := LoadConfig(get) |
| 78 | if err != nil || cfg.OficinaConnectClientID != "c" || cfg.oficinaConfig().Redirect != "https://equipo.example.com/oficina/connect/callback" { |
| 79 | t.Fatalf("set: %+v %v", cfg.oficinaConfig(), err) |
| 80 | } |
| 81 | } |
| 82 | |