Skip to content

Commit e375cbf

Browse files
authored
rsponse 404 when delete not exist email (#15383)
fix #15357 Signed-off-by: a1012112796 <[email protected]>
1 parent 9a0858c commit e375cbf

File tree

5 files changed

+130
-5
lines changed

5 files changed

+130
-5
lines changed

integrations/api_user_email_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package integrations
6+
7+
import (
8+
"net/http"
9+
"testing"
10+
11+
api "code.gitea.io/gitea/modules/structs"
12+
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
func TestAPIListEmails(t *testing.T) {
17+
defer prepareTestEnv(t)()
18+
19+
normalUsername := "user2"
20+
session := loginUser(t, normalUsername)
21+
token := getTokenForLoggedInUser(t, session)
22+
23+
req := NewRequest(t, "GET", "/api/v1/user/emails?token="+token)
24+
resp := session.MakeRequest(t, req, http.StatusOK)
25+
26+
var emails []*api.Email
27+
DecodeJSON(t, resp, &emails)
28+
29+
assert.EqualValues(t, []*api.Email{
30+
{
31+
32+
Verified: true,
33+
Primary: true,
34+
},
35+
{
36+
37+
Verified: false,
38+
Primary: false,
39+
},
40+
}, emails)
41+
}
42+
43+
func TestAPIAddEmail(t *testing.T) {
44+
defer prepareTestEnv(t)()
45+
46+
normalUsername := "user2"
47+
session := loginUser(t, normalUsername)
48+
token := getTokenForLoggedInUser(t, session)
49+
50+
opts := api.CreateEmailOption{
51+
Emails: []string{"[email protected]"},
52+
}
53+
54+
req := NewRequestWithJSON(t, "POST", "/api/v1/user/emails?token="+token, &opts)
55+
session.MakeRequest(t, req, http.StatusUnprocessableEntity)
56+
57+
opts = api.CreateEmailOption{
58+
Emails: []string{"[email protected]"},
59+
}
60+
req = NewRequestWithJSON(t, "POST", "/api/v1/user/emails?token="+token, &opts)
61+
resp := session.MakeRequest(t, req, http.StatusCreated)
62+
63+
var emails []*api.Email
64+
DecodeJSON(t, resp, &emails)
65+
assert.EqualValues(t, []*api.Email{
66+
{
67+
68+
Verified: true,
69+
Primary: false,
70+
},
71+
}, emails)
72+
}
73+
74+
func TestAPIDeleteEmail(t *testing.T) {
75+
defer prepareTestEnv(t)()
76+
77+
normalUsername := "user2"
78+
session := loginUser(t, normalUsername)
79+
token := getTokenForLoggedInUser(t, session)
80+
81+
opts := api.DeleteEmailOption{
82+
Emails: []string{"[email protected]"},
83+
}
84+
req := NewRequestWithJSON(t, "DELETE", "/api/v1/user/emails?token="+token, &opts)
85+
session.MakeRequest(t, req, http.StatusNotFound)
86+
87+
opts = api.DeleteEmailOption{
88+
Emails: []string{"[email protected]"},
89+
}
90+
req = NewRequestWithJSON(t, "DELETE", "/api/v1/user/emails?token="+token, &opts)
91+
session.MakeRequest(t, req, http.StatusNoContent)
92+
93+
req = NewRequest(t, "GET", "/api/v1/user/emails?token="+token)
94+
resp := session.MakeRequest(t, req, http.StatusOK)
95+
96+
var emails []*api.Email
97+
DecodeJSON(t, resp, &emails)
98+
assert.EqualValues(t, []*api.Email{
99+
{
100+
101+
Verified: true,
102+
Primary: true,
103+
},
104+
}, emails)
105+
}

models/error.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,21 @@ func (err ErrEmailInvalid) Error() string {
222222
return fmt.Sprintf("e-mail invalid [email: %s]", err.Email)
223223
}
224224

225+
// ErrEmailAddressNotExist email address not exist
226+
type ErrEmailAddressNotExist struct {
227+
Email string
228+
}
229+
230+
// IsErrEmailAddressNotExist checks if an error is an ErrEmailAddressNotExist
231+
func IsErrEmailAddressNotExist(err error) bool {
232+
_, ok := err.(ErrEmailAddressNotExist)
233+
return ok
234+
}
235+
236+
func (err ErrEmailAddressNotExist) Error() string {
237+
return fmt.Sprintf("Email address does not exist [email: %s]", err.Email)
238+
}
239+
225240
// ErrOpenIDAlreadyUsed represents a "OpenIDAlreadyUsed" kind of error.
226241
type ErrOpenIDAlreadyUsed struct {
227242
OpenID string

models/user_mail.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package models
77

88
import (
9-
"errors"
109
"fmt"
1110
"net/mail"
1211
"strings"
@@ -18,9 +17,6 @@ import (
1817
"xorm.io/builder"
1918
)
2019

21-
// ErrEmailAddressNotExist email address not exist
22-
var ErrEmailAddressNotExist = errors.New("Email address does not exist")
23-
2420
// EmailAddress is the list of all email addresses of a user. Can contain the
2521
// primary email address, but is not obligatory.
2622
type EmailAddress struct {
@@ -243,7 +239,7 @@ func DeleteEmailAddress(email *EmailAddress) (err error) {
243239
if err != nil {
244240
return err
245241
} else if deleted != 1 {
246-
return ErrEmailAddressNotExist
242+
return ErrEmailAddressNotExist{Email: email.Email}
247243
}
248244
return nil
249245
}

routers/api/v1/user/email.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ func DeleteEmail(ctx *context.APIContext) {
111111
// responses:
112112
// "204":
113113
// "$ref": "#/responses/empty"
114+
// "404":
115+
// "$ref": "#/responses/notFound"
114116
form := web.GetForm(ctx).(*api.DeleteEmailOption)
115117
if len(form.Emails) == 0 {
116118
ctx.Status(http.StatusNoContent)
@@ -126,6 +128,10 @@ func DeleteEmail(ctx *context.APIContext) {
126128
}
127129

128130
if err := models.DeleteEmailAddresses(emails); err != nil {
131+
if models.IsErrEmailAddressNotExist(err) {
132+
ctx.Error(http.StatusNotFound, "DeleteEmailAddresses", err)
133+
return
134+
}
129135
ctx.Error(http.StatusInternalServerError, "DeleteEmailAddresses", err)
130136
return
131137
}

templates/swagger/v1_json.tmpl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10221,6 +10221,9 @@
1022110221
"responses": {
1022210222
"204": {
1022310223
"$ref": "#/responses/empty"
10224+
},
10225+
"404": {
10226+
"$ref": "#/responses/notFound"
1022410227
}
1022510228
}
1022610229
}

0 commit comments

Comments
 (0)