From d888e277902ab823b642ebf4e717f1eff4d4d8fd Mon Sep 17 00:00:00 2001 From: Geno Date: Wed, 30 Jun 2021 15:55:34 +0200 Subject: [PATCH] web/auth: delete my user --- web/auth/api_my_delete.go | 37 +++++++++++++++++ web/auth/api_my_delete_test.go | 41 +++++++++++++++++++ .../{api_password.go => api_my_password.go} | 0 ...ssword_test.go => api_my_password_test.go} | 0 web/auth/{api_status.go => api_my_status.go} | 4 +- ...i_status_test.go => api_my_status_test.go} | 4 +- 6 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 web/auth/api_my_delete.go create mode 100644 web/auth/api_my_delete_test.go rename web/auth/{api_password.go => api_my_password.go} (100%) rename web/auth/{api_password_test.go => api_my_password_test.go} (100%) rename web/auth/{api_status.go => api_my_status.go} (81%) rename web/auth/{api_status_test.go => api_my_status_test.go} (77%) diff --git a/web/auth/api_my_delete.go b/web/auth/api_my_delete.go new file mode 100644 index 0000000..1a00c59 --- /dev/null +++ b/web/auth/api_my_delete.go @@ -0,0 +1,37 @@ +package auth + +import ( + "net/http" + + "dev.sum7.eu/genofire/golang-lib/web" + "github.com/gin-gonic/gin" +) + +// @Summary Delete own User +// @Description delete current loggedin user +// @Tags auth +// @Accept json +// @Produce json +// @Success 200 {object} bool "true if deleted" +// @Failure 401 {object} web.HTTPError +// @Failure 500 {object} web.HTTPError +// @Router /api/v1/my/profil [delete] +// @Security ApiKeyAuth +func init() { + web.ModuleRegister(func(r *gin.Engine, ws *web.Service) { + r.DELETE("/api/v1/my/profil", func(c *gin.Context) { + id, ok := GetCurrentUserID(c) + if !ok { + return + } + if err := ws.DB.Delete(&User{ID: id}).Error; err != nil { + c.JSON(http.StatusInternalServerError, web.HTTPError{ + Message: web.APIErrorInternalDatabase, + Error: err.Error(), + }) + return + } + c.JSON(http.StatusOK, true) + }) + }) +} diff --git a/web/auth/api_my_delete_test.go b/web/auth/api_my_delete_test.go new file mode 100644 index 0000000..a5c84b3 --- /dev/null +++ b/web/auth/api_my_delete_test.go @@ -0,0 +1,41 @@ +package auth + +import ( + "net/http" + "testing" + + "github.com/stretchr/testify/assert" + + "dev.sum7.eu/genofire/golang-lib/web" + "dev.sum7.eu/genofire/golang-lib/web/webtest" +) + +func TestAPIDeleteMyProfil(t *testing.T) { + assert := assert.New(t) + s, err := webtest.New() + assert.NoError(err) + defer s.Close() + assert.NotNil(s) + SetupMigration(s.DB) + s.DB.MigrateTestdata() + + hErr := web.HTTPError{} + // invalid + err = s.Request(http.MethodDelete, "/api/v1/my/profil", nil, http.StatusUnauthorized, &hErr) + assert.NoError(err) + assert.Equal(APIErrorNoSession, hErr.Message) + + err = s.Login(webtest.Login{ + Username: "admin", + Password: "CHANGEME", + }) + assert.NoError(err) + + res := false + // company + err = s.Request(http.MethodDelete, "/api/v1/my/profil", nil, http.StatusOK, &res) + assert.NoError(err) + assert.True(true) + + s.DB.ReRun("10-data-0008-01-user") +} diff --git a/web/auth/api_password.go b/web/auth/api_my_password.go similarity index 100% rename from web/auth/api_password.go rename to web/auth/api_my_password.go diff --git a/web/auth/api_password_test.go b/web/auth/api_my_password_test.go similarity index 100% rename from web/auth/api_password_test.go rename to web/auth/api_my_password_test.go diff --git a/web/auth/api_status.go b/web/auth/api_my_status.go similarity index 81% rename from web/auth/api_status.go rename to web/auth/api_my_status.go index c4b03d4..d8cd539 100644 --- a/web/auth/api_status.go +++ b/web/auth/api_my_status.go @@ -16,11 +16,11 @@ import ( // @Success 200 {object} User // @Failure 401 {object} web.HTTPError // @Failure 500 {object} web.HTTPError -// @Router /api/v1/auth/status [get] +// @Router /api/v1/my/auth/status [get] // @Security ApiKeyAuth func init() { web.ModuleRegister(func(r *gin.Engine, ws *web.Service) { - r.GET("/api/v1/auth/status", MiddlewareLogin(ws), func(c *gin.Context) { + r.GET("/api/v1/my/auth/status", MiddlewareLogin(ws), func(c *gin.Context) { d, ok := GetCurrentUser(c, ws) if ok { c.JSON(http.StatusOK, d) diff --git a/web/auth/api_status_test.go b/web/auth/api_my_status_test.go similarity index 77% rename from web/auth/api_status_test.go rename to web/auth/api_my_status_test.go index 5a89d08..8e5b414 100644 --- a/web/auth/api_status_test.go +++ b/web/auth/api_my_status_test.go @@ -21,7 +21,7 @@ func TestAPIStatus(t *testing.T) { hErr := web.HTTPError{} // invalid - err = s.Request(http.MethodGet, "/api/v1/auth/status", nil, http.StatusUnauthorized, &hErr) + err = s.Request(http.MethodGet, "/api/v1/my/auth/status", nil, http.StatusUnauthorized, &hErr) assert.NoError(err) assert.Equal(APIErrorNoSession, hErr.Message) @@ -30,7 +30,7 @@ func TestAPIStatus(t *testing.T) { obj := User{} // invalid - user - err = s.Request(http.MethodGet, "/api/v1/auth/status", nil, http.StatusOK, &obj) + err = s.Request(http.MethodGet, "/api/v1/my/auth/status", nil, http.StatusOK, &obj) assert.NoError(err) assert.Equal("admin", obj.Username)