[GITEA] notifies admins on new user registration (squash) fix locale

(cherry picked from commit 54cd100d8d)
This commit is contained in:
Aravinth Manivannan 2023-12-03 17:11:51 +05:30 committed by Earl Warren
parent 5ea7df79ad
commit 053dbd3d50
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
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,
"Body": body,
"Language": locale.Language(),
"locale": locale,
"Locale": locale,
"Str2html": templates.Str2html,
}

View file

@ -6,22 +6,25 @@ package mailer
import (
"context"
"strconv"
"strings"
"testing"
"code.gitea.io/gitea/models/db"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/translation"
"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.Name = "admin"
admin.Name = "testadmin"
admin.IsAdmin = true
admin.Language = "en_US"
admin.Email = "admin@example.com"
require.NoError(t, user_model.CreateUser(db.DefaultContext, admin))
newUser := new(user_model.User)
newUser.Name = "new_user"
@ -30,15 +33,9 @@ func getTestUsers() []*user_model.User {
newUser.Email = "new_user@example.com"
newUser.LastLoginUnix = 1693648327
newUser.CreatedUnix = 1693648027
require.NoError(t, user_model.CreateUser(db.DefaultContext, newUser))
user_model.CreateUser(db.DefaultContext, admin)
user_model.CreateUser(db.DefaultContext, newUser)
users := make([]*user_model.User, 0)
users = append(users, admin)
users = append(users, newUser)
return users
return []*user_model.User{admin, newUser}
}
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) {
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{
From: "test@example.com",
Protocol: "dummy",
@ -63,20 +66,28 @@ func TestAdminNotificationMail_test(t *testing.T) {
ctx := context.Background()
NewContext(ctx)
users := getTestUsers()
users := getTestUsers(t)
oldSendAsync := sa
defer func() {
sa = oldSendAsync
cleanUpUsers(ctx, users)
}()
called := false
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, 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)
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])
assert.True(t, called)
// test with SEND_NOTIFICATION_EMAIL_ON_NEW_USER disabled; emails shouldn't be sent
setting.Admin.SendNotificationEmailOnNewUser = false

View file

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