- Add tests for SanitizeEmail, SanitizeDescription, DefaultSanitizer - Add AuthMiddleware and RequireRole tests - Add admin_handlers_test.go and location_handlers_test.go - Expand application_service_test.go with more methods
152 lines
3.6 KiB
Go
152 lines
3.6 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestRateLimiter_isAllowed(t *testing.T) {
|
|
limiter := NewRateLimiter(3, time.Minute)
|
|
|
|
// First 3 requests should be allowed
|
|
for i := 0; i < 3; i++ {
|
|
if !limiter.isAllowed("192.168.1.1") {
|
|
t.Errorf("Request %d should be allowed", i+1)
|
|
}
|
|
}
|
|
|
|
// 4th request should be denied
|
|
if limiter.isAllowed("192.168.1.1") {
|
|
t.Error("Request 4 should be denied")
|
|
}
|
|
|
|
// Different IP should still be allowed
|
|
if !limiter.isAllowed("192.168.1.2") {
|
|
t.Error("Different IP should be allowed")
|
|
}
|
|
}
|
|
|
|
func TestRateLimitMiddleware(t *testing.T) {
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
|
|
middleware := RateLimitMiddleware(2, time.Minute)(handler)
|
|
|
|
// Create test requests
|
|
for i := 0; i < 3; i++ {
|
|
req := httptest.NewRequest("GET", "/test", nil)
|
|
req.RemoteAddr = "192.168.1.100:12345"
|
|
rr := httptest.NewRecorder()
|
|
|
|
middleware.ServeHTTP(rr, req)
|
|
|
|
if i < 2 {
|
|
if rr.Code != http.StatusOK {
|
|
t.Errorf("Request %d: expected status 200, got %d", i+1, rr.Code)
|
|
}
|
|
} else {
|
|
if rr.Code != http.StatusTooManyRequests {
|
|
t.Errorf("Request %d: expected status 429, got %d", i+1, rr.Code)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestSecurityHeadersMiddleware(t *testing.T) {
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
|
|
middleware := SecurityHeadersMiddleware(handler)
|
|
|
|
req := httptest.NewRequest("GET", "/test", nil)
|
|
rr := httptest.NewRecorder()
|
|
|
|
middleware.ServeHTTP(rr, req)
|
|
|
|
expectedHeaders := map[string]string{
|
|
"X-Frame-Options": "DENY",
|
|
"X-Content-Type-Options": "nosniff",
|
|
"X-XSS-Protection": "1; mode=block",
|
|
}
|
|
|
|
for header, expected := range expectedHeaders {
|
|
actual := rr.Header().Get(header)
|
|
if actual != expected {
|
|
t.Errorf("Header %s: expected %q, got %q", header, expected, actual)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAuthMiddleware_NoAuthHeader(t *testing.T) {
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
|
|
middleware := AuthMiddleware(handler)
|
|
|
|
req := httptest.NewRequest("GET", "/test", nil)
|
|
rr := httptest.NewRecorder()
|
|
|
|
middleware.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusUnauthorized {
|
|
t.Errorf("Expected status 401, got %d", rr.Code)
|
|
}
|
|
}
|
|
|
|
func TestAuthMiddleware_InvalidFormat(t *testing.T) {
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
|
|
middleware := AuthMiddleware(handler)
|
|
|
|
req := httptest.NewRequest("GET", "/test", nil)
|
|
req.Header.Set("Authorization", "InvalidFormat")
|
|
rr := httptest.NewRecorder()
|
|
|
|
middleware.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusUnauthorized {
|
|
t.Errorf("Expected status 401, got %d", rr.Code)
|
|
}
|
|
}
|
|
|
|
func TestAuthMiddleware_InvalidToken(t *testing.T) {
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
|
|
middleware := AuthMiddleware(handler)
|
|
|
|
req := httptest.NewRequest("GET", "/test", nil)
|
|
req.Header.Set("Authorization", "Bearer invalid.token.here")
|
|
rr := httptest.NewRecorder()
|
|
|
|
middleware.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusUnauthorized {
|
|
t.Errorf("Expected status 401, got %d", rr.Code)
|
|
}
|
|
}
|
|
|
|
func TestRequireRole_NoClaims(t *testing.T) {
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
})
|
|
|
|
middleware := RequireRole("admin")(handler)
|
|
|
|
req := httptest.NewRequest("GET", "/test", nil)
|
|
rr := httptest.NewRecorder()
|
|
|
|
middleware.ServeHTTP(rr, req)
|
|
|
|
if rr.Code != http.StatusUnauthorized {
|
|
t.Errorf("Expected status 401, got %d", rr.Code)
|
|
}
|
|
}
|