forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi_test.go
84 lines (78 loc) · 1.88 KB
/
api_test.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
package api
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
"github.com/grafana/grafana/pkg/services/dashboards"
"github.com/grafana/grafana/pkg/services/star/startest"
"github.com/grafana/grafana/pkg/services/user"
"github.com/grafana/grafana/pkg/web"
)
func TestStarDashboard(t *testing.T) {
api := ProvideApi(startest.NewStarServiceFake(), dashboards.NewFakeDashboardService(t))
testCases := []struct {
name string
signedInUser *user.SignedInUser
expectedStatus int
params map[string]string
}{
{
name: "Star dashboard with user",
params: map[string]string{
":id": "1",
},
signedInUser: &user.SignedInUser{
UserID: 1,
OrgID: 1,
IsAnonymous: false,
},
expectedStatus: 200,
},
{
name: "Star dashboard with anonymous user",
params: map[string]string{
":id": "1",
},
signedInUser: &user.SignedInUser{
UserID: 0,
OrgID: 1,
IsAnonymous: true,
},
expectedStatus: 400,
},
{
name: "Star dashboard with API Key",
params: map[string]string{
":id": "1",
},
signedInUser: &user.SignedInUser{
UserID: 0,
OrgID: 1,
ApiKeyID: 3,
IsAnonymous: false,
},
expectedStatus: 400,
},
{
name: "Star dashboard with Service Account",
params: map[string]string{
":id": "1",
},
signedInUser: &user.SignedInUser{
UserID: 1,
OrgID: 3,
IsServiceAccount: true,
},
expectedStatus: 200,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
req := web.SetURLParams(&http.Request{}, tc.params)
c := &contextmodel.ReqContext{SignedInUser: tc.signedInUser, Context: &web.Context{Req: req}}
resp := api.StarDashboard(c)
assert.Equal(t, tc.expectedStatus, resp.Status())
})
}
}