mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
SQLStore: Test admins/editors/viewers stats validity (#20751)
* Test admins/editors/viewers stats validity * Use standard testing library instead of Convey
This commit is contained in:
parent
6eb7813565
commit
0b7a5d4ed1
@ -2,50 +2,111 @@ package sqlstore
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
. "github.com/smartystreets/goconvey/convey"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestStatsDataAccess(t *testing.T) {
|
func TestStatsDataAccess(t *testing.T) {
|
||||||
Convey("Testing Stats Data Access", t, func() {
|
t.Run("Testing Stats Data Access", func(t *testing.T) {
|
||||||
InitTestDB(t)
|
InitTestDB(t)
|
||||||
|
|
||||||
Convey("Get system stats should not results in error", func() {
|
t.Run("Get system stats should not results in error", func(t *testing.T) {
|
||||||
|
populateDB(t)
|
||||||
|
|
||||||
query := models.GetSystemStatsQuery{}
|
query := models.GetSystemStatsQuery{}
|
||||||
err := GetSystemStats(&query)
|
err := GetSystemStats(&query)
|
||||||
So(err, ShouldBeNil)
|
assert.Nil(t, err)
|
||||||
|
assert.Equal(t, query.Result.Users, int64(3))
|
||||||
|
assert.Equal(t, query.Result.Editors, 1)
|
||||||
|
assert.Equal(t, query.Result.Viewers, 1)
|
||||||
|
assert.Equal(t, query.Result.Admins, 3)
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("Get system user count stats should not results in error", func() {
|
t.Run("Get system user count stats should not results in error", func(t *testing.T) {
|
||||||
query := models.GetSystemUserCountStatsQuery{}
|
query := models.GetSystemUserCountStatsQuery{}
|
||||||
err := GetSystemUserCountStats(context.Background(), &query)
|
err := GetSystemUserCountStats(context.Background(), &query)
|
||||||
So(err, ShouldBeNil)
|
assert.Nil(t, err)
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("Get datasource stats should not results in error", func() {
|
t.Run("Get datasource stats should not results in error", func(t *testing.T) {
|
||||||
query := models.GetDataSourceStatsQuery{}
|
query := models.GetDataSourceStatsQuery{}
|
||||||
err := GetDataSourceStats(&query)
|
err := GetDataSourceStats(&query)
|
||||||
So(err, ShouldBeNil)
|
assert.Nil(t, err)
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("Get datasource access stats should not results in error", func() {
|
t.Run("Get datasource access stats should not results in error", func(t *testing.T) {
|
||||||
query := models.GetDataSourceAccessStatsQuery{}
|
query := models.GetDataSourceAccessStatsQuery{}
|
||||||
err := GetDataSourceAccessStats(&query)
|
err := GetDataSourceAccessStats(&query)
|
||||||
So(err, ShouldBeNil)
|
assert.Nil(t, err)
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("Get alert notifier stats should not results in error", func() {
|
t.Run("Get alert notifier stats should not results in error", func(t *testing.T) {
|
||||||
query := models.GetAlertNotifierUsageStatsQuery{}
|
query := models.GetAlertNotifierUsageStatsQuery{}
|
||||||
err := GetAlertNotifiersUsageStats(context.Background(), &query)
|
err := GetAlertNotifiersUsageStats(context.Background(), &query)
|
||||||
So(err, ShouldBeNil)
|
assert.Nil(t, err)
|
||||||
})
|
})
|
||||||
|
|
||||||
Convey("Get admin stats should not result in error", func() {
|
t.Run("Get admin stats should not result in error", func(t *testing.T) {
|
||||||
query := models.GetAdminStatsQuery{}
|
query := models.GetAdminStatsQuery{}
|
||||||
err := GetAdminStats(&query)
|
err := GetAdminStats(&query)
|
||||||
So(err, ShouldBeNil)
|
assert.Nil(t, err)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func populateDB(t *testing.T) {
|
||||||
|
users := make([]models.User, 3)
|
||||||
|
for i := range users {
|
||||||
|
cmd := &models.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),
|
||||||
|
}
|
||||||
|
err := CreateUser(context.Background(), cmd)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
users[i] = cmd.Result
|
||||||
|
}
|
||||||
|
|
||||||
|
// get 1st user's organisation
|
||||||
|
getOrgByIdQuery := &models.GetOrgByIdQuery{Id: users[0].OrgId}
|
||||||
|
err := GetOrgById(getOrgByIdQuery)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
org := getOrgByIdQuery.Result
|
||||||
|
|
||||||
|
// add 2nd user as editor
|
||||||
|
cmd := &models.AddOrgUserCommand{
|
||||||
|
OrgId: org.Id,
|
||||||
|
UserId: users[1].Id,
|
||||||
|
Role: models.ROLE_EDITOR,
|
||||||
|
}
|
||||||
|
err = AddOrgUser(cmd)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
// add 3rd user as viewer
|
||||||
|
cmd = &models.AddOrgUserCommand{
|
||||||
|
OrgId: org.Id,
|
||||||
|
UserId: users[2].Id,
|
||||||
|
Role: models.ROLE_VIEWER,
|
||||||
|
}
|
||||||
|
err = AddOrgUser(cmd)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
// get 2nd user's organisation
|
||||||
|
getOrgByIdQuery = &models.GetOrgByIdQuery{Id: users[1].OrgId}
|
||||||
|
err = GetOrgById(getOrgByIdQuery)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
org = getOrgByIdQuery.Result
|
||||||
|
|
||||||
|
// add 1st user as admin
|
||||||
|
cmd = &models.AddOrgUserCommand{
|
||||||
|
OrgId: org.Id,
|
||||||
|
UserId: users[0].Id,
|
||||||
|
Role: models.ROLE_ADMIN,
|
||||||
|
}
|
||||||
|
err = AddOrgUser(cmd)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user