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