Chore: Ensure we save correct default admin user in integration test DB setup (#105752)

* fix helper + amend tests

* fix import + remove unused var

* remove more users

* remove unused code

* update test comment
This commit is contained in:
Will Browne
2025-05-28 11:25:01 +01:00
committed by GitHub
parent 10f2b76156
commit edb0865caa
31 changed files with 95 additions and 501 deletions
@@ -15,8 +15,6 @@ import (
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/server"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/user"
)
func TestIntegration_AdminApiReencrypt_Enterprise(t *testing.T) {
@@ -26,13 +24,6 @@ func TestIntegration_AdminApiReencrypt_Enterprise(t *testing.T) {
}
setup := func(t *testing.T, env *server.TestEnv, grafanaListenAddr string) {
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
IsAdmin: true,
})
addSetting(t, grafanaListenAddr)
}
@@ -13,20 +13,12 @@ import (
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/server"
"github.com/grafana/grafana/pkg/services/apiserver/options"
"github.com/grafana/grafana/pkg/services/authn"
"github.com/grafana/grafana/pkg/services/datasources"
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
"github.com/grafana/grafana/pkg/services/ngalert/notifier"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/org/orgimpl"
"github.com/grafana/grafana/pkg/services/quota/quotaimpl"
"github.com/grafana/grafana/pkg/services/supportbundles/supportbundlestest"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/services/user/userimpl"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tests/testinfra"
"github.com/grafana/grafana/pkg/tests/testsuite"
)
@@ -59,18 +51,12 @@ func TestIntegration_AdminApiReencrypt(t *testing.T) {
}
setup := func(t *testing.T, env *server.TestEnv, grafanaListenAddr string) {
userId := createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
dsCmd := &datasources.AddDataSourceCommand{
Name: "TestDatasource",
Type: "testdata",
Access: datasources.DS_ACCESS_DIRECT,
UID: "testuid",
UserID: userId,
UserID: 1,
OrgID: 1,
WithCredentials: true,
SecureJsonData: map[string]string{
@@ -82,7 +68,7 @@ func TestIntegration_AdminApiReencrypt(t *testing.T) {
require.NoError(t, err)
// Trigger creation of signing key
_, _, err = env.IDService.SignIdentity(context.Background(), &authn.Identity{ID: fmt.Sprintf("%d", userId), Type: claims.TypeUser})
_, _, err = env.IDService.SignIdentity(context.Background(), &authn.Identity{ID: "1", Type: claims.TypeUser})
require.NoError(t, err)
// Add alerting config with secure settings.
@@ -248,24 +234,6 @@ func verifySecrets(t *testing.T, env *server.TestEnv, before, after map[int]secr
}
}
func createUser(t *testing.T, db db.DB, cfg *setting.Cfg, cmd user.CreateUserCommand) int64 {
cfg.AutoAssignOrg = true
cfg.AutoAssignOrgId = 1
quotaService := quotaimpl.ProvideService(db, cfg)
orgService, err := orgimpl.ProvideService(db, cfg, quotaService)
require.NoError(t, err)
usrSvc, err := userimpl.ProvideService(
db, orgService, cfg, nil, nil, tracing.InitializeTracerForTest(),
quotaService, supportbundlestest.NewFakeBundleService(),
)
require.NoError(t, err)
u, err := usrSvc.Create(context.Background(), &cmd)
require.NoError(t, err)
return u.ID
}
func getSecureJsonSecrets(t *testing.T, store db.DB, table string, secureJsonDataKey string) map[int]secret {
var rows []struct {
Id int
@@ -29,13 +29,13 @@ import (
func TestIntegrationAdminConfiguration_SendingToExternalAlertmanagers(t *testing.T) {
testinfra.SQLiteIntegrationTest(t)
const disableOrgID int64 = 3
const disableOrgID int64 = 2
dir, path := testinfra.CreateGrafDir(t, testinfra.GrafanaOpts{
DisableLegacyAlerting: true,
EnableUnifiedAlerting: true,
DisableAnonymous: true,
NGAlertAdminConfigPollInterval: 2 * time.Second,
UnifiedAlertingDisabledOrgs: []int64{disableOrgID}, // disable unified alerting for organisation 3
UnifiedAlertingDisabledOrgs: []int64{disableOrgID}, // disable unified alerting for organisation 2
AppModeProduction: true,
})
@@ -37,12 +37,6 @@ func TestIntegrationSilenceAuth(t *testing.T) {
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
adminApiClient := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
// Create the namespace we'll save our alerts to.
@@ -47,11 +47,6 @@ func TestIntegrationAMConfigAccess(t *testing.T) {
Password: "editor",
Login: "editor",
})
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
type testCase struct {
desc string
@@ -365,12 +360,8 @@ func TestIntegrationAlertmanagerCreateSilence(t *testing.T) {
EnableUnifiedAlerting: true,
AppModeProduction: true,
})
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
grafanaListedAddr, _ := testinfra.StartGrafanaEnv(t, dir, path)
client := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
cases := []struct {
@@ -524,7 +515,7 @@ func TestIntegrationAlertmanagerStatus(t *testing.T) {
})
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
// Create a users to make authenticated requests
// Create users to make authenticated requests
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleViewer),
Password: "viewer",
@@ -535,11 +526,6 @@ func TestIntegrationAlertmanagerStatus(t *testing.T) {
Password: "editor",
Login: "editor",
})
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
type testCase struct {
desc string
@@ -16,7 +16,6 @@ import (
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/featuremgmt"
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/tests/testinfra"
)
@@ -35,12 +34,6 @@ func TestBacktesting(t *testing.T) {
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, grafanaPath)
userId := createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
apiCli := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
input, err := testData.ReadFile(path.Join("test-data", "api_backtesting_data.json"))
@@ -61,7 +54,7 @@ func TestBacktesting(t *testing.T) {
Type: "testdata",
Access: datasources.DS_ACCESS_PROXY,
UID: query.DatasourceUID,
UserID: userId,
UserID: 1,
OrgID: 1,
}
_, err := env.Server.HTTPServer.DataSourcesService.AddDataSource(context.Background(), dsCmd)
@@ -11,8 +11,6 @@ import (
"github.com/grafana/grafana/pkg/components/simplejson"
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/tests/testinfra"
"github.com/grafana/grafana/pkg/util"
)
@@ -34,13 +32,8 @@ func TestIntegrationConvertPrometheusNotificationSettings(t *testing.T) {
EnableRecordingRules: true,
})
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
grafanaListedAddr, _ := testinfra.StartGrafanaEnv(t, dir, path)
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
adminClient := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
ds := adminClient.CreateDatasource(t, "prometheus")
@@ -117,14 +117,9 @@ func TestIntegrationConvertPrometheusEndpoints_RecordingRuleTargetDatasource(t *
EnableRecordingRules: true,
})
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, gpath)
grafanaListedAddr, _ := testinfra.StartGrafanaEnv(t, dir, gpath)
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "password",
Login: "admin",
})
apiClient := newAlertingApiClient(grafanaListedAddr, "admin", "password")
apiClient := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
apiClient.prometheusConversionUseLokiPaths = enableLokiPaths
sourceDS := apiClient.CreateDatasource(t, datasources.DS_PROMETHEUS)
@@ -192,13 +187,7 @@ func TestIntegrationConvertPrometheusEndpoints(t *testing.T) {
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, gpath)
// Create users to make authenticated requests
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "password",
Login: "admin",
})
apiClient := newAlertingApiClient(grafanaListedAddr, "admin", "password")
apiClient := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
apiClient.prometheusConversionUseLokiPaths = enableLokiPaths
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
@@ -404,13 +393,7 @@ func TestIntegrationConvertPrometheusEndpoints_UpdateRule(t *testing.T) {
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, gpath)
// Create a user to make authenticated requests
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "password",
Login: "admin",
})
apiClient := newAlertingApiClient(grafanaListedAddr, "admin", "password")
apiClient := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
apiClient.prometheusConversionUseLokiPaths = enableLokiPaths
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
@@ -491,13 +474,7 @@ func TestIntegrationConvertPrometheusEndpoints_Conflict(t *testing.T) {
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, gpath)
// Create users to make authenticated requests
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "password",
Login: "admin",
})
apiClient := newAlertingApiClient(grafanaListedAddr, "admin", "password")
apiClient := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
apiClient.prometheusConversionUseLokiPaths = enableLokiPaths
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
@@ -577,15 +554,9 @@ func TestIntegrationConvertPrometheusEndpoints_CreatePausedRules(t *testing.T) {
EnableRecordingRules: true,
})
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
grafanaListedAddr, _ := testinfra.StartGrafanaEnv(t, dir, path)
// Create users to make authenticated requests
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "password",
Login: "admin",
})
apiClient := newAlertingApiClient(grafanaListedAddr, "admin", "password")
apiClient := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
apiClient.prometheusConversionUseLokiPaths = enableLokiPaths
ds := apiClient.CreateDatasource(t, datasources.DS_PROMETHEUS)
@@ -691,14 +662,9 @@ func TestIntegrationConvertPrometheusEndpoints_FolderUIDHeader(t *testing.T) {
EnableRecordingRules: true,
})
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
grafanaListedAddr, _ := testinfra.StartGrafanaEnv(t, dir, path)
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "password",
Login: "admin",
})
apiClient := newAlertingApiClient(grafanaListedAddr, "admin", "password")
apiClient := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
apiClient.prometheusConversionUseLokiPaths = enableLokiPaths
ds := apiClient.CreateDatasource(t, datasources.DS_PROMETHEUS)
@@ -793,15 +759,9 @@ func TestIntegrationConvertPrometheusEndpoints_Provenance(t *testing.T) {
EnableRecordingRules: true,
})
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, gpath)
grafanaListedAddr, _ := testinfra.StartGrafanaEnv(t, dir, gpath)
// Create admin user
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "password",
Login: "admin",
})
adminClient := newAlertingApiClient(grafanaListedAddr, "admin", "password")
adminClient := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
adminClient.prometheusConversionUseLokiPaths = enableLokiPaths
ds := adminClient.CreateDatasource(t, datasources.DS_PROMETHEUS)
@@ -911,13 +871,7 @@ func TestIntegrationConvertPrometheusEndpoints_Delete(t *testing.T) {
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, gpath)
// Create users with different permissions
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "password",
Login: "admin",
})
adminClient := newAlertingApiClient(grafanaListedAddr, "admin", "password")
adminClient := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
adminClient.prometheusConversionUseLokiPaths = enableLokiPaths
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
@@ -88,7 +88,7 @@ func TestIntegrationProvisioning(t *testing.T) {
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
// Create a users to make authenticated requests
// Create users to make authenticated requests
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleViewer),
Password: "viewer",
@@ -99,11 +99,6 @@ func TestIntegrationProvisioning(t *testing.T) {
Password: "editor",
Login: "editor",
})
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
apiClient := newAlertingApiClient(grafanaListedAddr, "editor", "editor")
// Create the namespace we'll save our alerts to.
@@ -575,7 +570,7 @@ func TestIntegrationProvisioningRules(t *testing.T) {
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
// Create a users to make authenticated requests
// Create users to make authenticated requests
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleViewer),
Password: "viewer",
@@ -586,11 +581,6 @@ func TestIntegrationProvisioningRules(t *testing.T) {
Password: "editor",
Login: "editor",
})
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
apiClient := newAlertingApiClient(grafanaListedAddr, "editor", "editor")
// Create the namespace we'll save our alerts to.
@@ -724,13 +714,7 @@ func TestMuteTimings(t *testing.T) {
AppModeProduction: true,
})
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
grafanaListedAddr, _ := testinfra.StartGrafanaEnv(t, dir, path)
apiClient := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
@@ -1008,16 +992,9 @@ func TestIntegrationExportFileProvision(t *testing.T) {
err := os.MkdirAll(alertingDir, 0750)
require.NoError(t, err)
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, p)
grafanaListedAddr, _ := testinfra.StartGrafanaEnv(t, dir, p)
apiClient := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
IsAdmin: true,
})
apiClient.ReloadCachedPermissions(t)
t.Run("when provisioning alert rules from files", func(t *testing.T) {
// add file provisioned alert rules
@@ -1103,16 +1080,9 @@ func TestIntegrationExportFileProvisionMixed(t *testing.T) {
err := os.MkdirAll(alertingDir, 0750)
require.NoError(t, err)
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, p)
grafanaListedAddr, _ := testinfra.StartGrafanaEnv(t, dir, p)
apiClient := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
IsAdmin: true,
})
apiClient.ReloadCachedPermissions(t)
t.Run("when provisioning mixed set of alerting configurations from files", func(t *testing.T) {
// add file provisioned mixed set of alerting configurations
@@ -1156,15 +1126,9 @@ func TestIntegrationExportFileProvisionContactPoints(t *testing.T) {
err := os.MkdirAll(alertingDir, 0750)
require.NoError(t, err)
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, p)
grafanaListedAddr, _ := testinfra.StartGrafanaEnv(t, dir, p)
apiClient := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
IsAdmin: true,
})
apiClient.ReloadCachedPermissions(t)
t.Run("when provisioning contact points from files", func(t *testing.T) {
@@ -27,13 +27,6 @@ func TestIntegrationAlertRulePauseNamespace(t *testing.T) {
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, p)
// Create a user to make authenticated requests
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleViewer),
Password: "viewer",
+6 -53
View File
@@ -799,13 +799,8 @@ func TestIntegrationAlertRuleEditorSettings(t *testing.T) {
AppModeProduction: true,
})
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
grafanaListedAddr, _ := testinfra.StartGrafanaEnv(t, dir, path)
apiClient := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
apiClient.CreateFolder(t, folderName, folderName)
createAlertInGrafana := func(metadata *apimodels.AlertRuleMetadata) apimodels.GettableRuleGroupConfig {
@@ -973,14 +968,7 @@ func TestIntegrationAlertRuleConflictingTitle(t *testing.T) {
AppModeProduction: true,
})
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
// Create user
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
grafanaListedAddr, _ := testinfra.StartGrafanaEnv(t, dir, path)
apiClient := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
@@ -1530,12 +1518,8 @@ func TestIntegrationRuleCreate(t *testing.T) {
EnableUnifiedAlerting: true,
AppModeProduction: true,
})
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
grafanaListedAddr, _ := testinfra.StartGrafanaEnv(t, dir, path)
client := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
namespaceUID := "default"
@@ -1698,13 +1682,6 @@ func TestIntegrationRuleUpdate(t *testing.T) {
require.NoError(t, err)
}
// Create a user to make authenticated requests
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
adminClient := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
client := newAlertingApiClient(grafanaListedAddr, "grafana", "password")
@@ -2117,7 +2094,7 @@ func TestIntegrationRulerAccess(t *testing.T) {
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
// Create a users to make authenticated requests
// Create users to make authenticated requests
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleViewer),
Password: "viewer",
@@ -2128,11 +2105,6 @@ func TestIntegrationRulerAccess(t *testing.T) {
Password: "editor",
Login: "editor",
})
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
client := newAlertingApiClient(grafanaListedAddr, "editor", "editor")
@@ -4638,14 +4610,7 @@ func TestIntegrationRuleUpdateAllDatabases(t *testing.T) {
DisableAnonymous: true,
AppModeProduction: true,
})
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
// Create a user to make authenticated requests
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
grafanaListedAddr, _ := testinfra.StartGrafanaEnv(t, dir, path)
client := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
@@ -4794,12 +4759,6 @@ func TestIntegrationRuleSoftDelete(t *testing.T) {
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, p)
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleEditor),
Password: "password",
@@ -4911,12 +4870,6 @@ func TestIntegrationRulePermanentlyDelete(t *testing.T) {
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, p)
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleEditor),
Password: "password",
+1 -8
View File
@@ -20,7 +20,6 @@ import (
"github.com/grafana/grafana/pkg/services/featuremgmt"
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tests/testinfra"
@@ -48,12 +47,6 @@ func TestGrafanaRuleConfig(t *testing.T) {
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
userId := createUser(t, env.SQLStore, env.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
apiCli := newAlertingApiClient(grafanaListedAddr, "admin", "admin")
apiCli.CreateFolder(t, "NamespaceUID", "NamespaceTitle")
@@ -63,7 +56,7 @@ func TestGrafanaRuleConfig(t *testing.T) {
Type: "testdata",
Access: datasources.DS_ACCESS_PROXY,
UID: TESTDATA_UID,
UserID: userId,
UserID: 1,
OrgID: 1,
}
_, err := env.Server.HTTPServer.DataSourcesService.AddDataSource(context.Background(), dsCmd)
@@ -23,8 +23,8 @@ func TestIntegrationCreateCorrelation(t *testing.T) {
adminUser := ctx.createUser(user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
Password: "admin2",
Login: "admin2",
})
editorUser := ctx.createUser(user.CreateUserCommand{
@@ -23,8 +23,8 @@ func TestIntegrationDeleteCorrelation(t *testing.T) {
adminUser := ctx.createUser(user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
Password: "admin2",
Login: "admin2",
})
editorUser := ctx.createUser(user.CreateUserCommand{
@@ -23,8 +23,8 @@ func TestIntegrationCreateOrUpdateCorrelation(t *testing.T) {
adminUser := ctx.createUser(user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
Password: "admin2",
Login: "admin2",
})
createDsCommand := &datasources.AddDataSourceCommand{
@@ -25,15 +25,15 @@ func TestIntegrationReadCorrelation(t *testing.T) {
adminUser := ctx.createUser(user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
Password: "admin2",
Login: "admin2",
})
otherOrgId := ctx.createOrg("New organization")
otherOrgUser := ctx.createUser(user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin2",
Login: "admin2",
Password: "admin3",
Login: "admin3",
OrgID: otherOrgId,
})
@@ -24,7 +24,7 @@ func TestIntegrationUpdateCorrelation(t *testing.T) {
adminUser := ctx.createUser(user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
Login: "admin2",
})
editorUser := ctx.createUser(user.CreateUserCommand{
@@ -2,7 +2,6 @@ package dashboards
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
@@ -18,21 +17,12 @@ import (
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/services/dashboardimport"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/folder"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/org/orgimpl"
"github.com/grafana/grafana/pkg/services/plugindashboards"
"github.com/grafana/grafana/pkg/services/quota/quotaimpl"
"github.com/grafana/grafana/pkg/services/search/model"
"github.com/grafana/grafana/pkg/services/supportbundles/supportbundlestest"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/services/user/userimpl"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tests/testinfra"
"github.com/grafana/grafana/pkg/tests/testsuite"
"github.com/grafana/grafana/pkg/util"
@@ -67,14 +57,7 @@ func testDashboardQuota(t *testing.T, featureToggles []string) {
EnableFeatureToggles: featureToggles,
})
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
store, cfg := env.SQLStore, env.Cfg
// Create user
createUser(t, store, cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
grafanaListedAddr, _ := testinfra.StartGrafanaEnv(t, dir, path)
t.Run("when quota limit doesn't exceed, importing a dashboard should succeed", func(t *testing.T) {
// Import dashboard
@@ -126,26 +109,6 @@ func testDashboardQuota(t *testing.T, featureToggles []string) {
})
}
func createUser(t *testing.T, db db.DB, cfg *setting.Cfg, cmd user.CreateUserCommand) int64 {
t.Helper()
cfg.AutoAssignOrg = true
cfg.AutoAssignOrgId = 1
quotaService := quotaimpl.ProvideService(db, cfg)
orgService, err := orgimpl.ProvideService(db, cfg, quotaService)
require.NoError(t, err)
usrSvc, err := userimpl.ProvideService(
db, orgService, cfg, nil, nil, tracing.InitializeTracerForTest(),
quotaService, supportbundlestest.NewFakeBundleService(),
)
require.NoError(t, err)
u, err := usrSvc.Create(context.Background(), &cmd)
require.NoError(t, err)
return u.ID
}
func TestIntegrationUpdatingProvisionionedDashboards(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
@@ -188,14 +151,7 @@ providers:
provDashboardFile := filepath.Join(provDashboardsDir, "home.json")
err = os.WriteFile(provDashboardFile, input, 0644)
require.NoError(t, err)
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
store, cfg := env.SQLStore, env.Cfg
// Create user
createUser(t, store, cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
grafanaListedAddr, _ := testinfra.StartGrafanaEnv(t, dir, path)
// give provisioner some time since we don't have a way to know when provisioning is complete
// TODO https://github.com/grafana/grafana/issues/85617
@@ -355,14 +311,7 @@ func testCreate(t *testing.T, featureToggles []string) {
EnableFeatureToggles: featureToggles,
})
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
store, cfg := env.SQLStore, env.Cfg
// Create user
createUser(t, store, cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
grafanaListedAddr, _ := testinfra.StartGrafanaEnv(t, dir, path)
t.Run("create dashboard should succeed", func(t *testing.T) {
dashboardDataOne, err := simplejson.NewJson([]byte(`{"title":"just testing"}`))
@@ -518,14 +467,7 @@ func testPreserveSchemaVersion(t *testing.T, featureToggles []string) {
EnableFeatureToggles: featureToggles,
})
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
store, cfg := env.SQLStore, env.Cfg
createUser(t, store, cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
grafanaListedAddr, _ := testinfra.StartGrafanaEnv(t, dir, path)
schemaVersions := []*int{intPtr(1), intPtr(36), intPtr(40), nil}
for _, schemaVersion := range schemaVersions {
@@ -15,8 +15,6 @@ import (
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/tests/testinfra"
"github.com/grafana/grafana/pkg/tests/testsuite"
)
@@ -36,12 +34,6 @@ func TestIntegrationElasticsearch(t *testing.T) {
grafanaListeningAddr, testEnv := testinfra.StartGrafanaEnv(t, dir, path)
ctx := context.Background()
u := testinfra.CreateUser(t, testEnv.SQLStore, testEnv.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
var outgoingRequest *http.Request
outgoingServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
outgoingRequest = r
@@ -61,7 +53,7 @@ func TestIntegrationElasticsearch(t *testing.T) {
uid := "es"
_, err := testEnv.Server.HTTPServer.DataSourcesService.AddDataSource(ctx, &datasources.AddDataSourceCommand{
OrgID: u.OrgID,
OrgID: 1,
Access: datasources.DS_ACCESS_PROXY,
Name: "Elasticsearch",
Type: datasources.DS_ES,
+3 -56
View File
@@ -1,29 +1,17 @@
package folders
import (
"context"
"net/http"
"testing"
"github.com/grafana/grafana-openapi-client-go/models"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/org/orgimpl"
"github.com/grafana/grafana/pkg/services/quota/quotaimpl"
"github.com/grafana/grafana/pkg/services/supportbundles/supportbundlestest"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/services/user/userimpl"
"github.com/grafana/grafana/pkg/setting"
"github.com/grafana/grafana/pkg/tests"
"github.com/grafana/grafana/pkg/tests/testinfra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const orgID = 1
func TestIntegrationUpdateFolder(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
@@ -45,14 +33,7 @@ func testUpdateFolder(t *testing.T, featureToggles []string) {
EnableFeatureToggles: featureToggles,
})
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
store, cfg := env.SQLStore, env.Cfg
// Create user
createUser(t, store, cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
grafanaListedAddr, _ := testinfra.StartGrafanaEnv(t, dir, path)
adminClient := tests.GetClient(grafanaListedAddr, "admin", "admin")
resp, err := adminClient.Folders.CreateFolder(&models.CreateFolderCommand{
@@ -93,14 +74,7 @@ func testCreateFolder(t *testing.T, featureToggles []string) {
EnableFeatureToggles: featureToggles,
})
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
store, cfg := env.SQLStore, env.Cfg
// Create user
createUser(t, store, cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
grafanaListedAddr, _ := testinfra.StartGrafanaEnv(t, dir, path)
adminClient := tests.GetClient(grafanaListedAddr, "admin", "admin")
@@ -142,14 +116,7 @@ func testNestedFoldersOn(t *testing.T, featureToggles []string) {
EnableFeatureToggles: featureToggles,
})
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
store, cfg := env.SQLStore, env.Cfg
// Create user
createUser(t, store, cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
grafanaListedAddr, _ := testinfra.StartGrafanaEnv(t, dir, path)
adminClient := tests.GetClient(grafanaListedAddr, "admin", "admin")
@@ -228,23 +195,3 @@ func testNestedFoldersOn(t *testing.T, featureToggles []string) {
})
})
}
func createUser(t *testing.T, db db.DB, cfg *setting.Cfg, cmd user.CreateUserCommand) int64 {
t.Helper()
cfg.AutoAssignOrg = true
cfg.AutoAssignOrgId = orgID
quotaService := quotaimpl.ProvideService(db, cfg)
orgService, err := orgimpl.ProvideService(db, cfg, quotaService)
require.NoError(t, err)
usrSvc, err := userimpl.ProvideService(
db, orgService, cfg, nil, nil, tracing.InitializeTracerForTest(),
quotaService, supportbundlestest.NewFakeBundleService(),
)
require.NoError(t, err)
u, err := usrSvc.Create(context.Background(), &cmd)
require.NoError(t, err)
return u.ID
}
+2 -2
View File
@@ -65,10 +65,10 @@ func testGetFolders(t *testing.T, featureToggles []string) {
OrgID: orgID,
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
Login: "admin2",
})
adminClient := tests.GetClient(grafanaListedAddr, "admin", "admin")
adminClient := tests.GetClient(grafanaListedAddr, "admin2", "admin")
editorClient := tests.GetClient(grafanaListedAddr, "editor", "editor")
viewerClient := tests.GetClient(grafanaListedAddr, "viewer", "viewer")
+1 -9
View File
@@ -15,8 +15,6 @@ import (
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/tests/testinfra"
"github.com/grafana/grafana/pkg/tests/testsuite"
)
@@ -36,12 +34,6 @@ func TestIntegrationGraphite(t *testing.T) {
grafanaListeningAddr, testEnv := testinfra.StartGrafanaEnv(t, dir, path)
ctx := context.Background()
u := testinfra.CreateUser(t, testEnv.SQLStore, testEnv.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
var outgoingRequest *http.Request
outgoingServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
outgoingRequest = r
@@ -60,7 +52,7 @@ func TestIntegrationGraphite(t *testing.T) {
uid := "graphite"
_, err := testEnv.Server.HTTPServer.DataSourcesService.AddDataSource(ctx, &datasources.AddDataSourceCommand{
OrgID: u.OrgID,
OrgID: 1,
Access: datasources.DS_ACCESS_PROXY,
Name: "graphite",
Type: datasources.DS_GRAPHITE,
+1 -9
View File
@@ -16,8 +16,6 @@ import (
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/featuremgmt"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/tests/testinfra"
"github.com/grafana/grafana/pkg/tests/testsuite"
)
@@ -38,12 +36,6 @@ func TestIntegrationLoki(t *testing.T) {
grafanaListeningAddr, testEnv := testinfra.StartGrafanaEnv(t, dir, path)
ctx := context.Background()
u := testinfra.CreateUser(t, testEnv.SQLStore, testEnv.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
var outgoingRequest *http.Request
outgoingServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
outgoingRequest = r
@@ -62,7 +54,7 @@ func TestIntegrationLoki(t *testing.T) {
uid := "loki"
_, err := testEnv.Server.HTTPServer.DataSourcesService.AddDataSource(ctx, &datasources.AddDataSourceCommand{
OrgID: u.OrgID,
OrgID: 1,
Access: datasources.DS_ACCESS_PROXY,
Name: "Loki",
Type: datasources.DS_LOKI,
+1 -9
View File
@@ -15,8 +15,6 @@ import (
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/tests/testinfra"
"github.com/grafana/grafana/pkg/tests/testsuite"
)
@@ -36,12 +34,6 @@ func TestIntegrationOpenTSDB(t *testing.T) {
grafanaListeningAddr, testEnv := testinfra.StartGrafanaEnv(t, dir, path)
ctx := context.Background()
u := testinfra.CreateUser(t, testEnv.SQLStore, testEnv.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
var outgoingRequest *http.Request
outgoingServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
outgoingRequest = r
@@ -60,7 +52,7 @@ func TestIntegrationOpenTSDB(t *testing.T) {
uid := "influxdb"
_, err := testEnv.Server.HTTPServer.DataSourcesService.AddDataSource(ctx, &datasources.AddDataSourceCommand{
OrgID: u.OrgID,
OrgID: 1,
Access: datasources.DS_ACCESS_PROXY,
Name: "opentsdb",
Type: datasources.DS_OPENTSDB,
+1 -1
View File
@@ -31,7 +31,7 @@ import (
)
const (
usernameAdmin = "admin"
usernameAdmin = "otherAdmin"
usernameNonAdmin = "nonAdmin"
defaultPassword = "password"
)
@@ -24,8 +24,6 @@ import (
"github.com/grafana/grafana/pkg/plugins/log"
"github.com/grafana/grafana/pkg/server"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/tests/testinfra"
"github.com/grafana/grafana/pkg/tests/testsuite"
)
@@ -486,12 +484,6 @@ func newTestScenario(t *testing.T, name string, opts []testScenarioOption, callb
tsCtx.testEnv = testEnv
ctx := context.Background()
u := testinfra.CreateUser(t, testEnv.SQLStore, testEnv.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
tsCtx.outgoingServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tsCtx.outgoingRequest = r
w.WriteHeader(http.StatusUnauthorized)
@@ -508,7 +500,7 @@ func newTestScenario(t *testing.T, name string, opts []testScenarioOption, callb
tsCtx.uid = "test-plugin"
cmd := &datasources.AddDataSourceCommand{
OrgID: u.OrgID,
OrgID: 1,
Access: datasources.DS_ACCESS_PROXY,
Name: "TestPlugin",
Type: tsCtx.testPluginID,
@@ -550,7 +542,7 @@ func newTestScenario(t *testing.T, name string, opts []testScenarioOption, callb
require.NoError(t, err)
getDataSourceQuery := &datasources.GetDataSourceQuery{
OrgID: u.OrgID,
OrgID: 1,
UID: tsCtx.uid,
}
dataSource, err := testEnv.Server.HTTPServer.DataSourcesService.GetDataSource(ctx, getDataSourceQuery)
+1 -9
View File
@@ -14,8 +14,6 @@ import (
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/tests/testinfra"
"github.com/grafana/grafana/pkg/tests/testsuite"
)
@@ -35,12 +33,6 @@ func TestIntegrationPrometheus(t *testing.T) {
grafanaListeningAddr, testEnv := testinfra.StartGrafanaEnv(t, dir, path)
ctx := context.Background()
u := testinfra.CreateUser(t, testEnv.SQLStore, testEnv.Cfg, user.CreateUserCommand{
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
var outgoingRequest *http.Request
outgoingServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
outgoingRequest = r
@@ -60,7 +52,7 @@ func TestIntegrationPrometheus(t *testing.T) {
uid := "prometheus"
_, err := testEnv.Server.HTTPServer.DataSourcesService.AddDataSource(ctx, &datasources.AddDataSourceCommand{
OrgID: u.OrgID,
OrgID: 1,
Access: datasources.DS_ACCESS_PROXY,
Name: "Prometheus",
Type: datasources.DS_PROMETHEUS,