Skip to content

fix: return 422 instead of 500 for duplicate email updates on admin endpoint #2014

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions internal/api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ func (a *API) adminUserUpdate(w http.ResponseWriter, r *http.Request) error {
if err != nil {
return err
}

if params.Email != user.GetEmail() {
if user, err := models.IsDuplicatedEmail(db, params.Email, user.Aud, nil); err != nil {
return apierrors.NewInternalServerError("Database error checking email").WithInternalError(err)
} else if user != nil {
return apierrors.NewUnprocessableEntityError(apierrors.ErrorCodeEmailExists, DuplicateEmailMsg)
}
}
}

if params.Phone != "" {
Expand Down Expand Up @@ -214,7 +222,7 @@ func (a *API) adminUserUpdate(w http.ResponseWriter, r *http.Request) error {
}

var identities []models.Identity
if params.Email != "" {
if params.Email != "" && user.GetEmail() != params.Email {
if identity, terr := models.FindIdentityByIdAndProvider(tx, user.ID.String(), "email"); terr != nil && !models.IsNotFoundError(terr) {
return terr
} else if identity == nil {
Expand Down Expand Up @@ -250,7 +258,7 @@ func (a *API) adminUserUpdate(w http.ResponseWriter, r *http.Request) error {
}
}

if params.Phone != "" {
if params.Phone != "" && user.GetPhone() != params.Phone {
if identity, terr := models.FindIdentityByIdAndProvider(tx, user.ID.String(), "phone"); terr != nil && !models.IsNotFoundError(terr) {
return terr
} else if identity == nil {
Expand Down
25 changes: 25 additions & 0 deletions internal/api/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,31 @@ func (ts *AdminTestSuite) TestAdminUserUpdate() {
}
}

func (ts *AdminTestSuite) TestAdminUserUpdateDuplicateEmailFailed() {
u1, err := models.NewUser("", "", "", ts.Config.JWT.Aud, nil)
require.NoError(ts.T(), err)
require.NoError(ts.T(), ts.API.db.Create(u1))

u2, err := models.NewUser("", "[email protected]", "test", ts.Config.JWT.Aud, nil)
require.NoError(ts.T(), err)
require.NoError(ts.T(), ts.API.db.Create(u2))

ts.Run("update user with duplicate email", func() {
var buffer bytes.Buffer
require.NoError(ts.T(), json.NewEncoder(&buffer).Encode(map[string]any{
"email": u2.Email,
}))

// Setup request
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPut, fmt.Sprintf("/admin/users/%s", u1.ID), &buffer)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", ts.token))

ts.API.handler.ServeHTTP(w, req)
require.Equal(ts.T(), http.StatusUnprocessableEntity, w.Code)
})
}

func (ts *AdminTestSuite) TestAdminUserUpdatePasswordFailed() {
u, err := models.NewUser("12345678", "[email protected]", "test", ts.Config.JWT.Aud, nil)
require.NoError(ts.T(), err, "Error making new user")
Expand Down