Chore: Remove x from dashboard snapshots (#48001)

This commit is contained in:
Kat Yang 2022-04-21 08:20:47 -04:00 committed by GitHub
parent d832bde270
commit eb05f6ead8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 47 deletions

View File

@ -40,7 +40,7 @@ func (s *Service) CreateDashboardSnapshot(ctx context.Context, cmd *models.Creat
} }
func (s *Service) GetDashboardSnapshot(ctx context.Context, query *models.GetDashboardSnapshotQuery) error { func (s *Service) GetDashboardSnapshot(ctx context.Context, query *models.GetDashboardSnapshotQuery) error {
err := s.SQLStore.GetDashboardSnapshot(query) err := s.SQLStore.GetDashboardSnapshot(ctx, query)
if err != nil { if err != nil {
return err return err
} }
@ -66,8 +66,8 @@ func (s *Service) DeleteDashboardSnapshot(ctx context.Context, cmd *models.Delet
return s.SQLStore.DeleteDashboardSnapshot(ctx, cmd) return s.SQLStore.DeleteDashboardSnapshot(ctx, cmd)
} }
func (s *Service) SearchDashboardSnapshots(_ context.Context, query *models.GetDashboardSnapshotsQuery) error { func (s *Service) SearchDashboardSnapshots(ctx context.Context, query *models.GetDashboardSnapshotsQuery) error {
return s.SQLStore.SearchDashboardSnapshots(query) return s.SQLStore.SearchDashboardSnapshots(ctx, query)
} }
func (s *Service) DeleteExpiredSnapshots(ctx context.Context, cmd *models.DeleteExpiredSnapshotsCommand) error { func (s *Service) DeleteExpiredSnapshots(ctx context.Context, cmd *models.DeleteExpiredSnapshotsCommand) error {

View File

@ -68,47 +68,51 @@ func (ss *SQLStore) DeleteDashboardSnapshot(ctx context.Context, cmd *models.Del
}) })
} }
func (ss *SQLStore) GetDashboardSnapshot(query *models.GetDashboardSnapshotQuery) error { func (ss *SQLStore) GetDashboardSnapshot(ctx context.Context, query *models.GetDashboardSnapshotQuery) error {
snapshot := models.DashboardSnapshot{Key: query.Key, DeleteKey: query.DeleteKey} return ss.WithDbSession(ctx, func(dbSess *DBSession) error {
has, err := x.Get(&snapshot) snapshot := models.DashboardSnapshot{Key: query.Key, DeleteKey: query.DeleteKey}
has, err := dbSess.Get(&snapshot)
if err != nil { if err != nil {
return err return err
} else if !has { } else if !has {
return models.ErrDashboardSnapshotNotFound return models.ErrDashboardSnapshotNotFound
} }
query.Result = &snapshot query.Result = &snapshot
return nil return nil
})
} }
// SearchDashboardSnapshots returns a list of all snapshots for admins // SearchDashboardSnapshots returns a list of all snapshots for admins
// for other roles, it returns snapshots created by the user // for other roles, it returns snapshots created by the user
func (ss *SQLStore) SearchDashboardSnapshots(query *models.GetDashboardSnapshotsQuery) error { func (ss *SQLStore) SearchDashboardSnapshots(ctx context.Context, query *models.GetDashboardSnapshotsQuery) error {
var snapshots = make(models.DashboardSnapshotsList, 0) return ss.WithDbSession(ctx, func(dbSess *DBSession) error {
var snapshots = make(models.DashboardSnapshotsList, 0)
sess := x.NewSession() sess := ss.NewSession(ctx)
if query.Limit > 0 { if query.Limit > 0 {
sess.Limit(query.Limit) sess.Limit(query.Limit)
} }
sess.Table("dashboard_snapshot") sess.Table("dashboard_snapshot")
if query.Name != "" { if query.Name != "" {
sess.Where("name LIKE ?", query.Name) sess.Where("name LIKE ?", query.Name)
} }
// admins can see all snapshots, everyone else can only see their own snapshots // admins can see all snapshots, everyone else can only see their own snapshots
switch { switch {
case query.SignedInUser.OrgRole == models.ROLE_ADMIN: case query.SignedInUser.OrgRole == models.ROLE_ADMIN:
sess.Where("org_id = ?", query.OrgId) sess.Where("org_id = ?", query.OrgId)
case !query.SignedInUser.IsAnonymous: case !query.SignedInUser.IsAnonymous:
sess.Where("org_id = ? AND user_id = ?", query.OrgId, query.SignedInUser.UserId) sess.Where("org_id = ? AND user_id = ?", query.OrgId, query.SignedInUser.UserId)
default: default:
query.Result = snapshots
return nil
}
err := sess.Find(&snapshots)
query.Result = snapshots query.Result = snapshots
return nil return err
} })
err := sess.Find(&snapshots)
query.Result = snapshots
return err
} }

View File

