2017-06-12 08:48:55 -05:00
package api
import (
"encoding/json"
2018-02-19 04:12:56 -06:00
"fmt"
2020-06-22 11:00:39 -05:00
"io/ioutil"
2020-07-15 13:09:03 -05:00
"path/filepath"
2017-06-12 08:48:55 -05:00
"testing"
"github.com/grafana/grafana/pkg/api/dtos"
2021-01-15 07:43:20 -06:00
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/api/routing"
2017-06-12 08:48:55 -05:00
"github.com/grafana/grafana/pkg/bus"
2017-06-12 16:05:32 -05:00
"github.com/grafana/grafana/pkg/components/simplejson"
2021-03-17 10:06:10 -05:00
dboards "github.com/grafana/grafana/pkg/dashboards"
2020-03-04 05:57:20 -06:00
"github.com/grafana/grafana/pkg/models"
2018-10-13 00:53:28 -05:00
"github.com/grafana/grafana/pkg/services/alerting"
2017-12-12 09:15:24 -06:00
"github.com/grafana/grafana/pkg/services/dashboards"
2020-10-28 03:36:57 -05:00
"github.com/grafana/grafana/pkg/services/live"
2019-04-30 06:32:18 -05:00
"github.com/grafana/grafana/pkg/services/provisioning"
2020-12-15 12:09:04 -06:00
"github.com/grafana/grafana/pkg/services/quota"
2017-12-15 07:19:49 -06:00
"github.com/grafana/grafana/pkg/setting"
2020-11-13 02:52:38 -06:00
"github.com/stretchr/testify/assert"
2020-06-22 11:00:39 -05:00
"github.com/stretchr/testify/require"
2017-06-12 08:48:55 -05:00
)
2020-06-22 11:00:39 -05:00
func TestGetHomeDashboard ( t * testing . T ) {
req := & models . ReqContext { SignedInUser : & models . SignedInUser { } }
cfg := setting . NewCfg ( )
cfg . StaticRootPath = "../../public/"
2021-03-17 10:06:10 -05:00
hs := & HTTPServer {
Cfg : cfg , Bus : bus . New ( ) ,
PluginManager : & fakePluginManager { } ,
}
2020-06-22 11:00:39 -05:00
hs . Bus . AddHandler ( func ( query * models . GetPreferencesWithDefaultsQuery ) error {
query . Result = & models . Preferences {
HomeDashboardId : 0 ,
}
return nil
} )
tests := [ ] struct {
name string
defaultSetting string
expectedDashboardPath string
} {
{ name : "using default config" , defaultSetting : "" , expectedDashboardPath : "../../public/dashboards/home.json" } ,
{ name : "custom path" , defaultSetting : "../../public/dashboards/default.json" , expectedDashboardPath : "../../public/dashboards/default.json" } ,
}
for _ , tc := range tests {
t . Run ( tc . name , func ( t * testing . T ) {
dash := dtos . DashboardFullWithMeta { }
dash . Meta . IsHome = true
dash . Meta . FolderTitle = "General"
homeDashJSON , err := ioutil . ReadFile ( tc . expectedDashboardPath )
require . NoError ( t , err , "must be able to read expected dashboard file" )
hs . Cfg . DefaultHomeDashboardPath = tc . defaultSetting
bytes , err := simplejson . NewJson ( homeDashJSON )
require . NoError ( t , err , "must be able to encode file as JSON" )
dash . Dashboard = bytes
b , err := json . Marshal ( dash )
require . NoError ( t , err , "must be able to marshal object to JSON" )
res := hs . GetHomeDashboard ( req )
2021-01-15 07:43:20 -06:00
nr , ok := res . ( * response . NormalResponse )
2020-06-22 11:00:39 -05:00
require . True ( t , ok , "should return *NormalResponse" )
2021-01-15 07:43:20 -06:00
require . Equal ( t , b , nr . Body ( ) , "default home dashboard should equal content on disk" )
2020-06-22 11:00:39 -05:00
} )
}
}
2020-11-13 02:52:38 -06:00
type testState struct {
dashQueries [ ] * models . GetDashboardQuery
}
2018-02-19 04:12:56 -06:00
// This tests three main scenarios.
// If a user has access to execute an action on a dashboard:
// 1. and the dashboard is in a folder which does not have an acl
// 2. and the dashboard is in a folder which does have an acl
// 3. Post dashboard response tests
2018-01-30 07:09:30 -06:00
2020-11-13 02:52:38 -06:00
func TestDashboardAPIEndpoint ( t * testing . T ) {
t . Run ( "Given a dashboard with a parent folder which does not have an ACL" , func ( t * testing . T ) {
setUp := func ( ) * testState {
fakeDash := models . NewDashboard ( "Child dash" )
fakeDash . Id = 1
fakeDash . FolderId = 1
fakeDash . HasAcl = false
bus . AddHandler ( "test" , func ( query * models . GetDashboardsBySlugQuery ) error {
dashboards := [ ] * models . Dashboard { fakeDash }
query . Result = dashboards
return nil
} )
2017-06-12 08:48:55 -05:00
2020-11-13 02:52:38 -06:00
state := & testState { }
2018-01-31 09:51:06 -06:00
2020-11-13 02:52:38 -06:00
bus . AddHandler ( "test" , func ( query * models . GetDashboardQuery ) error {
query . Result = fakeDash
state . dashQueries = append ( state . dashQueries , query )
return nil
} )
2018-01-29 14:23:07 -06:00
2020-11-13 02:52:38 -06:00
viewerRole := models . ROLE_VIEWER
editorRole := models . ROLE_EDITOR
2018-04-10 03:48:10 -05:00
2020-11-13 02:52:38 -06:00
aclMockResp := [ ] * models . DashboardAclInfoDTO {
{ Role : & viewerRole , Permission : models . PERMISSION_VIEW } ,
{ Role : & editorRole , Permission : models . PERMISSION_EDIT } ,
}
2017-06-22 16:43:55 -05:00
2020-11-13 02:52:38 -06:00
bus . AddHandler ( "test" , func ( query * models . GetDashboardAclInfoListQuery ) error {
query . Result = aclMockResp
return nil
} )
2017-06-22 16:43:55 -05:00
2020-11-13 02:52:38 -06:00
bus . AddHandler ( "test" , func ( query * models . GetTeamsByUserQuery ) error {
query . Result = [ ] * models . TeamDTO { }
return nil
} )
2017-06-19 14:22:42 -05:00
2020-11-13 02:52:38 -06:00
return state
}
2017-06-22 16:43:55 -05:00
2018-01-30 07:09:30 -06:00
// This tests two scenarios:
// 1. user is an org viewer
// 2. user is an org editor
2020-11-13 02:52:38 -06:00
t . Run ( "When user is an Org Viewer" , func ( t * testing . T ) {
2020-03-04 05:57:20 -06:00
role := models . ROLE_VIEWER
2017-06-12 08:48:55 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/db/child-dash" ,
"/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
state := setUp ( )
2017-06-12 08:48:55 -05:00
2020-11-13 02:52:38 -06:00
dash := getDashboardShouldReturn200 ( sc )
2018-01-29 14:23:07 -06:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , "child-dash" , state . dashQueries [ 0 ] . Slug )
assert . False ( t , dash . Meta . CanEdit )
assert . False ( t , dash . Meta . CanSave )
assert . False ( t , dash . Meta . CanAdmin )
2017-06-12 08:48:55 -05:00
} )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
state := setUp ( )
2017-06-12 08:48:55 -05:00
2020-11-13 02:52:38 -06:00
dash := getDashboardShouldReturn200 ( sc )
assert . Equal ( t , "abcdefghi" , state . dashQueries [ 0 ] . Uid )
assert . False ( t , dash . Meta . CanEdit )
assert . False ( t , dash . Meta . CanSave )
assert . False ( t , dash . Meta . CanAdmin )
2018-01-29 14:23:07 -06:00
} )
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/db/child-dash" ,
"/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
state := setUp ( )
2021-03-17 10:06:10 -05:00
callDeleteDashboardBySlug ( sc , & HTTPServer { Cfg : setting . NewCfg ( ) } )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
assert . Equal ( t , "child-dash" , state . dashQueries [ 0 ] . Slug )
2017-06-12 08:48:55 -05:00
} )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
state := setUp ( )
2021-03-17 10:06:10 -05:00
callDeleteDashboardByUID ( sc , & HTTPServer { Cfg : setting . NewCfg ( ) } )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
2018-01-29 14:23:07 -06:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , "abcdefghi" , state . dashQueries [ 0 ] . Uid )
2018-01-29 14:23:07 -06:00
} )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions/1" ,
"/api/dashboards/id/:dashboardId/versions/:id" , role , func ( sc * scenarioContext ) {
setUp ( )
2018-01-31 09:46:31 -06:00
2020-11-13 02:52:38 -06:00
callGetDashboardVersion ( sc )
assert . Equal ( t , 403 , sc . resp . Code )
2018-01-31 09:46:31 -06:00
} )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" ,
"/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
setUp ( )
2017-06-13 17:28:34 -05:00
2020-11-13 02:52:38 -06:00
callGetDashboardVersions ( sc )
assert . Equal ( t , 403 , sc . resp . Code )
} )
2017-06-12 08:48:55 -05:00
} )
2020-11-13 02:52:38 -06:00
t . Run ( "When user is an Org Editor" , func ( t * testing . T ) {
2020-03-04 05:57:20 -06:00
role := models . ROLE_EDITOR
2017-06-12 08:48:55 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/db/child-dash" ,
"/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
state := setUp ( )
2017-06-12 08:48:55 -05:00
2020-11-13 02:52:38 -06:00
dash := getDashboardShouldReturn200 ( sc )
2018-01-29 14:23:07 -06:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , "child-dash" , state . dashQueries [ 0 ] . Slug )
assert . True ( t , dash . Meta . CanEdit )
assert . True ( t , dash . Meta . CanSave )
assert . False ( t , dash . Meta . CanAdmin )
2017-06-12 08:48:55 -05:00
} )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
state := setUp ( )
dash := getDashboardShouldReturn200 ( sc )
2017-06-12 08:48:55 -05:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , "abcdefghi" , state . dashQueries [ 0 ] . Uid )
assert . True ( t , dash . Meta . CanEdit )
assert . True ( t , dash . Meta . CanSave )
assert . False ( t , dash . Meta . CanAdmin )
2018-01-29 14:23:07 -06:00
} )
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/db/child-dash" ,
"/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
state := setUp ( )
2021-03-17 10:06:10 -05:00
callDeleteDashboardBySlug ( sc , & HTTPServer { Cfg : setting . NewCfg ( ) } )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 200 , sc . resp . Code )
assert . Equal ( t , "child-dash" , state . dashQueries [ 0 ] . Slug )
2017-06-12 08:48:55 -05:00
} )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
state := setUp ( )
2018-01-29 14:23:07 -06:00
2021-03-17 10:06:10 -05:00
callDeleteDashboardByUID ( sc , & HTTPServer { Cfg : setting . NewCfg ( ) } )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 200 , sc . resp . Code )
assert . Equal ( t , "abcdefghi" , state . dashQueries [ 0 ] . Uid )
2018-01-29 14:23:07 -06:00
} )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions/1" ,
"/api/dashboards/id/:dashboardId/versions/:id" , role , func ( sc * scenarioContext ) {
setUp ( )
2018-01-31 09:46:31 -06:00
2020-11-13 02:52:38 -06:00
callGetDashboardVersion ( sc )
assert . Equal ( t , 200 , sc . resp . Code )
2018-01-31 09:46:31 -06:00
} )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" ,
"/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
setUp ( )
2017-06-13 17:28:34 -05:00
2020-11-13 02:52:38 -06:00
callGetDashboardVersions ( sc )
assert . Equal ( t , 200 , sc . resp . Code )
} )
2017-06-12 08:48:55 -05:00
} )
} )
2020-11-13 02:52:38 -06:00
t . Run ( "Given a dashboard with a parent folder which has an ACL" , func ( t * testing . T ) {
hs := & HTTPServer {
Cfg : setting . NewCfg ( ) ,
}
2017-06-12 08:48:55 -05:00
2020-11-13 02:52:38 -06:00
setUp := func ( ) * testState {
state := & testState { }
2018-04-10 03:48:10 -05:00
2020-11-13 02:52:38 -06:00
fakeDash := models . NewDashboard ( "Child dash" )
fakeDash . Id = 1
fakeDash . FolderId = 1
fakeDash . HasAcl = true
2018-01-31 09:51:06 -06:00
2020-11-13 02:52:38 -06:00
origCanEdit := setting . ViewersCanEdit
t . Cleanup ( func ( ) {
setting . ViewersCanEdit = origCanEdit
} )
setting . ViewersCanEdit = false
2017-06-19 14:22:42 -05:00
2020-11-13 02:52:38 -06:00
bus . AddHandler ( "test" , func ( query * models . GetDashboardsBySlugQuery ) error {
dashboards := [ ] * models . Dashboard { fakeDash }
query . Result = dashboards
return nil
} )
2018-01-29 14:23:07 -06:00
2020-11-13 02:52:38 -06:00
aclMockResp := [ ] * models . DashboardAclInfoDTO {
{
DashboardId : 1 ,
Permission : models . PERMISSION_EDIT ,
UserId : 200 ,
} ,
}
2017-06-12 08:48:55 -05:00
2020-11-13 02:52:38 -06:00
bus . AddHandler ( "test" , func ( query * models . GetDashboardAclInfoListQuery ) error {
query . Result = aclMockResp
return nil
} )
2017-06-12 08:48:55 -05:00
2020-11-13 02:52:38 -06:00
bus . AddHandler ( "test" , func ( query * models . GetDashboardQuery ) error {
query . Result = fakeDash
state . dashQueries = append ( state . dashQueries , query )
return nil
} )
bus . AddHandler ( "test" , func ( query * models . GetTeamsByUserQuery ) error {
query . Result = [ ] * models . TeamDTO { }
return nil
} )
return state
2019-04-30 06:32:18 -05:00
}
2018-01-30 07:09:30 -06:00
// This tests six scenarios:
// 1. user is an org viewer AND has no permissions for this dashboard
// 2. user is an org editor AND has no permissions for this dashboard
// 3. user is an org viewer AND has been granted edit permission for the dashboard
// 4. user is an org viewer AND all viewers have edit permission for this dashboard
// 5. user is an org viewer AND has been granted an admin permission
// 6. user is an org editor AND has been granted a view permission
2020-11-13 02:52:38 -06:00
t . Run ( "When user is an Org Viewer and has no permissions for this dashboard" , func ( t * testing . T ) {
2020-03-04 05:57:20 -06:00
role := models . ROLE_VIEWER
2017-06-12 08:48:55 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/db/child-dash" ,
"/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
state := setUp ( )
2017-06-12 08:48:55 -05:00
2020-11-13 02:52:38 -06:00
sc . handlerFunc = hs . GetDashboard
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
assert . Equal ( t , "child-dash" , state . dashQueries [ 0 ] . Slug )
assert . Equal ( t , 403 , sc . resp . Code )
2018-01-29 14:23:07 -06:00
} )
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
state := setUp ( )
2017-06-12 08:48:55 -05:00
2020-11-13 02:52:38 -06:00
sc . handlerFunc = hs . GetDashboard
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
assert . Equal ( t , "abcdefghi" , state . dashQueries [ 0 ] . Uid )
assert . Equal ( t , 403 , sc . resp . Code )
2018-01-29 14:23:07 -06:00
} )
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/db/child-dash" ,
"/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
state := setUp ( )
2021-03-17 10:06:10 -05:00
callDeleteDashboardBySlug ( sc , hs )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
assert . Equal ( t , "child-dash" , state . dashQueries [ 0 ] . Slug )
2017-06-12 08:48:55 -05:00
} )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
state := setUp ( )
2018-01-29 14:23:07 -06:00
2021-03-17 10:06:10 -05:00
callDeleteDashboardByUID ( sc , hs )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
assert . Equal ( t , "abcdefghi" , state . dashQueries [ 0 ] . Uid )
2018-01-29 14:23:07 -06:00
} )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions/1" ,
"/api/dashboards/id/:dashboardId/versions/:id" , role , func ( sc * scenarioContext ) {
setUp ( )
2018-01-31 09:46:31 -06:00
2020-11-13 02:52:38 -06:00
callGetDashboardVersion ( sc )
assert . Equal ( t , 403 , sc . resp . Code )
2018-01-31 09:46:31 -06:00
} )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" ,
"/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
setUp ( )
2017-06-13 17:28:34 -05:00
2020-11-13 02:52:38 -06:00
callGetDashboardVersions ( sc )
assert . Equal ( t , 403 , sc . resp . Code )
} )
2017-06-12 08:48:55 -05:00
} )
2020-11-13 02:52:38 -06:00
t . Run ( "When user is an Org Editor and has no permissions for this dashboard" , func ( t * testing . T ) {
2020-03-04 05:57:20 -06:00
role := models . ROLE_EDITOR
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/db/child-dash" ,
"/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
state := setUp ( )
2017-06-12 08:48:55 -05:00
2020-11-13 02:52:38 -06:00
sc . handlerFunc = hs . GetDashboard
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
2018-01-29 14:23:07 -06:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , "child-dash" , state . dashQueries [ 0 ] . Slug )
assert . Equal ( t , 403 , sc . resp . Code )
2017-06-12 08:48:55 -05:00
} )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
state := setUp ( )
2017-06-12 08:48:55 -05:00
2020-11-13 02:52:38 -06:00
sc . handlerFunc = hs . GetDashboard
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
assert . Equal ( t , "abcdefghi" , state . dashQueries [ 0 ] . Uid )
assert . Equal ( t , 403 , sc . resp . Code )
2018-01-29 14:23:07 -06:00
} )
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/db/child-dash" ,
"/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
state := setUp ( )
2021-03-17 10:06:10 -05:00
callDeleteDashboardBySlug ( sc , hs )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
assert . Equal ( t , "child-dash" , state . dashQueries [ 0 ] . Slug )
2017-06-12 08:48:55 -05:00
} )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
state := setUp ( )
2018-01-29 14:23:07 -06:00
2021-03-17 10:06:10 -05:00
callDeleteDashboardByUID ( sc , hs )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
assert . Equal ( t , "abcdefghi" , state . dashQueries [ 0 ] . Uid )
2018-01-29 14:23:07 -06:00
} )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions/1" ,
"/api/dashboards/id/:dashboardId/versions/:id" , role , func ( sc * scenarioContext ) {
setUp ( )
2018-01-31 09:46:31 -06:00
2020-11-13 02:52:38 -06:00
callGetDashboardVersion ( sc )
assert . Equal ( t , 403 , sc . resp . Code )
2018-01-31 09:46:31 -06:00
} )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" ,
"/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
setUp ( )
2017-06-13 17:28:34 -05:00
2020-11-13 02:52:38 -06:00
callGetDashboardVersions ( sc )
assert . Equal ( t , 403 , sc . resp . Code )
} )
2017-06-12 08:48:55 -05:00
} )
2020-11-13 02:52:38 -06:00
t . Run ( "When user is an Org Viewer but has an edit permission" , func ( t * testing . T ) {
2020-03-04 05:57:20 -06:00
role := models . ROLE_VIEWER
2017-06-12 16:05:32 -05:00
2020-03-04 05:57:20 -06:00
mockResult := [ ] * models . DashboardAclInfoDTO {
{ OrgId : 1 , DashboardId : 2 , UserId : 1 , Permission : models . PERMISSION_EDIT } ,
2017-06-12 08:48:55 -05:00
}
2020-11-13 02:52:38 -06:00
setUpInner := func ( ) * testState {
state := setUp ( )
bus . AddHandler ( "test" , func ( query * models . GetDashboardAclInfoListQuery ) error {
query . Result = mockResult
return nil
} )
return state
}
2017-06-12 08:48:55 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/db/child-dash" ,
"/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
state := setUpInner ( )
bus . AddHandler ( "test" , func ( query * models . GetDashboardAclInfoListQuery ) error {
query . Result = mockResult
return nil
} )
2017-06-12 08:48:55 -05:00
2020-11-13 02:52:38 -06:00
dash := getDashboardShouldReturn200 ( sc )
2018-01-29 14:23:07 -06:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , "child-dash" , state . dashQueries [ 0 ] . Slug )
assert . True ( t , dash . Meta . CanEdit )
assert . True ( t , dash . Meta . CanSave )
assert . False ( t , dash . Meta . CanAdmin )
2017-06-22 17:34:19 -05:00
} )
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
state := setUpInner ( )
2018-01-29 14:23:07 -06:00
2020-11-13 02:52:38 -06:00
dash := getDashboardShouldReturn200 ( sc )
2018-01-29 14:23:07 -06:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , "abcdefghi" , state . dashQueries [ 0 ] . Uid )
assert . True ( t , dash . Meta . CanEdit )
assert . True ( t , dash . Meta . CanSave )
assert . False ( t , dash . Meta . CanAdmin )
2017-06-22 17:34:19 -05:00
} )
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
state := setUpInner ( )
2018-01-29 14:23:07 -06:00
2021-03-17 10:06:10 -05:00
callDeleteDashboardBySlug ( sc , hs )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 200 , sc . resp . Code )
assert . Equal ( t , "child-dash" , state . dashQueries [ 0 ] . Slug )
2017-06-22 17:34:19 -05:00
} )
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
state := setUpInner ( )
2018-01-31 09:46:31 -06:00
2021-03-17 10:06:10 -05:00
callDeleteDashboardByUID ( sc , hs )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 200 , sc . resp . Code )
assert . Equal ( t , "abcdefghi" , state . dashQueries [ 0 ] . Uid )
2017-06-22 17:34:19 -05:00
} )
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions/1" , "/api/dashboards/id/:dashboardId/versions/:id" , role , func ( sc * scenarioContext ) {
setUpInner ( )
callGetDashboardVersion ( sc )
assert . Equal ( t , 200 , sc . resp . Code )
2017-06-22 17:34:19 -05:00
} )
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" , "/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
setUpInner ( )
callGetDashboardVersions ( sc )
assert . Equal ( t , 200 , sc . resp . Code )
2017-06-22 17:34:19 -05:00
} )
} )
2020-11-13 02:52:38 -06:00
t . Run ( "When user is an Org Viewer and viewers can edit" , func ( t * testing . T ) {
2020-03-04 05:57:20 -06:00
role := models . ROLE_VIEWER
2017-12-15 07:19:49 -06:00
2020-11-13 02:52:38 -06:00
setUpInner := func ( ) * testState {
state := setUp ( )
2017-12-15 07:19:49 -06:00
2020-11-13 02:52:38 -06:00
mockResult := [ ] * models . DashboardAclInfoDTO {
{ OrgId : 1 , DashboardId : 2 , UserId : 1 , Permission : models . PERMISSION_VIEW } ,
}
2017-12-15 07:19:49 -06:00
2020-11-13 02:52:38 -06:00
bus . AddHandler ( "test" , func ( query * models . GetDashboardAclInfoListQuery ) error {
query . Result = mockResult
return nil
2018-01-29 14:23:07 -06:00
} )
2020-11-13 02:52:38 -06:00
origCanEdit := setting . ViewersCanEdit
t . Cleanup ( func ( ) {
setting . ViewersCanEdit = origCanEdit
2017-12-15 07:19:49 -06:00
} )
2020-11-13 02:52:38 -06:00
setting . ViewersCanEdit = true
return state
}
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
state := setUpInner ( )
dash := getDashboardShouldReturn200 ( sc )
assert . Equal ( t , "child-dash" , state . dashQueries [ 0 ] . Slug )
assert . True ( t , dash . Meta . CanEdit )
assert . False ( t , dash . Meta . CanSave )
assert . False ( t , dash . Meta . CanAdmin )
2017-12-15 07:19:49 -06:00
} )
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
state := setUpInner ( )
2017-12-15 07:19:49 -06:00
2020-11-13 02:52:38 -06:00
require . True ( t , setting . ViewersCanEdit )
dash := getDashboardShouldReturn200 ( sc )
2018-01-29 14:23:07 -06:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , "abcdefghi" , state . dashQueries [ 0 ] . Uid )
assert . True ( t , dash . Meta . CanEdit )
assert . False ( t , dash . Meta . CanSave )
assert . False ( t , dash . Meta . CanAdmin )
2017-12-15 07:19:49 -06:00
} )
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
state := setUpInner ( )
2018-01-29 14:23:07 -06:00
2021-03-17 10:06:10 -05:00
callDeleteDashboardBySlug ( sc , hs )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
assert . Equal ( t , "child-dash" , state . dashQueries [ 0 ] . Slug )
2017-12-15 07:19:49 -06:00
} )
2018-01-31 09:46:31 -06:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
state := setUpInner ( )
2018-01-31 09:46:31 -06:00
2021-03-17 10:06:10 -05:00
callDeleteDashboardByUID ( sc , hs )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
assert . Equal ( t , "abcdefghi" , state . dashQueries [ 0 ] . Uid )
2017-12-15 07:19:49 -06:00
} )
} )
2020-11-13 02:52:38 -06:00
t . Run ( "When user is an Org Viewer but has an admin permission" , func ( t * testing . T ) {
2020-03-04 05:57:20 -06:00
role := models . ROLE_VIEWER
2017-06-22 17:34:19 -05:00
2020-11-13 02:52:38 -06:00
setUpInner := func ( ) * testState {
state := setUp ( )
mockResult := [ ] * models . DashboardAclInfoDTO {
{ OrgId : 1 , DashboardId : 2 , UserId : 1 , Permission : models . PERMISSION_ADMIN } ,
}
bus . AddHandler ( "test" , func ( query * models . GetDashboardAclInfoListQuery ) error {
query . Result = mockResult
return nil
} )
return state
2017-06-22 17:34:19 -05:00
}
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
state := setUpInner ( )
dash := getDashboardShouldReturn200 ( sc )
assert . Equal ( t , "child-dash" , state . dashQueries [ 0 ] . Slug )
assert . True ( t , dash . Meta . CanEdit )
assert . True ( t , dash . Meta . CanSave )
assert . True ( t , dash . Meta . CanAdmin )
2017-06-22 17:34:19 -05:00
} )
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
state := setUpInner ( )
2017-06-22 17:34:19 -05:00
2020-11-13 02:52:38 -06:00
dash := getDashboardShouldReturn200 ( sc )
2018-01-29 14:23:07 -06:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , "abcdefghi" , state . dashQueries [ 0 ] . Uid )
assert . True ( t , dash . Meta . CanEdit )
assert . True ( t , dash . Meta . CanSave )
assert . True ( t , dash . Meta . CanAdmin )
2017-06-12 08:48:55 -05:00
} )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
state := setUpInner ( )
2018-01-29 14:23:07 -06:00
2021-03-17 10:06:10 -05:00
callDeleteDashboardBySlug ( sc , hs )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 200 , sc . resp . Code )
assert . Equal ( t , "child-dash" , state . dashQueries [ 0 ] . Slug )
2017-06-12 08:48:55 -05:00
} )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
state := setUpInner ( )
2018-01-29 14:23:07 -06:00
2021-03-17 10:06:10 -05:00
callDeleteDashboardByUID ( sc , hs )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 200 , sc . resp . Code )
assert . Equal ( t , "abcdefghi" , state . dashQueries [ 0 ] . Uid )
2017-06-12 16:05:32 -05:00
} )
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions/1" , "/api/dashboards/id/:dashboardId/versions/:id" , role , func ( sc * scenarioContext ) {
setUpInner ( )
2018-01-31 09:46:31 -06:00
2020-11-13 02:52:38 -06:00
callGetDashboardVersion ( sc )
assert . Equal ( t , 200 , sc . resp . Code )
2017-06-12 16:05:32 -05:00
} )
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" , "/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
setUpInner ( )
2017-06-13 17:28:34 -05:00
2020-11-13 02:52:38 -06:00
callGetDashboardVersions ( sc )
assert . Equal ( t , 200 , sc . resp . Code )
2017-06-13 17:28:34 -05:00
} )
2017-06-12 08:48:55 -05:00
} )
2020-11-13 02:52:38 -06:00
t . Run ( "When user is an Org Editor but has a view permission" , func ( t * testing . T ) {
2020-03-04 05:57:20 -06:00
role := models . ROLE_EDITOR
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
setUpInner := func ( ) * testState {
state := setUp ( )
2017-06-12 08:48:55 -05:00
2020-11-13 02:52:38 -06:00
mockResult := [ ] * models . DashboardAclInfoDTO {
{ OrgId : 1 , DashboardId : 2 , UserId : 1 , Permission : models . PERMISSION_VIEW } ,
}
bus . AddHandler ( "test" , func ( query * models . GetDashboardAclInfoListQuery ) error {
query . Result = mockResult
return nil
} )
return state
}
2017-06-12 08:48:55 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
state := setUpInner ( )
2017-06-12 08:48:55 -05:00
2020-11-13 02:52:38 -06:00
dash := getDashboardShouldReturn200 ( sc )
2018-01-29 14:23:07 -06:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , "child-dash" , state . dashQueries [ 0 ] . Slug )
assert . False ( t , dash . Meta . CanEdit )
assert . False ( t , dash . Meta . CanSave )
2018-01-29 14:23:07 -06:00
} )
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
state := setUpInner ( )
2017-06-12 08:48:55 -05:00
2020-11-13 02:52:38 -06:00
dash := getDashboardShouldReturn200 ( sc )
assert . Equal ( t , "abcdefghi" , state . dashQueries [ 0 ] . Uid )
assert . False ( t , dash . Meta . CanEdit )
assert . False ( t , dash . Meta . CanSave )
2017-06-12 08:48:55 -05:00
} )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
state := setUpInner ( )
2018-01-29 14:23:07 -06:00
2021-03-17 10:06:10 -05:00
callDeleteDashboardBySlug ( sc , hs )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
assert . Equal ( t , "child-dash" , state . dashQueries [ 0 ] . Slug )
2017-06-12 16:05:32 -05:00
} )
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
state := setUpInner ( )
2018-01-31 09:46:31 -06:00
2021-03-17 10:06:10 -05:00
callDeleteDashboardByUID ( sc , hs )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
assert . Equal ( t , "abcdefghi" , state . dashQueries [ 0 ] . Uid )
2017-06-12 16:05:32 -05:00
} )
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions/1" , "/api/dashboards/id/:dashboardId/versions/:id" , role , func ( sc * scenarioContext ) {
setUpInner ( )
callGetDashboardVersion ( sc )
assert . Equal ( t , 403 , sc . resp . Code )
2017-06-13 17:28:34 -05:00
} )
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" , "/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
setUpInner ( )
callGetDashboardVersions ( sc )
assert . Equal ( t , 403 , sc . resp . Code )
2017-06-13 17:28:34 -05:00
} )
2017-06-12 08:48:55 -05:00
} )
} )
2018-01-31 09:51:06 -06:00
2020-11-13 02:52:38 -06:00
t . Run ( "Given two dashboards with the same title in different folders" , func ( t * testing . T ) {
2020-03-04 05:57:20 -06:00
dashOne := models . NewDashboard ( "dash" )
2018-01-31 09:51:06 -06:00
dashOne . Id = 2
dashOne . FolderId = 1
dashOne . HasAcl = false
2020-03-04 05:57:20 -06:00
dashTwo := models . NewDashboard ( "dash" )
2018-01-31 09:51:06 -06:00
dashTwo . Id = 4
dashTwo . FolderId = 3
dashTwo . HasAcl = false
2020-03-04 05:57:20 -06:00
bus . AddHandler ( "test" , func ( query * models . GetDashboardsBySlugQuery ) error {
dashboards := [ ] * models . Dashboard { dashOne , dashTwo }
2018-01-31 09:51:06 -06:00
query . Result = dashboards
return nil
} )
2020-03-04 05:57:20 -06:00
role := models . ROLE_EDITOR
2018-01-31 09:51:06 -06:00
2021-03-17 10:06:10 -05:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/db/dash" ,
"/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
callDeleteDashboardBySlug ( sc , & HTTPServer { Cfg : setting . NewCfg ( ) } )
2018-01-31 09:51:06 -06:00
2021-03-17 10:06:10 -05:00
assert . Equal ( t , 412 , sc . resp . Code )
result := sc . ToJSON ( )
assert . Equal ( t , "multiple-slugs-exists" , result . Get ( "status" ) . MustString ( ) )
assert . Equal ( t , models . ErrDashboardsWithSameSlugExists . Error ( ) , result . Get ( "message" ) . MustString ( ) )
} )
2018-01-31 09:51:06 -06:00
} )
2018-02-19 04:12:56 -06:00
2020-11-13 02:52:38 -06:00
t . Run ( "Post dashboard response tests" , func ( t * testing . T ) {
2018-02-19 04:12:56 -06:00
// This tests that a valid request returns correct response
2020-11-13 02:52:38 -06:00
t . Run ( "Given a correct request for creating a dashboard" , func ( t * testing . T ) {
const folderID int64 = 3
const dashID int64 = 2
2018-02-19 04:12:56 -06:00
2020-03-04 05:57:20 -06:00
cmd := models . SaveDashboardCommand {
2018-02-19 04:12:56 -06:00
OrgId : 1 ,
UserId : 5 ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "Dash" ,
} ) ,
Overwrite : true ,
2020-11-13 02:52:38 -06:00
FolderId : folderID ,
2018-02-19 04:12:56 -06:00
IsFolder : false ,
Message : "msg" ,
}
mock := & dashboards . FakeDashboardService {
2020-03-04 05:57:20 -06:00
SaveDashboardResult : & models . Dashboard {
2020-11-13 02:52:38 -06:00
Id : dashID ,
2018-02-19 04:12:56 -06:00
Uid : "uid" ,
Title : "Dash" ,
Slug : "dash" ,
Version : 2 ,
} ,
}
2020-11-13 02:52:38 -06:00
postDashboardScenario ( t , "When calling POST on" , "/api/dashboards" , "/api/dashboards" , mock , cmd , func ( sc * scenarioContext ) {
callPostDashboardShouldReturnSuccess ( sc )
2018-02-19 04:12:56 -06:00
2020-11-13 02:52:38 -06:00
dto := mock . SavedDashboards [ 0 ]
assert . Equal ( t , cmd . OrgId , dto . OrgId )
assert . Equal ( t , cmd . UserId , dto . User . UserId )
assert . Equal ( t , folderID , dto . Dashboard . FolderId )
assert . Equal ( t , "Dash" , dto . Dashboard . Title )
assert . True ( t , dto . Overwrite )
assert . Equal ( t , "msg" , dto . Message )
result := sc . ToJSON ( )
assert . Equal ( t , "success" , result . Get ( "status" ) . MustString ( ) )
assert . Equal ( t , dashID , result . Get ( "id" ) . MustInt64 ( ) )
assert . Equal ( t , "uid" , result . Get ( "uid" ) . MustString ( ) )
assert . Equal ( t , "dash" , result . Get ( "slug" ) . MustString ( ) )
assert . Equal ( t , "/d/uid/dash" , result . Get ( "url" ) . MustString ( ) )
2018-02-19 04:12:56 -06:00
} )
} )
// This tests that invalid requests returns expected error responses
2020-11-13 02:52:38 -06:00
t . Run ( "Given incorrect requests for creating a dashboard" , func ( t * testing . T ) {
2018-02-19 04:12:56 -06:00
testCases := [ ] struct {
SaveError error
ExpectedStatusCode int
} {
2020-03-04 05:57:20 -06:00
{ SaveError : models . ErrDashboardNotFound , ExpectedStatusCode : 404 } ,
{ SaveError : models . ErrFolderNotFound , ExpectedStatusCode : 400 } ,
{ SaveError : models . ErrDashboardWithSameUIDExists , ExpectedStatusCode : 400 } ,
{ SaveError : models . ErrDashboardWithSameNameInFolderExists , ExpectedStatusCode : 412 } ,
{ SaveError : models . ErrDashboardVersionMismatch , ExpectedStatusCode : 412 } ,
{ SaveError : models . ErrDashboardTitleEmpty , ExpectedStatusCode : 400 } ,
{ SaveError : models . ErrDashboardFolderCannotHaveParent , ExpectedStatusCode : 400 } ,
2018-10-13 00:53:28 -05:00
{ SaveError : alerting . ValidationError { Reason : "Mu" } , ExpectedStatusCode : 422 } ,
2020-03-04 05:57:20 -06:00
{ SaveError : models . ErrDashboardFailedGenerateUniqueUid , ExpectedStatusCode : 500 } ,
{ SaveError : models . ErrDashboardTypeMismatch , ExpectedStatusCode : 400 } ,
{ SaveError : models . ErrDashboardFolderWithSameNameAsDashboard , ExpectedStatusCode : 400 } ,
{ SaveError : models . ErrDashboardWithSameNameAsFolder , ExpectedStatusCode : 400 } ,
{ SaveError : models . ErrDashboardFolderNameExists , ExpectedStatusCode : 400 } ,
{ SaveError : models . ErrDashboardUpdateAccessDenied , ExpectedStatusCode : 403 } ,
{ SaveError : models . ErrDashboardInvalidUid , ExpectedStatusCode : 400 } ,
2020-07-21 04:12:01 -05:00
{ SaveError : models . ErrDashboardUidTooLong , ExpectedStatusCode : 400 } ,
2020-03-04 05:57:20 -06:00
{ SaveError : models . ErrDashboardCannotSaveProvisionedDashboard , ExpectedStatusCode : 400 } ,
{ SaveError : models . UpdatePluginDashboardError { PluginId : "plug" } , ExpectedStatusCode : 412 } ,
2018-02-19 04:12:56 -06:00
}
2020-03-04 05:57:20 -06:00
cmd := models . SaveDashboardCommand {
2018-02-19 04:12:56 -06:00
OrgId : 1 ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "" ,
} ) ,
}
for _ , tc := range testCases {
mock := & dashboards . FakeDashboardService {
SaveDashboardError : tc . SaveError ,
}
2020-11-13 02:52:38 -06:00
postDashboardScenario ( t , fmt . Sprintf ( "Expect '%s' error when calling POST on" , tc . SaveError . Error ( ) ) ,
"/api/dashboards" , "/api/dashboards" , mock , cmd , func ( sc * scenarioContext ) {
callPostDashboard ( sc )
assert . Equal ( t , tc . ExpectedStatusCode , sc . resp . Code )
} )
2018-02-19 04:12:56 -06:00
}
} )
} )
2018-02-27 10:53:30 -06:00
2020-11-13 02:52:38 -06:00
t . Run ( "Given two dashboards being compared" , func ( t * testing . T ) {
setUp := func ( ) {
mockResult := [ ] * models . DashboardAclInfoDTO { }
bus . AddHandler ( "test" , func ( query * models . GetDashboardAclInfoListQuery ) error {
query . Result = mockResult
return nil
} )
2018-02-27 10:53:30 -06:00
2020-11-13 02:52:38 -06:00
bus . AddHandler ( "test" , func ( query * models . GetDashboardVersionQuery ) error {
query . Result = & models . DashboardVersion {
Data : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : fmt . Sprintf ( "Dash%d" , query . DashboardId ) ,
} ) ,
}
return nil
} )
}
2018-02-27 10:53:30 -06:00
cmd := dtos . CalculateDiffOptions {
Base : dtos . CalculateDiffTarget {
DashboardId : 1 ,
Version : 1 ,
} ,
New : dtos . CalculateDiffTarget {
DashboardId : 2 ,
Version : 2 ,
} ,
DiffType : "basic" ,
}
2020-11-13 02:52:38 -06:00
t . Run ( "when user does not have permission" , func ( t * testing . T ) {
2020-03-04 05:57:20 -06:00
role := models . ROLE_VIEWER
2018-02-27 10:53:30 -06:00
2020-11-13 02:52:38 -06:00
postDiffScenario ( t , "When calling POST on" , "/api/dashboards/calculate-diff" , "/api/dashboards/calculate-diff" , cmd , role , func ( sc * scenarioContext ) {
setUp ( )
callPostDashboard ( sc )
assert . Equal ( t , 403 , sc . resp . Code )
2018-02-27 10:53:30 -06:00
} )
} )
2020-11-13 02:52:38 -06:00
t . Run ( "when user does have permission" , func ( t * testing . T ) {
2020-03-04 05:57:20 -06:00
role := models . ROLE_ADMIN
2018-02-27 10:53:30 -06:00
2020-11-13 02:52:38 -06:00
postDiffScenario ( t , "When calling POST on" , "/api/dashboards/calculate-diff" , "/api/dashboards/calculate-diff" , cmd , role , func ( sc * scenarioContext ) {
setUp ( )
callPostDashboard ( sc )
assert . Equal ( t , 200 , sc . resp . Code )
2018-02-27 10:53:30 -06:00
} )
} )
} )
2019-03-06 07:38:40 -06:00
2020-11-13 02:52:38 -06:00
t . Run ( "Given dashboard in folder being restored should restore to folder" , func ( t * testing . T ) {
const folderID int64 = 1
setUp := func ( ) {
fakeDash := models . NewDashboard ( "Child dash" )
fakeDash . Id = 2
fakeDash . FolderId = folderID
fakeDash . HasAcl = false
2019-03-06 07:38:40 -06:00
2020-11-13 02:52:38 -06:00
bus . AddHandler ( "test" , func ( query * models . GetDashboardQuery ) error {
query . Result = fakeDash
return nil
} )
2019-03-06 07:38:40 -06:00
2020-11-13 02:52:38 -06:00
bus . AddHandler ( "test" , func ( query * models . GetDashboardVersionQuery ) error {
query . Result = & models . DashboardVersion {
DashboardId : 2 ,
Version : 1 ,
Data : fakeDash . Data ,
}
return nil
} )
}
2019-03-06 07:38:40 -06:00
mock := & dashboards . FakeDashboardService {
2020-03-04 05:57:20 -06:00
SaveDashboardResult : & models . Dashboard {
2019-03-06 07:38:40 -06:00
Id : 2 ,
Uid : "uid" ,
Title : "Dash" ,
Slug : "dash" ,
Version : 1 ,
} ,
}
cmd := dtos . RestoreDashboardVersionCommand {
Version : 1 ,
}
2020-11-13 02:52:38 -06:00
restoreDashboardVersionScenario ( t , "When calling POST on" , "/api/dashboards/id/1/restore" ,
"/api/dashboards/id/:dashboardId/restore" , mock , cmd , func ( sc * scenarioContext ) {
setUp ( )
callRestoreDashboardVersion ( sc )
assert . Equal ( t , 200 , sc . resp . Code )
dto := mock . SavedDashboards [ 0 ]
assert . Equal ( t , folderID , dto . Dashboard . FolderId )
assert . Equal ( t , "Child dash" , dto . Dashboard . Title )
assert . Equal ( t , "Restored from version 1" , dto . Message )
} )
2019-03-06 07:38:40 -06:00
} )
2020-11-13 02:52:38 -06:00
t . Run ( "Given dashboard in general folder being restored should restore to general folder" , func ( t * testing . T ) {
setUp := func ( ) {
fakeDash := models . NewDashboard ( "Child dash" )
fakeDash . Id = 2
fakeDash . HasAcl = false
2019-03-06 07:38:40 -06:00
2020-11-13 02:52:38 -06:00
bus . AddHandler ( "test" , func ( query * models . GetDashboardQuery ) error {
query . Result = fakeDash
return nil
} )
2019-03-06 07:38:40 -06:00
2020-11-13 02:52:38 -06:00
bus . AddHandler ( "test" , func ( query * models . GetDashboardVersionQuery ) error {
query . Result = & models . DashboardVersion {
DashboardId : 2 ,
Version : 1 ,
Data : fakeDash . Data ,
}
return nil
} )
}
2019-03-06 07:38:40 -06:00
mock := & dashboards . FakeDashboardService {
2020-03-04 05:57:20 -06:00
SaveDashboardResult : & models . Dashboard {
2019-03-06 07:38:40 -06:00
Id : 2 ,
Uid : "uid" ,
Title : "Dash" ,
Slug : "dash" ,
Version : 1 ,
} ,
}
cmd := dtos . RestoreDashboardVersionCommand {
Version : 1 ,
}
2020-11-13 02:52:38 -06:00
restoreDashboardVersionScenario ( t , "When calling POST on" , "/api/dashboards/id/1/restore" ,
"/api/dashboards/id/:dashboardId/restore" , mock , cmd , func ( sc * scenarioContext ) {
setUp ( )
callRestoreDashboardVersion ( sc )
assert . Equal ( t , 200 , sc . resp . Code )
dto := mock . SavedDashboards [ 0 ]
assert . Equal ( t , int64 ( 0 ) , dto . Dashboard . FolderId )
assert . Equal ( t , "Child dash" , dto . Dashboard . Title )
assert . Equal ( t , "Restored from version 1" , dto . Message )
} )
2019-03-06 07:38:40 -06:00
} )
2019-04-10 06:29:10 -05:00
2020-11-13 02:52:38 -06:00
t . Run ( "Given provisioned dashboard" , func ( t * testing . T ) {
setUp := func ( ) {
bus . AddHandler ( "test" , func ( query * models . GetDashboardsBySlugQuery ) error {
query . Result = [ ] * models . Dashboard { { } }
return nil
} )
bus . AddHandler ( "test" , func ( query * models . GetDashboardQuery ) error {
2021-01-04 07:41:17 -06:00
dataValue , err := simplejson . NewJson ( [ ] byte ( ` { "id": 1, "editable": true, "style": "dark"} ` ) )
require . NoError ( t , err )
query . Result = & models . Dashboard { Id : 1 , Data : dataValue }
2020-11-13 02:52:38 -06:00
return nil
} )
2019-04-10 06:29:10 -05:00
2021-03-17 10:06:10 -05:00
origGetProvisionedData := dashboards . GetProvisionedData
t . Cleanup ( func ( ) {
dashboards . GetProvisionedData = origGetProvisionedData
2020-11-13 02:52:38 -06:00
} )
2021-03-17 10:06:10 -05:00
dashboards . GetProvisionedData = func ( dboards . Store , int64 ) ( * models . DashboardProvisioning , error ) {
return & models . DashboardProvisioning { ExternalId : "/tmp/grafana/dashboards/test/dashboard1.json" } , nil
}
2019-04-10 06:29:10 -05:00
2020-11-13 02:52:38 -06:00
bus . AddHandler ( "test" , func ( query * models . GetDashboardAclInfoListQuery ) error {
query . Result = [ ] * models . DashboardAclInfoDTO {
{ OrgId : testOrgID , DashboardId : 1 , UserId : testUserID , Permission : models . PERMISSION_EDIT } ,
}
return nil
} )
}
2021-03-17 10:06:10 -05:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/db/dash" ,
"/api/dashboards/db/:slug" , models . ROLE_EDITOR , func ( sc * scenarioContext ) {
setUp ( )
2019-04-10 06:29:10 -05:00
2021-03-17 10:06:10 -05:00
callDeleteDashboardBySlug ( sc , & HTTPServer { Cfg : setting . NewCfg ( ) } )
2019-04-10 06:29:10 -05:00
2021-03-17 10:06:10 -05:00
assert . Equal ( t , 400 , sc . resp . Code )
result := sc . ToJSON ( )
assert . Equal ( t , models . ErrDashboardCannotDeleteProvisionedDashboard . Error ( ) , result . Get ( "error" ) . MustString ( ) )
} )
2019-04-10 06:29:10 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/db/abcdefghi" , "/api/dashboards/db/:uid" , models . ROLE_EDITOR , func ( sc * scenarioContext ) {
setUp ( )
2021-03-17 10:06:10 -05:00
callDeleteDashboardByUID ( sc , & HTTPServer { Cfg : setting . NewCfg ( ) } )
2019-04-10 06:29:10 -05:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 400 , sc . resp . Code )
result := sc . ToJSON ( )
assert . Equal ( t , models . ErrDashboardCannotDeleteProvisionedDashboard . Error ( ) , result . Get ( "error" ) . MustString ( ) )
2019-04-10 06:29:10 -05:00
} )
2019-04-30 06:32:18 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/dash" , "/api/dashboards/uid/:uid" , models . ROLE_EDITOR , func ( sc * scenarioContext ) {
setUp ( )
2019-04-30 06:32:18 -05:00
mock := provisioning . NewProvisioningServiceMock ( )
mock . GetDashboardProvisionerResolvedPathFunc = func ( name string ) string {
return "/tmp/grafana/dashboards"
}
2020-11-13 02:52:38 -06:00
dash := getDashboardShouldReturn200WithConfig ( sc , mock )
2019-04-30 06:32:18 -05:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , filepath . Join ( "test" , "dashboard1.json" ) , dash . Meta . ProvisionedExternalId )
2019-04-30 06:32:18 -05:00
} )
2019-10-31 08:27:31 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When allowUiUpdates is true and calling GET on" , "GET" , "/api/dashboards/uid/dash" , "/api/dashboards/uid/:uid" , models . ROLE_EDITOR , func ( sc * scenarioContext ) {
setUp ( )
2019-10-31 08:27:31 -05:00
mock := provisioning . NewProvisioningServiceMock ( )
mock . GetDashboardProvisionerResolvedPathFunc = func ( name string ) string {
return "/tmp/grafana/dashboards"
}
2020-04-15 01:12:52 -05:00
mock . GetAllowUIUpdatesFromConfigFunc = func ( name string ) bool {
2019-10-31 08:27:31 -05:00
return true
}
hs := & HTTPServer {
Cfg : setting . NewCfg ( ) ,
ProvisioningService : mock ,
}
2020-11-13 02:52:38 -06:00
callGetDashboard ( sc , hs )
2019-10-31 08:27:31 -05:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 200 , sc . resp . Code )
2019-10-31 08:27:31 -05:00
dash := dtos . DashboardFullWithMeta { }
err := json . NewDecoder ( sc . resp . Body ) . Decode ( & dash )
2020-11-13 02:52:38 -06:00
require . NoError ( t , err )
2019-10-31 08:27:31 -05:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , false , dash . Meta . Provisioned )
2019-10-31 08:27:31 -05:00
} )
2019-04-10 06:29:10 -05:00
} )
2017-06-12 08:48:55 -05:00
}
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
func getDashboardShouldReturn200WithConfig ( sc * scenarioContext , provisioningService provisioning . ProvisioningService ) dtos .
2020-03-25 08:14:24 -05:00
DashboardFullWithMeta {
2019-04-30 06:32:18 -05:00
if provisioningService == nil {
provisioningService = provisioning . NewProvisioningServiceMock ( )
}
hs := & HTTPServer {
Cfg : setting . NewCfg ( ) ,
ProvisioningService : provisioningService ,
}
2020-11-13 02:52:38 -06:00
callGetDashboard ( sc , hs )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
require . Equal ( sc . t , 200 , sc . resp . Code )
2017-06-12 16:05:32 -05:00
dash := dtos . DashboardFullWithMeta { }
err := json . NewDecoder ( sc . resp . Body ) . Decode ( & dash )
2020-11-13 02:52:38 -06:00
require . NoError ( sc . t , err )
2017-06-12 16:05:32 -05:00
return dash
}
2020-11-13 02:52:38 -06:00
func getDashboardShouldReturn200 ( sc * scenarioContext ) dtos . DashboardFullWithMeta {
return getDashboardShouldReturn200WithConfig ( sc , nil )
2019-04-30 06:32:18 -05:00
}
2020-11-13 02:52:38 -06:00
func callGetDashboard ( sc * scenarioContext , hs * HTTPServer ) {
2019-04-30 06:32:18 -05:00
sc . handlerFunc = hs . GetDashboard
2018-01-29 06:51:01 -06:00
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
}
2020-11-13 02:52:38 -06:00
func callGetDashboardVersion ( sc * scenarioContext ) {
2020-03-04 05:57:20 -06:00
bus . AddHandler ( "test" , func ( query * models . GetDashboardVersionQuery ) error {
query . Result = & models . DashboardVersion { }
2017-06-13 17:28:34 -05:00
return nil
} )
sc . handlerFunc = GetDashboardVersion
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
}
2020-11-13 02:52:38 -06:00
func callGetDashboardVersions ( sc * scenarioContext ) {
2020-03-04 05:57:20 -06:00
bus . AddHandler ( "test" , func ( query * models . GetDashboardVersionsQuery ) error {
query . Result = [ ] * models . DashboardVersionDTO { }
2017-06-13 17:28:34 -05:00
return nil
} )
sc . handlerFunc = GetDashboardVersions
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
}
2021-03-17 10:06:10 -05:00
func callDeleteDashboardBySlug ( sc * scenarioContext , hs * HTTPServer ) {
2020-03-04 05:57:20 -06:00
bus . AddHandler ( "test" , func ( cmd * models . DeleteDashboardCommand ) error {
2017-06-12 16:05:32 -05:00
return nil
} )
2021-01-20 02:28:10 -06:00
sc . handlerFunc = hs . DeleteDashboardBySlug
2017-06-12 16:05:32 -05:00
sc . fakeReqWithParams ( "DELETE" , sc . url , map [ string ] string { } ) . exec ( )
}
2021-03-17 10:06:10 -05:00
func callDeleteDashboardByUID ( sc * scenarioContext , hs * HTTPServer ) {
2020-03-04 05:57:20 -06:00
bus . AddHandler ( "test" , func ( cmd * models . DeleteDashboardCommand ) error {
2018-01-31 09:46:31 -06:00
return nil
} )
2021-01-20 02:28:10 -06:00
sc . handlerFunc = hs . DeleteDashboardByUID
2018-01-31 09:46:31 -06:00
sc . fakeReqWithParams ( "DELETE" , sc . url , map [ string ] string { } ) . exec ( )
}
2020-11-13 02:52:38 -06:00
func callPostDashboard ( sc * scenarioContext ) {
2017-06-12 16:05:32 -05:00
sc . fakeReqWithParams ( "POST" , sc . url , map [ string ] string { } ) . exec ( )
}
2020-11-13 02:52:38 -06:00
func callRestoreDashboardVersion ( sc * scenarioContext ) {
2019-03-06 07:38:40 -06:00
sc . fakeReqWithParams ( "POST" , sc . url , map [ string ] string { } ) . exec ( )
}
2020-11-13 02:52:38 -06:00
func callPostDashboardShouldReturnSuccess ( sc * scenarioContext ) {
callPostDashboard ( sc )
2018-01-30 16:37:54 -06:00
2020-11-13 02:52:38 -06:00
assert . Equal ( sc . t , 200 , sc . resp . Code )
2018-01-30 16:37:54 -06:00
}
2020-11-13 02:52:38 -06:00
func postDashboardScenario ( t * testing . T , desc string , url string , routePattern string ,
mock * dashboards . FakeDashboardService , cmd models . SaveDashboardCommand , fn scenarioFunc ) {
t . Run ( fmt . Sprintf ( "%s %s" , desc , url ) , func ( t * testing . T ) {
t . Cleanup ( bus . ClearBusHandlers )
2017-06-12 16:05:32 -05:00
2020-12-15 12:09:04 -06:00
cfg := setting . NewCfg ( )
2019-02-11 14:12:01 -06:00
hs := HTTPServer {
2019-10-31 08:27:31 -05:00
Bus : bus . GetBus ( ) ,
2020-12-15 12:09:04 -06:00
Cfg : cfg ,
2019-10-31 08:27:31 -05:00
ProvisioningService : provisioning . NewProvisioningServiceMock ( ) ,
2020-10-28 03:36:57 -05:00
Live : & live . GrafanaLive { Cfg : setting . NewCfg ( ) } ,
2020-12-15 12:09:04 -06:00
QuotaService : & quota . QuotaService {
Cfg : cfg ,
} ,
2021-03-17 10:06:10 -05:00
PluginManager : & fakePluginManager { } ,
2019-02-11 14:12:01 -06:00
}
2020-11-13 02:52:38 -06:00
sc := setupScenarioContext ( t , url )
2021-01-15 07:43:20 -06:00
sc . defaultHandler = routing . Wrap ( func ( c * models . ReqContext ) response . Response {
2017-06-12 16:05:32 -05:00
sc . context = c
2020-03-04 05:57:20 -06:00
sc . context . SignedInUser = & models . SignedInUser { OrgId : cmd . OrgId , UserId : cmd . UserId }
2017-06-12 16:05:32 -05:00
2019-02-11 14:12:01 -06:00
return hs . PostDashboard ( c , cmd )
2017-06-12 16:05:32 -05:00
} )
2018-02-19 04:12:56 -06:00
origNewDashboardService := dashboards . NewService
2019-10-31 08:27:31 -05:00
origProvisioningService := dashboards . NewProvisioningService
2021-03-12 04:51:02 -06:00
t . Cleanup ( func ( ) {
dashboards . NewService = origNewDashboardService
dashboards . NewProvisioningService = origProvisioningService
} )
dashboards . MockDashboardService ( mock )
2021-03-17 10:06:10 -05:00
dashboards . NewProvisioningService = func ( dboards . Store ) dashboards . DashboardProvisioningService {
2019-10-31 08:27:31 -05:00
return mockDashboardProvisioningService { }
}
2017-06-12 16:05:32 -05:00
sc . m . Post ( routePattern , sc . defaultHandler )
fn ( sc )
} )
}
2018-01-29 12:27:53 -06:00
2020-11-13 02:52:38 -06:00
func postDiffScenario ( t * testing . T , desc string , url string , routePattern string , cmd dtos . CalculateDiffOptions , role models . RoleType , fn scenarioFunc ) {
t . Run ( fmt . Sprintf ( "%s %s" , desc , url ) , func ( t * testing . T ) {
2018-02-27 10:53:30 -06:00
defer bus . ClearBusHandlers ( )
2020-11-13 02:52:38 -06:00
sc := setupScenarioContext ( t , url )
2021-01-15 07:43:20 -06:00
sc . defaultHandler = routing . Wrap ( func ( c * models . ReqContext ) response . Response {
2018-02-27 10:53:30 -06:00
sc . context = c
2020-03-04 05:57:20 -06:00
sc . context . SignedInUser = & models . SignedInUser {
2020-11-13 02:52:38 -06:00
OrgId : testOrgID ,
UserId : testUserID ,
2018-02-27 10:53:30 -06:00
}
sc . context . OrgRole = role
return CalculateDashboardDiff ( c , cmd )
} )
sc . m . Post ( routePattern , sc . defaultHandler )
fn ( sc )
} )
}
2020-11-13 02:52:38 -06:00
func restoreDashboardVersionScenario ( t * testing . T , desc string , url string , routePattern string ,
mock * dashboards . FakeDashboardService , cmd dtos . RestoreDashboardVersionCommand , fn scenarioFunc ) {
t . Run ( fmt . Sprintf ( "%s %s" , desc , url ) , func ( t * testing . T ) {
2019-03-06 07:38:40 -06:00
defer bus . ClearBusHandlers ( )
2020-12-15 12:09:04 -06:00
cfg := setting . NewCfg ( )
2019-03-06 07:38:40 -06:00
hs := HTTPServer {
2020-12-15 12:09:04 -06:00
Cfg : cfg ,
2019-10-31 08:27:31 -05:00
Bus : bus . GetBus ( ) ,
ProvisioningService : provisioning . NewProvisioningServiceMock ( ) ,
2020-12-15 12:09:04 -06:00
Live : & live . GrafanaLive { Cfg : cfg } ,
QuotaService : & quota . QuotaService { Cfg : cfg } ,
2019-03-06 07:38:40 -06:00
}
2020-11-13 02:52:38 -06:00
sc := setupScenarioContext ( t , url )
2021-01-15 07:43:20 -06:00
sc . defaultHandler = routing . Wrap ( func ( c * models . ReqContext ) response . Response {
2019-03-06 07:38:40 -06:00
sc . context = c
2020-03-04 05:57:20 -06:00
sc . context . SignedInUser = & models . SignedInUser {
2020-11-13 02:52:38 -06:00
OrgId : testOrgID ,
UserId : testUserID ,
2019-03-06 07:38:40 -06:00
}
2020-03-04 05:57:20 -06:00
sc . context . OrgRole = models . ROLE_ADMIN
2019-03-06 07:38:40 -06:00
return hs . RestoreDashboardVersion ( c , cmd )
} )
2019-10-31 08:27:31 -05:00
origProvisioningService := dashboards . NewProvisioningService
2021-03-12 04:51:02 -06:00
origNewDashboardService := dashboards . NewService
t . Cleanup ( func ( ) {
dashboards . NewService = origNewDashboardService
dashboards . NewProvisioningService = origProvisioningService
} )
2021-03-17 10:06:10 -05:00
dashboards . NewProvisioningService = func ( dboards . Store ) dashboards . DashboardProvisioningService {
2019-10-31 08:27:31 -05:00
return mockDashboardProvisioningService { }
}
2019-03-06 07:38:40 -06:00
dashboards . MockDashboardService ( mock )
sc . m . Post ( routePattern , sc . defaultHandler )
fn ( sc )
} )
}
2018-03-22 16:13:46 -05:00
func ( sc * scenarioContext ) ToJSON ( ) * simplejson . Json {
2018-01-29 12:27:53 -06:00
var result * simplejson . Json
err := json . NewDecoder ( sc . resp . Body ) . Decode ( & result )
2020-11-13 02:52:38 -06:00
require . NoError ( sc . t , err )
2018-01-29 12:27:53 -06:00
return result
}
2019-10-31 08:27:31 -05:00
type mockDashboardProvisioningService struct {
2021-03-12 04:51:02 -06:00
dashboards . DashboardProvisioningService
2019-10-31 08:27:31 -05:00
}
2021-03-17 10:06:10 -05:00
func ( s mockDashboardProvisioningService ) GetProvisionedDashboardDataByDashboardID ( dashboardID int64 ) (
* models . DashboardProvisioning , error ) {
return nil , nil
2019-10-31 08:27:31 -05:00
}