2021-11-11 09:10:24 -06:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2022-10-19 08:02:15 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/infra/db"
|
2021-11-11 09:10:24 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
|
|
|
accesscontrolmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
|
2022-08-04 07:19:09 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/apikey"
|
2022-08-03 07:13:05 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/apikey/apikeyimpl"
|
2022-08-10 04:56:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/org"
|
2022-12-07 10:03:22 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/org/orgimpl"
|
|
|
|
"github.com/grafana/grafana/pkg/services/quota/quotaimpl"
|
2022-11-14 13:08:10 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/quota/quotatest"
|
2021-11-11 09:10:24 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
2022-06-28 07:32:25 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2022-12-07 10:03:22 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/user/userimpl"
|
2021-11-11 09:10:24 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
type TestUser struct {
|
2022-02-17 06:19:58 -06:00
|
|
|
Name string
|
|
|
|
Role string
|
2021-11-11 09:10:24 -06:00
|
|
|
Login string
|
|
|
|
IsServiceAccount bool
|
2022-09-29 06:11:53 -05:00
|
|
|
OrgID int64
|
2021-11-11 09:10:24 -06:00
|
|
|
}
|
|
|
|
|
2022-06-15 07:59:40 -05:00
|
|
|
type TestApiKey struct {
|
2022-09-29 06:11:53 -05:00
|
|
|
Name string
|
|
|
|
Role org.RoleType
|
|
|
|
OrgId int64
|
|
|
|
Key string
|
|
|
|
IsExpired bool
|
|
|
|
ServiceAccountID *int64
|
2022-06-15 07:59:40 -05:00
|
|
|
}
|
|
|
|
|
2022-06-28 07:32:25 -05:00
|
|
|
func SetupUserServiceAccount(t *testing.T, sqlStore *sqlstore.SQLStore, testUser TestUser) *user.User {
|
2022-08-10 04:56:48 -05:00
|
|
|
role := string(org.RoleViewer)
|
2022-02-17 06:19:58 -06:00
|
|
|
if testUser.Role != "" {
|
|
|
|
role = testUser.Role
|
|
|
|
}
|
|
|
|
|
2022-12-07 10:03:22 -06:00
|
|
|
quotaService := quotaimpl.ProvideService(sqlStore, sqlStore.Cfg)
|
|
|
|
orgService, err := orgimpl.ProvideService(sqlStore, sqlStore.Cfg, quotaService)
|
|
|
|
require.NoError(t, err)
|
|
|
|
usrSvc, err := userimpl.ProvideService(sqlStore, orgService, sqlStore.Cfg, nil, nil, quotaService)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
u1, err := usrSvc.CreateUserForTests(context.Background(), &user.CreateUserCommand{
|
2021-11-11 09:10:24 -06:00
|
|
|
Login: testUser.Login,
|
|
|
|
IsServiceAccount: testUser.IsServiceAccount,
|
2022-02-17 06:19:58 -06:00
|
|
|
DefaultOrgRole: role,
|
|
|
|
Name: testUser.Name,
|
2022-09-29 06:11:53 -05:00
|
|
|
OrgID: testUser.OrgID,
|
2021-11-11 09:10:24 -06:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
return u1
|
|
|
|
}
|
|
|
|
|
2022-08-04 07:19:09 -05:00
|
|
|
func SetupApiKey(t *testing.T, sqlStore *sqlstore.SQLStore, testKey TestApiKey) *apikey.APIKey {
|
2022-08-10 04:56:48 -05:00
|
|
|
role := org.RoleViewer
|
2022-06-15 07:59:40 -05:00
|
|
|
if testKey.Role != "" {
|
|
|
|
role = testKey.Role
|
|
|
|
}
|
|
|
|
|
2022-08-04 07:19:09 -05:00
|
|
|
addKeyCmd := &apikey.AddCommand{
|
2022-09-29 06:11:53 -05:00
|
|
|
Name: testKey.Name,
|
|
|
|
Role: role,
|
|
|
|
OrgId: testKey.OrgId,
|
|
|
|
ServiceAccountID: testKey.ServiceAccountID,
|
2022-06-15 07:59:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if testKey.Key != "" {
|
|
|
|
addKeyCmd.Key = testKey.Key
|
|
|
|
} else {
|
|
|
|
addKeyCmd.Key = "secret"
|
|
|
|
}
|
2022-08-03 07:13:05 -05:00
|
|
|
|
2022-11-14 13:08:10 -06:00
|
|
|
quotaService := quotatest.New(false, nil)
|
|
|
|
apiKeyService, err := apikeyimpl.ProvideService(sqlStore, sqlStore.Cfg, quotaService)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = apiKeyService.AddAPIKey(context.Background(), addKeyCmd)
|
2022-06-15 07:59:40 -05:00
|
|
|
require.NoError(t, err)
|
2022-06-16 09:01:50 -05:00
|
|
|
|
|
|
|
if testKey.IsExpired {
|
2022-10-19 08:02:15 -05:00
|
|
|
err := sqlStore.WithTransactionalDbSession(context.Background(), func(sess *db.Session) error {
|
2022-06-16 09:01:50 -05:00
|
|
|
// Force setting expires to time before now to make key expired
|
|
|
|
var expires int64 = 1
|
2022-08-04 07:19:09 -05:00
|
|
|
key := apikey.APIKey{Expires: &expires}
|
2022-06-16 09:01:50 -05:00
|
|
|
rowsAffected, err := sess.ID(addKeyCmd.Result.Id).Update(&key)
|
|
|
|
require.Equal(t, int64(1), rowsAffected)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-06-15 07:59:40 -05:00
|
|
|
return addKeyCmd.Result
|
|
|
|
}
|
|
|
|
|
2022-02-11 10:40:43 -06:00
|
|
|
func SetupMockAccesscontrol(t *testing.T,
|
2022-08-10 04:56:48 -05:00
|
|
|
userpermissionsfunc func(c context.Context, siu *user.SignedInUser, opt accesscontrol.Options) ([]accesscontrol.Permission, error),
|
2022-02-11 10:40:43 -06:00
|
|
|
disableAccessControl bool) *accesscontrolmock.Mock {
|
2021-11-11 09:10:24 -06:00
|
|
|
t.Helper()
|
|
|
|
acmock := accesscontrolmock.New()
|
|
|
|
if disableAccessControl {
|
|
|
|
acmock = acmock.WithDisabled()
|
|
|
|
}
|
|
|
|
acmock.GetUserPermissionsFunc = userpermissionsfunc
|
|
|
|
return acmock
|
|
|
|
}
|