forked from grafana/grafana
/
common.go
104 lines (89 loc) · 3.01 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package tests
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/infra/db"
"github.com/grafana/grafana/pkg/services/apikey"
"github.com/grafana/grafana/pkg/services/apikey/apikeyimpl"
"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/quota/quotatest"
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/supportbundles/supportbundlestest"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/services/user/userimpl"
)
type TestUser struct {
Name string
Role string
Login string
IsServiceAccount bool
}
type TestApiKey struct {
Name string
Role org.RoleType
OrgId int64
Key string
IsExpired bool
ServiceAccountID *int64
}
func SetupUserServiceAccount(t *testing.T, sqlStore *sqlstore.SQLStore, testUser TestUser) *user.User {
role := string(org.RoleViewer)
if testUser.Role != "" {
role = testUser.Role
}
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, supportbundlestest.NewFakeBundleService())
require.NoError(t, err)
org, err := orgService.CreateWithMember(context.Background(), &org.CreateOrgCommand{
Name: "test org",
})
require.NoError(t, err)
u1, err := usrSvc.Create(context.Background(), &user.CreateUserCommand{
Login: testUser.Login,
IsServiceAccount: testUser.IsServiceAccount,
DefaultOrgRole: role,
Name: testUser.Name,
OrgID: org.ID,
})
require.NoError(t, err)
return u1
}
func SetupApiKey(t *testing.T, sqlStore *sqlstore.SQLStore, testKey TestApiKey) *apikey.APIKey {
role := org.RoleViewer
if testKey.Role != "" {
role = testKey.Role
}
addKeyCmd := &apikey.AddCommand{
Name: testKey.Name,
Role: role,
OrgID: testKey.OrgId,
ServiceAccountID: testKey.ServiceAccountID,
}
if testKey.Key != "" {
addKeyCmd.Key = testKey.Key
} else {
addKeyCmd.Key = "secret"
}
quotaService := quotatest.New(false, nil)
apiKeyService, err := apikeyimpl.ProvideService(sqlStore, sqlStore.Cfg, quotaService)
require.NoError(t, err)
key, err := apiKeyService.AddAPIKey(context.Background(), addKeyCmd)
require.NoError(t, err)
if testKey.IsExpired {
err := sqlStore.WithTransactionalDbSession(context.Background(), func(sess *db.Session) error {
// Force setting expires to time before now to make key expired
var expires int64 = 1
expiringKey := apikey.APIKey{Expires: &expires}
rowsAffected, err := sess.ID(key.ID).Update(&expiringKey)
require.Equal(t, int64(1), rowsAffected)
return err
})
require.NoError(t, err)
}
return key
}