|
| 1 | +package handlers_test |
| 2 | + |
| 3 | +// small_handlers_final_test.go — FINAL coverage pass for small handler arms: |
| 4 | +// - whoami.Get: nil-db early return + email-enrichment success. |
| 5 | +// - usage_wall.GetWall: db_failed via faultdb. |
| 6 | +// - deploys_audit.List: db_failed via faultdb. |
| 7 | + |
| 8 | +import ( |
| 9 | + "net/http" |
| 10 | + "net/http/httptest" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/gofiber/fiber/v2" |
| 14 | + "github.com/google/uuid" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/require" |
| 17 | + |
| 18 | + "instant.dev/internal/handlers" |
| 19 | + "instant.dev/internal/middleware" |
| 20 | + "instant.dev/internal/testhelpers" |
| 21 | +) |
| 22 | + |
| 23 | +// whoami.Get with a nil DB → early return without enrichment (whoami.go:55). |
| 24 | +func TestWhoamiFinal_NilDB_EarlyReturn(t *testing.T) { |
| 25 | + app := fiber.New() |
| 26 | + app.Use(func(c *fiber.Ctx) error { |
| 27 | + c.Locals(middleware.LocalKeyTeamID, uuid.NewString()) |
| 28 | + c.Locals(middleware.LocalKeyUserID, uuid.NewString()) |
| 29 | + return c.Next() |
| 30 | + }) |
| 31 | + h := handlers.NewWhoamiHandler(nil) |
| 32 | + app.Get("/whoami", h.Get) |
| 33 | + resp, err := app.Test(httptest.NewRequest(http.MethodGet, "/whoami", nil), 5000) |
| 34 | + require.NoError(t, err) |
| 35 | + defer resp.Body.Close() |
| 36 | + assert.Equal(t, http.StatusOK, resp.StatusCode) |
| 37 | +} |
| 38 | + |
| 39 | +// whoami.Get with a real team + user → tier + email enrichment (whoami.go:63,74). |
| 40 | +func TestWhoamiFinal_Enrichment(t *testing.T) { |
| 41 | + db, clean := testhelpers.SetupTestDB(t) |
| 42 | + defer clean() |
| 43 | + teamID := testhelpers.MustCreateTeamDB(t, db, "pro") |
| 44 | + email := testhelpers.UniqueEmail(t) |
| 45 | + var userID string |
| 46 | + require.NoError(t, db.QueryRow( |
| 47 | + `INSERT INTO users (team_id, email) VALUES ($1::uuid, $2) RETURNING id::text`, teamID, email).Scan(&userID)) |
| 48 | + |
| 49 | + app := fiber.New() |
| 50 | + app.Use(func(c *fiber.Ctx) error { |
| 51 | + c.Locals(middleware.LocalKeyTeamID, teamID) |
| 52 | + c.Locals(middleware.LocalKeyUserID, userID) |
| 53 | + return c.Next() |
| 54 | + }) |
| 55 | + h := handlers.NewWhoamiHandler(db) |
| 56 | + app.Get("/whoami", h.Get) |
| 57 | + resp, err := app.Test(httptest.NewRequest(http.MethodGet, "/whoami", nil), 5000) |
| 58 | + require.NoError(t, err) |
| 59 | + defer resp.Body.Close() |
| 60 | + require.Equal(t, http.StatusOK, resp.StatusCode) |
| 61 | + var m map[string]any |
| 62 | + require.NoError(t, decodeJSON(resp, &m)) |
| 63 | + assert.Equal(t, "pro", m["tier"]) |
| 64 | + assert.Equal(t, email, m["email"]) |
| 65 | +} |
| 66 | + |
| 67 | +// usage_wall.GetWall: the usage query errors → db_failed (usage_wall.go:118). |
| 68 | +// team-tier check(1) errors-or-misses, then the usage query(2) errors. Use a |
| 69 | +// non-team tier so the early-return is skipped; failAfter=1 makes the usage |
| 70 | +// query error. |
| 71 | +func TestUsageWallFinal_DBError_503(t *testing.T) { |
| 72 | + seedDB, clean := testhelpers.SetupTestDB(t) |
| 73 | + defer clean() |
| 74 | + teamID := uuid.MustParse(testhelpers.MustCreateTeamDB(t, seedDB, "pro")) |
| 75 | + |
| 76 | + app := newUsageWallApp(t, openFaultDB(t, 1), teamID) |
| 77 | + resp, err := app.Test(httptest.NewRequest(http.MethodGet, "/api/v1/usage/wall", nil), 5000) |
| 78 | + require.NoError(t, err) |
| 79 | + defer resp.Body.Close() |
| 80 | + require.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) |
| 81 | +} |
| 82 | + |
| 83 | +// deploys_audit.List: the list query errors → db_failed (deploys_audit.go:121). |
| 84 | +func TestDeploysAuditFinal_DBError_503(t *testing.T) { |
| 85 | + t.Setenv(middleware.AdminEmailsEnvVar, deploysAuditAdminEmail) |
| 86 | + app := deploysAuditApp(t, openFaultDB(t, 0), deploysAuditAdminEmail) |
| 87 | + status, _ := deploysAuditDoGET(t, app, "/api/v1/admin/deploys") |
| 88 | + assert.Equal(t, http.StatusServiceUnavailable, status) |
| 89 | +} |
0 commit comments