return UpdateUserCols(db.DefaultContext, u, "theme")
}
+// GetPlaceholderEmail returns an noreply email
+func (u *User) GetPlaceholderEmail() string {
+ return fmt.Sprintf("%s@%s", u.LowerName, setting.Service.NoReplyAddress)
+}
+
// GetEmail returns an noreply email, if the user has set to keep his
// email address private, otherwise the primary email address.
func (u *User) GetEmail() string {
if u.KeepEmailPrivate {
- return fmt.Sprintf("%s@%s", u.LowerName, setting.Service.NoReplyAddress)
+ return u.GetPlaceholderEmail()
}
return u.Email
}
ID: user.ID,
UserName: user.Name,
FullName: user.FullName,
- Email: user.GetEmail(),
+ Email: user.GetPlaceholderEmail(),
AvatarURL: user.AvatarLink(ctx),
Created: user.CreatedUnix.AsTime(),
Restricted: user.IsRestricted,
"testing"
auth_model "code.gitea.io/gitea/models/auth"
+ "code.gitea.io/gitea/models/unittest"
+ user_model "code.gitea.io/gitea/models/user"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/tests"
user := "user1"
user2 := "user31"
+ user3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user3"})
+
session := loginUser(t, user)
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeReadUser)
req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s", user2))
MakeRequest(t, req, http.StatusNotFound)
+
+ // test if the placaholder Mail is returned if a User is not logged in
+ req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s", user3.Name))
+ resp = MakeRequest(t, req, http.StatusOK)
+ DecodeJSON(t, resp, &u)
+ assert.Equal(t, user3.GetPlaceholderEmail(), u.Email)
+
+ // Test if the correct Mail is returned if a User is logged in
+ req = NewRequest(t, "GET", fmt.Sprintf("/api/v1/users/%s?token=%s", user3.Name, token))
+ resp = MakeRequest(t, req, http.StatusOK)
+ DecodeJSON(t, resp, &u)
+ assert.Equal(t, user3.GetEmail(), u.Email)
})
t.Run("GetAuthenticatedUser", func(t *testing.T) {
package integration
import (
- "fmt"
"net/http"
"testing"
auth_model "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
- "code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/tests"
for _, user := range results.Data {
assert.Contains(t, user.UserName, query)
modelUser = unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: user.ID})
- if modelUser.KeepEmailPrivate {
- assert.EqualValues(t, fmt.Sprintf("%s@%s", modelUser.LowerName, setting.Service.NoReplyAddress), user.Email)
- } else {
- assert.EqualValues(t, modelUser.Email, user.Email)
- }
+ assert.EqualValues(t, modelUser.GetPlaceholderEmail(), user.Email)
}
}