@ -21,6 +21,7 @@ import (
"github.com/grafana/grafana/pkg/services/guardian"
"github.com/grafana/grafana/pkg/services/org"
"github.com/grafana/grafana/pkg/services/team/teamtest"
"github.com/grafana/grafana/pkg/setting"
)
func TestDashboardSnapshotAPIEndpoint_singleSnapshot ( t * testing . T ) {
@ -64,7 +65,8 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
t . Run ( "When user has editor role and is not in the ACL" , func ( t * testing . T ) {
loggedInUserScenarioWithRole ( t , "Should not be able to delete snapshot when calling DELETE on" ,
"DELETE" , "/api/snapshots/12345" , "/api/snapshots/:key" , org . RoleEditor , func ( sc * scenarioContext ) {
hs := & HTTPServer { dashboardsnapshotsService : setUpSnapshotTest ( t , 0 , "" ) }
d := setUpSnapshotTest ( t , 0 , "" )
hs := buildHttpServer ( d , true )
sc . handlerFunc = hs . DeleteDashboardSnapshot
teamSvc := & teamtest . FakeService { }
@ -95,7 +97,8 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
rw . WriteHeader ( 200 )
externalRequest = req
} )
hs := & HTTPServer { dashboardsnapshotsService : setUpSnapshotTest ( t , 0 , ts . URL ) }
d := setUpSnapshotTest ( t , 0 , ts . URL )
hs := buildHttpServer ( d , true )
sc . handlerFunc = hs . DeleteDashboardSnapshotByDeleteKey
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { "deleteKey" : "12345" } ) . exec ( )
@ -138,7 +141,9 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
}
dashSvc . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Return ( qResultACL , nil )
guardian . InitLegacyGuardian ( sc . sqlStore , dashSvc , teamSvc )
hs := & HTTPServer { dashboardsnapshotsService : setUpSnapshotTest ( t , 0 , ts . URL ) , DashboardService : dashSvc }
d := setUpSnapshotTest ( t , 0 , ts . URL )
hs := buildHttpServer ( d , true )
hs . DashboardService = dashSvc
sc . handlerFunc = hs . DeleteDashboardSnapshot
sc . fakeReqWithParams ( "DELETE" , sc . url , map [ string ] string { "key" : "12345" } ) . exec ( )
@ -159,7 +164,8 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
d := setUpSnapshotTest ( t , testUserID , "" )
dashSvc := dashboards . NewFakeDashboardService ( t )
hs := & HTTPServer { dashboardsnapshotsService : d , DashboardService : dashSvc }
hs := buildHttpServer ( d , true )
hs . DashboardService = dashSvc
sc . handlerFunc = hs . DeleteDashboardSnapshot
sc . fakeReqWithParams ( "DELETE" , sc . url , map [ string ] string { "key" : "12345" } ) . exec ( )
@ -183,7 +189,9 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
} )
dashSvc := dashboards . NewFakeDashboardService ( t )
hs := & HTTPServer { dashboardsnapshotsService : setUpSnapshotTest ( t , testUserID , ts . URL ) , DashboardService : dashSvc }
d := setUpSnapshotTest ( t , testUserID , ts . URL )
hs := buildHttpServer ( d , true )
hs . DashboardService = dashSvc
sc . handlerFunc = hs . DeleteDashboardSnapshot
sc . fakeReqWithParams ( "DELETE" , sc . url , map [ string ] string { "key" : "12345" } ) . exec ( )
@ -204,7 +212,8 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
rw . WriteHeader ( 500 )
_ , writeErr = rw . Write ( [ ] byte ( ` { "message":"Unexpected"} ` ) )
} )
hs := & HTTPServer { dashboardsnapshotsService : setUpSnapshotTest ( t , testUserID , ts . URL ) }
d := setUpSnapshotTest ( t , testUserID , ts . URL )
hs := buildHttpServer ( d , true )
sc . handlerFunc = hs . DeleteDashboardSnapshot
sc . fakeReqWithParams ( "DELETE" , sc . url , map [ string ] string { "key" : "12345" } ) . exec ( )
@ -218,7 +227,8 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
ts := setupRemoteServer ( func ( rw http . ResponseWriter , req * http . Request ) {
rw . WriteHeader ( 404 )
} )
hs := & HTTPServer { dashboardsnapshotsService : setUpSnapshotTest ( t , testUserID , ts . URL ) }
d := setUpSnapshotTest ( t , testUserID , ts . URL )
hs := buildHttpServer ( d , true )
sc . handlerFunc = hs . DeleteDashboardSnapshot
sc . fakeReqWithParams ( "DELETE" , sc . url , map [ string ] string { "key" : "12345" } ) . exec ( )
@ -227,7 +237,8 @@ func TestDashboardSnapshotAPIEndpoint_singleSnapshot(t *testing.T) {
loggedInUserScenarioWithRole ( t , "Should be able to read a snapshot's unencrypted data when calling GET on" ,
"GET" , "/api/snapshots/12345" , "/api/snapshots/:key" , org . RoleEditor , func ( sc * scenarioContext ) {
hs := & HTTPServer { dashboardsnapshotsService : setUpSnapshotTest ( t , 0 , "" ) }
d := setUpSnapshotTest ( t , 0 , "" )
hs := buildHttpServer ( d , true )
sc . handlerFunc = hs . GetDashboardSnapshot
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { "key" : "12345" } ) . exec ( )
@ -262,7 +273,7 @@ func TestGetDashboardSnapshotNotFound(t *testing.T) {
"GET /snapshots/{key} should return 404 when the snapshot does not exist" , "GET" ,
"/api/snapshots/12345" , "/api/snapshots/:key" , org . RoleEditor , func ( sc * scenarioContext ) {
d := setUpSnapshotTest ( t )
hs := & HTTPServer { dashboardsnapshotsService : d }
hs := buildHttpServer ( d , true )
sc . handlerFunc = hs . GetDashboardSnapshot
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { "key" : "12345" } ) . exec ( )
@ -273,7 +284,7 @@ func TestGetDashboardSnapshotNotFound(t *testing.T) {
"DELETE /snapshots/{key} should return 404 when the snapshot does not exist" , "DELETE" ,
"/api/snapshots/12345" , "/api/snapshots/:key" , org . RoleEditor , func ( sc * scenarioContext ) {
d := setUpSnapshotTest ( t )
hs := & HTTPServer { dashboardsnapshotsService : d }
hs := buildHttpServer ( d , true )
sc . handlerFunc = hs . DeleteDashboardSnapshot
sc . fakeReqWithParams ( "DELETE" , sc . url , map [ string ] string { "key" : "12345" } ) . exec ( )
@ -284,7 +295,7 @@ func TestGetDashboardSnapshotNotFound(t *testing.T) {
"GET /snapshots-delete/{deleteKey} should return 404 when the snapshot does not exist" , "DELETE" ,
"/api/snapshots-delete/12345" , "/api/snapshots-delete/:deleteKey" , org . RoleEditor , func ( sc * scenarioContext ) {
d := setUpSnapshotTest ( t )
hs := & HTTPServer { dashboardsnapshotsService : d }
hs := buildHttpServer ( d , true )
sc . handlerFunc = hs . DeleteDashboardSnapshotByDeleteKey
sc . fakeReqWithParams ( "DELETE" , sc . url , map [ string ] string { "deleteKey" : "12345" } ) . exec ( )
@ -295,48 +306,94 @@ func TestGetDashboardSnapshotNotFound(t *testing.T) {
func TestGetDashboardSnapshotFailure ( t * testing . T ) {
sqlmock := dbtest . NewFakeDB ( )
setUpSnapshotTest := func ( t * testing . T ) dashboardsnapshots . Service {
setUpSnapshotTest := func ( t * testing . T , shouldMockDashSnapServ bool ) dashboardsnapshots . Service {
t . Helper ( )
dashSnapSvc := dashboardsnapshots . NewMockService ( t )
dashSnapSvc .
On ( "GetDashboardSnapshot" , mock . Anything , mock . AnythingOfType ( "*dashboardsnapshots.GetDashboardSnapshotQuery" ) ) .
Run ( func ( args mock . Arguments ) { } ) .
Return ( nil , errors . New ( "something went wrong" ) )
return dashSnapSvc
if shouldMockDashSnapServ {
dashSnapSvc := dashboardsnapshots . NewMockService ( t )
dashSnapSvc .
On ( "GetDashboardSnapshot" , mock . Anything , mock . AnythingOfType ( "*dashboardsnapshots.GetDashboardSnapshotQuery" ) ) .
Run ( func ( args mock . Arguments ) { } ) .
Return ( nil , errors . New ( "something went wrong" ) )
return dashSnapSvc
} else {
return nil
}
}
loggedInUserScenarioWithRole ( t ,
"GET /snapshots/{key} should return 404 when the snapshot does not exist" , "GET" ,
"/api/snapshots/12345" , "/api/snapshots/:key" , org . RoleEditor , func ( sc * scenarioContext ) {
d := setUpSnapshotTest ( t )
hs := & HTTPServer { dashboardsnapshotsService : d }
d := setUpSnapshotTest ( t , true )
hs := buildHttpServer ( d , true )
sc . handlerFunc = hs . GetDashboardSnapshot
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { "key" : "12345" } ) . exec ( )
assert . Equal ( t , http . StatusInternalServerError , sc . resp . Code )
} , sqlmock )
loggedInUserScenarioWithRole ( t ,
"GET /snapshots/{key} should return 403 when snapshot is disabled" , "GET" ,
"/api/snapshots/12345" , "/api/snapshots/:key" , org . RoleEditor , func ( sc * scenarioContext ) {
d := setUpSnapshotTest ( t , false )
hs := buildHttpServer ( d , false )
sc . handlerFunc = hs . GetDashboardSnapshot
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { "key" : "12345" } ) . exec ( )
assert . Equal ( t , http . StatusForbidden , sc . resp . Code )
} , sqlmock )
loggedInUserScenarioWithRole ( t ,
"DELETE /snapshots/{key} should return 404 when the snapshot does not exist" , "DELETE" ,
"/api/snapshots/12345" , "/api/snapshots/:key" , org . RoleEditor , func ( sc * scenarioContext ) {
d := setUpSnapshotTest ( t )
hs := & HTTPServer { dashboardsnapshotsService : d }
d := setUpSnapshotTest ( t , true )
hs := buildHttpServer ( d , true )
sc . handlerFunc = hs . DeleteDashboardSnapshot
sc . fakeReqWithParams ( "DELETE" , sc . url , map [ string ] string { "key" : "12345" } ) . exec ( )
assert . Equal ( t , http . StatusInternalServerError , sc . resp . Code )
} , sqlmock )
loggedInUserScenarioWithRole ( t ,
"DELETE /snapshots/{key} should return 403 when snapshot is disabled" , "DELETE" ,
"/api/snapshots/12345" , "/api/snapshots/:key" , org . RoleEditor , func ( sc * scenarioContext ) {
d := setUpSnapshotTest ( t , false )
hs := buildHttpServer ( d , false )
sc . handlerFunc = hs . DeleteDashboardSnapshot
sc . fakeReqWithParams ( "DELETE" , sc . url , map [ string ] string { "key" : "12345" } ) . exec ( )
assert . Equal ( t , http . StatusForbidden , sc . resp . Code )
} , sqlmock )
loggedInUserScenarioWithRole ( t ,
"GET /snapshots-delete/{deleteKey} should return 404 when the snapshot does not exist" , "DELETE" ,
"/api/snapshots-delete/12345" , "/api/snapshots-delete/:deleteKey" , org . RoleEditor , func ( sc * scenarioContext ) {
d := setUpSnapshotTest ( t )
hs := & HTTPServer { dashboardsnapshotsService : d }
d := setUpSnapshotTest ( t , true )
hs := buildHttpServer ( d , true )
sc . handlerFunc = hs . DeleteDashboardSnapshotByDeleteKey
sc . fakeReqWithParams ( "DELETE" , sc . url , map [ string ] string { "deleteKey" : "12345" } ) . exec ( )
assert . Equal ( t , http . StatusInternalServerError , sc . resp . Code )
} , sqlmock )
loggedInUserScenarioWithRole ( t ,
"GET /snapshots-delete/{deleteKey} should return 403 when snapshot is disabled" , "DELETE" ,
"/api/snapshots-delete/12345" , "/api/snapshots-delete/:deleteKey" , org . RoleEditor , func ( sc * scenarioContext ) {
d := setUpSnapshotTest ( t , false )
hs := buildHttpServer ( d , false )
sc . handlerFunc = hs . DeleteDashboardSnapshotByDeleteKey
sc . fakeReqWithParams ( "DELETE" , sc . url , map [ string ] string { "deleteKey" : "12345" } ) . exec ( )
assert . Equal ( t , http . StatusForbidden , sc . resp . Code )
} , sqlmock )
}
func buildHttpServer ( d dashboardsnapshots . Service , snapshotEnabled bool ) * HTTPServer {
hs := & HTTPServer {
dashboardsnapshotsService : d ,
Cfg : & setting . Cfg {
SnapshotEnabled : snapshotEnabled ,
} ,
}
return hs
}