Skip to content

rsponse 404 when delete not exist email #15383

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

Merged
merged 1 commit into from
Apr 10, 2021
Merged
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
105 changes: 105 additions & 0 deletions integrations/api_user_email_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2021 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package integrations

import (
"net/http"
"testing"

api "code.gitea.io/gitea/modules/structs"

"github.com/stretchr/testify/assert"
)

func TestAPIListEmails(t *testing.T) {
defer prepareTestEnv(t)()

normalUsername := "user2"
session := loginUser(t, normalUsername)
token := getTokenForLoggedInUser(t, session)

req := NewRequest(t, "GET", "/api/v1/user/emails?token="+token)
resp := session.MakeRequest(t, req, http.StatusOK)

var emails []*api.Email
DecodeJSON(t, resp, &emails)

assert.EqualValues(t, []*api.Email{
{
Email: "[email protected]",
Verified: true,
Primary: true,
},
{
Email: "[email protected]",
Verified: false,
Primary: false,
},
}, emails)
}

func TestAPIAddEmail(t *testing.T) {
defer prepareTestEnv(t)()

normalUsername := "user2"
session := loginUser(t, normalUsername)
token := getTokenForLoggedInUser(t, session)

opts := api.CreateEmailOption{
Emails: []string{"[email protected]"},
}

req := NewRequestWithJSON(t, "POST", "/api/v1/user/emails?token="+token, &opts)
session.MakeRequest(t, req, http.StatusUnprocessableEntity)

opts = api.CreateEmailOption{
Emails: []string{"[email protected]"},
}
req = NewRequestWithJSON(t, "POST", "/api/v1/user/emails?token="+token, &opts)
resp := session.MakeRequest(t, req, http.StatusCreated)

var emails []*api.Email
DecodeJSON(t, resp, &emails)
assert.EqualValues(t, []*api.Email{
{
Email: "[email protected]",
Verified: true,
Primary: false,
},
}, emails)
}

func TestAPIDeleteEmail(t *testing.T) {
defer prepareTestEnv(t)()

normalUsername := "user2"
session := loginUser(t, normalUsername)
token := getTokenForLoggedInUser(t, session)

opts := api.DeleteEmailOption{
Emails: []string{"[email protected]"},
}
req := NewRequestWithJSON(t, "DELETE", "/api/v1/user/emails?token="+token, &opts)
session.MakeRequest(t, req, http.StatusNotFound)

opts = api.DeleteEmailOption{
Emails: []string{"[email protected]"},
}
req = NewRequestWithJSON(t, "DELETE", "/api/v1/user/emails?token="+token, &opts)
session.MakeRequest(t, req, http.StatusNoContent)

req = NewRequest(t, "GET", "/api/v1/user/emails?token="+token)
resp := session.MakeRequest(t, req, http.StatusOK)

var emails []*api.Email
DecodeJSON(t, resp, &emails)
assert.EqualValues(t, []*api.Email{
{
Email: "[email protected]",
Verified: true,
Primary: true,
},
}, emails)
}
15 changes: 15 additions & 0 deletions models/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,21 @@ func (err ErrEmailInvalid) Error() string {
return fmt.Sprintf("e-mail invalid [email: %s]", err.Email)
}

// ErrEmailAddressNotExist email address not exist
type ErrEmailAddressNotExist struct {
Email string
}

// IsErrEmailAddressNotExist checks if an error is an ErrEmailAddressNotExist
func IsErrEmailAddressNotExist(err error) bool {
_, ok := err.(ErrEmailAddressNotExist)
return ok
}

func (err ErrEmailAddressNotExist) Error() string {
return fmt.Sprintf("Email address does not exist [email: %s]", err.Email)
}

// ErrOpenIDAlreadyUsed represents a "OpenIDAlreadyUsed" kind of error.
type ErrOpenIDAlreadyUsed struct {
OpenID string
Expand Down
6 changes: 1 addition & 5 deletions models/user_mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package models

import (
"errors"
"fmt"
"net/mail"
"strings"
Expand All @@ -18,9 +17,6 @@ import (
"xorm.io/builder"
)

// ErrEmailAddressNotExist email address not exist
var ErrEmailAddressNotExist = errors.New("Email address does not exist")

// EmailAddress is the list of all email addresses of a user. Can contain the
// primary email address, but is not obligatory.
type EmailAddress struct {
Expand Down Expand Up @@ -243,7 +239,7 @@ func DeleteEmailAddress(email *EmailAddress) (err error) {
if err != nil {
return err
} else if deleted != 1 {
return ErrEmailAddressNotExist
return ErrEmailAddressNotExist{Email: email.Email}
}
return nil
}
Expand Down
6 changes: 6 additions & 0 deletions routers/api/v1/user/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ func DeleteEmail(ctx *context.APIContext) {
// responses:
// "204":
// "$ref": "#/responses/empty"
// "404":
// "$ref": "#/responses/notFound"
form := web.GetForm(ctx).(*api.DeleteEmailOption)
if len(form.Emails) == 0 {
ctx.Status(http.StatusNoContent)
Expand All @@ -126,6 +128,10 @@ func DeleteEmail(ctx *context.APIContext) {
}

if err := models.DeleteEmailAddresses(emails); err != nil {
if models.IsErrEmailAddressNotExist(err) {
ctx.Error(http.StatusNotFound, "DeleteEmailAddresses", err)
return
}
ctx.Error(http.StatusInternalServerError, "DeleteEmailAddresses", err)
return
}
Expand Down
3 changes: 3 additions & 0 deletions templates/swagger/v1_json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10221,6 +10221,9 @@
"responses": {
"204": {
"$ref": "#/responses/empty"
},
"404": {
"$ref": "#/responses/notFound"
}
}
}
Expand Down