2015-06-05 17:08:19 +08:00
|
|
|
package notifications
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
2018-04-27 19:01:32 +08:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2020-02-29 20:35:15 +08:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2015-06-05 17:08:19 +08:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2021-03-10 19:41:29 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2015-06-05 17:08:19 +08:00
|
|
|
)
|
|
|
|
|
|
2021-03-10 19:41:29 +08:00
|
|
|
func TestNotificationService(t *testing.T) {
|
|
|
|
|
ns := &NotificationService{
|
|
|
|
|
Cfg: setting.NewCfg(),
|
|
|
|
|
}
|
|
|
|
|
ns.Cfg.StaticRootPath = "../../../public/"
|
|
|
|
|
ns.Cfg.Smtp.Enabled = true
|
2021-07-19 18:31:51 +08:00
|
|
|
ns.Cfg.Smtp.TemplatesPatterns = []string{"emails/*.html", "emails/*.txt"}
|
2021-03-10 19:41:29 +08:00
|
|
|
ns.Cfg.Smtp.FromAddress = "from@address.com"
|
|
|
|
|
ns.Cfg.Smtp.FromName = "Grafana Admin"
|
2021-07-19 18:31:51 +08:00
|
|
|
ns.Cfg.Smtp.ContentTypes = []string{"text/html", "text/plain"}
|
2021-03-10 19:41:29 +08:00
|
|
|
ns.Bus = bus.New()
|
2015-06-05 17:08:19 +08:00
|
|
|
|
2021-08-25 21:11:22 +08:00
|
|
|
ns, err := ProvideService(bus.New(), ns.Cfg)
|
2021-03-10 19:41:29 +08:00
|
|
|
require.NoError(t, err)
|
2015-06-05 17:08:19 +08:00
|
|
|
|
2021-03-10 19:41:29 +08:00
|
|
|
t.Run("When sending reset email password", func(t *testing.T) {
|
|
|
|
|
err := ns.sendResetPasswordEmail(&models.SendResetPasswordEmailCommand{User: &models.User{Email: "asd@asd.com"}})
|
|
|
|
|
require.NoError(t, err)
|
2015-06-05 17:08:19 +08:00
|
|
|
|
2021-03-10 19:41:29 +08:00
|
|
|
sentMsg := <-ns.mailQueue
|
2021-07-19 18:31:51 +08:00
|
|
|
assert.Contains(t, sentMsg.Body["text/html"], "body")
|
|
|
|
|
assert.NotContains(t, sentMsg.Body["text/plain"], "body")
|
2021-03-10 19:41:29 +08:00
|
|
|
assert.Equal(t, "Reset your Grafana password - asd@asd.com", sentMsg.Subject)
|
2021-07-19 18:31:51 +08:00
|
|
|
assert.NotContains(t, sentMsg.Body["text/html"], "Subject")
|
|
|
|
|
assert.NotContains(t, sentMsg.Body["text/plain"], "Subject")
|
2016-06-17 21:30:17 +08:00
|
|
|
})
|
2015-06-05 17:08:19 +08:00
|
|
|
}
|