|
5 | 5 | "net/http" |
6 | 6 | "net/http/httptest" |
7 | 7 | "testing" |
| 8 | + "time" |
8 | 9 |
|
9 | 10 | "github.com/hatmaxkit/hatmax/config" |
10 | 11 | "github.com/hatmaxkit/hatmax/log" |
@@ -227,3 +228,144 @@ func TestClearSessionCookie(t *testing.T) { |
227 | 228 | t.Errorf("ClearSessionCookie() cookie.MaxAge = %v, want -1", cookie.MaxAge) |
228 | 229 | } |
229 | 230 | } |
| 231 | + |
| 232 | +func TestRequireTOTP(t *testing.T) { |
| 233 | + tests := []struct { |
| 234 | + name string |
| 235 | + cfg TOTPEnforcement |
| 236 | + user *User |
| 237 | + wantStatus int |
| 238 | + wantRedirect string |
| 239 | + }{ |
| 240 | + { |
| 241 | + name: "enforcement disabled", |
| 242 | + cfg: TOTPEnforcement{ |
| 243 | + Enabled: func() bool { return false }, |
| 244 | + }, |
| 245 | + user: &User{TOTPEnabled: false}, |
| 246 | + wantStatus: http.StatusOK, |
| 247 | + }, |
| 248 | + { |
| 249 | + name: "enforcement nil", |
| 250 | + cfg: TOTPEnforcement{}, |
| 251 | + user: &User{TOTPEnabled: false}, |
| 252 | + wantStatus: http.StatusOK, |
| 253 | + }, |
| 254 | + { |
| 255 | + name: "totp enabled", |
| 256 | + cfg: TOTPEnforcement{ |
| 257 | + Enabled: func() bool { return true }, |
| 258 | + }, |
| 259 | + user: &User{TOTPEnabled: true}, |
| 260 | + wantStatus: http.StatusOK, |
| 261 | + }, |
| 262 | + { |
| 263 | + name: "totp not enabled redirects", |
| 264 | + cfg: TOTPEnforcement{ |
| 265 | + Enabled: func() bool { return true }, |
| 266 | + GraceDays: func() int { return 0 }, |
| 267 | + }, |
| 268 | + user: &User{TOTPEnabled: false}, |
| 269 | + wantStatus: http.StatusSeeOther, |
| 270 | + wantRedirect: "/totp-setup", |
| 271 | + }, |
| 272 | + { |
| 273 | + name: "custom setup url", |
| 274 | + cfg: TOTPEnforcement{ |
| 275 | + Enabled: func() bool { return true }, |
| 276 | + GraceDays: func() int { return 0 }, |
| 277 | + SetupURL: "/settings/2fa", |
| 278 | + }, |
| 279 | + user: &User{TOTPEnabled: false}, |
| 280 | + wantStatus: http.StatusSeeOther, |
| 281 | + wantRedirect: "/settings/2fa", |
| 282 | + }, |
| 283 | + { |
| 284 | + name: "no user in context passes", |
| 285 | + cfg: TOTPEnforcement{ |
| 286 | + Enabled: func() bool { return true }, |
| 287 | + }, |
| 288 | + user: nil, |
| 289 | + wantStatus: http.StatusOK, |
| 290 | + }, |
| 291 | + } |
| 292 | + |
| 293 | + for _, tt := range tests { |
| 294 | + t.Run(tt.name, func(t *testing.T) { |
| 295 | + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 296 | + w.WriteHeader(http.StatusOK) |
| 297 | + }) |
| 298 | + |
| 299 | + middleware := RequireTOTP(tt.cfg) |
| 300 | + wrapped := middleware(handler) |
| 301 | + |
| 302 | + req := httptest.NewRequest(http.MethodGet, "/protected", nil) |
| 303 | + if tt.user != nil { |
| 304 | + ctx := WithUser(req.Context(), tt.user) |
| 305 | + req = req.WithContext(ctx) |
| 306 | + } |
| 307 | + |
| 308 | + w := httptest.NewRecorder() |
| 309 | + wrapped.ServeHTTP(w, req) |
| 310 | + |
| 311 | + if w.Code != tt.wantStatus { |
| 312 | + t.Errorf("RequireTOTP() status = %v, want %v", w.Code, tt.wantStatus) |
| 313 | + } |
| 314 | + |
| 315 | + if tt.wantRedirect != "" { |
| 316 | + location := w.Header().Get("Location") |
| 317 | + if location != tt.wantRedirect { |
| 318 | + t.Errorf("RequireTOTP() redirect = %v, want %v", location, tt.wantRedirect) |
| 319 | + } |
| 320 | + } |
| 321 | + }) |
| 322 | + } |
| 323 | +} |
| 324 | + |
| 325 | +func TestRequireTOTPGracePeriod(t *testing.T) { |
| 326 | + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 327 | + w.WriteHeader(http.StatusOK) |
| 328 | + }) |
| 329 | + |
| 330 | + cfg := TOTPEnforcement{ |
| 331 | + Enabled: func() bool { return true }, |
| 332 | + GraceDays: func() int { return 7 }, |
| 333 | + } |
| 334 | + |
| 335 | + middleware := RequireTOTP(cfg) |
| 336 | + wrapped := middleware(handler) |
| 337 | + |
| 338 | + // User created recently should pass |
| 339 | + recentUser := &User{ |
| 340 | + TOTPEnabled: false, |
| 341 | + CreatedAt: time.Now().AddDate(0, 0, -3), |
| 342 | + } |
| 343 | + |
| 344 | + req := httptest.NewRequest(http.MethodGet, "/protected", nil) |
| 345 | + ctx := WithUser(req.Context(), recentUser) |
| 346 | + req = req.WithContext(ctx) |
| 347 | + |
| 348 | + w := httptest.NewRecorder() |
| 349 | + wrapped.ServeHTTP(w, req) |
| 350 | + |
| 351 | + if w.Code != http.StatusOK { |
| 352 | + t.Errorf("RequireTOTP() with grace period status = %v, want %v", w.Code, http.StatusOK) |
| 353 | + } |
| 354 | + |
| 355 | + // User created long ago should redirect |
| 356 | + oldUser := &User{ |
| 357 | + TOTPEnabled: false, |
| 358 | + CreatedAt: time.Now().AddDate(0, 0, -30), |
| 359 | + } |
| 360 | + |
| 361 | + req = httptest.NewRequest(http.MethodGet, "/protected", nil) |
| 362 | + ctx = WithUser(req.Context(), oldUser) |
| 363 | + req = req.WithContext(ctx) |
| 364 | + |
| 365 | + w = httptest.NewRecorder() |
| 366 | + wrapped.ServeHTTP(w, req) |
| 367 | + |
| 368 | + if w.Code != http.StatusSeeOther { |
| 369 | + t.Errorf("RequireTOTP() outside grace period status = %v, want %v", w.Code, http.StatusSeeOther) |
| 370 | + } |
| 371 | +} |
0 commit comments