Chore: Remove Result field from dashboard snapshot mode (#62089)

Chore: Remove Result field from dashboard snapshot mode;
This commit is contained in:
idafurjes
2023-01-25 15:09:44 +01:00
committed by GitHub
parent 13de1afcbe
commit 529e6c379f
12 changed files with 220 additions and 182 deletions
@@ -43,23 +43,23 @@ func TestIntegrationDashboardSnapshotDBAccess(t *testing.T) {
cmd := dashboardsnapshots.CreateDashboardSnapshotCommand{
Key: "hej",
DashboardEncrypted: encryptedDashboard,
UserId: 1000,
OrgId: 1,
UserID: 1000,
OrgID: 1,
}
err = dashStore.CreateDashboardSnapshot(context.Background(), &cmd)
result, err := dashStore.CreateDashboardSnapshot(context.Background(), &cmd)
require.NoError(t, err)
t.Run("Should be able to get snapshot by key", func(t *testing.T) {
query := dashboardsnapshots.GetDashboardSnapshotQuery{Key: "hej"}
err := dashStore.GetDashboardSnapshot(context.Background(), &query)
queryResult, err := dashStore.GetDashboardSnapshot(context.Background(), &query)
require.NoError(t, err)
assert.NotNil(t, query.Result)
assert.NotNil(t, queryResult)
decryptedDashboard, err := secretsService.Decrypt(
context.Background(),
query.Result.DashboardEncrypted,
queryResult.DashboardEncrypted,
)
require.NoError(t, err)
@@ -71,43 +71,43 @@ func TestIntegrationDashboardSnapshotDBAccess(t *testing.T) {
t.Run("And the user has the admin role", func(t *testing.T) {
query := dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
OrgID: 1,
SignedInUser: &user.SignedInUser{OrgRole: org.RoleAdmin},
}
err := dashStore.SearchDashboardSnapshots(context.Background(), &query)
queryResult, err := dashStore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err)
t.Run("Should return all the snapshots", func(t *testing.T) {
assert.NotNil(t, query.Result)
assert.Len(t, query.Result, 1)
assert.NotNil(t, queryResult)
assert.Len(t, queryResult, 1)
})
})
t.Run("And the user has the editor role and has created a snapshot", func(t *testing.T) {
query := dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
OrgID: 1,
SignedInUser: &user.SignedInUser{OrgRole: org.RoleEditor, UserID: 1000},
}
err := dashStore.SearchDashboardSnapshots(context.Background(), &query)
queryResult, err := dashStore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err)
t.Run("Should return all the snapshots", func(t *testing.T) {
require.NotNil(t, query.Result)
assert.Len(t, query.Result, 1)
require.NotNil(t, queryResult)
assert.Len(t, queryResult, 1)
})
})
t.Run("And the user has the editor role and has not created any snapshot", func(t *testing.T) {
query := dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
OrgID: 1,
SignedInUser: &user.SignedInUser{OrgRole: org.RoleEditor, UserID: 2},
}
err := dashStore.SearchDashboardSnapshots(context.Background(), &query)
queryResult, err := dashStore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err)
t.Run("Should not return any snapshots", func(t *testing.T) {
require.NotNil(t, query.Result)
assert.Empty(t, query.Result)
require.NotNil(t, queryResult)
assert.Empty(t, queryResult)
})
})
@@ -118,29 +118,29 @@ func TestIntegrationDashboardSnapshotDBAccess(t *testing.T) {
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"hello": "mupp",
}),
UserId: 0,
OrgId: 1,
UserID: 0,
OrgID: 1,
}
err := dashStore.CreateDashboardSnapshot(context.Background(), &cmd)
_, err := dashStore.CreateDashboardSnapshot(context.Background(), &cmd)
require.NoError(t, err)
t.Run("Should not return any snapshots", func(t *testing.T) {
query := dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
OrgID: 1,
SignedInUser: &user.SignedInUser{OrgRole: org.RoleEditor, IsAnonymous: true, UserID: 0},
}
err := dashStore.SearchDashboardSnapshots(context.Background(), &query)
queryResult, err := dashStore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err)
require.NotNil(t, query.Result)
assert.Empty(t, query.Result)
require.NotNil(t, queryResult)
assert.Empty(t, queryResult)
})
})
t.Run("Should have encrypted dashboard data", func(t *testing.T) {
decryptedDashboard, err := secretsService.Decrypt(
context.Background(),
cmd.Result.DashboardEncrypted,
result.DashboardEncrypted,
)
require.NoError(t, err)
@@ -167,27 +167,27 @@ func TestIntegrationDeleteExpiredSnapshots(t *testing.T) {
require.NoError(t, err)
query := dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
OrgID: 1,
SignedInUser: &user.SignedInUser{OrgRole: org.RoleAdmin},
}
err = dashStore.SearchDashboardSnapshots(context.Background(), &query)
queryResult, err := dashStore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err)
assert.Len(t, query.Result, 1)
assert.Equal(t, nonExpiredSnapshot.Key, query.Result[0].Key)
assert.Len(t, queryResult, 1)
assert.Equal(t, nonExpiredSnapshot.Key, queryResult[0].Key)
err = dashStore.DeleteExpiredSnapshots(context.Background(), &dashboardsnapshots.DeleteExpiredSnapshotsCommand{})
require.NoError(t, err)
query = dashboardsnapshots.GetDashboardSnapshotsQuery{
OrgId: 1,
OrgID: 1,
SignedInUser: &user.SignedInUser{OrgRole: org.RoleAdmin},
}
err = dashStore.SearchDashboardSnapshots(context.Background(), &query)
queryResult, err = dashStore.SearchDashboardSnapshots(context.Background(), &query)
require.NoError(t, err)
require.Len(t, query.Result, 1)
require.Equal(t, nonExpiredSnapshot.Key, query.Result[0].Key)
require.Len(t, queryResult, 1)
require.Equal(t, nonExpiredSnapshot.Key, queryResult[0].Key)
})
}
@@ -198,22 +198,22 @@ func createTestSnapshot(t *testing.T, dashStore *DashboardSnapshotStore, key str
Dashboard: simplejson.NewFromAny(map[string]interface{}{
"hello": "mupp",
}),
UserId: 1000,
OrgId: 1,
UserID: 1000,
OrgID: 1,
Expires: expires,
}
err := dashStore.CreateDashboardSnapshot(context.Background(), &cmd)
result, err := dashStore.CreateDashboardSnapshot(context.Background(), &cmd)
require.NoError(t, err)
// Set expiry date manually - to be able to create expired snapshots
if expires < 0 {
expireDate := time.Now().Add(time.Second * time.Duration(expires))
err = dashStore.store.WithDbSession(context.Background(), func(sess *db.Session) error {
_, err := sess.Exec("UPDATE dashboard_snapshot SET expires = ? WHERE id = ?", expireDate, cmd.Result.Id)
_, err := sess.Exec("UPDATE dashboard_snapshot SET expires = ? WHERE id = ?", expireDate, result.ID)
return err
})
require.NoError(t, err)
}
return cmd.Result
return result
}