You've already forked nginx-proxy-manager
mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2025-07-15 03:41:48 +03:00
Add more unit tests
This commit is contained in:
61
backend/internal/api/middleware/enforce_setup_test.go
Normal file
61
backend/internal/api/middleware/enforce_setup_test.go
Normal file
@ -0,0 +1,61 @@
|
||||
package middleware_test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"npm/internal/api/middleware"
|
||||
"npm/internal/config"
|
||||
)
|
||||
|
||||
func TestEnforceSetup(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
shouldBeSetup bool
|
||||
isSetup bool
|
||||
expectedCode int
|
||||
}{
|
||||
{
|
||||
name: "should allow request when setup is expected and is setup",
|
||||
shouldBeSetup: true,
|
||||
isSetup: true,
|
||||
expectedCode: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "should error when setup is expected but not setup",
|
||||
shouldBeSetup: true,
|
||||
isSetup: false,
|
||||
expectedCode: http.StatusForbidden,
|
||||
},
|
||||
{
|
||||
name: "should allow request when setup is not expected and not setup",
|
||||
shouldBeSetup: false,
|
||||
isSetup: false,
|
||||
expectedCode: http.StatusOK,
|
||||
},
|
||||
{
|
||||
name: "should error when setup is not expected but is setup",
|
||||
shouldBeSetup: false,
|
||||
isSetup: true,
|
||||
expectedCode: http.StatusForbidden,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
config.IsSetup = tt.isSetup
|
||||
|
||||
handler := middleware.EnforceSetup(tt.shouldBeSetup)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
w := httptest.NewRecorder()
|
||||
handler.ServeHTTP(w, req)
|
||||
assert.Equal(t, tt.expectedCode, w.Code)
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user