83 lines
1.9 KiB
Go
83 lines
1.9 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestMaintenanceGin_Disabled(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
SetMaintenanceMode(false)
|
|
defer SetMaintenanceMode(false)
|
|
|
|
router := gin.New()
|
|
router.Use(MaintenanceGin())
|
|
router.GET("/api/v1/dashboard", func(c *gin.Context) {
|
|
c.Status(http.StatusOK)
|
|
})
|
|
|
|
req := httptest.NewRequest("GET", "/api/v1/dashboard", nil)
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
}
|
|
|
|
func TestMaintenanceGin_Enabled_Returns503(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
SetMaintenanceMode(true)
|
|
defer SetMaintenanceMode(false)
|
|
|
|
router := gin.New()
|
|
router.Use(MaintenanceGin())
|
|
router.GET("/api/v1/dashboard", func(c *gin.Context) {
|
|
c.Status(http.StatusOK)
|
|
})
|
|
|
|
req := httptest.NewRequest("GET", "/api/v1/dashboard", nil)
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusServiceUnavailable, w.Code)
|
|
assert.Contains(t, w.Body.String(), "maintenance")
|
|
}
|
|
|
|
func TestMaintenanceGin_HealthExempt(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
SetMaintenanceMode(true)
|
|
defer SetMaintenanceMode(false)
|
|
|
|
router := gin.New()
|
|
router.Use(MaintenanceGin())
|
|
router.GET("/health", func(c *gin.Context) {
|
|
c.Status(http.StatusOK)
|
|
})
|
|
|
|
req := httptest.NewRequest("GET", "/health", nil)
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
}
|
|
|
|
func TestMaintenanceGin_AdminExempt(t *testing.T) {
|
|
gin.SetMode(gin.TestMode)
|
|
SetMaintenanceMode(true)
|
|
defer SetMaintenanceMode(false)
|
|
|
|
router := gin.New()
|
|
router.Use(MaintenanceGin())
|
|
router.GET("/api/v1/admin/reports", func(c *gin.Context) {
|
|
c.Status(http.StatusOK)
|
|
})
|
|
|
|
req := httptest.NewRequest("GET", "/api/v1/admin/reports", nil)
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, req)
|
|
|
|
assert.Equal(t, http.StatusOK, w.Code)
|
|
}
|