S39: middleware.WithCurrentUserForTest helper for handler tests
- SHA
d80d1da670b3048183ddd379bdfa961649deaaa8- Parents
-
c197c28 - Tree
8f0adda
d80d1da
d80d1da670b3048183ddd379bdfa961649deaaa8c197c28
8f0adda| Status | File | + | - |
|---|---|---|---|
| M |
internal/web/middleware/auth.go
|
8 | 0 |
internal/web/middleware/auth.gomodified@@ -164,6 +164,14 @@ func CurrentUserFromContext(ctx context.Context) CurrentUser { | ||
| 164 | 164 | return CurrentUser{} |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | +// WithCurrentUserForTest binds u onto ctx the same way the OptionalUser | |
| 168 | +// middleware does. Test-only — production code paths reach a request | |
| 169 | +// through OptionalUser/RequireUser and never need this. Lives here so | |
| 170 | +// the context key stays unexported. | |
| 171 | +func WithCurrentUserForTest(ctx context.Context, u CurrentUser) context.Context { | |
| 172 | + return context.WithValue(ctx, currentUserKey, u) | |
| 173 | +} | |
| 174 | + | |
| 167 | 175 | // MaxBodySize returns middleware that caps r.Body at maxBytes via |
| 168 | 176 | // http.MaxBytesReader. Use this on auth POST endpoints (signup, login, |
| 169 | 177 | // password reset) so a misbehaving client can't ship a 10 MB password |