| 1 | package calendar |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "log/slog" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "os" |
| 9 | "os/exec" |
| 10 | "path/filepath" |
| 11 | "testing" |
| 12 | ) |
| 13 | |
| 14 | const asDeployedEnv = "CALENDAR_TEST_AUTH_AS_DEPLOYED" |
| 15 | |
| 16 | // listKeymailFixtures is TestMain's first step. It lists the Keymail |
| 17 | // fixture's host for every test here (SetKeymailServersForTest), except |
| 18 | // in TestAuthIsBuiltAsDeployed's child, which it runs with no fixtures |
| 19 | // and no list, and exits. |
| 20 | func listKeymailFixtures(m *testing.M) { |
| 21 | if os.Getenv(asDeployedEnv) == "1" { |
| 22 | os.Exit(m.Run()) |
| 23 | } |
| 24 | SetKeymailServersForTest([]string{keymailHost}) |
| 25 | } |
| 26 | |
| 27 | // TestAuthIsBuiltAsDeployed: every other test here lists its Keymail |
| 28 | // fixture's host (SetKeymailServersForTest), so auth takes its |
| 29 | // listed-servers path. A deployment lists none, and from rastrillo |
| 30 | // v0.45.0 that path builds a public-address-only client from |
| 31 | // http.DefaultTransport. This builds the app that way, in a child |
| 32 | // process with neither the list nor keymailtest's transport, and opens |
| 33 | // the door. |
| 34 | func TestAuthIsBuiltAsDeployed(t *testing.T) { |
| 35 | if os.Getenv(asDeployedEnv) != "1" { |
| 36 | cmd := exec.Command(os.Args[0], "-test.run=^TestAuthIsBuiltAsDeployed$", "-test.count=1") |
| 37 | cmd.Env = append(os.Environ(), asDeployedEnv+"=1") |
| 38 | if out, err := cmd.CombinedOutput(); err != nil { |
| 39 | t.Fatalf("built as deployed: %v\n%s", err, out) |
| 40 | } |
| 41 | return |
| 42 | } |
| 43 | if keymailTestServers != nil { |
| 44 | t.Fatal("the child lists Keymail servers; it is meant to be built as deployed") |
| 45 | } |
| 46 | a, err := New(Config{DBPath: filepath.Join(t.TempDir(), "calendar.db"), Env: "development", Origin: "https://calendar.example.test", Logger: slog.New(slog.NewTextHandler(io.Discard, nil))}) |
| 47 | if err != nil { |
| 48 | t.Fatal(err) |
| 49 | } |
| 50 | t.Cleanup(func() { a.Close() }) |
| 51 | rec := httptest.NewRecorder() |
| 52 | a.Handler().ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "https://calendar.example.test/signin", nil)) |
| 53 | if rec.Code != http.StatusOK { |
| 54 | t.Fatalf("GET /signin: %d", rec.Code) |
| 55 | } |
| 56 | } |
| 57 | |