fix #15357
Signed-off-by: a101211279
<1012112796@qq.com>
tags/v1.15.0-rc1
// 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: "user2@example.com", | |||||
Verified: true, | |||||
Primary: true, | |||||
}, | |||||
{ | |||||
Email: "user21@example.com", | |||||
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{"user101@example.com"}, | |||||
} | |||||
req := NewRequestWithJSON(t, "POST", "/api/v1/user/emails?token="+token, &opts) | |||||
session.MakeRequest(t, req, http.StatusUnprocessableEntity) | |||||
opts = api.CreateEmailOption{ | |||||
Emails: []string{"user22@example.com"}, | |||||
} | |||||
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: "user22@example.com", | |||||
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{"user22@example.com"}, | |||||
} | |||||
req := NewRequestWithJSON(t, "DELETE", "/api/v1/user/emails?token="+token, &opts) | |||||
session.MakeRequest(t, req, http.StatusNotFound) | |||||
opts = api.DeleteEmailOption{ | |||||
Emails: []string{"user21@example.com"}, | |||||
} | |||||
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: "user2@example.com", | |||||
Verified: true, | |||||
Primary: true, | |||||
}, | |||||
}, emails) | |||||
} |
return fmt.Sprintf("e-mail invalid [email: %s]", err.Email) | 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. | // ErrOpenIDAlreadyUsed represents a "OpenIDAlreadyUsed" kind of error. | ||||
type ErrOpenIDAlreadyUsed struct { | type ErrOpenIDAlreadyUsed struct { | ||||
OpenID string | OpenID string |
package models | package models | ||||
import ( | import ( | ||||
"errors" | |||||
"fmt" | "fmt" | ||||
"net/mail" | "net/mail" | ||||
"strings" | "strings" | ||||
"xorm.io/builder" | "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 | // EmailAddress is the list of all email addresses of a user. Can contain the | ||||
// primary email address, but is not obligatory. | // primary email address, but is not obligatory. | ||||
type EmailAddress struct { | type EmailAddress struct { | ||||
if err != nil { | if err != nil { | ||||
return err | return err | ||||
} else if deleted != 1 { | } else if deleted != 1 { | ||||
return ErrEmailAddressNotExist | |||||
return ErrEmailAddressNotExist{Email: email.Email} | |||||
} | } | ||||
return nil | return nil | ||||
} | } |
// responses: | // responses: | ||||
// "204": | // "204": | ||||
// "$ref": "#/responses/empty" | // "$ref": "#/responses/empty" | ||||
// "404": | |||||
// "$ref": "#/responses/notFound" | |||||
form := web.GetForm(ctx).(*api.DeleteEmailOption) | form := web.GetForm(ctx).(*api.DeleteEmailOption) | ||||
if len(form.Emails) == 0 { | if len(form.Emails) == 0 { | ||||
ctx.Status(http.StatusNoContent) | ctx.Status(http.StatusNoContent) | ||||
} | } | ||||
if err := models.DeleteEmailAddresses(emails); err != nil { | if err := models.DeleteEmailAddresses(emails); err != nil { | ||||
if models.IsErrEmailAddressNotExist(err) { | |||||
ctx.Error(http.StatusNotFound, "DeleteEmailAddresses", err) | |||||
return | |||||
} | |||||
ctx.Error(http.StatusInternalServerError, "DeleteEmailAddresses", err) | ctx.Error(http.StatusInternalServerError, "DeleteEmailAddresses", err) | ||||
return | return | ||||
} | } |
"responses": { | "responses": { | ||||
"204": { | "204": { | ||||
"$ref": "#/responses/empty" | "$ref": "#/responses/empty" | ||||
}, | |||||
"404": { | |||||
"$ref": "#/responses/notFound" | |||||
} | } | ||||
} | } | ||||
} | } |