tenseleyflow/shithub / d80d1da

Browse files

S39: middleware.WithCurrentUserForTest helper for handler tests

Authored by espadonne
SHA
d80d1da670b3048183ddd379bdfa961649deaaa8
Parents
c197c28
Tree
8f0adda

1 changed file

StatusFile+-
M internal/web/middleware/auth.go 8 0
internal/web/middleware/auth.gomodified
@@ -164,6 +164,14 @@ func CurrentUserFromContext(ctx context.Context) CurrentUser {
164164
 	return CurrentUser{}
165165
 }
166166
 
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
+
167175
 // MaxBodySize returns middleware that caps r.Body at maxBytes via
168176
 // http.MaxBytesReader. Use this on auth POST endpoints (signup, login,
169177
 // password reset) so a misbehaving client can't ship a 10 MB password