Move SignedInUser to user service and RoleType and Roles to org (#53445)

* Move SignedInUser to user service and RoleType and Roles to org

* Use go naming convention for roles

* Fix some imports and leftovers

* Fix ldap debug test

* Fix lint

* Fix lint 2

* Fix lint 3

* Fix type and not needed conversion

* Clean up messages in api tests

* Clean up api tests 2
This commit is contained in:
idafurjes
2022-08-10 11:56:48 +02:00
committed by GitHub
parent 46004037e2
commit 6afad51761
278 changed files with 1758 additions and 1543 deletions
@@ -12,10 +12,10 @@ import (
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/models"
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/ngalert/sender"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/tests/testinfra"
)
@@ -35,7 +35,7 @@ func TestAdminConfiguration_SendingToExternalAlertmanagers(t *testing.T) {
// Create a user to make authenticated requests
userID := createUser(t, s, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_ADMIN),
DefaultOrgRole: string(org.RoleAdmin),
Login: "grafana",
Password: "password",
})
@@ -47,7 +47,7 @@ func TestAdminConfiguration_SendingToExternalAlertmanagers(t *testing.T) {
// create user under different organisation
createUser(t, s, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_ADMIN),
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin-42",
Login: "admin-42",
OrgID: orgID,
@@ -9,8 +9,8 @@ import (
"testing"
"time"
"github.com/grafana/grafana/pkg/models"
"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/stretchr/testify/assert"
@@ -33,7 +33,7 @@ func TestAlertmanagerConfigurationIsTransactional(t *testing.T) {
// create user under main organisation
userID := createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: "editor",
Login: "editor",
})
@@ -43,7 +43,7 @@ func TestAlertmanagerConfigurationIsTransactional(t *testing.T) {
// create user under different organisation
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: "editor-42",
Login: "editor-42",
OrgID: orgID,
@@ -141,7 +141,7 @@ func TestAlertmanagerConfigurationPersistSecrets(t *testing.T) {
alertConfigURL := fmt.Sprintf("http://editor:editor@%s/api/alertmanager/grafana/config/api/v1/alerts", grafanaListedAddr)
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: "editor",
Login: "editor",
})
+13 -12
View File
@@ -20,6 +20,7 @@ import (
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
ngstore "github.com/grafana/grafana/pkg/services/ngalert/store"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/setting"
@@ -43,17 +44,17 @@ func TestAMConfigAccess(t *testing.T) {
// Create a users to make authenticated requests
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_VIEWER),
DefaultOrgRole: string(org.RoleViewer),
Password: "viewer",
Login: "viewer",
})
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: "editor",
Login: "editor",
})
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_ADMIN),
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
@@ -417,7 +418,7 @@ func TestAlertAndGroupsQuery(t *testing.T) {
// Create a user to make authenticated requests
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: "password",
Login: "grafana",
})
@@ -561,17 +562,17 @@ func TestRulerAccess(t *testing.T) {
// Create a users to make authenticated requests
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_VIEWER),
DefaultOrgRole: string(org.RoleViewer),
Password: "viewer",
Login: "viewer",
})
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: "editor",
Login: "editor",
})
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_ADMIN),
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
@@ -675,12 +676,12 @@ func TestDeleteFolderWithRules(t *testing.T) {
grafanaListedAddr, store := testinfra.StartGrafana(t, dir, path)
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_VIEWER),
DefaultOrgRole: string(org.RoleViewer),
Password: "viewer",
Login: "viewer",
})
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: "editor",
Login: "editor",
})
@@ -835,7 +836,7 @@ func TestAlertRuleCRUD(t *testing.T) {
grafanaListedAddr, store := testinfra.StartGrafana(t, dir, path)
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: "password",
Login: "grafana",
})
@@ -1878,7 +1879,7 @@ func TestQuota(t *testing.T) {
// Create a user to make authenticated requests
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: "password",
Login: "grafana",
})
@@ -2094,7 +2095,7 @@ func TestEval(t *testing.T) {
grafanaListedAddr, store := testinfra.StartGrafana(t, dir, path)
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: "password",
Login: "grafana",
})
@@ -9,8 +9,8 @@ import (
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/ngalert/notifier/channels_config"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/tests/testinfra"
)
@@ -27,7 +27,7 @@ func TestAvailableChannels(t *testing.T) {
// Create a user to make authenticated requests
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: "password",
Login: "grafana",
})
@@ -26,6 +26,7 @@ import (
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
"github.com/grafana/grafana/pkg/services/ngalert/notifier/channels"
"github.com/grafana/grafana/pkg/services/ngalert/store"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/tests/testinfra"
@@ -43,7 +44,7 @@ func TestTestReceivers(t *testing.T) {
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
createUser(t, env.SQLStore, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Login: "grafana",
Password: "password",
})
@@ -77,7 +78,7 @@ func TestTestReceivers(t *testing.T) {
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
createUser(t, env.SQLStore, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Login: "grafana",
Password: "password",
})
@@ -159,7 +160,7 @@ func TestTestReceivers(t *testing.T) {
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
createUser(t, env.SQLStore, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Login: "grafana",
Password: "password",
})
@@ -236,7 +237,7 @@ func TestTestReceivers(t *testing.T) {
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
createUser(t, env.SQLStore, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Login: "grafana",
Password: "password",
})
@@ -323,7 +324,7 @@ func TestTestReceivers(t *testing.T) {
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
createUser(t, env.SQLStore, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Login: "grafana",
Password: "password",
})
@@ -436,7 +437,7 @@ func TestTestReceiversAlertCustomization(t *testing.T) {
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
createUser(t, env.SQLStore, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Login: "grafana",
Password: "password",
})
@@ -529,7 +530,7 @@ func TestTestReceiversAlertCustomization(t *testing.T) {
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
createUser(t, env.SQLStore, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Login: "grafana",
Password: "password",
})
@@ -617,7 +618,7 @@ func TestTestReceiversAlertCustomization(t *testing.T) {
grafanaListedAddr, env := testinfra.StartGrafanaEnv(t, dir, path)
createUser(t, env.SQLStore, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Login: "grafana",
Password: "password",
})
@@ -740,7 +741,7 @@ func TestNotificationChannels(t *testing.T) {
// Create a user to make authenticated requests
createUser(t, env.SQLStore, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: "password",
Login: "grafana",
})
@@ -15,12 +15,12 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/accesscontrol"
acdb "github.com/grafana/grafana/pkg/services/accesscontrol/database"
"github.com/grafana/grafana/pkg/services/accesscontrol/resourcepermissions/types"
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/tests/testinfra"
)
@@ -37,7 +37,7 @@ func TestPrometheusRules(t *testing.T) {
// Create a user to make authenticated requests
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: "password",
Login: "grafana",
})
@@ -331,7 +331,7 @@ func TestPrometheusRulesFilterByDashboard(t *testing.T) {
// Create a user to make authenticated requests
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: "password",
Login: "grafana",
})
@@ -625,7 +625,7 @@ func TestPrometheusRulesPermissions(t *testing.T) {
// Create a user to make authenticated requests
userID := createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: "password",
Login: "grafana",
})
@@ -673,8 +673,8 @@ func TestPrometheusRulesPermissions(t *testing.T) {
require.Equal(t, "folder2", body.Data.Groups[1].File)
}
// remove permissions from folder2
removeFolderPermission(t, permissionsStore, 1, userID, models.ROLE_EDITOR, "folder2")
// remove permissions from folder2org.ROLE
removeFolderPermission(t, permissionsStore, 1, userID, org.RoleEditor, "folder2")
apiClient.ReloadCachedPermissions(t)
// make sure that folder2 is not included in the response
@@ -698,8 +698,8 @@ func TestPrometheusRulesPermissions(t *testing.T) {
require.Equal(t, "folder1", body.Data.Groups[0].File)
}
// remove permissions from folder1
removeFolderPermission(t, permissionsStore, 1, userID, models.ROLE_EDITOR, "folder1")
// remove permissions from folder1org.ROLE
removeFolderPermission(t, permissionsStore, 1, userID, org.RoleEditor, "folder1")
apiClient.ReloadCachedPermissions(t)
// make sure that no folders are included in the response
@@ -726,7 +726,7 @@ func TestPrometheusRulesPermissions(t *testing.T) {
}
}
func removeFolderPermission(t *testing.T, store *acdb.AccessControlStore, orgID, userID int64, role models.RoleType, uid string) {
func removeFolderPermission(t *testing.T, store *acdb.AccessControlStore, orgID, userID int64, role org.RoleType, uid string) {
t.Helper()
// remove user permissions on folder
_, _ = store.SetUserResourcePermission(context.Background(), orgID, accesscontrol.User{ID: userID}, types.SetResourcePermissionCommand{
@@ -7,7 +7,7 @@ import (
"net/http"
"testing"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/tests/testinfra"
"github.com/stretchr/testify/require"
@@ -25,17 +25,17 @@ func TestProvisioning(t *testing.T) {
// Create a users to make authenticated requests
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_VIEWER),
DefaultOrgRole: string(org.RoleViewer),
Password: "viewer",
Login: "viewer",
})
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: "editor",
Login: "editor",
})
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_ADMIN),
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
+8 -8
View File
@@ -13,10 +13,10 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/models"
acdb "github.com/grafana/grafana/pkg/services/accesscontrol/database"
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/tests/testinfra"
"github.com/grafana/grafana/pkg/util"
@@ -36,7 +36,7 @@ func TestAlertRulePermissions(t *testing.T) {
// Create a user to make authenticated requests
userID := createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: "password",
Login: "grafana",
})
@@ -176,7 +176,7 @@ func TestAlertRulePermissions(t *testing.T) {
assert.JSONEq(t, expectedGetNamespaceResponseBody, body)
// remove permissions from folder2
removeFolderPermission(t, permissionsStore, 1, userID, models.ROLE_EDITOR, "folder2")
removeFolderPermission(t, permissionsStore, 1, userID, org.RoleEditor, "folder2")
apiClient.ReloadCachedPermissions(t)
// make sure that folder2 is not included in the response
@@ -250,7 +250,7 @@ func TestAlertRulePermissions(t *testing.T) {
}
// Remove permissions from folder1.
removeFolderPermission(t, permissionsStore, 1, userID, models.ROLE_EDITOR, "folder1")
removeFolderPermission(t, permissionsStore, 1, userID, org.RoleEditor, "folder1")
apiClient.ReloadCachedPermissions(t)
{
u := fmt.Sprintf("http://grafana:password@%s/api/ruler/grafana/api/v1/rules", grafanaListedAddr)
@@ -326,7 +326,7 @@ func TestAlertRuleConflictingTitle(t *testing.T) {
// Create user
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_ADMIN),
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
@@ -393,7 +393,7 @@ func TestRulerRulesFilterByDashboard(t *testing.T) {
// Create a user to make authenticated requests
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: "password",
Login: "grafana",
})
@@ -731,7 +731,7 @@ func TestRuleGroupSequence(t *testing.T) {
// Create a user to make authenticated requests
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: "password",
Login: "grafana",
})
@@ -827,7 +827,7 @@ func TestRuleUpdate(t *testing.T) {
// Create a user to make authenticated requests
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: "password",
Login: "grafana",
})
@@ -7,9 +7,9 @@ import (
"net/http"
"testing"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/correlations"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/user"
"github.com/stretchr/testify/require"
)
@@ -30,12 +30,12 @@ func TestIntegrationCreateCorrelation(t *testing.T) {
}
ctx.createUser(user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: editorUser.password,
Login: editorUser.username,
})
ctx.createUser(user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_ADMIN),
DefaultOrgRole: string(org.RoleAdmin),
Password: adminUser.password,
Login: adminUser.username,
})
@@ -7,9 +7,9 @@ import (
"net/http"
"testing"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/correlations"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/user"
"github.com/stretchr/testify/require"
)
@@ -30,12 +30,12 @@ func TestIntegrationDeleteCorrelation(t *testing.T) {
}
ctx.createUser(user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: editorUser.password,
Login: editorUser.username,
})
ctx.createUser(user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_ADMIN),
DefaultOrgRole: string(org.RoleAdmin),
Password: adminUser.password,
Login: adminUser.username,
})
@@ -7,9 +7,9 @@ import (
"net/http"
"testing"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/correlations"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/user"
"github.com/stretchr/testify/require"
)
@@ -30,12 +30,12 @@ func TestIntegrationUpdateCorrelation(t *testing.T) {
}
ctx.createUser(user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_EDITOR),
DefaultOrgRole: string(org.RoleEditor),
Password: editorUser.password,
Login: editorUser.username,
})
ctx.createUser(user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_ADMIN),
DefaultOrgRole: string(org.RoleAdmin),
Password: adminUser.password,
Login: adminUser.username,
})
@@ -19,6 +19,7 @@ import (
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/dashboardimport"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/plugindashboards"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/user"
@@ -38,7 +39,7 @@ func TestDashboardQuota(t *testing.T) {
grafanaListedAddr, store := testinfra.StartGrafana(t, dir, path)
// Create user
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_ADMIN),
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})
@@ -131,7 +132,7 @@ providers:
grafanaListedAddr, store := testinfra.StartGrafana(t, dir, path)
// Create user
createUser(t, store, user.CreateUserCommand{
DefaultOrgRole: string(models.ROLE_ADMIN),
DefaultOrgRole: string(org.RoleAdmin),
Password: "admin",
Login: "admin",
})