From 8921159fedcfc518ac71361745e797b12dc89da3 Mon Sep 17 00:00:00 2001 From: kekskurse <git@ekskurse.de> Date: Fri, 14 Mar 2025 02:52:18 +0100 Subject: [PATCH] chore: config setup for web system --- main.go | 9 ++++++++- main_test.go | 8 ++++++++ pkg/web/web.go | 25 ++++++++++++++++++++++++- pkg/web/web_test.go | 28 +++++++++++++++++++++++++++- templates/register.html | 3 +++ 5 files changed, 70 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index e0a6325..c3dda57 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "fmt" "html/template" + "git.keks.cloud/kekskurse/miniauth/pkg/miniauth" "git.keks.cloud/kekskurse/miniauth/pkg/smtpclient" "git.keks.cloud/kekskurse/miniauth/pkg/userstore" "git.keks.cloud/kekskurse/miniauth/pkg/web" @@ -52,7 +53,13 @@ func setupRouter(cfg gloableConfig) *gin.Engine { router.SetHTMLTemplate(loadTemplates()) routesWeb := router.Group("/web") - webServer := web.NewWeb(cfg.WebConfig) + us, err := userstore.NewStore(cfg.UserStoreConfig) + if err != nil { + log.Fatal().Err(err).Msg("cant init userstore") + } + ma := miniauth.NewMiniauth(us) + + webServer := web.NewWeb(cfg.WebConfig, ma) webServer.RegisterRoutes(routesWeb) router.GET("/", func(c *gin.Context) {}) diff --git a/main_test.go b/main_test.go index 91ac37c..fa7baca 100644 --- a/main_test.go +++ b/main_test.go @@ -3,13 +3,18 @@ package main import ( "net/http" "net/http/httptest" + "os" "testing" + "github.com/google/uuid" "github.com/stretchr/testify/assert" ) func TestStartpageEmpty(t *testing.T) { + name, err := os.MkdirTemp("", "miniauth-teststore") + assert.Nil(t, err, "[setup] should be abel to create tmp dir") gc := gloableConfig{} + gc.UserStoreConfig.SQLite.Path = "file:" + name + "/" + uuid.NewString() + ".sqlite" router := setupRouter(gc) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/", nil) @@ -20,7 +25,10 @@ func TestStartpageEmpty(t *testing.T) { } func TestPingRoute(t *testing.T) { + name, err := os.MkdirTemp("", "miniauth-teststore") + assert.Nil(t, err, "[setup] should be abel to create tmp dir") gc := gloableConfig{} + gc.UserStoreConfig.SQLite.Path = "file:" + name + "/" + uuid.NewString() + ".sqlite" router := setupRouter(gc) w := httptest.NewRecorder() req, _ := http.NewRequest("GET", "/ping", nil) diff --git a/pkg/web/web.go b/pkg/web/web.go index c4bb1eb..c49529b 100644 --- a/pkg/web/web.go +++ b/pkg/web/web.go @@ -3,6 +3,7 @@ package web import ( "net/http" + "git.keks.cloud/kekskurse/miniauth/pkg/miniauth" "github.com/gin-gonic/gin" ) @@ -12,16 +13,19 @@ type WebConfig struct { type Web struct { config WebConfig + ma miniauth.Miniauth } -func NewWeb(config WebConfig) Web { +func NewWeb(config WebConfig, ma miniauth.Miniauth) Web { w := Web{} w.config = config + w.ma = ma return w } func (w Web) RegisterRoutes(routing *gin.RouterGroup) error { routing.GET("/register", w.GetRegisterPage) + routing.POST("/register", w.PostRegisterPage) return nil } @@ -32,3 +36,22 @@ func (w Web) GetRegisterPage(c *gin.Context) { } c.HTML(http.StatusOK, "register.html", nil) } + +func (w Web) PostRegisterPage(c *gin.Context) { + if !w.config.PublicRegistration { + c.HTML(403, "msg.html", gin.H{"msg": "Public registration disabled"}) + return + } + + if c.PostForm("password") != c.PostForm("confirm_password") { + c.HTML(http.StatusOK, "register.html", gin.H{"msg": "Passworts dont match"}) + } + + err := w.ma.RegisterUser(c.PostForm("username"), c.PostForm("email"), c.PostForm("password")) + if err != nil { + c.HTML(http.StatusOK, "register.html", gin.H{"msg": err.Error()}) + return + } + + c.HTML(403, "msg.html", gin.H{"msg": "Your account was created, you can login now"}) +} diff --git a/pkg/web/web_test.go b/pkg/web/web_test.go index 2b40287..7a50767 100644 --- a/pkg/web/web_test.go +++ b/pkg/web/web_test.go @@ -6,6 +6,8 @@ import ( "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" ) @@ -14,7 +16,9 @@ func TestRegistrationDisabled(t *testing.T) { webConfig := WebConfig{} webConfig.PublicRegistration = false - web := NewWeb(webConfig) + 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) @@ -30,6 +34,28 @@ func TestRegistrationDisabled(t *testing.T) { 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 { diff --git a/templates/register.html b/templates/register.html index 66a53a6..ab01512 100644 --- a/templates/register.html +++ b/templates/register.html @@ -9,6 +9,9 @@ <body class="bg-gray-100 flex items-center justify-center min-h-screen"> <div class="bg-white p-8 shadow-xl w-full max-w-md"> <h2 class="text-2xl font-semibold text-gray-900 text-center mb-6">Registrieren</h2> + <div id="global-error" class=" bg-red-100 text-red-700 p-3 text-center mb-4 border border-red-400"> + {{ .msg }} + </div> <form action="#" method="POST" class="space-y-4"> <div> <label for="username" class="block text-sm font-medium text-gray-700">Benutzername</label>