usageHandler reproduces the /api/usage handler logic from routes/usage.go without going through application.Application, which drags in galleryop, model loaders, etc. Keeping this tight test surface lets the no-auth path (the user-visible feature here) be covered without the auth build tag.
(rec *billing.Recorder, fallback *auth.User)
| 35 | // surface lets the no-auth path (the user-visible feature here) be |
| 36 | // covered without the auth build tag. |
| 37 | func usageHandler(rec *billing.Recorder, fallback *auth.User) echo.HandlerFunc { |
| 38 | return func(c echo.Context) error { |
| 39 | user := auth.GetUser(c) |
| 40 | if user == nil { |
| 41 | user = fallback |
| 42 | } |
| 43 | if user == nil { |
| 44 | return c.JSON(http.StatusUnauthorized, map[string]string{"error": "not authenticated"}) |
| 45 | } |
| 46 | period := c.QueryParam("period") |
| 47 | if period == "" { |
| 48 | period = "month" |
| 49 | } |
| 50 | buckets, err := rec.Aggregate(c.Request().Context(), billing.AggregateQuery{ |
| 51 | UserID: user.ID, |
| 52 | Period: period, |
| 53 | }) |
| 54 | if err != nil { |
| 55 | return c.JSON(http.StatusInternalServerError, map[string]string{"error": "agg failed"}) |
| 56 | } |
| 57 | return c.JSON(http.StatusOK, map[string]any{ |
| 58 | "usage": buckets, |
| 59 | "viewer": map[string]string{ |
| 60 | "id": user.ID, |
| 61 | "name": user.Name, |
| 62 | "role": user.Role, |
| 63 | }, |
| 64 | }) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | var _ = Describe("Usage endpoint", func() { |
| 69 | It("resolves the local user in no-auth mode", func() { |