|
| 1 | +// Copyright 2025 The Forgejo Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | + |
| 4 | +package mailer |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + actions_model "forgejo.org/models/actions" |
| 10 | + "forgejo.org/models/db" |
| 11 | + repo_model "forgejo.org/models/repo" |
| 12 | + user_model "forgejo.org/models/user" |
| 13 | + "forgejo.org/modules/setting" |
| 14 | + notify_service "forgejo.org/services/notify" |
| 15 | + |
| 16 | + "github.com/stretchr/testify/assert" |
| 17 | + "github.com/stretchr/testify/require" |
| 18 | +) |
| 19 | + |
| 20 | +func getActionsNowDoneTestUsers(t *testing.T) []*user_model.User { |
| 21 | + t.Helper() |
| 22 | + newTriggerUser := new(user_model.User) |
| 23 | + newTriggerUser.Name = "new_trigger_user" |
| 24 | + newTriggerUser.Language = "en_US" |
| 25 | + newTriggerUser.IsAdmin = false |
| 26 | + newTriggerUser. Email = "[email protected]" |
| 27 | + newTriggerUser.LastLoginUnix = 1693648327 |
| 28 | + newTriggerUser.CreatedUnix = 1693648027 |
| 29 | + newTriggerUser.EmailNotificationsPreference = user_model.EmailNotificationsEnabled |
| 30 | + require.NoError(t, user_model.CreateUser(db.DefaultContext, newTriggerUser)) |
| 31 | + |
| 32 | + newOwner := new(user_model.User) |
| 33 | + newOwner.Name = "new_owner" |
| 34 | + newOwner.Language = "en_US" |
| 35 | + newOwner.IsAdmin = false |
| 36 | + newOwner. Email = "[email protected]" |
| 37 | + newOwner.LastLoginUnix = 1693648329 |
| 38 | + newOwner.CreatedUnix = 1693648029 |
| 39 | + newOwner.EmailNotificationsPreference = user_model.EmailNotificationsEnabled |
| 40 | + require.NoError(t, user_model.CreateUser(db.DefaultContext, newOwner)) |
| 41 | + |
| 42 | + return []*user_model.User{newTriggerUser, newOwner} |
| 43 | +} |
| 44 | + |
| 45 | +func assertTranslatedLocaleMailActionsNowDone(t *testing.T, msgBody string) { |
| 46 | + AssertTranslatedLocale(t, msgBody, "mail.actions.successful_run_after_failure", "mail.actions.not_successful_run", "mail.actions.run_info_cur_status", "mail.actions.run_info_ref", "mail.actions.run_info_previous_status", "mail.actions.run_info_trigger", "mail.view_it_on") |
| 47 | +} |
| 48 | + |
| 49 | +func TestActionRunNowDoneNotificationMail(t *testing.T) { |
| 50 | + ctx := t.Context() |
| 51 | + |
| 52 | + users := getActionsNowDoneTestUsers(t) |
| 53 | + defer CleanUpUsers(ctx, users) |
| 54 | + triggerUser := users[0] |
| 55 | + ownerUser := users[1] |
| 56 | + |
| 57 | + repo := repo_model.Repository{ |
| 58 | + Name: "some repo", |
| 59 | + Description: "rockets are cool", |
| 60 | + Owner: ownerUser, |
| 61 | + OwnerID: ownerUser.ID, |
| 62 | + } |
| 63 | + |
| 64 | + // Do some funky stuff with the action run's ids: |
| 65 | + // The run with the larger ID finished first. |
| 66 | + // This is odd but something that must work. |
| 67 | + run1 := &actions_model.ActionRun{ID: 2, Repo: &repo, RepoID: repo.ID, Title: "some workflow", TriggerUser: triggerUser, TriggerUserID: triggerUser.ID, Status: actions_model.StatusFailure, Stopped: 1745821796, TriggerEvent: "workflow_dispatch"} |
| 68 | + run2 := &actions_model.ActionRun{ID: 1, Repo: &repo, RepoID: repo.ID, Title: "some workflow", TriggerUser: triggerUser, TriggerUserID: triggerUser.ID, Status: actions_model.StatusSuccess, Stopped: 1745822796, TriggerEvent: "push"} |
| 69 | + |
| 70 | + notify_service.RegisterNotifier(NewNotifier()) |
| 71 | + |
| 72 | + t.Run("DontSendNotificationEmailOnFirstActionSuccess", func(t *testing.T) { |
| 73 | + defer MockMailSettings(func(msgs ...*Message) { |
| 74 | + assert.Fail(t, "no mail should be sent") |
| 75 | + })() |
| 76 | + notify_service.ActionRunNowDone(ctx, run2, actions_model.StatusRunning, nil) |
| 77 | + }) |
| 78 | + |
| 79 | + t.Run("SendNotificationEmailOnActionRunFailed", func(t *testing.T) { |
| 80 | + mailSentToOwner := false |
| 81 | + mailSentToTriggerUser := false |
| 82 | + defer MockMailSettings(func(msgs ...*Message) { |
| 83 | + assert.LessOrEqual(t, len(msgs), 2) |
| 84 | + for _, msg := range msgs { |
| 85 | + switch msg.To { |
| 86 | + case triggerUser.EmailTo(): |
| 87 | + assert.False(t, mailSentToTriggerUser, "sent mail twice") |
| 88 | + mailSentToTriggerUser = true |
| 89 | + case ownerUser.EmailTo(): |
| 90 | + assert.False(t, mailSentToOwner, "sent mail twice") |
| 91 | + mailSentToOwner = true |
| 92 | + default: |
| 93 | + assert.Fail(t, "sent mail to unknown sender", msg.To) |
| 94 | + } |
| 95 | + assert.Contains(t, msg.Body, triggerUser.HTMLURL()) |
| 96 | + assert.Contains(t, msg.Body, triggerUser.Name) |
| 97 | + // what happened |
| 98 | + assert.Contains(t, msg.Body, "failed") |
| 99 | + // new status of run |
| 100 | + assert.Contains(t, msg.Body, "failure") |
| 101 | + // prior status of this run |
| 102 | + assert.Contains(t, msg.Body, "waiting") |
| 103 | + assertTranslatedLocaleMailActionsNowDone(t, msg.Body) |
| 104 | + } |
| 105 | + })() |
| 106 | + notify_service.ActionRunNowDone(ctx, run1, actions_model.StatusWaiting, nil) |
| 107 | + assert.True(t, mailSentToOwner) |
| 108 | + assert.True(t, mailSentToTriggerUser) |
| 109 | + }) |
| 110 | + |
| 111 | + t.Run("SendNotificationEmailOnActionRunRecovered", func(t *testing.T) { |
| 112 | + mailSentToOwner := false |
| 113 | + mailSentToTriggerUser := false |
| 114 | + defer MockMailSettings(func(msgs ...*Message) { |
| 115 | + assert.LessOrEqual(t, len(msgs), 2) |
| 116 | + for _, msg := range msgs { |
| 117 | + switch msg.To { |
| 118 | + case triggerUser.EmailTo(): |
| 119 | + assert.False(t, mailSentToTriggerUser, "sent mail twice") |
| 120 | + mailSentToTriggerUser = true |
| 121 | + case ownerUser.EmailTo(): |
| 122 | + assert.False(t, mailSentToOwner, "sent mail twice") |
| 123 | + mailSentToOwner = true |
| 124 | + default: |
| 125 | + assert.Fail(t, "sent mail to unknown sender", msg.To) |
| 126 | + } |
| 127 | + assert.Contains(t, msg.Body, triggerUser.HTMLURL()) |
| 128 | + assert.Contains(t, msg.Body, triggerUser.Name) |
| 129 | + // what happened |
| 130 | + assert.Contains(t, msg.Body, "recovered") |
| 131 | + // old status of run |
| 132 | + assert.Contains(t, msg.Body, "failure") |
| 133 | + // new status of run |
| 134 | + assert.Contains(t, msg.Body, "success") |
| 135 | + // prior status of this run |
| 136 | + assert.Contains(t, msg.Body, "running") |
| 137 | + assertTranslatedLocaleMailActionsNowDone(t, msg.Body) |
| 138 | + } |
| 139 | + })() |
| 140 | + assert.NotNil(t, setting.MailService) |
| 141 | + |
| 142 | + notify_service.ActionRunNowDone(ctx, run2, actions_model.StatusRunning, run1) |
| 143 | + assert.True(t, mailSentToOwner) |
| 144 | + assert.True(t, mailSentToTriggerUser) |
| 145 | + }) |
| 146 | +} |
0 commit comments