(t *testing.T)
| 216 | } |
| 217 | |
| 218 | func TestUpdateUserPassword(t *testing.T) { |
| 219 | t.Run("success", func(t *testing.T) { |
| 220 | db := testutils.InitMemoryDB(t) |
| 221 | |
| 222 | user := testutils.SetupUserData(db, "alice@example.com", "oldpassword123") |
| 223 | |
| 224 | err := UpdateUserPassword(db, &user, "newpassword123") |
| 225 | |
| 226 | assert.Equal(t, err, nil, "should not error") |
| 227 | |
| 228 | // Verify password was updated in database |
| 229 | var updatedUser database.User |
| 230 | testutils.MustExec(t, db.Where("id = ?", user.ID).First(&updatedUser), "finding updated user") |
| 231 | |
| 232 | // Verify new password works |
| 233 | passwordErr := bcrypt.CompareHashAndPassword([]byte(updatedUser.Password.String), []byte("newpassword123")) |
| 234 | assert.Equal(t, passwordErr, nil, "New password should match") |
| 235 | |
| 236 | // Verify old password no longer works |
| 237 | oldPasswordErr := bcrypt.CompareHashAndPassword([]byte(updatedUser.Password.String), []byte("oldpassword123")) |
| 238 | assert.NotEqual(t, oldPasswordErr, nil, "Old password should not match") |
| 239 | }) |
| 240 | |
| 241 | t.Run("password too short", func(t *testing.T) { |
| 242 | db := testutils.InitMemoryDB(t) |
| 243 | |
| 244 | user := testutils.SetupUserData(db, "alice@example.com", "oldpassword123") |
| 245 | |
| 246 | err := UpdateUserPassword(db, &user, "short") |
| 247 | |
| 248 | assert.Equal(t, err, ErrPasswordTooShort, "should return ErrPasswordTooShort") |
| 249 | |
| 250 | // Verify password was NOT updated in database |
| 251 | var unchangedUser database.User |
| 252 | testutils.MustExec(t, db.Where("id = ?", user.ID).First(&unchangedUser), "finding unchanged user") |
| 253 | |
| 254 | // Verify old password still works |
| 255 | passwordErr := bcrypt.CompareHashAndPassword([]byte(unchangedUser.Password.String), []byte("oldpassword123")) |
| 256 | assert.Equal(t, passwordErr, nil, "Old password should still match") |
| 257 | }) |
| 258 | |
| 259 | t.Run("empty password", func(t *testing.T) { |
| 260 | db := testutils.InitMemoryDB(t) |
| 261 | |
| 262 | user := testutils.SetupUserData(db, "alice@example.com", "oldpassword123") |
| 263 | |
| 264 | err := UpdateUserPassword(db, &user, "") |
| 265 | |
| 266 | assert.Equal(t, err, ErrPasswordTooShort, "should return ErrPasswordTooShort") |
| 267 | |
| 268 | // Verify password was NOT updated in database |
| 269 | var unchangedUser database.User |
| 270 | testutils.MustExec(t, db.Where("id = ?", user.ID).First(&unchangedUser), "finding unchanged user") |
| 271 | |
| 272 | // Verify old password still works |
| 273 | passwordErr := bcrypt.CompareHashAndPassword([]byte(unchangedUser.Password.String), []byte("oldpassword123")) |
| 274 | assert.Equal(t, passwordErr, nil, "Old password should still match") |
| 275 | }) |
nothing calls this directly
no test coverage detected