@ -47,7 +47,7 @@ func TestDashboardSnapshotDBAccess(t *testing.T) {
t.Run("Should be able to get snapshot by key", func(t *testing.T) { t.Run("Should be able to get snapshot by key", func(t *testing.T) {
query := models.GetDashboardSnapshotQuery{Key: "hej"} query := models.GetDashboardSnapshotQuery{Key: "hej"}
err := sqlstore.GetDashboardSnapshot(&query) err := sqlstore.GetDashboardSnapshot(context.Background(), &query)
require.NoError(t, err) require.NoError(t, err)
assert.NotNil(t, query.Result) assert.NotNil(t, query.Result)
@ -69,7 +69,7 @@ func TestDashboardSnapshotDBAccess(t *testing.T) {
OrgId: 1, OrgId: 1,
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_ADMIN}, SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_ADMIN},
} }
err := sqlstore.SearchDashboardSnapshots(&query) err := sqlstore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err) require.NoError(t, err)
t.Run("Should return all the snapshots", func(t *testing.T) { t.Run("Should return all the snapshots", func(t *testing.T) {
@ -83,7 +83,7 @@ func TestDashboardSnapshotDBAccess(t *testing.T) {
OrgId: 1, OrgId: 1,
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR, UserId: 1000}, SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR, UserId: 1000},
} }
err := sqlstore.SearchDashboardSnapshots(&query) err := sqlstore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err) require.NoError(t, err)
t.Run("Should return all the snapshots", func(t *testing.T) { t.Run("Should return all the snapshots", func(t *testing.T) {
@ -97,7 +97,7 @@ func TestDashboardSnapshotDBAccess(t *testing.T) {
OrgId: 1, OrgId: 1,
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR, UserId: 2}, SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR, UserId: 2},
} }
err := sqlstore.SearchDashboardSnapshots(&query) err := sqlstore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err) require.NoError(t, err)
t.Run("Should not return any snapshots", func(t *testing.T) { t.Run("Should not return any snapshots", func(t *testing.T) {
@ -124,7 +124,7 @@ func TestDashboardSnapshotDBAccess(t *testing.T) {
OrgId: 1, OrgId: 1,
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR, IsAnonymous: true, UserId: 0}, SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_EDITOR, IsAnonymous: true, UserId: 0},
} }
err := sqlstore.SearchDashboardSnapshots(&query) err := sqlstore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err) require.NoError(t, err)
require.NotNil(t, query.Result) require.NotNil(t, query.Result)
@ -161,7 +161,7 @@ func TestDeleteExpiredSnapshots(t *testing.T) {
OrgId: 1, OrgId: 1,
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_ADMIN}, SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_ADMIN},
} }
err = sqlstore.SearchDashboardSnapshots(&query) err = sqlstore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err) require.NoError(t, err)
assert.Len(t, query.Result, 1) assert.Len(t, query.Result, 1)
@ -174,7 +174,7 @@ func TestDeleteExpiredSnapshots(t *testing.T) {
OrgId: 1, OrgId: 1,
SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_ADMIN}, SignedInUser: &models.SignedInUser{OrgRole: models.ROLE_ADMIN},
} }
err = sqlstore.SearchDashboardSnapshots(&query) err = sqlstore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err) require.NoError(t, err)
require.Len(t, query.Result, 1) require.Len(t, query.Result, 1)

View File

@ -88,7 +88,7 @@ func (m *SQLStoreMock) DeleteDashboardSnapshot(ctx context.Context, cmd *models.
return m.ExpectedError return m.ExpectedError
} }
func (m *SQLStoreMock) GetDashboardSnapshot(query *models.GetDashboardSnapshotQuery) error { func (m *SQLStoreMock) GetDashboardSnapshot(ctx context.Context, query *models.GetDashboardSnapshotQuery) error {
query.Result = m.ExpectedDashboardSnapshot query.Result = m.ExpectedDashboardSnapshot
return m.ExpectedError return m.ExpectedError
} }
@ -97,7 +97,7 @@ func (m *SQLStoreMock) HasEditPermissionInFolders(ctx context.Context, query *mo
return m.ExpectedError return m.ExpectedError
} }
func (m *SQLStoreMock) SearchDashboardSnapshots(query *models.GetDashboardSnapshotsQuery) error { func (m *SQLStoreMock) SearchDashboardSnapshots(ctx context.Context, query *models.GetDashboardSnapshotsQuery) error {
return m.ExpectedError return m.ExpectedError
} }

View File

@ -15,9 +15,9 @@ type Store interface {
DeleteExpiredSnapshots(ctx context.Context, cmd *models.DeleteExpiredSnapshotsCommand) error DeleteExpiredSnapshots(ctx context.Context, cmd *models.DeleteExpiredSnapshotsCommand) error
CreateDashboardSnapshot(ctx context.Context, cmd *models.CreateDashboardSnapshotCommand) error CreateDashboardSnapshot(ctx context.Context, cmd *models.CreateDashboardSnapshotCommand) error
DeleteDashboardSnapshot(ctx context.Context, cmd *models.DeleteDashboardSnapshotCommand) error DeleteDashboardSnapshot(ctx context.Context, cmd *models.DeleteDashboardSnapshotCommand) error
GetDashboardSnapshot(query *models.GetDashboardSnapshotQuery) error GetDashboardSnapshot(ctx context.Context, query *models.GetDashboardSnapshotQuery) error
HasEditPermissionInFolders(ctx context.Context, query *models.HasEditPermissionInFoldersQuery) error HasEditPermissionInFolders(ctx context.Context, query *models.HasEditPermissionInFoldersQuery) error
SearchDashboardSnapshots(query *models.GetDashboardSnapshotsQuery) error SearchDashboardSnapshots(ctx context.Context, query *models.GetDashboardSnapshotsQuery) error
GetOrgByName(name string) (*models.Org, error) GetOrgByName(name string) (*models.Org, error)
CreateOrgWithMember(name string, userID int64) (models.Org, error) CreateOrgWithMember(name string, userID int64) (models.Org, error)
UpdateOrg(ctx context.Context, cmd *models.UpdateOrgCommand) error UpdateOrg(ctx context.Context, cmd *models.UpdateOrgCommand) error