Skip to content

Commit

Permalink
Fixed send to email not address not being saved when creating a new user
Browse files Browse the repository at this point in the history
  • Loading branch information
svera committed May 12, 2024
1 parent 22593ab commit fb2bac1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
13 changes: 7 additions & 6 deletions internal/webserver/controller/user/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import (
func (u *Controller) Create(c *fiber.Ctx) error {
role, _ := strconv.Atoi(c.FormValue("role"))
user := model.User{
Name: c.FormValue("name"),
Username: strings.ToLower(c.FormValue("username")),
Email: c.FormValue("email"),
Password: c.FormValue("password"),
Role: role,
Uuid: uuid.NewString(),
Name: c.FormValue("name"),
Username: strings.ToLower(c.FormValue("username")),
Email: c.FormValue("email"),
SendToEmail: c.FormValue("send-to-email"),
Password: c.FormValue("password"),
Role: role,
Uuid: uuid.NewString(),
}
user.WordsPerMinute, _ = strconv.ParseFloat(c.FormValue("words-per-minute"), 64)

Expand Down
37 changes: 19 additions & 18 deletions internal/webserver/user_management_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func TestUserManagement(t *testing.T) {
)

reset := func() {
t.Helper()

var err error
db = infrastructure.Connect("file::memory:", 250)
app = bootstrapApp(db, &infrastructure.NoEmail{}, afero.NewMemMapFs(), webserver.Config{})
Expand Down Expand Up @@ -65,10 +67,8 @@ func TestUserManagement(t *testing.T) {
}
}

reset()

t.Run("Try to add a user without an active session", func(t *testing.T) {
t.Cleanup(reset)
reset()

response, err := getRequest(&http.Cookie{}, app, "/en/users/new", t)
if response == nil {
Expand Down Expand Up @@ -96,7 +96,7 @@ func TestUserManagement(t *testing.T) {
})

t.Run("Try to add a user with an admin active session", func(t *testing.T) {
t.Cleanup(reset)
reset()

response, err := getRequest(adminCookie, app, "/en/users/new", t)
if response == nil {
Expand All @@ -109,6 +109,7 @@ func TestUserManagement(t *testing.T) {
"name": {"New user"},
"username": {"new"},
"email": {"new@example.com"},
"send-to-email": {"send@example.com"},
"password": {"new"},
"confirm-password": {"new"},
"role": {fmt.Sprint(model.RoleRegular)},
Expand All @@ -130,7 +131,7 @@ func TestUserManagement(t *testing.T) {
})

t.Run("Try to add a user with a regular user active session", func(t *testing.T) {
t.Cleanup(reset)
reset()

response, err := getRequest(regularUserCookie, app, "/en/users/new", t)
if response == nil {
Expand All @@ -148,7 +149,7 @@ func TestUserManagement(t *testing.T) {
})

t.Run("Try to add a user with errors in form", func(t *testing.T) {
t.Cleanup(reset)
reset()

response, err := postRequest(url.Values{}, adminCookie, app, "/en/users/new", t)
expectedErrorMessages := []string{
Expand All @@ -167,7 +168,7 @@ func TestUserManagement(t *testing.T) {
})

t.Run("Try to add a user with already registered email and username", func(t *testing.T) {
t.Cleanup(reset)
reset()

newUserData := url.Values{
"name": {"Test user"},
Expand All @@ -192,7 +193,7 @@ func TestUserManagement(t *testing.T) {
})

t.Run("Try to update a user without an active session", func(t *testing.T) {
t.Cleanup(reset)
reset()

response, err := getRequest(&http.Cookie{}, app, fmt.Sprintf("/en/users/%s/edit", regularUser.Username), t)
if response == nil {
Expand All @@ -210,7 +211,7 @@ func TestUserManagement(t *testing.T) {
})

t.Run("Try to update a user using another, non admin user session", func(t *testing.T) {
t.Cleanup(reset)
reset()

adminUserData := regularUserData
adminUserData.Set("id", adminUser.Uuid)
Expand All @@ -231,7 +232,7 @@ func TestUserManagement(t *testing.T) {
})

t.Run("Try to update the user in session", func(t *testing.T) {
t.Cleanup(reset)
reset()

regularUserData.Set("name", "Updated regular user")
regularUserData.Set("id", regularUser.Uuid)
Expand All @@ -255,7 +256,7 @@ func TestUserManagement(t *testing.T) {
})

t.Run("Try to update a user with an admin session", func(t *testing.T) {
t.Cleanup(reset)
reset()

regularUserData.Set("name", "Updated regular user by an admin")
regularUserData.Set("id", regularUser.Uuid)
Expand All @@ -279,7 +280,7 @@ func TestUserManagement(t *testing.T) {
})

t.Run("Try to edit a non existing user with an admin session", func(t *testing.T) {
t.Cleanup(reset)
reset()

response, err := getRequest(adminCookie, app, fmt.Sprintf("/en/users/%s/edit", "abcde"), t)
if response == nil {
Expand All @@ -289,7 +290,7 @@ func TestUserManagement(t *testing.T) {
})

t.Run("Try to update a non existing user with an admin session", func(t *testing.T) {
t.Cleanup(reset)
reset()

regularUserData.Set("name", "Updated test user by an admin")

Expand All @@ -301,7 +302,7 @@ func TestUserManagement(t *testing.T) {
})

t.Run("Try to delete a user without an active session", func(t *testing.T) {
t.Cleanup(reset)
reset()

regularUserData = url.Values{
"id": {regularUser.Uuid},
Expand All @@ -316,7 +317,7 @@ func TestUserManagement(t *testing.T) {
})

t.Run("Try to delete a user with a regular user's session", func(t *testing.T) {
t.Cleanup(reset)
reset()

regularUserData = url.Values{
"id": {regularUser.Uuid},
Expand All @@ -333,7 +334,7 @@ func TestUserManagement(t *testing.T) {
})

t.Run("Try to delete a user with an admin session", func(t *testing.T) {
t.Cleanup(reset)
reset()

regularUserData = url.Values{
"id": {regularUser.Uuid},
Expand All @@ -352,7 +353,7 @@ func TestUserManagement(t *testing.T) {
})

t.Run("Try to delete the only existing admin user", func(t *testing.T) {
t.Cleanup(reset)
reset()

regularUserData = url.Values{
"id": {adminUser.Uuid},
Expand All @@ -366,7 +367,7 @@ func TestUserManagement(t *testing.T) {
})

t.Run("Try to delete a non existing user with an admin session", func(t *testing.T) {
t.Cleanup(reset)
reset()

regularUserData = url.Values{
"id": {"abcde"},
Expand Down

0 comments on commit fb2bac1

Please sign in to comment.