miniauth/pkg/web/web_test.go
2025-03-14 02:52:18 +01:00

65 lines
1.6 KiB
Go

package web
import (
"html/template"
"net/http"
"net/http/httptest"
"testing"
"git.keks.cloud/kekskurse/miniauth/pkg/miniauth"
"git.keks.cloud/kekskurse/miniauth/pkg/userstore"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
)
func TestRegistrationDisabled(t *testing.T) {
webConfig := WebConfig{}
webConfig.PublicRegistration = false
us, err := userstore.NewDummyStore()
assert.Nil(t, err, "[setup] should be abel to create userstore")
web := NewWeb(webConfig, miniauth.NewMiniauth(us))
router := gin.New()
loadTemplates(router)
grpup := router.Group("/web")
web.RegisterRoutes(grpup)
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/web/register", nil)
router.ServeHTTP(w, req)
assert.Equal(t, 403, w.Code)
content := string(w.Body.Bytes())
assert.Contains(t, content, "Public registration disabled")
}
func TestRegistrationDisabledOnPost(t *testing.T) {
webConfig := WebConfig{}
webConfig.PublicRegistration = false
us, err := userstore.NewDummyStore()
assert.Nil(t, err, "[setup] should be abel to create userstore")
web := NewWeb(webConfig, miniauth.NewMiniauth(us))
router := gin.New()
loadTemplates(router)
grpup := router.Group("/web")
web.RegisterRoutes(grpup)
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", "/web/register", nil)
router.ServeHTTP(w, req)
assert.Equal(t, 403, w.Code)
content := string(w.Body.Bytes())
assert.Contains(t, content, "Public registration disabled")
}
func loadTemplates(r *gin.Engine) {
tmpl, err := template.ParseGlob("../../templates/*.html")
if err != nil {
panic(err) // Handle Fehler entsprechend
}
r.SetHTMLTemplate(tmpl)
}