| 1 | package appboot |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "io" |
| 7 | "log/slog" |
| 8 | "net/http" |
| 9 | "net/http/httptest" |
| 10 | "path/filepath" |
| 11 | "strings" |
| 12 | "testing" |
| 13 | |
| 14 | "amadan.net/rastrillo/rastrillo/crypto" |
| 15 | "amadan.net/rastrillo/rastrillo/db" |
| 16 | |
| 17 | "amadan.net/oficina/base/internal/config" |
| 18 | "amadan.net/oficina/base/internal/consumer" |
| 19 | "amadan.net/oficina/base/internal/install" |
| 20 | "amadan.net/oficina/base/internal/welcome" |
| 21 | ) |
| 22 | |
| 23 | var quiet = slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 24 | |
| 25 | func appVars(t *testing.T) func(string) string { |
| 26 | t.Helper() |
| 27 | key, err := crypto.Generate() |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | vars := install.AppVars(config.Config{Origin: "https://base.test", AssertionKey: key}, "welcome", "https://acme-welcome.test") |
| 32 | return func(k string) string { return vars[k] } |
| 33 | } |
| 34 | |
| 35 | func TestRunRefusesMissingSettingsFirst(t *testing.T) { |
| 36 | built := false |
| 37 | app := App{Name: "welcome", DBFile: "welcome.db", Schema: welcome.BootSchema, |
| 38 | Build: func(*db.DB, consumer.Config, *slog.Logger) (http.Handler, error) { |
| 39 | built = true |
| 40 | return http.NotFoundHandler(), nil |
| 41 | }} |
| 42 | err := Run(app, func(string) string { return "" }, quiet) |
| 43 | if err == nil || !strings.Contains(err.Error(), "BASE_APP_ORIGIN") { |
| 44 | t.Fatalf("Run with no settings: %v", err) |
| 45 | } |
| 46 | if built { |
| 47 | t.Error("the app was built without its settings") |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func TestRunRefusesAnIncompleteApp(t *testing.T) { |
| 52 | for name, app := range map[string]App{ |
| 53 | "no name": {DBFile: "x.db", Schema: welcome.BootSchema, Build: welcome.App}, |
| 54 | "no file": {Name: "x", Schema: welcome.BootSchema, Build: welcome.App}, |
| 55 | "no schema": {Name: "x", DBFile: "x.db", Build: welcome.App}, |
| 56 | "no build": {Name: "x", DBFile: "x.db", Schema: welcome.BootSchema}, |
| 57 | } { |
| 58 | if err := Run(app, appVars(t), quiet); err == nil { |
| 59 | t.Errorf("%s: Run accepted it", name) |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func TestOpenMigratesAndBuilds(t *testing.T) { |
| 65 | cfg, err := consumer.Load(appVars(t)) |
| 66 | if err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | var gotLogger *slog.Logger |
| 70 | app := App{Name: "welcome", DBFile: "welcome.db", Schema: welcome.BootSchema, |
| 71 | Build: func(d *db.DB, c consumer.Config, l *slog.Logger) (http.Handler, error) { |
| 72 | gotLogger = c.Logger |
| 73 | return welcome.App(d, c, l) |
| 74 | }} |
| 75 | h, closeDB, err := open(app, cfg, filepath.Join(t.TempDir(), "welcome.db"), quiet) |
| 76 | if err != nil { |
| 77 | t.Fatalf("open: %v", err) |
| 78 | } |
| 79 | defer closeDB() |
| 80 | if gotLogger != quiet { |
| 81 | t.Error("the consumer was not given the boot logger") |
| 82 | } |
| 83 | req := httptest.NewRequest(http.MethodGet, "https://acme-welcome.test/", nil) |
| 84 | rec := httptest.NewRecorder() |
| 85 | h.ServeHTTP(rec, req) |
| 86 | if rec.Code != http.StatusSeeOther || !strings.HasPrefix(rec.Header().Get("Location"), consumer.StartPath) { |
| 87 | t.Errorf("GET / on a fresh app: %d %q", rec.Code, rec.Header().Get("Location")) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | func TestOpenClosesTheDatabaseWhenBuildFails(t *testing.T) { |
| 92 | cfg, err := consumer.Load(appVars(t)) |
| 93 | if err != nil { |
| 94 | t.Fatal(err) |
| 95 | } |
| 96 | var opened *db.DB |
| 97 | boom := errors.New("boom") |
| 98 | app := App{Name: "welcome", DBFile: "welcome.db", Schema: welcome.BootSchema, |
| 99 | Build: func(d *db.DB, _ consumer.Config, _ *slog.Logger) (http.Handler, error) { opened = d; return nil, boom }} |
| 100 | if _, _, err := open(app, cfg, filepath.Join(t.TempDir(), "welcome.db"), quiet); !errors.Is(err, boom) { |
| 101 | t.Fatalf("open: %v, want the build error", err) |
| 102 | } |
| 103 | if err := opened.Writer().PingContext(context.Background()); err == nil { |
| 104 | t.Error("the database is still open after a failed build") |
| 105 | } |
| 106 | } |
| 107 | |