2fas-server/internal/common/security/middleware_test.go

29 lines
678 B
Go
Raw Normal View History

2022-12-31 10:22:38 +01:00
package security
import (
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
2023-01-30 19:59:42 +01:00
"github.com/twofas/2fas-server/config"
2023-04-11 23:35:16 +02:00
httpsec "github.com/twofas/2fas-server/internal/common/http"
2022-12-31 10:22:38 +01:00
"net/http"
"net/http/httptest"
"testing"
)
func Test_DoNotAllowUntrustedIp(t *testing.T) {
c := config.SecurityConfig{TrustedIP: []string{
"192.168.0.1/32",
}}
2023-04-11 23:35:16 +02:00
whitelistMiddleware := httpsec.IPWhitelistMiddleware(c)
2022-12-31 10:22:38 +01:00
recorder := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(recorder)
ctx.Request, _ = http.NewRequest("POST", "/", nil)
ctx.Request.Header.Set("X-Forwarded-For", "192.168.0.2")
whitelistMiddleware(ctx)
assert.Equal(t, recorder.Code, 401)
}