mirror of
https://github.com/grafana/grafana.git
synced 2025-02-12 00:25:46 -06:00
* UserService: update callers to use the UserService instead of calling sqlstore directly There is one major change hiding in this PR. UserService.Delete originally called a number of services to delete user-related records. I moved everything except the actual call to the user table, and moved those into the API. This was done to avoid dependencies cycles; many of our services depend on the user service, so the user service itself should have as few dependencies as possible.
126 lines
3.7 KiB
Go
126 lines
3.7 KiB
Go
package sqlstore
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
"github.com/grafana/grafana/pkg/services/org"
|
|
"github.com/grafana/grafana/pkg/services/user"
|
|
)
|
|
|
|
func TestIntegrationStatsDataAccess(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("skipping integration test")
|
|
}
|
|
sqlStore := InitTestDB(t)
|
|
populateDB(t, sqlStore)
|
|
|
|
t.Run("Get system stats should not results in error", func(t *testing.T) {
|
|
query := models.GetSystemStatsQuery{}
|
|
err := sqlStore.GetSystemStats(context.Background(), &query)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, int64(3), query.Result.Users)
|
|
assert.Equal(t, int64(0), query.Result.Editors)
|
|
assert.Equal(t, int64(0), query.Result.Viewers)
|
|
assert.Equal(t, int64(3), query.Result.Admins)
|
|
assert.Equal(t, int64(0), query.Result.LibraryPanels)
|
|
assert.Equal(t, int64(0), query.Result.LibraryVariables)
|
|
assert.Equal(t, int64(0), query.Result.APIKeys)
|
|
})
|
|
|
|
t.Run("Get system user count stats should not results in error", func(t *testing.T) {
|
|
query := models.GetSystemUserCountStatsQuery{}
|
|
err := sqlStore.GetSystemUserCountStats(context.Background(), &query)
|
|
assert.NoError(t, err)
|
|
})
|
|
|
|
t.Run("Get datasource stats should not results in error", func(t *testing.T) {
|
|
query := models.GetDataSourceStatsQuery{}
|
|
err := sqlStore.GetDataSourceStats(context.Background(), &query)
|
|
assert.NoError(t, err)
|
|
})
|
|
|
|
t.Run("Get datasource access stats should not results in error", func(t *testing.T) {
|
|
query := models.GetDataSourceAccessStatsQuery{}
|
|
err := sqlStore.GetDataSourceAccessStats(context.Background(), &query)
|
|
assert.NoError(t, err)
|
|
})
|
|
|
|
t.Run("Get alert notifier stats should not results in error", func(t *testing.T) {
|
|
query := models.GetAlertNotifierUsageStatsQuery{}
|
|
err := sqlStore.GetAlertNotifiersUsageStats(context.Background(), &query)
|
|
assert.NoError(t, err)
|
|
})
|
|
|
|
t.Run("Get admin stats should not result in error", func(t *testing.T) {
|
|
query := models.GetAdminStatsQuery{}
|
|
err := sqlStore.GetAdminStats(context.Background(), &query)
|
|
assert.NoError(t, err)
|
|
})
|
|
}
|
|
|
|
func populateDB(t *testing.T, sqlStore *SQLStore) {
|
|
t.Helper()
|
|
|
|
users := make([]user.User, 3)
|
|
for i := range users {
|
|
cmd := user.CreateUserCommand{
|
|
Email: fmt.Sprintf("usertest%v@test.com", i),
|
|
Name: fmt.Sprintf("user name %v", i),
|
|
Login: fmt.Sprintf("user_test_%v_login", i),
|
|
OrgName: fmt.Sprintf("Org #%v", i),
|
|
}
|
|
user, err := sqlStore.CreateUser(context.Background(), cmd)
|
|
require.NoError(t, err)
|
|
users[i] = *user
|
|
}
|
|
|
|
// get 1st user's organisation
|
|
getOrgByIdQuery := &models.GetOrgByIdQuery{Id: users[0].OrgID}
|
|
err := sqlStore.GetOrgById(context.Background(), getOrgByIdQuery)
|
|
require.NoError(t, err)
|
|
orga := getOrgByIdQuery.Result
|
|
|
|
// add 2nd user as editor
|
|
cmd := &models.AddOrgUserCommand{
|
|
OrgId: orga.Id,
|
|
UserId: users[1].ID,
|
|
Role: org.RoleEditor,
|
|
}
|
|
err = sqlStore.AddOrgUser(context.Background(), cmd)
|
|
require.NoError(t, err)
|
|
|
|
// add 3rd user as viewer
|
|
cmd = &models.AddOrgUserCommand{
|
|
OrgId: orga.Id,
|
|
UserId: users[2].ID,
|
|
Role: org.RoleViewer,
|
|
}
|
|
err = sqlStore.AddOrgUser(context.Background(), cmd)
|
|
require.NoError(t, err)
|
|
|
|
// get 2nd user's organisation
|
|
getOrgByIdQuery = &models.GetOrgByIdQuery{Id: users[1].OrgID}
|
|
err = sqlStore.GetOrgById(context.Background(), getOrgByIdQuery)
|
|
require.NoError(t, err)
|
|
orga = getOrgByIdQuery.Result
|
|
|
|
// add 1st user as admin
|
|
cmd = &models.AddOrgUserCommand{
|
|
OrgId: orga.Id,
|
|
UserId: users[0].ID,
|
|
Role: org.RoleAdmin,
|
|
}
|
|
err = sqlStore.AddOrgUser(context.Background(), cmd)
|
|
require.NoError(t, err)
|
|
|
|
// force renewal of user stats
|
|
err = sqlStore.updateUserRoleCountsIfNecessary(context.Background(), true)
|
|
require.NoError(t, err)
|
|
}
|