fixed unit testing

This commit is contained in:
2025-12-16 13:26:07 +08:00
parent 7e42d04fde
commit 5828a2ff21
7 changed files with 296 additions and 221 deletions
+22 -47
View File
@@ -2,7 +2,7 @@ package routes
import (
"authorization/db"
"authorization/handlers"
"bytes"
"database/sql"
"net/http"
"net/http/httptest"
@@ -85,35 +85,25 @@ func TestSetupRoutes_SwaggerEndpoint(t *testing.T) {
}
func TestSetupRoutes_AuthCheckEndpoint(t *testing.T) {
t.Skip("Test requires global database initialization which is difficult to mock in unit tests")
// Initialize the auth service
handlers.InitAuthService()
mockDB, mock, cleanup := setupMockDB(t)
defer cleanup()
// Mock initial cache load for auth service
permRows := sqlmock.NewRows([]string{"id", "permission_name", "description", "resource", "action"})
mock.ExpectQuery("SELECT id, permission_name, description, resource, action FROM permissions ORDER BY id").
WillReturnRows(permRows)
policyRows := sqlmock.NewRows([]string{"id", "attribute_name", "attribute_type", "comparison", "attribute_value", "permission_id"})
mock.ExpectQuery("SELECT id, attribute_name, attribute_type, comparison, attribute_value, permission_id FROM policy_attributes ORDER BY permission_id, id").
WillReturnRows(policyRows)
// Test that the auth check endpoint is properly registered
// We can test the routing without full DB initialization
router := mux.NewRouter()
SetupRoutes(router, mockDB)
SetupRoutes(router, nil) // Pass nil DB, handlers should handle it gracefully
req := httptest.NewRequest("POST", "/v1/auth/check", nil)
req := httptest.NewRequest("POST", "/v1/auth/check", bytes.NewBufferString(`{"userId":"user123","resource":"doc","action":"read"}`))
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
// Should return 401 or 400 (no JWT or invalid request) not 404
// Endpoint is registered (status won't be 404)
if w.Code == http.StatusNotFound {
t.Error("Auth check endpoint should be registered")
}
// Will likely return 401 (no JWT) or 500 (no DB) but that's OK - route exists
if w.Code != http.StatusUnauthorized && w.Code != http.StatusInternalServerError && w.Code != http.StatusForbidden {
t.Logf("Auth check returned status %d (expected 401, 403, or 500 without proper setup)", w.Code)
}
}
func TestSetupRoutes_MethodRestrictions(t *testing.T) {
@@ -259,45 +249,30 @@ func TestSetupRoutes_MultipleInitializations(t *testing.T) {
}
func TestSetupRoutes_AllEndpoints(t *testing.T) {
t.Skip("Test requires global database initialization which is difficult to mock in unit tests")
mockDB, mock, cleanup := setupMockDB(t)
defer cleanup()
handlers.InitAuthService()
// Mock initial cache load
permRows := sqlmock.NewRows([]string{"id", "permission_name", "description", "resource", "action"})
mock.ExpectQuery("SELECT id, permission_name, description, resource, action FROM permissions ORDER BY id").
WillReturnRows(permRows)
policyRows := sqlmock.NewRows([]string{"id", "attribute_name", "attribute_type", "comparison", "attribute_value", "permission_id"})
mock.ExpectQuery("SELECT id, attribute_name, attribute_type, comparison, attribute_value, permission_id FROM policy_attributes ORDER BY permission_id, id").
WillReturnRows(policyRows)
// Test that all endpoints are properly registered
router := mux.NewRouter()
SetupRoutes(router, mockDB)
SetupRoutes(router, nil)
endpoints := []struct {
method string
path string
name string
}{
{"GET", "/health", "Health check"},
{"GET", "/ready", "Ready check"},
{"POST", "/v1/auth/check", "Authorization check"},
{"GET", "/swagger/", "Swagger UI"},
{"GET", "/health"},
{"GET", "/ready"},
{"POST", "/v1/auth/check"},
{"GET", "/swagger/"},
}
for _, endpoint := range endpoints {
t.Run(endpoint.name, func(t *testing.T) {
req := httptest.NewRequest(endpoint.method, endpoint.path, nil)
for _, ep := range endpoints {
t.Run(ep.method+" "+ep.path, func(t *testing.T) {
req := httptest.NewRequest(ep.method, ep.path, nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
// Endpoint should be registered (not 404 or 405)
if w.Code == http.StatusNotFound {
t.Errorf("Endpoint %s %s should exist", endpoint.method, endpoint.path)
t.Errorf("Endpoint %s %s should be registered", ep.method, ep.path)
}
})
}