refactor public dashboards middleware testing (#55706)
This PR refactors how we add the orgId to the context on a public dashboard paths. We also split out accessToken handling into its own package and rework status code for "RequiresValidAccessToken". We will be modeling all endpoints to use these status codes going forward. Additionally, it includes a scaffold for better middleware testing and refactors existing tests to table drive tests.
This commit is contained in:
@@ -52,8 +52,8 @@ func TestIntegrationGetDashboard(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// GetPublicDashboard
|
||||
func TestIntegrationGetPublicDashboard(t *testing.T) {
|
||||
// AccessTokenExists
|
||||
func TestIntegrationAccessTokenExists(t *testing.T) {
|
||||
var sqlStore *sqlstore.SQLStore
|
||||
var dashboardStore *dashboardsDB.DashboardStore
|
||||
var publicdashboardStore *PublicDashboardStoreImpl
|
||||
@@ -87,6 +87,28 @@ func TestIntegrationGetPublicDashboard(t *testing.T) {
|
||||
require.True(t, res)
|
||||
})
|
||||
|
||||
t.Run("AccessTokenExists will return false when IsEnabled=false", func(t *testing.T) {
|
||||
setup()
|
||||
|
||||
err := publicdashboardStore.SavePublicDashboardConfig(context.Background(), SavePublicDashboardConfigCommand{
|
||||
PublicDashboard: PublicDashboard{
|
||||
IsEnabled: false,
|
||||
Uid: "abc123",
|
||||
DashboardUid: savedDashboard.Uid,
|
||||
OrgId: savedDashboard.OrgId,
|
||||
CreatedAt: time.Now(),
|
||||
CreatedBy: 7,
|
||||
AccessToken: "accessToken",
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
res, err := publicdashboardStore.AccessTokenExists(context.Background(), "accessToken")
|
||||
require.NoError(t, err)
|
||||
|
||||
require.False(t, res)
|
||||
})
|
||||
|
||||
t.Run("AccessTokenExists will return false when no public dashboard has matching access token", func(t *testing.T) {
|
||||
setup()
|
||||
|
||||
@@ -95,6 +117,21 @@ func TestIntegrationGetPublicDashboard(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.False(t, res)
|
||||
})
|
||||
}
|
||||
|
||||
// PublicDashboardEnabled
|
||||
func TestIntegrationPublicDashboardEnabled(t *testing.T) {
|
||||
var sqlStore *sqlstore.SQLStore
|
||||
var dashboardStore *dashboardsDB.DashboardStore
|
||||
var publicdashboardStore *PublicDashboardStoreImpl
|
||||
var savedDashboard *models.Dashboard
|
||||
|
||||
setup := func() {
|
||||
sqlStore = sqlstore.InitTestDB(t)
|
||||
dashboardStore = dashboardsDB.ProvideDashboardStore(sqlStore, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore, sqlStore.Cfg))
|
||||
publicdashboardStore = ProvideStore(sqlStore)
|
||||
savedDashboard = insertTestDashboard(t, dashboardStore, "testDashie", 1, 0, true)
|
||||
}
|
||||
|
||||
t.Run("PublicDashboardEnabled Will return true when dashboard has at least one enabled public dashboard", func(t *testing.T) {
|
||||
setup()
|
||||
@@ -139,6 +176,21 @@ func TestIntegrationGetPublicDashboard(t *testing.T) {
|
||||
|
||||
require.False(t, res)
|
||||
})
|
||||
}
|
||||
|
||||
// GetPublicDashboard
|
||||
func TestIntegrationGetPublicDashboard(t *testing.T) {
|
||||
var sqlStore *sqlstore.SQLStore
|
||||
var dashboardStore *dashboardsDB.DashboardStore
|
||||
var publicdashboardStore *PublicDashboardStoreImpl
|
||||
var savedDashboard *models.Dashboard
|
||||
|
||||
setup := func() {
|
||||
sqlStore = sqlstore.InitTestDB(t)
|
||||
dashboardStore = dashboardsDB.ProvideDashboardStore(sqlStore, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore, sqlStore.Cfg))
|
||||
publicdashboardStore = ProvideStore(sqlStore)
|
||||
savedDashboard = insertTestDashboard(t, dashboardStore, "testDashie", 1, 0, true)
|
||||
}
|
||||
|
||||
t.Run("returns PublicDashboard and Dashboard", func(t *testing.T) {
|
||||
setup()
|
||||
@@ -310,6 +362,7 @@ func TestIntegrationSavePublicDashboardConfig(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// UpdatePublicDashboardConfig
|
||||
func TestIntegrationUpdatePublicDashboard(t *testing.T) {
|
||||
var sqlStore *sqlstore.SQLStore
|
||||
var dashboardStore *dashboardsDB.DashboardStore
|
||||
@@ -389,6 +442,71 @@ func TestIntegrationUpdatePublicDashboard(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// GetPublicDashboardOrgId
|
||||
func TestIntegrationGetPublicDashboardOrgId(t *testing.T) {
|
||||
var sqlStore *sqlstore.SQLStore
|
||||
var dashboardStore *dashboardsDB.DashboardStore
|
||||
var publicdashboardStore *PublicDashboardStoreImpl
|
||||
var savedDashboard *models.Dashboard
|
||||
|
||||
setup := func() {
|
||||
sqlStore = sqlstore.InitTestDB(t)
|
||||
dashboardStore = dashboardsDB.ProvideDashboardStore(sqlStore, featuremgmt.WithFeatures(), tagimpl.ProvideService(sqlStore, sqlStore.Cfg))
|
||||
publicdashboardStore = ProvideStore(sqlStore)
|
||||
savedDashboard = insertTestDashboard(t, dashboardStore, "testDashie", 1, 0, true)
|
||||
}
|
||||
t.Run("GetPublicDashboardOrgId will OrgId when enabled", func(t *testing.T) {
|
||||
setup()
|
||||
|
||||
err := publicdashboardStore.SavePublicDashboardConfig(context.Background(), SavePublicDashboardConfigCommand{
|
||||
PublicDashboard: PublicDashboard{
|
||||
IsEnabled: true,
|
||||
Uid: "abc123",
|
||||
DashboardUid: savedDashboard.Uid,
|
||||
OrgId: savedDashboard.OrgId,
|
||||
CreatedAt: time.Now(),
|
||||
CreatedBy: 7,
|
||||
AccessToken: "accessToken",
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
orgId, err := publicdashboardStore.GetPublicDashboardOrgId(context.Background(), "accessToken")
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, savedDashboard.OrgId, orgId)
|
||||
})
|
||||
|
||||
t.Run("GetPublicDashboardOrgId will return 0 when IsEnabled=false", func(t *testing.T) {
|
||||
setup()
|
||||
|
||||
err := publicdashboardStore.SavePublicDashboardConfig(context.Background(), SavePublicDashboardConfigCommand{
|
||||
PublicDashboard: PublicDashboard{
|
||||
IsEnabled: false,
|
||||
Uid: "abc123",
|
||||
DashboardUid: savedDashboard.Uid,
|
||||
OrgId: savedDashboard.OrgId,
|
||||
CreatedAt: time.Now(),
|
||||
CreatedBy: 7,
|
||||
AccessToken: "accessToken",
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
orgId, err := publicdashboardStore.GetPublicDashboardOrgId(context.Background(), "accessToken")
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, savedDashboard.OrgId, orgId)
|
||||
})
|
||||
|
||||
t.Run("GetPublicDashboardOrgId will return 0 when no public dashboard has matching access token", func(t *testing.T) {
|
||||
setup()
|
||||
|
||||
orgId, err := publicdashboardStore.GetPublicDashboardOrgId(context.Background(), "nonExistentAccessToken")
|
||||
require.NoError(t, err)
|
||||
assert.NotEqual(t, savedDashboard.OrgId, orgId)
|
||||
})
|
||||
}
|
||||
|
||||
// helper function insertTestDashboard
|
||||
func insertTestDashboard(t *testing.T, dashboardStore *dashboardsDB.DashboardStore, title string, orgId int64,
|
||||
folderId int64, isFolder bool, tags ...interface{}) *models.Dashboard {
|
||||
t.Helper()
|
||||
|
||||
Reference in New Issue
Block a user