mirror of
https://github.com/grafana/grafana.git
synced 2024-12-28 18:01:40 -06:00
Chore: Move plugin settings store code into a dedicated service (#53560)
This commit is contained in:
parent
818bc88242
commit
72367f99cf
@ -10,11 +10,12 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
||||
"github.com/grafana/grafana/pkg/services/secrets"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore/db"
|
||||
)
|
||||
|
||||
func ProvideService(store *sqlstore.SQLStore, secretsService secrets.Service) *Service {
|
||||
func ProvideService(db db.DB, secretsService secrets.Service) *Service {
|
||||
s := &Service{
|
||||
sqlStore: store,
|
||||
db: db,
|
||||
decryptionCache: secureJSONDecryptionCache{
|
||||
cache: make(map[int64]cachedDecryptedJSON),
|
||||
},
|
||||
@ -26,7 +27,7 @@ func ProvideService(store *sqlstore.SQLStore, secretsService secrets.Service) *S
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
sqlStore *sqlstore.SQLStore
|
||||
db db.DB
|
||||
decryptionCache secureJSONDecryptionCache
|
||||
secretsService secrets.Service
|
||||
|
||||
@ -44,7 +45,7 @@ type secureJSONDecryptionCache struct {
|
||||
}
|
||||
|
||||
func (s *Service) GetPluginSettings(ctx context.Context, args *pluginsettings.GetArgs) ([]*pluginsettings.DTO, error) {
|
||||
ps, err := s.sqlStore.GetPluginSettings(ctx, args.OrgID)
|
||||
ps, err := s.getPluginSettings(ctx, args.OrgID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -73,7 +74,7 @@ func (s *Service) GetPluginSettingByPluginID(ctx context.Context, args *pluginse
|
||||
PluginId: args.PluginID,
|
||||
}
|
||||
|
||||
err := s.sqlStore.GetPluginSettingById(ctx, query)
|
||||
err := s.getPluginSettingById(ctx, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -97,7 +98,7 @@ func (s *Service) UpdatePluginSetting(ctx context.Context, args *pluginsettings.
|
||||
return err
|
||||
}
|
||||
|
||||
return s.sqlStore.UpdatePluginSetting(ctx, &models.UpdatePluginSettingCmd{
|
||||
return s.updatePluginSetting(ctx, &models.UpdatePluginSettingCmd{
|
||||
Enabled: args.Enabled,
|
||||
Pinned: args.Pinned,
|
||||
JsonData: args.JSONData,
|
||||
@ -110,7 +111,7 @@ func (s *Service) UpdatePluginSetting(ctx context.Context, args *pluginsettings.
|
||||
}
|
||||
|
||||
func (s *Service) UpdatePluginSettingPluginVersion(ctx context.Context, args *pluginsettings.UpdatePluginVersionArgs) error {
|
||||
return s.sqlStore.UpdatePluginSettingVersion(ctx, &models.UpdatePluginSettingVersionCmd{
|
||||
return s.updatePluginSettingVersion(ctx, &models.UpdatePluginSettingVersionCmd{
|
||||
PluginVersion: args.PluginVersion,
|
||||
PluginId: args.PluginID,
|
||||
OrgId: args.OrgID,
|
||||
@ -138,3 +139,102 @@ func (s *Service) DecryptedValues(ps *pluginsettings.DTO) map[string]string {
|
||||
|
||||
return json
|
||||
}
|
||||
|
||||
func (s *Service) getPluginSettings(ctx context.Context, orgID int64) ([]*models.PluginSetting, error) {
|
||||
sql := `SELECT org_id, plugin_id, enabled, pinned, plugin_version FROM plugin_setting `
|
||||
params := make([]interface{}, 0)
|
||||
|
||||
if orgID != 0 {
|
||||
sql += "WHERE org_id=?"
|
||||
params = append(params, orgID)
|
||||
}
|
||||
|
||||
var rslt []*models.PluginSetting
|
||||
err := s.db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
return sess.SQL(sql, params...).Find(&rslt)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rslt, nil
|
||||
}
|
||||
|
||||
func (s *Service) getPluginSettingById(ctx context.Context, query *models.GetPluginSettingByIdQuery) error {
|
||||
return s.db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
pluginSetting := models.PluginSetting{OrgId: query.OrgId, PluginId: query.PluginId}
|
||||
has, err := sess.Get(&pluginSetting)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !has {
|
||||
return models.ErrPluginSettingNotFound
|
||||
}
|
||||
query.Result = &pluginSetting
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Service) updatePluginSetting(ctx context.Context, cmd *models.UpdatePluginSettingCmd) error {
|
||||
return s.db.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
var pluginSetting models.PluginSetting
|
||||
|
||||
exists, err := sess.Where("org_id=? and plugin_id=?", cmd.OrgId, cmd.PluginId).Get(&pluginSetting)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sess.UseBool("enabled")
|
||||
sess.UseBool("pinned")
|
||||
if !exists {
|
||||
pluginSetting = models.PluginSetting{
|
||||
PluginId: cmd.PluginId,
|
||||
OrgId: cmd.OrgId,
|
||||
Enabled: cmd.Enabled,
|
||||
Pinned: cmd.Pinned,
|
||||
JsonData: cmd.JsonData,
|
||||
PluginVersion: cmd.PluginVersion,
|
||||
SecureJsonData: cmd.EncryptedSecureJsonData,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
// add state change event on commit success
|
||||
sess.PublishAfterCommit(&models.PluginStateChangedEvent{
|
||||
PluginId: cmd.PluginId,
|
||||
OrgId: cmd.OrgId,
|
||||
Enabled: cmd.Enabled,
|
||||
})
|
||||
|
||||
_, err = sess.Insert(&pluginSetting)
|
||||
return err
|
||||
}
|
||||
|
||||
for key, encryptedData := range cmd.EncryptedSecureJsonData {
|
||||
pluginSetting.SecureJsonData[key] = encryptedData
|
||||
}
|
||||
|
||||
// add state change event on commit success
|
||||
if pluginSetting.Enabled != cmd.Enabled {
|
||||
sess.PublishAfterCommit(&models.PluginStateChangedEvent{
|
||||
PluginId: cmd.PluginId,
|
||||
OrgId: cmd.OrgId,
|
||||
Enabled: cmd.Enabled,
|
||||
})
|
||||
}
|
||||
|
||||
pluginSetting.Updated = time.Now()
|
||||
pluginSetting.Enabled = cmd.Enabled
|
||||
pluginSetting.JsonData = cmd.JsonData
|
||||
pluginSetting.Pinned = cmd.Pinned
|
||||
pluginSetting.PluginVersion = cmd.PluginVersion
|
||||
|
||||
_, err = sess.ID(pluginSetting.Id).Update(&pluginSetting)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Service) updatePluginSettingVersion(ctx context.Context, cmd *models.UpdatePluginSettingVersionCmd) error {
|
||||
return s.db.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
||||
_, err := sess.Exec("UPDATE plugin_setting SET plugin_version=? WHERE org_id=? AND plugin_id=?", cmd.PluginVersion, cmd.OrgId, cmd.PluginId)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
@ -5,10 +5,12 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/services/pluginsettings"
|
||||
"github.com/grafana/grafana/pkg/services/secrets"
|
||||
"github.com/grafana/grafana/pkg/services/secrets/fakes"
|
||||
secretsManager "github.com/grafana/grafana/pkg/services/secrets/manager"
|
||||
"github.com/grafana/grafana/pkg/services/sqlstore"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@ -91,3 +93,185 @@ func TestService_DecryptedValuesCache(t *testing.T) {
|
||||
require.True(t, ok)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntegrationPluginSettings(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test")
|
||||
}
|
||||
db := sqlstore.InitTestDB(t)
|
||||
secretsService := secretsManager.SetupTestService(t, fakes.NewFakeSecretsStore())
|
||||
psService := ProvideService(db, secretsService)
|
||||
|
||||
t.Run("Existing plugin settings", func(t *testing.T) {
|
||||
secureJsonData, err := secretsService.EncryptJsonData(context.Background(), map[string]string{"secureKey": "secureValue"}, secrets.WithoutScope())
|
||||
require.NoError(t, err)
|
||||
|
||||
existing := models.PluginSetting{
|
||||
OrgId: 1,
|
||||
PluginId: "existing",
|
||||
Enabled: false,
|
||||
Pinned: false,
|
||||
JsonData: map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
SecureJsonData: secureJsonData,
|
||||
PluginVersion: "1.0.0",
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
err = db.WithTransactionalDbSession(context.Background(), func(sess *sqlstore.DBSession) error {
|
||||
affectedRows, innerErr := sess.Insert(&existing)
|
||||
require.Equal(t, int64(1), affectedRows)
|
||||
return innerErr
|
||||
})
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Greater(t, existing.Id, int64(0))
|
||||
|
||||
t.Run("GetPluginSettings with orgID=0 should return all existing plugin settings", func(t *testing.T) {
|
||||
pluginSettings, err := psService.GetPluginSettings(context.Background(), &pluginsettings.GetArgs{OrgID: 0})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, pluginSettings, 1)
|
||||
ps := pluginSettings[0]
|
||||
require.Equal(t, existing.OrgId, ps.OrgID)
|
||||
require.Equal(t, existing.PluginId, ps.PluginID)
|
||||
require.False(t, ps.Enabled)
|
||||
require.Nil(t, ps.JSONData)
|
||||
require.Nil(t, ps.SecureJSONData)
|
||||
})
|
||||
|
||||
t.Run("GetPluginSettings with orgID=1 should return all existing plugin settings", func(t *testing.T) {
|
||||
pluginSettings, err := psService.GetPluginSettings(context.Background(), &pluginsettings.GetArgs{OrgID: 1})
|
||||
require.NoError(t, err)
|
||||
require.Len(t, pluginSettings, 1)
|
||||
ps := pluginSettings[0]
|
||||
require.Equal(t, existing.OrgId, ps.OrgID)
|
||||
require.Equal(t, existing.PluginId, ps.PluginID)
|
||||
require.False(t, ps.Enabled)
|
||||
require.Nil(t, ps.JSONData)
|
||||
require.Nil(t, ps.SecureJSONData)
|
||||
})
|
||||
|
||||
t.Run("GetPluginSettingById should return existing plugin settings", func(t *testing.T) {
|
||||
query := &pluginsettings.GetByPluginIDArgs{
|
||||
OrgID: existing.OrgId,
|
||||
PluginID: existing.PluginId,
|
||||
}
|
||||
ps, err := psService.GetPluginSettingByPluginID(context.Background(), query)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, ps)
|
||||
require.Equal(t, existing.OrgId, ps.OrgID)
|
||||
require.Equal(t, existing.PluginId, ps.PluginID)
|
||||
require.False(t, ps.Enabled)
|
||||
require.NotNil(t, ps.JSONData)
|
||||
require.Equal(t, existing.JsonData, ps.JSONData)
|
||||
require.NotNil(t, ps.SecureJSONData)
|
||||
require.Equal(t, existing.SecureJsonData, ps.SecureJSONData)
|
||||
})
|
||||
|
||||
t.Run("UpdatePluginSetting should update existing plugin settings and publish PluginStateChangedEvent", func(t *testing.T) {
|
||||
var pluginStateChangedEvent *models.PluginStateChangedEvent
|
||||
db.Bus().AddEventListener(func(_ context.Context, evt *models.PluginStateChangedEvent) error {
|
||||
pluginStateChangedEvent = evt
|
||||
return nil
|
||||
})
|
||||
|
||||
cmd := &pluginsettings.UpdateArgs{
|
||||
OrgID: existing.OrgId,
|
||||
PluginID: existing.PluginId,
|
||||
Enabled: true,
|
||||
PluginVersion: "1.0.1",
|
||||
JSONData: map[string]interface{}{
|
||||
"key2": "value2",
|
||||
},
|
||||
SecureJSONData: map[string]string{
|
||||
"secureKey": "secureValue",
|
||||
"secureKey2": "secureValue2",
|
||||
},
|
||||
Pinned: true,
|
||||
}
|
||||
err := psService.UpdatePluginSetting(context.Background(), cmd)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, pluginStateChangedEvent)
|
||||
require.Equal(t, existing.OrgId, pluginStateChangedEvent.OrgId)
|
||||
require.Equal(t, existing.PluginId, pluginStateChangedEvent.PluginId)
|
||||
require.True(t, pluginStateChangedEvent.Enabled)
|
||||
|
||||
err = psService.UpdatePluginSettingPluginVersion(context.Background(), &pluginsettings.UpdatePluginVersionArgs{
|
||||
OrgID: cmd.OrgID,
|
||||
PluginID: cmd.PluginID,
|
||||
PluginVersion: "1.0.2",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("GetPluginSettingById should return updated plugin settings", func(t *testing.T) {
|
||||
query := &pluginsettings.GetByPluginIDArgs{
|
||||
OrgID: existing.OrgId,
|
||||
PluginID: existing.PluginId,
|
||||
}
|
||||
ps, err := psService.GetPluginSettingByPluginID(context.Background(), query)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, ps)
|
||||
require.Equal(t, existing.OrgId, ps.OrgID)
|
||||
require.Equal(t, existing.PluginId, ps.PluginID)
|
||||
require.True(t, ps.Enabled)
|
||||
require.NotNil(t, ps.JSONData)
|
||||
require.Equal(t, cmd.JSONData, ps.JSONData)
|
||||
require.NotNil(t, ps.SecureJSONData)
|
||||
require.Equal(t, cmd.SecureJSONData, psService.DecryptedValues(ps))
|
||||
require.Equal(t, "1.0.2", ps.PluginVersion)
|
||||
require.True(t, ps.Pinned)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Non-existing plugin settings", func(t *testing.T) {
|
||||
t.Run("UpdatePluginSetting should insert plugin settings and publish PluginStateChangedEvent", func(t *testing.T) {
|
||||
var pluginStateChangedEvent *models.PluginStateChangedEvent
|
||||
db.Bus().AddEventListener(func(_ context.Context, evt *models.PluginStateChangedEvent) error {
|
||||
pluginStateChangedEvent = evt
|
||||
return nil
|
||||
})
|
||||
|
||||
cmd := &pluginsettings.UpdateArgs{
|
||||
PluginID: "test",
|
||||
Enabled: true,
|
||||
OrgID: 1,
|
||||
PluginVersion: "1.0.0",
|
||||
JSONData: map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
SecureJSONData: map[string]string{
|
||||
"secureKey": "secureValue",
|
||||
},
|
||||
}
|
||||
err := psService.UpdatePluginSetting(context.Background(), cmd)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, pluginStateChangedEvent)
|
||||
require.Equal(t, cmd.OrgID, pluginStateChangedEvent.OrgId)
|
||||
require.Equal(t, cmd.PluginID, pluginStateChangedEvent.PluginId)
|
||||
require.True(t, pluginStateChangedEvent.Enabled)
|
||||
|
||||
t.Run("GetPluginSettingById should return inserted plugin settings", func(t *testing.T) {
|
||||
query := &pluginsettings.GetByPluginIDArgs{
|
||||
OrgID: cmd.OrgID,
|
||||
PluginID: cmd.PluginID,
|
||||
}
|
||||
ps, err := psService.GetPluginSettingByPluginID(context.Background(), query)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, ps)
|
||||
require.Equal(t, cmd.OrgID, ps.OrgID)
|
||||
require.Equal(t, cmd.PluginID, ps.PluginID)
|
||||
require.True(t, ps.Enabled)
|
||||
require.NotNil(t, ps.JSONData)
|
||||
require.Equal(t, cmd.JSONData, ps.JSONData)
|
||||
require.NotNil(t, ps.SecureJSONData)
|
||||
require.Equal(t, cmd.PluginVersion, ps.PluginVersion)
|
||||
require.False(t, ps.Pinned)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
@ -1,108 +0,0 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
)
|
||||
|
||||
func (ss *SQLStore) GetPluginSettings(ctx context.Context, orgID int64) ([]*models.PluginSetting, error) {
|
||||
sql := `SELECT org_id, plugin_id, enabled, pinned, plugin_version
|
||||
FROM plugin_setting `
|
||||
params := make([]interface{}, 0)
|
||||
|
||||
if orgID != 0 {
|
||||
sql += "WHERE org_id=?"
|
||||
params = append(params, orgID)
|
||||
}
|
||||
|
||||
var rslt []*models.PluginSetting
|
||||
err := ss.WithDbSession(ctx, func(sess *DBSession) error {
|
||||
return sess.SQL(sql, params...).Find(&rslt)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rslt, nil
|
||||
}
|
||||
|
||||
func (ss *SQLStore) GetPluginSettingById(ctx context.Context, query *models.GetPluginSettingByIdQuery) error {
|
||||
return ss.WithDbSession(ctx, func(sess *DBSession) error {
|
||||
pluginSetting := models.PluginSetting{OrgId: query.OrgId, PluginId: query.PluginId}
|
||||
has, err := sess.Get(&pluginSetting)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !has {
|
||||
return models.ErrPluginSettingNotFound
|
||||
}
|
||||
query.Result = &pluginSetting
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (ss *SQLStore) UpdatePluginSetting(ctx context.Context, cmd *models.UpdatePluginSettingCmd) error {
|
||||
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
|
||||
var pluginSetting models.PluginSetting
|
||||
|
||||
exists, err := sess.Where("org_id=? and plugin_id=?", cmd.OrgId, cmd.PluginId).Get(&pluginSetting)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sess.UseBool("enabled")
|
||||
sess.UseBool("pinned")
|
||||
if !exists {
|
||||
pluginSetting = models.PluginSetting{
|
||||
PluginId: cmd.PluginId,
|
||||
OrgId: cmd.OrgId,
|
||||
Enabled: cmd.Enabled,
|
||||
Pinned: cmd.Pinned,
|
||||
JsonData: cmd.JsonData,
|
||||
PluginVersion: cmd.PluginVersion,
|
||||
SecureJsonData: cmd.EncryptedSecureJsonData,
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
// add state change event on commit success
|
||||
sess.events = append(sess.events, &models.PluginStateChangedEvent{
|
||||
PluginId: cmd.PluginId,
|
||||
OrgId: cmd.OrgId,
|
||||
Enabled: cmd.Enabled,
|
||||
})
|
||||
|
||||
_, err = sess.Insert(&pluginSetting)
|
||||
return err
|
||||
}
|
||||
|
||||
for key, encryptedData := range cmd.EncryptedSecureJsonData {
|
||||
pluginSetting.SecureJsonData[key] = encryptedData
|
||||
}
|
||||
|
||||
// add state change event on commit success
|
||||
if pluginSetting.Enabled != cmd.Enabled {
|
||||
sess.events = append(sess.events, &models.PluginStateChangedEvent{
|
||||
PluginId: cmd.PluginId,
|
||||
OrgId: cmd.OrgId,
|
||||
Enabled: cmd.Enabled,
|
||||
})
|
||||
}
|
||||
|
||||
pluginSetting.Updated = time.Now()
|
||||
pluginSetting.Enabled = cmd.Enabled
|
||||
pluginSetting.JsonData = cmd.JsonData
|
||||
pluginSetting.Pinned = cmd.Pinned
|
||||
pluginSetting.PluginVersion = cmd.PluginVersion
|
||||
|
||||
_, err = sess.ID(pluginSetting.Id).Update(&pluginSetting)
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func (ss *SQLStore) UpdatePluginSettingVersion(ctx context.Context, cmd *models.UpdatePluginSettingVersionCmd) error {
|
||||
return ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
|
||||
_, err := sess.Exec("UPDATE plugin_setting SET plugin_version=? WHERE org_id=? AND plugin_id=?", cmd.PluginVersion, cmd.OrgId, cmd.PluginId)
|
||||
return err
|
||||
})
|
||||
}
|
@ -1,193 +0,0 @@
|
||||
package sqlstore
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestIntegrationPluginSettings(t *testing.T) {
|
||||
if testing.Short() {
|
||||
t.Skip("skipping integration test")
|
||||
}
|
||||
store := InitTestDB(t)
|
||||
|
||||
t.Run("Existing plugin settings", func(t *testing.T) {
|
||||
existing := models.PluginSetting{
|
||||
OrgId: 1,
|
||||
PluginId: "existing",
|
||||
Enabled: false,
|
||||
Pinned: false,
|
||||
JsonData: map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
SecureJsonData: map[string][]byte{
|
||||
"secureKey": []byte("secureValue"),
|
||||
},
|
||||
PluginVersion: "1.0.0",
|
||||
Created: time.Now(),
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
err := store.WithTransactionalDbSession(context.Background(), func(sess *DBSession) error {
|
||||
affectedRows, innerErr := sess.Insert(&existing)
|
||||
require.Equal(t, int64(1), affectedRows)
|
||||
return innerErr
|
||||
})
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Greater(t, existing.Id, int64(0))
|
||||
|
||||
t.Run("GetPluginSettings with orgID=0 should return all existing plugin settings", func(t *testing.T) {
|
||||
pluginSettings, err := store.GetPluginSettings(context.Background(), 0)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, pluginSettings, 1)
|
||||
ps := pluginSettings[0]
|
||||
require.Equal(t, existing.OrgId, ps.OrgId)
|
||||
require.Equal(t, existing.PluginId, ps.PluginId)
|
||||
require.False(t, ps.Enabled)
|
||||
require.Nil(t, ps.JsonData)
|
||||
require.Nil(t, ps.SecureJsonData)
|
||||
})
|
||||
|
||||
t.Run("GetPluginSettings with orgID=1 should return all existing plugin settings", func(t *testing.T) {
|
||||
pluginSettings, err := store.GetPluginSettings(context.Background(), 1)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, pluginSettings, 1)
|
||||
ps := pluginSettings[0]
|
||||
require.Equal(t, existing.OrgId, ps.OrgId)
|
||||
require.Equal(t, existing.PluginId, ps.PluginId)
|
||||
require.False(t, ps.Enabled)
|
||||
require.Nil(t, ps.JsonData)
|
||||
require.Nil(t, ps.SecureJsonData)
|
||||
})
|
||||
|
||||
t.Run("GetPluginSettingById should return existing plugin settings", func(t *testing.T) {
|
||||
query := &models.GetPluginSettingByIdQuery{
|
||||
OrgId: existing.OrgId,
|
||||
PluginId: existing.PluginId,
|
||||
}
|
||||
err := store.GetPluginSettingById(context.Background(), query)
|
||||
require.NoError(t, err)
|
||||
ps := query.Result
|
||||
require.NotNil(t, ps)
|
||||
require.Equal(t, existing.OrgId, ps.OrgId)
|
||||
require.Equal(t, existing.PluginId, ps.PluginId)
|
||||
require.False(t, ps.Enabled)
|
||||
require.NotNil(t, ps.JsonData)
|
||||
require.Equal(t, existing.JsonData, ps.JsonData)
|
||||
require.NotNil(t, ps.SecureJsonData)
|
||||
require.Equal(t, existing.SecureJsonData, ps.SecureJsonData)
|
||||
})
|
||||
|
||||
t.Run("UpdatePluginSetting should update existing plugin settings and publish PluginStateChangedEvent", func(t *testing.T) {
|
||||
var pluginStateChangedEvent *models.PluginStateChangedEvent
|
||||
store.bus.AddEventListener(func(_ context.Context, evt *models.PluginStateChangedEvent) error {
|
||||
pluginStateChangedEvent = evt
|
||||
return nil
|
||||
})
|
||||
|
||||
cmd := &models.UpdatePluginSettingCmd{
|
||||
OrgId: existing.OrgId,
|
||||
PluginId: existing.PluginId,
|
||||
Enabled: true,
|
||||
PluginVersion: "1.0.1",
|
||||
JsonData: map[string]interface{}{
|
||||
"key2": "value2",
|
||||
},
|
||||
EncryptedSecureJsonData: map[string][]byte{
|
||||
"secureKey": []byte("secureValue"),
|
||||
"secureKey2": []byte("secureValue2"),
|
||||
},
|
||||
Pinned: true,
|
||||
}
|
||||
err := store.UpdatePluginSetting(context.Background(), cmd)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, pluginStateChangedEvent)
|
||||
require.Equal(t, existing.OrgId, pluginStateChangedEvent.OrgId)
|
||||
require.Equal(t, existing.PluginId, pluginStateChangedEvent.PluginId)
|
||||
require.True(t, pluginStateChangedEvent.Enabled)
|
||||
|
||||
err = store.UpdatePluginSettingVersion(context.Background(), &models.UpdatePluginSettingVersionCmd{
|
||||
OrgId: cmd.OrgId,
|
||||
PluginId: cmd.PluginId,
|
||||
PluginVersion: "1.0.2",
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("GetPluginSettingById should return updated plugin settings", func(t *testing.T) {
|
||||
query := &models.GetPluginSettingByIdQuery{
|
||||
OrgId: existing.OrgId,
|
||||
PluginId: existing.PluginId,
|
||||
}
|
||||
err := store.GetPluginSettingById(context.Background(), query)
|
||||
require.NoError(t, err)
|
||||
ps := query.Result
|
||||
require.NotNil(t, ps)
|
||||
require.Equal(t, existing.OrgId, ps.OrgId)
|
||||
require.Equal(t, existing.PluginId, ps.PluginId)
|
||||
require.True(t, ps.Enabled)
|
||||
require.NotNil(t, ps.JsonData)
|
||||
require.Equal(t, cmd.JsonData, ps.JsonData)
|
||||
require.NotNil(t, ps.SecureJsonData)
|
||||
require.Equal(t, cmd.EncryptedSecureJsonData, ps.SecureJsonData)
|
||||
require.Equal(t, "1.0.2", ps.PluginVersion)
|
||||
require.True(t, ps.Pinned)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Non-existing plugin settings", func(t *testing.T) {
|
||||
t.Run("UpdatePluginSetting should insert plugin settings and publish PluginStateChangedEvent", func(t *testing.T) {
|
||||
var pluginStateChangedEvent *models.PluginStateChangedEvent
|
||||
store.bus.AddEventListener(func(_ context.Context, evt *models.PluginStateChangedEvent) error {
|
||||
pluginStateChangedEvent = evt
|
||||
return nil
|
||||
})
|
||||
|
||||
cmd := &models.UpdatePluginSettingCmd{
|
||||
PluginId: "test",
|
||||
Enabled: true,
|
||||
OrgId: 1,
|
||||
PluginVersion: "1.0.0",
|
||||
JsonData: map[string]interface{}{
|
||||
"key": "value",
|
||||
},
|
||||
EncryptedSecureJsonData: map[string][]byte{
|
||||
"secureKey": []byte("secureValue"),
|
||||
},
|
||||
}
|
||||
err := store.UpdatePluginSetting(context.Background(), cmd)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, pluginStateChangedEvent)
|
||||
require.Equal(t, cmd.OrgId, pluginStateChangedEvent.OrgId)
|
||||
require.Equal(t, cmd.PluginId, pluginStateChangedEvent.PluginId)
|
||||
require.True(t, pluginStateChangedEvent.Enabled)
|
||||
|
||||
t.Run("GetPluginSettingById should return inserted plugin settings", func(t *testing.T) {
|
||||
query := &models.GetPluginSettingByIdQuery{
|
||||
OrgId: cmd.OrgId,
|
||||
PluginId: cmd.PluginId,
|
||||
}
|
||||
err := store.GetPluginSettingById(context.Background(), query)
|
||||
require.NoError(t, err)
|
||||
ps := query.Result
|
||||
require.NotNil(t, ps)
|
||||
require.Equal(t, cmd.OrgId, ps.OrgId)
|
||||
require.Equal(t, cmd.PluginId, ps.PluginId)
|
||||
require.True(t, ps.Enabled)
|
||||
require.NotNil(t, ps.JsonData)
|
||||
require.Equal(t, cmd.JsonData, ps.JsonData)
|
||||
require.NotNil(t, ps.SecureJsonData)
|
||||
require.Equal(t, cmd.EncryptedSecureJsonData, ps.SecureJsonData)
|
||||
require.Equal(t, cmd.PluginVersion, ps.PluginVersion)
|
||||
require.False(t, ps.Pinned)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
@ -179,6 +179,10 @@ func (ss *SQLStore) GetDialect() migrator.Dialect {
|
||||
return ss.Dialect
|
||||
}
|
||||
|
||||
func (ss *SQLStore) Bus() bus.Bus {
|
||||
return ss.bus
|
||||
}
|
||||
|
||||
func (ss *SQLStore) ensureMainOrgAndAdminUser() error {
|
||||
ctx := context.Background()
|
||||
err := ss.WithTransactionalDbSession(ctx, func(sess *DBSession) error {
|
||||
|
Loading…
Reference in New Issue
Block a user