| 1 | package equipotest |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/url" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/equipo" |
| 12 | "amadan.net/rastrillo/rastrillo/mail" |
| 13 | ) |
| 14 | |
| 15 | func TestProductionDoesNotUseDriveAuth(t *testing.T) { |
| 16 | t.Parallel() |
| 17 | for _, name := range []string{"missing key", "blank key", "missing origin", "invalid origin", "public logged mail", "nil mailer", "unconfigured mailer", "missing owners"} { |
| 18 | t.Run(name, func(t *testing.T) { |
| 19 | t.Parallel() |
| 20 | d, logger := foundationDB(t) |
| 21 | cfg := testConfig(testOrigin) |
| 22 | var sender mail.Sender = &capturingMailer{} |
| 23 | switch name { |
| 24 | case "missing key": |
| 25 | cfg.InstanceKey = "" |
| 26 | case "blank key": |
| 27 | cfg.InstanceKey = " " |
| 28 | case "missing origin": |
| 29 | cfg.Origin = "" |
| 30 | case "invalid origin": |
| 31 | cfg.Origin = "http://public.example.com" |
| 32 | case "public logged mail": |
| 33 | cfg.Origin = "https://public.example.com" |
| 34 | case "nil mailer": |
| 35 | sender = nil |
| 36 | case "unconfigured mailer": |
| 37 | cfg.AllowLoggedMail = false |
| 38 | sender = mail.Logged(logger) |
| 39 | case "missing owners": |
| 40 | cfg.Owners = nil |
| 41 | } |
| 42 | handler, err := equipo.App(d, cfg, sender, logger) |
| 43 | if err == nil || handler != nil { |
| 44 | t.Fatal("production constructed a handler without valid auth configuration") |
| 45 | } |
| 46 | }) |
| 47 | } |
| 48 | t.Run("valid configuration still requires a session", func(t *testing.T) { |
| 49 | h := newApp(t) |
| 50 | for _, path := range append(append([]string{}, railPaths...), "/work", "/projects/new") { |
| 51 | res := get(t, h, path) |
| 52 | if res.Code != http.StatusSeeOther || res.Header().Get("Location") != "/signin?return_to="+url.QueryEscape(path) { |
| 53 | t.Fatalf("%s allowed anonymous access: %d", path, res.Code) |
| 54 | } |
| 55 | } |
| 56 | }) |
| 57 | } |
| 58 | |
| 59 | func TestLocalStartupScript(t *testing.T) { |
| 60 | script, err := filepath.Abs("../../run.sh") |
| 61 | if err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | if _, err := os.Stat(script); err != nil { |
| 65 | t.Fatal(err) |
| 66 | } |
| 67 | dir := t.TempDir() |
| 68 | bin := filepath.Join(dir, "equipo") |
| 69 | if err := os.WriteFile(bin, []byte("#!/bin/sh\nprintf '%s\\n' \"$@\"\n"), 0700); err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | envfile := filepath.Join(dir, "local.env") |
| 73 | valid := "EQUIPO_ORIGIN=http://127.0.0.1:8824\nEQUIPO_INSTANCE_KEY=persistent-test-key\nEQUIPO_ALLOW_LOGGED_MAIL=1\n" |
| 74 | for _, tc := range []struct { |
| 75 | name, config string |
| 76 | ok bool |
| 77 | }{ |
| 78 | {"valid", valid, true}, |
| 79 | {"missing key", "EQUIPO_ORIGIN=http://127.0.0.1:8824\n", false}, |
| 80 | {"missing origin", "EQUIPO_INSTANCE_KEY=test-key\n", false}, |
| 81 | {"public origin", valid + "EQUIPO_ORIGIN=https://public.example.com\n", false}, |
| 82 | {"public listener", valid + "EQUIPO_ADDR=0.0.0.0:8824\n", false}, |
| 83 | } { |
| 84 | t.Run(tc.name, func(t *testing.T) { |
| 85 | if err := os.WriteFile(envfile, []byte(tc.config), 0600); err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | cmd := exec.Command(script) |
| 89 | cmd.Env = []string{"PATH=" + os.Getenv("PATH"), "EQUIPO_ENV_FILE=" + envfile, "EQUIPO_BIN=" + bin, "EQUIPO_DB=" + filepath.Join(dir, "local.db")} |
| 90 | output, err := cmd.CombinedOutput() |
| 91 | if (err == nil) != tc.ok { |
| 92 | t.Fatalf("startup: %v: %s", err, output) |
| 93 | } |
| 94 | if tc.ok && string(output) != "--db\n"+filepath.Join(dir, "local.db")+"\n--addr\n127.0.0.1:8824\n" { |
| 95 | t.Fatalf("arguments: %s", output) |
| 96 | } |
| 97 | }) |
| 98 | } |
| 99 | } |
| 100 | |