mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-24 05:02:39 +00:00

Send a Mail when an action run fails or a workflow recovers. This PR depends on https://codeberg.org/forgejo/forgejo/pulls/7491 closes #3719 ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [x] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] I do not want this change to show in the release notes. - [x] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. <!--start release-notes-assistant--> ## Release notes <!--URL:https://codeberg.org/forgejo/forgejo--> - Features - [PR](https://codeberg.org/forgejo/forgejo/pulls/7509): <!--number 7509 --><!--line 0 --><!--description c2VuZCBtYWlsIG9uIGZhaWxlZCBvciByZWNvdmVyZWQgd29ya2Zsb3cgcnVu-->send mail on failed or recovered workflow run<!--description--> <!--end release-notes-assistant--> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/7509 Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org> Co-authored-by: christopher-besch <mail@chris-besch.com> Co-committed-by: christopher-besch <mail@chris-besch.com>
72 lines
2.3 KiB
Go
72 lines
2.3 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package mailer
|
|
|
|
import (
|
|
"strconv"
|
|
"testing"
|
|
|
|
"forgejo.org/models/db"
|
|
user_model "forgejo.org/models/user"
|
|
"forgejo.org/modules/setting"
|
|
"forgejo.org/modules/test"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func getAdminNewUserTestUsers(t *testing.T) []*user_model.User {
|
|
t.Helper()
|
|
admin := new(user_model.User)
|
|
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"
|
|
newUser.Language = "en_US"
|
|
newUser.IsAdmin = false
|
|
newUser.Email = "new_user@example.com"
|
|
newUser.LastLoginUnix = 1693648327
|
|
newUser.CreatedUnix = 1693648027
|
|
require.NoError(t, user_model.CreateUser(db.DefaultContext, newUser))
|
|
|
|
return []*user_model.User{admin, newUser}
|
|
}
|
|
|
|
func TestAdminNotificationMail_test(t *testing.T) {
|
|
ctx := t.Context()
|
|
|
|
users := getAdminNewUserTestUsers(t)
|
|
|
|
t.Run("SendNotificationEmailOnNewUser_true", func(t *testing.T) {
|
|
defer test.MockVariableValue(&setting.Admin.SendNotificationEmailOnNewUser, true)()
|
|
|
|
called := false
|
|
defer MockMailSettings(func(msgs ...*Message) {
|
|
assert.Len(t, 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 := setting.AppURL + "admin/users/" + strconv.FormatInt(users[1].ID, 10)
|
|
assert.Contains(t, msgs[0].Body, manageUserURL)
|
|
assert.Contains(t, msgs[0].Body, users[1].HTMLURL())
|
|
assert.Contains(t, msgs[0].Body, users[1].Name, "user name of the newly created user")
|
|
AssertTranslatedLocale(t, msgs[0].Body, "mail.admin", "admin.users")
|
|
called = true
|
|
})()
|
|
MailNewUser(ctx, users[1])
|
|
assert.True(t, called)
|
|
})
|
|
|
|
t.Run("SendNotificationEmailOnNewUser_false", func(t *testing.T) {
|
|
defer test.MockVariableValue(&setting.Admin.SendNotificationEmailOnNewUser, false)()
|
|
defer MockMailSettings(func(msgs ...*Message) {
|
|
assert.Equal(t, 1, 0, "this shouldn't execute. MailNewUser must exit early since SEND_NOTIFICATION_EMAIL_ON_NEW_USER is disabled")
|
|
})()
|
|
MailNewUser(ctx, users[1])
|
|
})
|
|
|
|
CleanUpUsers(ctx, users)
|
|
}
|