From 54cd100d8da37ccb0a545e2545995066f92180f0 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Sun, 3 Dec 2023 17:11:51 +0530 Subject: [PATCH] [GITEA] notifies admins on new user registration (squash) fix locale --- services/mailer/mail_admin_new_user.go | 2 +- services/mailer/mail_admin_new_user_test.go | 37 +++++++++++++-------- templates/mail/notify/admin_new_user.tmpl | 5 ++- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/services/mailer/mail_admin_new_user.go b/services/mailer/mail_admin_new_user.go index b5c7fd8fed..e6372c5200 100644 --- a/services/mailer/mail_admin_new_user.go +++ b/services/mailer/mail_admin_new_user.go @@ -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, } diff --git a/services/mailer/mail_admin_new_user_test.go b/services/mailer/mail_admin_new_user_test.go index e6149a6a53..9e88972ca6 100644 --- a/services/mailer/mail_admin_new_user_test.go +++ b/services/mailer/mail_admin_new_user_test.go @@ -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 diff --git a/templates/mail/notify/admin_new_user.tmpl b/templates/mail/notify/admin_new_user.tmpl index 03ef4abe21..07e7a602b2 100644 --- a/templates/mail/notify/admin_new_user.tmpl +++ b/templates/mail/notify/admin_new_user.tmpl @@ -13,9 +13,8 @@

{{.Body | Str2html}}