@@ -8,13 +8,16 @@ import (
8
8
"testing"
9
9
"time"
10
10
11
+ "github.com/stretchr/testify/assert"
12
+ "github.com/stretchr/testify/mock"
13
+ "github.com/stretchr/testify/require"
14
+
11
15
"github.com/grafana/grafana/pkg/components/simplejson"
12
16
"github.com/grafana/grafana/pkg/models"
17
+ "github.com/grafana/grafana/pkg/services/dashboards"
13
18
"github.com/grafana/grafana/pkg/services/dashboardsnapshots"
14
19
"github.com/grafana/grafana/pkg/services/guardian"
15
20
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
16
- "github.com/stretchr/testify/assert"
17
- "github.com/stretchr/testify/require"
18
21
)
19
22
20
23
func TestDashboardSnapshotAPIEndpoint_singleSnapshot (t * testing.T ) {
@@ -29,10 +32,9 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
29
32
jsonModel , err := simplejson .NewJson ([]byte (`{"id":100}` ))
30
33
require .NoError (t , err )
31
34
32
- viewerRole := models .ROLE_VIEWER
33
- editorRole := models .ROLE_EDITOR
34
35
sqlmock := mockstore .NewSQLStoreMock ()
35
- aclMockResp := []* models.DashboardAclInfoDTO {}
36
+ dashSvc := & dashboards.FakeDashboardService {}
37
+ dashSvc .On ("GetDashboardAclInfoList" , mock .Anything , mock .AnythingOfType ("*models.GetDashboardAclInfoListQuery" )).Return (nil )
36
38
hs := & HTTPServer {DashboardsnapshotsService : & dashboardsnapshots.Service {SQLStore : sqlmock }}
37
39
38
40
setUpSnapshotTest := func (t * testing.T ) * models.DashboardSnapshot {
@@ -47,7 +49,6 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
47
49
External : true ,
48
50
}
49
51
sqlmock .ExpectedDashboardSnapshot = mockSnapshotResult
50
- sqlmock .ExpectedDashboardAclInfoList = aclMockResp
51
52
sqlmock .ExpectedTeamsByUser = []* models.TeamDTO {}
52
53
53
54
return mockSnapshotResult
@@ -63,7 +64,7 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
63
64
})
64
65
mockSnapshotResult .ExternalDeleteUrl = ts .URL
65
66
sc .handlerFunc = hs .DeleteDashboardSnapshot
66
- guardian .InitLegacyGuardian (sc .sqlStore )
67
+ guardian .InitLegacyGuardian (sc .sqlStore , dashSvc )
67
68
sc .fakeReqWithParams ("DELETE" , sc .url , map [string ]string {"key" : "12345" }).exec ()
68
69
69
70
assert .Equal (t , 403 , sc .resp .Code )
@@ -100,15 +101,19 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
100
101
})
101
102
102
103
t .Run ("When user is editor and dashboard has default ACL" , func (t * testing.T ) {
103
- aclMockResp = []* models.DashboardAclInfoDTO {
104
- {Role : & viewerRole , Permission : models .PERMISSION_VIEW },
105
- {Role : & editorRole , Permission : models .PERMISSION_EDIT },
106
- }
104
+ dashSvc := & dashboards.FakeDashboardService {}
105
+ dashSvc .On ("GetDashboardAclInfoList" , mock .Anything , mock .AnythingOfType ("*models.GetDashboardAclInfoListQuery" )).Run (func (args mock.Arguments ) {
106
+ q := args .Get (1 ).(* models.GetDashboardAclInfoListQuery )
107
+ q .Result = []* models.DashboardAclInfoDTO {
108
+ {Role : & viewerRole , Permission : models .PERMISSION_VIEW },
109
+ {Role : & editorRole , Permission : models .PERMISSION_EDIT },
110
+ }
111
+ }).Return (nil )
107
112
108
113
loggedInUserScenarioWithRole (t , "Should be able to delete a snapshot when calling DELETE on" , "DELETE" ,
109
114
"/api/snapshots/12345" , "/api/snapshots/:key" , models .ROLE_EDITOR , func (sc * scenarioContext ) {
110
115
mockSnapshotResult := setUpSnapshotTest (t )
111
-
116
+ guardian . InitLegacyGuardian ( sc . sqlStore , dashSvc )
112
117
var externalRequest * http.Request
113
118
ts := setupRemoteServer (func (rw http.ResponseWriter , req * http.Request ) {
114
119
rw .WriteHeader (200 )
@@ -130,11 +135,9 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
130
135
})
131
136
132
137
t .Run ("When user is editor and creator of the snapshot" , func (t * testing.T ) {
133
- aclMockResp := []* models.DashboardAclInfoDTO {}
134
138
loggedInUserScenarioWithRole (t , "Should be able to delete a snapshot when calling DELETE on" ,
135
139
"DELETE" , "/api/snapshots/12345" , "/api/snapshots/:key" , models .ROLE_EDITOR , func (sc * scenarioContext ) {
136
140
mockSnapshotResult := setUpSnapshotTest (t )
137
- sqlmock .ExpectedDashboardAclInfoList = aclMockResp
138
141
mockSnapshotResult .UserId = testUserID
139
142
mockSnapshotResult .External = false
140
143
@@ -151,7 +154,6 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
151
154
})
152
155
153
156
t .Run ("When deleting an external snapshot" , func (t * testing.T ) {
154
- aclMockResp = []* models.DashboardAclInfoDTO {}
155
157
loggedInUserScenarioWithRole (t ,
156
158
"Should gracefully delete local snapshot when remote snapshot has already been removed when calling DELETE on" ,
157
159
"DELETE" , "/api/snapshots/12345" , "/api/snapshots/:key" , models .ROLE_EDITOR , func (sc * scenarioContext ) {
0 commit comments