2021-10-07 09:33:50 -05:00
|
|
|
package alerting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/encryption/ossencryption"
|
2022-02-03 06:26:05 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/notifications"
|
2021-10-07 09:33:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestService(t *testing.T) {
|
|
|
|
sqlStore := sqlstore.InitTestDB(t)
|
|
|
|
|
2021-11-02 08:11:19 -05:00
|
|
|
nType := "test"
|
|
|
|
registerTestNotifier(nType)
|
|
|
|
|
2022-02-03 06:26:05 -06:00
|
|
|
s := ProvideService(bus.New(), sqlStore, ossencryption.ProvideService(), nil)
|
2021-10-07 09:33:50 -05:00
|
|
|
|
|
|
|
origSecret := setting.SecretKey
|
|
|
|
setting.SecretKey = "alert_notification_service_test"
|
2021-11-02 08:11:19 -05:00
|
|
|
|
2021-10-07 09:33:50 -05:00
|
|
|
t.Cleanup(func() {
|
|
|
|
setting.SecretKey = origSecret
|
|
|
|
})
|
|
|
|
|
2021-11-02 08:11:19 -05:00
|
|
|
t.Run("create alert notification should reject an invalid command", func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
ss := map[string]string{"password": "12345"}
|
|
|
|
cmd := models.CreateAlertNotificationCommand{SecureSettings: ss}
|
|
|
|
|
|
|
|
err := s.CreateAlertNotificationCommand(ctx, &cmd)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
2021-10-07 09:33:50 -05:00
|
|
|
|
|
|
|
t.Run("create alert notification should encrypt the secure json data", func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
ss := map[string]string{"password": "12345"}
|
2021-11-02 08:11:19 -05:00
|
|
|
cmd := models.CreateAlertNotificationCommand{SecureSettings: ss, Type: nType}
|
2021-10-07 09:33:50 -05:00
|
|
|
|
|
|
|
err := s.CreateAlertNotificationCommand(ctx, &cmd)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-11-02 08:11:19 -05:00
|
|
|
an := cmd.Result
|
2021-10-07 09:33:50 -05:00
|
|
|
decrypted, err := s.EncryptionService.DecryptJsonData(ctx, an.SecureSettings, setting.SecretKey)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, ss, decrypted)
|
2021-11-02 08:11:19 -05:00
|
|
|
|
|
|
|
// Delete the created alert notification
|
|
|
|
delCmd := models.DeleteAlertNotificationCommand{
|
|
|
|
Id: cmd.Result.Id,
|
|
|
|
OrgId: cmd.Result.OrgId,
|
|
|
|
}
|
2021-11-05 03:41:24 -05:00
|
|
|
err = s.DeleteAlertNotification(context.Background(), &delCmd)
|
2021-11-02 08:11:19 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("update alert notification should reject an invalid command", func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// Save test notification.
|
|
|
|
ss := map[string]string{"password": "12345"}
|
|
|
|
createCmd := models.CreateAlertNotificationCommand{SecureSettings: ss, Type: nType}
|
|
|
|
|
|
|
|
err := s.CreateAlertNotificationCommand(ctx, &createCmd)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Try to update it with an invalid type.
|
|
|
|
updateCmd := models.UpdateAlertNotificationCommand{Id: createCmd.Result.Id, Settings: simplejson.New(), SecureSettings: ss, Type: "invalid"}
|
|
|
|
err = s.UpdateAlertNotification(ctx, &updateCmd)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// Delete the created alert notification.
|
|
|
|
delCmd := models.DeleteAlertNotificationCommand{
|
|
|
|
Id: createCmd.Result.Id,
|
|
|
|
OrgId: createCmd.Result.OrgId,
|
|
|
|
}
|
2021-11-05 03:41:24 -05:00
|
|
|
err = s.DeleteAlertNotification(context.Background(), &delCmd)
|
2021-11-02 08:11:19 -05:00
|
|
|
require.NoError(t, err)
|
2021-10-07 09:33:50 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("update alert notification should encrypt the secure json data", func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-11-02 08:11:19 -05:00
|
|
|
// Save test notification.
|
|
|
|
ss := map[string]string{"password": "12345"}
|
|
|
|
createCmd := models.CreateAlertNotificationCommand{SecureSettings: ss, Type: nType}
|
|
|
|
|
|
|
|
err := s.CreateAlertNotificationCommand(ctx, &createCmd)
|
2021-10-07 09:33:50 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-11-02 08:11:19 -05:00
|
|
|
// Update test notification.
|
|
|
|
updateCmd := models.UpdateAlertNotificationCommand{Id: createCmd.Result.Id, Settings: simplejson.New(), SecureSettings: ss, Type: nType}
|
|
|
|
err = s.UpdateAlertNotification(ctx, &updateCmd)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
decrypted, err := s.EncryptionService.DecryptJsonData(ctx, updateCmd.Result.SecureSettings, setting.SecretKey)
|
2021-10-07 09:33:50 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, ss, decrypted)
|
2021-11-02 08:11:19 -05:00
|
|
|
|
|
|
|
// Delete the created alert notification.
|
|
|
|
delCmd := models.DeleteAlertNotificationCommand{
|
|
|
|
Id: createCmd.Result.Id,
|
|
|
|
OrgId: createCmd.Result.OrgId,
|
|
|
|
}
|
2021-11-05 03:41:24 -05:00
|
|
|
err = s.DeleteAlertNotification(context.Background(), &delCmd)
|
2021-11-02 08:11:19 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerTestNotifier(notifierType string) {
|
|
|
|
RegisterNotifier(&NotifierPlugin{
|
2022-02-03 06:26:05 -06:00
|
|
|
Type: notifierType,
|
|
|
|
Factory: func(*models.AlertNotification, GetDecryptedValueFn, notifications.Service) (Notifier, error) {
|
|
|
|
return nil, nil
|
|
|
|
},
|
2021-10-07 09:33:50 -05:00
|
|
|
})
|
|
|
|
}
|