Merge pull request '[GITEA] notifies admins on new user registration (squash) fix locale' (#1870) from realaravinth/forgejo:fix-1852 into forgejo-dependency

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/1870
This commit is contained in:
Earl Warren 2023-12-06 17:54:40 +00:00
commit bcd1cd3bc4
3 changed files with 27 additions and 17 deletions

View file

@ -59,7 +59,7 @@ func mailNewUser(ctx context.Context, u *user_model.User, lang string, tos []str
"Subject": subject, "Subject": subject,
"Body": body, "Body": body,
"Language": locale.Language(), "Language": locale.Language(),
"locale": locale, "Locale": locale,
"Str2html": templates.Str2html, "Str2html": templates.Str2html,
} }

View file

@ -6,22 +6,25 @@ package mailer
import ( import (
"context" "context"
"strconv" "strconv"
"strings"
"testing" "testing"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/translation"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func getTestUsers() []*user_model.User { func getTestUsers(t *testing.T) []*user_model.User {
t.Helper()
admin := new(user_model.User) admin := new(user_model.User)
admin.Name = "admin" admin.Name = "testadmin"
admin.IsAdmin = true admin.IsAdmin = true
admin.Language = "en_US" admin.Language = "en_US"
admin.Email = "admin@example.com" admin.Email = "admin@example.com"
require.NoError(t, user_model.CreateUser(db.DefaultContext, admin))
newUser := new(user_model.User) newUser := new(user_model.User)
newUser.Name = "new_user" newUser.Name = "new_user"
@ -30,15 +33,9 @@ func getTestUsers() []*user_model.User {
newUser.Email = "new_user@example.com" newUser.Email = "new_user@example.com"
newUser.LastLoginUnix = 1693648327 newUser.LastLoginUnix = 1693648327
newUser.CreatedUnix = 1693648027 newUser.CreatedUnix = 1693648027
require.NoError(t, user_model.CreateUser(db.DefaultContext, newUser))
user_model.CreateUser(db.DefaultContext, admin) return []*user_model.User{admin, newUser}
user_model.CreateUser(db.DefaultContext, newUser)
users := make([]*user_model.User, 0)
users = append(users, admin)
users = append(users, newUser)
return users
} }
func cleanUpUsers(ctx context.Context, users []*user_model.User) { func cleanUpUsers(ctx context.Context, users []*user_model.User) {
@ -48,6 +45,12 @@ func cleanUpUsers(ctx context.Context, users []*user_model.User) {
} }
func TestAdminNotificationMail_test(t *testing.T) { func TestAdminNotificationMail_test(t *testing.T) {
translation.InitLocales(context.Background())
locale := translation.NewLocale("")
key := "mail.admin.new_user.user_info"
translatedKey := locale.Tr(key)
require.NotEqualValues(t, key, translatedKey)
mailService := setting.Mailer{ mailService := setting.Mailer{
From: "test@example.com", From: "test@example.com",
Protocol: "dummy", Protocol: "dummy",
@ -63,20 +66,28 @@ func TestAdminNotificationMail_test(t *testing.T) {
ctx := context.Background() ctx := context.Background()
NewContext(ctx) NewContext(ctx)
users := getTestUsers() users := getTestUsers(t)
oldSendAsync := sa oldSendAsync := sa
defer func() { defer func() {
sa = oldSendAsync sa = oldSendAsync
cleanUpUsers(ctx, users) cleanUpUsers(ctx, users)
}() }()
called := false
sa = func(msgs ...*Message) { sa = func(msgs ...*Message) {
assert.Equal(t, len(msgs), 1, "Test provides only one admin user, so only one email must be sent") assert.Equal(t, len(msgs), 1, "Test provides only one admin user, so only one email must be sent")
assert.Equal(t, msgs[0].To, users[0].Email, "checks if the recipient is the admin of the instance") assert.Equal(t, msgs[0].To, users[0].Email, "checks if the recipient is the admin of the instance")
manageUserURL := "/admin/users/" + strconv.FormatInt(users[1].ID, 10) manageUserURL := "/admin/users/" + strconv.FormatInt(users[1].ID, 10)
assert.True(t, strings.ContainsAny(msgs[0].Body, manageUserURL), "checks if the message contains the link to manage the newly created user from the admin panel") assert.Contains(t, msgs[0].Body, manageUserURL)
assert.Contains(t, msgs[0].Body, translatedKey, "the .Locale translates to nothing")
assert.Contains(t, msgs[0].Body, users[1].Name, "user name of the newly created user")
for _, untranslated := range []string{"mail.admin", "admin.users"} {
assert.NotContains(t, msgs[0].Body, untranslated, "this is an untranslated placeholder prefix")
}
called = true
} }
MailNewUser(ctx, users[1]) MailNewUser(ctx, users[1])
assert.True(t, called)
// test with SEND_NOTIFICATION_EMAIL_ON_NEW_USER disabled; emails shouldn't be sent // test with SEND_NOTIFICATION_EMAIL_ON_NEW_USER disabled; emails shouldn't be sent
setting.Admin.SendNotificationEmailOnNewUser = false setting.Admin.SendNotificationEmailOnNewUser = false

View file

@ -13,9 +13,8 @@
<body> <body>
<ul> <ul>
<h3>{{ctx.Locale.Tr "mail.admin.new_user.user_info"}}</h3> <h3>{{.Locale.Tr "mail.admin.new_user.user_info" | Str2html}}: {{.NewUser.Name}}</h3>
<li>{{ctx.Locale.Tr "admin.users.created"}}: {{DateTime "full" .NewUser.LastLoginUnix}}</li> <li>{{.Locale.Tr "admin.users.created" | Str2html}}: {{DateTime "full" .NewUser.CreatedUnix}}</li>
<li>{{ctx.Locale.Tr "admin.users.last_login"}}: {{DateTime "full" .NewUser.CreatedUnix}}</li>
</ul> </ul>
<p> {{.Body | Str2html}} </p> <p> {{.Body | Str2html}} </p>
</body> </body>