2017-06-12 08:48:55 -05:00
package api
import (
"encoding/json"
2018-02-19 04:12:56 -06:00
"fmt"
2017-06-12 08:48:55 -05:00
"testing"
"github.com/grafana/grafana/pkg/api/dtos"
"github.com/grafana/grafana/pkg/bus"
2017-06-12 16:05:32 -05:00
"github.com/grafana/grafana/pkg/components/simplejson"
2017-06-22 16:43:55 -05:00
m "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"
2017-12-15 07:19:49 -06:00
"github.com/grafana/grafana/pkg/setting"
2017-06-12 08:48:55 -05:00
. "github.com/smartystreets/goconvey/convey"
)
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
2017-06-12 08:48:55 -05:00
func TestDashboardApiEndpoint ( t * testing . T ) {
Convey ( "Given a dashboard with a parent folder which does not have an acl" , t , func ( ) {
2017-06-22 16:43:55 -05:00
fakeDash := m . NewDashboard ( "Child dash" )
2017-06-17 17:24:38 -05:00
fakeDash . Id = 1
2017-06-23 15:00:26 -05:00
fakeDash . FolderId = 1
2017-06-12 08:48:55 -05:00
fakeDash . HasAcl = false
2018-01-31 09:51:06 -06:00
bus . AddHandler ( "test" , func ( query * m . GetDashboardsBySlugQuery ) error {
dashboards := [ ] * m . Dashboard { fakeDash }
query . Result = dashboards
return nil
} )
2018-01-29 14:23:07 -06:00
var getDashboardQueries [ ] * m . GetDashboardQuery
2017-06-22 16:43:55 -05:00
bus . AddHandler ( "test" , func ( query * m . GetDashboardQuery ) error {
2017-06-12 08:48:55 -05:00
query . Result = fakeDash
2018-01-29 14:23:07 -06:00
getDashboardQueries = append ( getDashboardQueries , query )
2017-06-12 08:48:55 -05:00
return nil
} )
2018-04-10 05:30:37 -05:00
bus . AddHandler ( "test" , func ( query * m . IsDashboardProvisionedQuery ) error {
query . Result = false
2018-04-10 03:48:10 -05:00
return nil
} )
2017-06-22 16:43:55 -05:00
viewerRole := m . ROLE_VIEWER
editorRole := m . ROLE_EDITOR
aclMockResp := [ ] * m . DashboardAclInfoDTO {
{ Role : & viewerRole , Permission : m . PERMISSION_VIEW } ,
{ Role : & editorRole , Permission : m . PERMISSION_EDIT } ,
}
bus . AddHandler ( "test" , func ( query * m . GetDashboardAclInfoListQuery ) error {
2017-06-19 14:22:42 -05:00
query . Result = aclMockResp
return nil
} )
2017-12-08 09:25:45 -06:00
bus . AddHandler ( "test" , func ( query * m . GetTeamsByUserQuery ) error {
2018-07-11 13:23:07 -05:00
query . Result = [ ] * m . TeamDTO { }
2017-06-22 16:43:55 -05:00
return nil
} )
2018-01-30 07:09:30 -06:00
// This tests two scenarios:
// 1. user is an org viewer
// 2. user is an org editor
2017-06-12 08:48:55 -05:00
Convey ( "When user is an Org Viewer" , func ( ) {
2017-06-22 16:43:55 -05:00
role := m . ROLE_VIEWER
2017-06-12 08:48:55 -05:00
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
2017-06-12 16:05:32 -05:00
dash := GetDashboardShouldReturn200 ( sc )
2017-06-12 08:48:55 -05:00
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by slug" , func ( ) {
So ( getDashboardQueries [ 0 ] . Slug , ShouldEqual , "child-dash" )
} )
2017-06-12 08:48:55 -05:00
Convey ( "Should not be able to edit or save dashboard" , func ( ) {
So ( dash . Meta . CanEdit , ShouldBeFalse )
So ( dash . Meta . CanSave , ShouldBeFalse )
2017-06-22 17:34:19 -05:00
So ( dash . Meta . CanAdmin , ShouldBeFalse )
2017-06-12 08:48:55 -05:00
} )
} )
2017-06-12 16:05:32 -05:00
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2017-06-12 16:05:32 -05:00
dash := GetDashboardShouldReturn200 ( sc )
2017-06-12 08:48:55 -05:00
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by uid" , func ( ) {
So ( getDashboardQueries [ 0 ] . Uid , ShouldEqual , "abcdefghi" )
} )
2017-06-12 08:48:55 -05:00
Convey ( "Should not be able to edit or save dashboard" , func ( ) {
So ( dash . Meta . CanEdit , ShouldBeFalse )
So ( dash . Meta . CanSave , ShouldBeFalse )
2017-06-22 17:34:19 -05:00
So ( dash . Meta . CanAdmin , ShouldBeFalse )
2017-06-12 08:48:55 -05:00
} )
} )
2017-06-12 16:05:32 -05:00
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
2017-06-12 16:05:32 -05:00
CallDeleteDashboard ( sc )
So ( sc . resp . Code , ShouldEqual , 403 )
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by slug" , func ( ) {
So ( getDashboardQueries [ 0 ] . Slug , ShouldEqual , "child-dash" )
} )
2017-06-12 16:05:32 -05:00
} )
2018-01-31 09:46:31 -06:00
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2018-03-22 06:37:35 -05:00
CallDeleteDashboardByUID ( sc )
2018-01-31 09:46:31 -06:00
So ( sc . resp . Code , ShouldEqual , 403 )
Convey ( "Should lookup dashboard by uid" , func ( ) {
So ( getDashboardQueries [ 0 ] . Uid , ShouldEqual , "abcdefghi" )
} )
2017-06-12 16:05:32 -05:00
} )
2017-06-13 17:28:34 -05:00
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/id/2/versions/1" , "/api/dashboards/id/:dashboardId/versions/:id" , role , func ( sc * scenarioContext ) {
CallGetDashboardVersion ( sc )
So ( sc . resp . Code , ShouldEqual , 403 )
} )
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" , "/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
CallGetDashboardVersions ( sc )
So ( sc . resp . Code , ShouldEqual , 403 )
} )
2017-06-12 08:48:55 -05:00
} )
Convey ( "When user is an Org Editor" , func ( ) {
2017-06-22 16:43:55 -05:00
role := m . ROLE_EDITOR
2017-06-12 08:48:55 -05:00
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
2017-06-12 16:05:32 -05:00
dash := GetDashboardShouldReturn200 ( sc )
2017-06-12 08:48:55 -05:00
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by slug" , func ( ) {
So ( getDashboardQueries [ 0 ] . Slug , ShouldEqual , "child-dash" )
} )
2017-06-12 08:48:55 -05:00
Convey ( "Should be able to edit or save dashboard" , func ( ) {
So ( dash . Meta . CanEdit , ShouldBeTrue )
So ( dash . Meta . CanSave , ShouldBeTrue )
2017-06-22 17:34:19 -05:00
So ( dash . Meta . CanAdmin , ShouldBeFalse )
2017-06-12 08:48:55 -05:00
} )
} )
2017-06-12 16:05:32 -05:00
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2017-06-12 16:05:32 -05:00
dash := GetDashboardShouldReturn200 ( sc )
2017-06-12 08:48:55 -05:00
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by uid" , func ( ) {
So ( getDashboardQueries [ 0 ] . Uid , ShouldEqual , "abcdefghi" )
} )
2017-06-12 08:48:55 -05:00
Convey ( "Should be able to edit or save dashboard" , func ( ) {
So ( dash . Meta . CanEdit , ShouldBeTrue )
So ( dash . Meta . CanSave , ShouldBeTrue )
2017-06-22 17:34:19 -05:00
So ( dash . Meta . CanAdmin , ShouldBeFalse )
2017-06-12 08:48:55 -05:00
} )
} )
2017-06-12 16:05:32 -05:00
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
2017-06-12 16:05:32 -05:00
CallDeleteDashboard ( sc )
So ( sc . resp . Code , ShouldEqual , 200 )
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by slug" , func ( ) {
So ( getDashboardQueries [ 0 ] . Slug , ShouldEqual , "child-dash" )
} )
2017-06-12 16:05:32 -05:00
} )
2018-01-31 09:46:31 -06:00
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2018-03-22 06:37:35 -05:00
CallDeleteDashboardByUID ( sc )
2018-01-31 09:46:31 -06:00
So ( sc . resp . Code , ShouldEqual , 200 )
Convey ( "Should lookup dashboard by uid" , func ( ) {
So ( getDashboardQueries [ 0 ] . Uid , ShouldEqual , "abcdefghi" )
} )
2017-06-12 16:05:32 -05:00
} )
2017-06-13 17:28:34 -05:00
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/id/2/versions/1" , "/api/dashboards/id/:dashboardId/versions/:id" , role , func ( sc * scenarioContext ) {
CallGetDashboardVersion ( sc )
So ( sc . resp . Code , ShouldEqual , 200 )
} )
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" , "/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
CallGetDashboardVersions ( sc )
So ( sc . resp . Code , ShouldEqual , 200 )
} )
2017-06-12 08:48:55 -05:00
} )
} )
Convey ( "Given a dashboard with a parent folder which has an acl" , t , func ( ) {
2017-06-22 16:43:55 -05:00
fakeDash := m . NewDashboard ( "Child dash" )
2017-06-17 17:24:38 -05:00
fakeDash . Id = 1
2017-06-23 15:00:26 -05:00
fakeDash . FolderId = 1
2017-06-12 08:48:55 -05:00
fakeDash . HasAcl = true
2017-12-15 07:19:49 -06:00
setting . ViewersCanEdit = false
2017-06-12 08:48:55 -05:00
2018-04-10 05:30:37 -05:00
bus . AddHandler ( "test" , func ( query * m . IsDashboardProvisionedQuery ) error {
query . Result = false
2018-04-10 03:48:10 -05:00
return nil
} )
2018-01-31 09:51:06 -06:00
bus . AddHandler ( "test" , func ( query * m . GetDashboardsBySlugQuery ) error {
dashboards := [ ] * m . Dashboard { fakeDash }
query . Result = dashboards
return nil
} )
2017-06-22 16:43:55 -05:00
aclMockResp := [ ] * m . DashboardAclInfoDTO {
2017-06-19 14:22:42 -05:00
{
DashboardId : 1 ,
2017-06-22 16:43:55 -05:00
Permission : m . PERMISSION_EDIT ,
2017-06-19 14:22:42 -05:00
UserId : 200 ,
} ,
}
2017-06-22 16:43:55 -05:00
bus . AddHandler ( "test" , func ( query * m . GetDashboardAclInfoListQuery ) error {
2017-06-19 14:22:42 -05:00
query . Result = aclMockResp
return nil
} )
2018-01-29 14:23:07 -06:00
var getDashboardQueries [ ] * m . GetDashboardQuery
2017-06-22 16:43:55 -05:00
bus . AddHandler ( "test" , func ( query * m . GetDashboardQuery ) error {
2017-06-12 08:48:55 -05:00
query . Result = fakeDash
2018-01-29 14:23:07 -06:00
getDashboardQueries = append ( getDashboardQueries , query )
2017-06-12 08:48:55 -05:00
return nil
} )
2017-12-08 09:25:45 -06:00
bus . AddHandler ( "test" , func ( query * m . GetTeamsByUserQuery ) error {
2018-07-11 13:23:07 -05:00
query . Result = [ ] * m . TeamDTO { }
2017-06-12 08:48:55 -05:00
return nil
} )
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
2017-06-12 08:48:55 -05:00
Convey ( "When user is an Org Viewer and has no permissions for this dashboard" , func ( ) {
2017-06-22 16:43:55 -05:00
role := m . ROLE_VIEWER
2017-06-12 08:48:55 -05:00
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
2017-06-12 08:48:55 -05:00
sc . handlerFunc = GetDashboard
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by slug" , func ( ) {
So ( getDashboardQueries [ 0 ] . Slug , ShouldEqual , "child-dash" )
} )
2017-06-12 08:48:55 -05:00
Convey ( "Should be denied access" , func ( ) {
So ( sc . resp . Code , ShouldEqual , 403 )
} )
} )
2017-06-12 16:05:32 -05:00
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2017-06-12 08:48:55 -05:00
sc . handlerFunc = GetDashboard
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by uid" , func ( ) {
So ( getDashboardQueries [ 0 ] . Uid , ShouldEqual , "abcdefghi" )
} )
2017-06-12 08:48:55 -05:00
Convey ( "Should be denied access" , func ( ) {
So ( sc . resp . Code , ShouldEqual , 403 )
} )
} )
2017-06-12 16:05:32 -05:00
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
2017-06-12 16:05:32 -05:00
CallDeleteDashboard ( sc )
So ( sc . resp . Code , ShouldEqual , 403 )
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by slug" , func ( ) {
So ( getDashboardQueries [ 0 ] . Slug , ShouldEqual , "child-dash" )
} )
2017-06-12 16:05:32 -05:00
} )
2018-01-31 09:46:31 -06:00
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2018-03-22 06:37:35 -05:00
CallDeleteDashboardByUID ( sc )
2018-01-31 09:46:31 -06:00
So ( sc . resp . Code , ShouldEqual , 403 )
Convey ( "Should lookup dashboard by uid" , func ( ) {
So ( getDashboardQueries [ 0 ] . Uid , ShouldEqual , "abcdefghi" )
} )
2017-06-12 16:05:32 -05:00
} )
2017-06-13 17:28:34 -05:00
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/id/2/versions/1" , "/api/dashboards/id/:dashboardId/versions/:id" , role , func ( sc * scenarioContext ) {
CallGetDashboardVersion ( sc )
So ( sc . resp . Code , ShouldEqual , 403 )
} )
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" , "/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
CallGetDashboardVersions ( sc )
So ( sc . resp . Code , ShouldEqual , 403 )
} )
2017-06-12 08:48:55 -05:00
} )
Convey ( "When user is an Org Editor and has no permissions for this dashboard" , func ( ) {
2017-06-22 16:43:55 -05:00
role := m . ROLE_EDITOR
2017-06-12 16:05:32 -05:00
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
2017-06-12 08:48:55 -05:00
sc . handlerFunc = GetDashboard
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by slug" , func ( ) {
So ( getDashboardQueries [ 0 ] . Slug , ShouldEqual , "child-dash" )
} )
2017-06-12 08:48:55 -05:00
Convey ( "Should be denied access" , func ( ) {
So ( sc . resp . Code , ShouldEqual , 403 )
} )
} )
2017-06-12 16:05:32 -05:00
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2017-06-12 08:48:55 -05:00
sc . handlerFunc = GetDashboard
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by uid" , func ( ) {
So ( getDashboardQueries [ 0 ] . Uid , ShouldEqual , "abcdefghi" )
} )
2017-06-12 08:48:55 -05:00
Convey ( "Should be denied access" , func ( ) {
So ( sc . resp . Code , ShouldEqual , 403 )
} )
} )
2017-06-12 16:05:32 -05:00
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
2017-06-12 16:05:32 -05:00
CallDeleteDashboard ( sc )
So ( sc . resp . Code , ShouldEqual , 403 )
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by slug" , func ( ) {
So ( getDashboardQueries [ 0 ] . Slug , ShouldEqual , "child-dash" )
} )
2017-06-12 16:05:32 -05:00
} )
2018-01-31 09:46:31 -06:00
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2018-03-22 06:37:35 -05:00
CallDeleteDashboardByUID ( sc )
2018-01-31 09:46:31 -06:00
So ( sc . resp . Code , ShouldEqual , 403 )
Convey ( "Should lookup dashboard by uid" , func ( ) {
So ( getDashboardQueries [ 0 ] . Uid , ShouldEqual , "abcdefghi" )
} )
2017-06-12 16:05:32 -05:00
} )
2017-06-13 17:28:34 -05:00
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/id/2/versions/1" , "/api/dashboards/id/:dashboardId/versions/:id" , role , func ( sc * scenarioContext ) {
CallGetDashboardVersion ( sc )
So ( sc . resp . Code , ShouldEqual , 403 )
} )
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" , "/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
CallGetDashboardVersions ( sc )
So ( sc . resp . Code , ShouldEqual , 403 )
} )
2017-06-12 08:48:55 -05:00
} )
Convey ( "When user is an Org Viewer but has an edit permission" , func ( ) {
2017-06-22 16:43:55 -05:00
role := m . ROLE_VIEWER
2017-06-12 16:05:32 -05:00
2017-06-22 16:43:55 -05:00
mockResult := [ ] * m . DashboardAclInfoDTO {
2018-02-14 08:04:26 -06:00
{ OrgId : 1 , DashboardId : 2 , UserId : 1 , Permission : m . PERMISSION_EDIT } ,
2017-06-12 08:48:55 -05:00
}
2017-06-22 16:43:55 -05:00
bus . AddHandler ( "test" , func ( query * m . GetDashboardAclInfoListQuery ) error {
2017-06-12 08:48:55 -05:00
query . Result = mockResult
return nil
} )
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
2017-06-12 16:05:32 -05:00
dash := GetDashboardShouldReturn200 ( sc )
2017-06-12 08:48:55 -05:00
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by slug" , func ( ) {
So ( getDashboardQueries [ 0 ] . Slug , ShouldEqual , "child-dash" )
} )
2017-06-12 08:48:55 -05:00
Convey ( "Should be able to get dashboard with edit rights" , func ( ) {
So ( dash . Meta . CanEdit , ShouldBeTrue )
So ( dash . Meta . CanSave , ShouldBeTrue )
2017-06-22 17:34:19 -05:00
So ( dash . Meta . CanAdmin , ShouldBeFalse )
} )
} )
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
dash := GetDashboardShouldReturn200 ( sc )
Convey ( "Should lookup dashboard by uid" , func ( ) {
So ( getDashboardQueries [ 0 ] . Uid , ShouldEqual , "abcdefghi" )
} )
2017-06-12 08:48:55 -05:00
Convey ( "Should be able to get dashboard with edit rights" , func ( ) {
So ( dash . Meta . CanEdit , ShouldBeTrue )
So ( dash . Meta . CanSave , ShouldBeTrue )
2017-06-22 17:34:19 -05:00
So ( dash . Meta . CanAdmin , ShouldBeFalse )
} )
} )
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
2017-06-22 17:34:19 -05:00
CallDeleteDashboard ( sc )
So ( sc . resp . Code , ShouldEqual , 200 )
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by slug" , func ( ) {
So ( getDashboardQueries [ 0 ] . Slug , ShouldEqual , "child-dash" )
} )
2017-06-22 17:34:19 -05:00
} )
2018-01-31 09:46:31 -06:00
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2018-03-22 06:37:35 -05:00
CallDeleteDashboardByUID ( sc )
2018-01-31 09:46:31 -06:00
So ( sc . resp . Code , ShouldEqual , 200 )
Convey ( "Should lookup dashboard by uid" , func ( ) {
So ( getDashboardQueries [ 0 ] . Uid , ShouldEqual , "abcdefghi" )
} )
2017-06-22 17:34:19 -05:00
} )
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/id/2/versions/1" , "/api/dashboards/id/:dashboardId/versions/:id" , role , func ( sc * scenarioContext ) {
CallGetDashboardVersion ( sc )
So ( sc . resp . Code , ShouldEqual , 200 )
} )
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" , "/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
CallGetDashboardVersions ( sc )
So ( sc . resp . Code , ShouldEqual , 200 )
} )
} )
2017-12-15 07:19:49 -06:00
Convey ( "When user is an Org Viewer and viewers can edit" , func ( ) {
role := m . ROLE_VIEWER
setting . ViewersCanEdit = true
mockResult := [ ] * m . DashboardAclInfoDTO {
2018-02-14 08:04:26 -06:00
{ OrgId : 1 , DashboardId : 2 , UserId : 1 , Permission : m . PERMISSION_VIEW } ,
2017-12-15 07:19:49 -06:00
}
bus . AddHandler ( "test" , func ( query * m . GetDashboardAclInfoListQuery ) error {
query . Result = mockResult
return nil
} )
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
2017-12-15 07:19:49 -06:00
dash := GetDashboardShouldReturn200 ( sc )
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by slug" , func ( ) {
So ( getDashboardQueries [ 0 ] . Slug , ShouldEqual , "child-dash" )
} )
2017-12-15 07:19:49 -06:00
Convey ( "Should be able to get dashboard with edit rights but can save should be false" , func ( ) {
So ( dash . Meta . CanEdit , ShouldBeTrue )
So ( dash . Meta . CanSave , ShouldBeFalse )
So ( dash . Meta . CanAdmin , ShouldBeFalse )
} )
} )
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2017-12-15 07:19:49 -06:00
dash := GetDashboardShouldReturn200 ( sc )
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by uid" , func ( ) {
So ( getDashboardQueries [ 0 ] . Uid , ShouldEqual , "abcdefghi" )
} )
2017-12-15 07:19:49 -06:00
Convey ( "Should be able to get dashboard with edit rights but can save should be false" , func ( ) {
So ( dash . Meta . CanEdit , ShouldBeTrue )
So ( dash . Meta . CanSave , ShouldBeFalse )
So ( dash . Meta . CanAdmin , ShouldBeFalse )
} )
} )
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
2017-12-15 07:19:49 -06:00
CallDeleteDashboard ( sc )
So ( sc . resp . Code , ShouldEqual , 403 )
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by slug" , func ( ) {
So ( getDashboardQueries [ 0 ] . Slug , ShouldEqual , "child-dash" )
} )
2017-12-15 07:19:49 -06:00
} )
2018-01-31 09:46:31 -06:00
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2018-03-22 06:37:35 -05:00
CallDeleteDashboardByUID ( sc )
2018-01-31 09:46:31 -06:00
So ( sc . resp . Code , ShouldEqual , 403 )
Convey ( "Should lookup dashboard by uid" , func ( ) {
So ( getDashboardQueries [ 0 ] . Uid , ShouldEqual , "abcdefghi" )
} )
2017-12-15 07:19:49 -06:00
} )
} )
2017-06-22 17:34:19 -05:00
Convey ( "When user is an Org Viewer but has an admin permission" , func ( ) {
role := m . ROLE_VIEWER
mockResult := [ ] * m . DashboardAclInfoDTO {
2018-02-14 08:04:26 -06:00
{ OrgId : 1 , DashboardId : 2 , UserId : 1 , Permission : m . PERMISSION_ADMIN } ,
2017-06-22 17:34:19 -05:00
}
bus . AddHandler ( "test" , func ( query * m . GetDashboardAclInfoListQuery ) error {
query . Result = mockResult
return nil
} )
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
2017-06-22 17:34:19 -05:00
dash := GetDashboardShouldReturn200 ( sc )
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by slug" , func ( ) {
So ( getDashboardQueries [ 0 ] . Slug , ShouldEqual , "child-dash" )
} )
2017-06-22 17:34:19 -05:00
Convey ( "Should be able to get dashboard with edit rights" , func ( ) {
So ( dash . Meta . CanEdit , ShouldBeTrue )
So ( dash . Meta . CanSave , ShouldBeTrue )
So ( dash . Meta . CanAdmin , ShouldBeTrue )
2017-06-12 08:48:55 -05:00
} )
} )
2017-06-12 16:05:32 -05:00
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2017-06-22 17:34:19 -05:00
dash := GetDashboardShouldReturn200 ( sc )
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by uid" , func ( ) {
So ( getDashboardQueries [ 0 ] . Uid , ShouldEqual , "abcdefghi" )
} )
2017-06-22 17:34:19 -05:00
Convey ( "Should be able to get dashboard with edit rights" , func ( ) {
So ( dash . Meta . CanEdit , ShouldBeTrue )
So ( dash . Meta . CanSave , ShouldBeTrue )
So ( dash . Meta . CanAdmin , ShouldBeTrue )
2017-06-12 08:48:55 -05:00
} )
} )
2017-06-12 16:05:32 -05:00
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
2017-06-12 16:05:32 -05:00
CallDeleteDashboard ( sc )
So ( sc . resp . Code , ShouldEqual , 200 )
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by slug" , func ( ) {
So ( getDashboardQueries [ 0 ] . Slug , ShouldEqual , "child-dash" )
} )
2017-06-12 16:05:32 -05:00
} )
2018-01-31 09:46:31 -06:00
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2018-03-22 06:37:35 -05:00
CallDeleteDashboardByUID ( sc )
2018-01-31 09:46:31 -06:00
So ( sc . resp . Code , ShouldEqual , 200 )
Convey ( "Should lookup dashboard by uid" , func ( ) {
So ( getDashboardQueries [ 0 ] . Uid , ShouldEqual , "abcdefghi" )
} )
2017-06-12 16:05:32 -05:00
} )
2017-06-13 17:28:34 -05:00
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/id/2/versions/1" , "/api/dashboards/id/:dashboardId/versions/:id" , role , func ( sc * scenarioContext ) {
CallGetDashboardVersion ( sc )
So ( sc . resp . Code , ShouldEqual , 200 )
} )
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" , "/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
CallGetDashboardVersions ( sc )
So ( sc . resp . Code , ShouldEqual , 200 )
} )
2017-06-12 08:48:55 -05:00
} )
Convey ( "When user is an Org Editor but has a view permission" , func ( ) {
2017-06-22 16:43:55 -05:00
role := m . ROLE_EDITOR
2017-06-12 16:05:32 -05:00
2017-06-22 16:43:55 -05:00
mockResult := [ ] * m . DashboardAclInfoDTO {
2018-02-14 08:04:26 -06:00
{ OrgId : 1 , DashboardId : 2 , UserId : 1 , Permission : m . PERMISSION_VIEW } ,
2017-06-12 08:48:55 -05:00
}
2017-06-22 16:43:55 -05:00
bus . AddHandler ( "test" , func ( query * m . GetDashboardAclInfoListQuery ) error {
2017-06-12 08:48:55 -05:00
query . Result = mockResult
return nil
} )
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
2017-06-12 16:05:32 -05:00
dash := GetDashboardShouldReturn200 ( sc )
2017-06-12 08:48:55 -05:00
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by slug" , func ( ) {
So ( getDashboardQueries [ 0 ] . Slug , ShouldEqual , "child-dash" )
} )
Convey ( "Should not be able to edit or save dashboard" , func ( ) {
So ( dash . Meta . CanEdit , ShouldBeFalse )
So ( dash . Meta . CanSave , ShouldBeFalse )
} )
} )
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2017-06-12 16:05:32 -05:00
dash := GetDashboardShouldReturn200 ( sc )
2017-06-12 08:48:55 -05:00
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by uid" , func ( ) {
So ( getDashboardQueries [ 0 ] . Uid , ShouldEqual , "abcdefghi" )
} )
2017-06-12 08:48:55 -05:00
Convey ( "Should not be able to edit or save dashboard" , func ( ) {
So ( dash . Meta . CanEdit , ShouldBeFalse )
So ( dash . Meta . CanSave , ShouldBeFalse )
} )
} )
2017-06-12 16:05:32 -05:00
2018-01-29 14:23:07 -06:00
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/db/child-dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
2017-06-12 16:05:32 -05:00
CallDeleteDashboard ( sc )
So ( sc . resp . Code , ShouldEqual , 403 )
2018-01-29 14:23:07 -06:00
Convey ( "Should lookup dashboard by slug" , func ( ) {
So ( getDashboardQueries [ 0 ] . Slug , ShouldEqual , "child-dash" )
} )
2017-06-12 16:05:32 -05:00
} )
2018-01-31 09:46:31 -06:00
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2018-03-22 06:37:35 -05:00
CallDeleteDashboardByUID ( sc )
2018-01-31 09:46:31 -06:00
So ( sc . resp . Code , ShouldEqual , 403 )
Convey ( "Should lookup dashboard by uid" , func ( ) {
So ( getDashboardQueries [ 0 ] . Uid , ShouldEqual , "abcdefghi" )
} )
2017-06-12 16:05:32 -05:00
} )
2017-06-13 17:28:34 -05:00
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/id/2/versions/1" , "/api/dashboards/id/:dashboardId/versions/:id" , role , func ( sc * scenarioContext ) {
CallGetDashboardVersion ( sc )
So ( sc . resp . Code , ShouldEqual , 403 )
} )
loggedInUserScenarioWithRole ( "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" , "/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
CallGetDashboardVersions ( sc )
So ( sc . resp . Code , ShouldEqual , 403 )
} )
2017-06-12 08:48:55 -05:00
} )
} )
2018-01-31 09:51:06 -06:00
Convey ( "Given two dashboards with the same title in different folders" , t , func ( ) {
dashOne := m . NewDashboard ( "dash" )
dashOne . Id = 2
dashOne . FolderId = 1
dashOne . HasAcl = false
dashTwo := m . NewDashboard ( "dash" )
dashTwo . Id = 4
dashTwo . FolderId = 3
dashTwo . HasAcl = false
2018-04-10 05:30:37 -05:00
bus . AddHandler ( "test" , func ( query * m . IsDashboardProvisionedQuery ) error {
query . Result = false
2018-04-10 03:48:10 -05:00
return nil
} )
2018-01-31 09:51:06 -06:00
bus . AddHandler ( "test" , func ( query * m . GetDashboardsBySlugQuery ) error {
dashboards := [ ] * m . Dashboard { dashOne , dashTwo }
query . Result = dashboards
return nil
} )
role := m . ROLE_EDITOR
loggedInUserScenarioWithRole ( "When calling DELETE on" , "DELETE" , "/api/dashboards/db/dash" , "/api/dashboards/db/:slug" , role , func ( sc * scenarioContext ) {
CallDeleteDashboard ( sc )
Convey ( "Should result in 412 Precondition failed" , func ( ) {
So ( sc . resp . Code , ShouldEqual , 412 )
2018-03-22 16:13:46 -05:00
result := sc . ToJSON ( )
2018-01-31 09:51:06 -06:00
So ( result . Get ( "status" ) . MustString ( ) , ShouldEqual , "multiple-slugs-exists" )
So ( result . Get ( "message" ) . MustString ( ) , ShouldEqual , m . ErrDashboardsWithSameSlugExists . Error ( ) )
} )
} )
} )
2018-02-19 04:12:56 -06:00
Convey ( "Post dashboard response tests" , t , func ( ) {
// This tests that a valid request returns correct response
Convey ( "Given a correct request for creating a dashboard" , func ( ) {
cmd := m . SaveDashboardCommand {
OrgId : 1 ,
UserId : 5 ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "Dash" ,
} ) ,
Overwrite : true ,
FolderId : 3 ,
IsFolder : false ,
Message : "msg" ,
}
mock := & dashboards . FakeDashboardService {
SaveDashboardResult : & m . Dashboard {
Id : 2 ,
Uid : "uid" ,
Title : "Dash" ,
Slug : "dash" ,
Version : 2 ,
} ,
}
postDashboardScenario ( "When calling POST on" , "/api/dashboards" , "/api/dashboards" , mock , cmd , func ( sc * scenarioContext ) {
CallPostDashboardShouldReturnSuccess ( sc )
Convey ( "It should call dashboard service with correct data" , func ( ) {
dto := mock . SavedDashboards [ 0 ]
So ( dto . OrgId , ShouldEqual , cmd . OrgId )
So ( dto . User . UserId , ShouldEqual , cmd . UserId )
So ( dto . Dashboard . FolderId , ShouldEqual , 3 )
So ( dto . Dashboard . Title , ShouldEqual , "Dash" )
So ( dto . Overwrite , ShouldBeTrue )
So ( dto . Message , ShouldEqual , "msg" )
} )
Convey ( "It should return correct response data" , func ( ) {
2018-03-22 16:13:46 -05:00
result := sc . ToJSON ( )
2018-02-19 04:12:56 -06:00
So ( result . Get ( "status" ) . MustString ( ) , ShouldEqual , "success" )
So ( result . Get ( "id" ) . MustInt64 ( ) , ShouldEqual , 2 )
So ( result . Get ( "uid" ) . MustString ( ) , ShouldEqual , "uid" )
So ( result . Get ( "slug" ) . MustString ( ) , ShouldEqual , "dash" )
So ( result . Get ( "url" ) . MustString ( ) , ShouldEqual , "/d/uid/dash" )
} )
} )
} )
// This tests that invalid requests returns expected error responses
Convey ( "Given incorrect requests for creating a dashboard" , func ( ) {
testCases := [ ] struct {
SaveError error
ExpectedStatusCode int
} {
{ SaveError : m . ErrDashboardNotFound , ExpectedStatusCode : 404 } ,
{ SaveError : m . ErrFolderNotFound , ExpectedStatusCode : 400 } ,
{ SaveError : m . ErrDashboardWithSameUIDExists , ExpectedStatusCode : 400 } ,
{ SaveError : m . ErrDashboardWithSameNameInFolderExists , ExpectedStatusCode : 412 } ,
{ SaveError : m . ErrDashboardVersionMismatch , ExpectedStatusCode : 412 } ,
{ SaveError : m . ErrDashboardTitleEmpty , ExpectedStatusCode : 400 } ,
{ SaveError : m . ErrDashboardFolderCannotHaveParent , ExpectedStatusCode : 400 } ,
2018-10-13 00:53:28 -05:00
{ SaveError : alerting . ValidationError { Reason : "Mu" } , ExpectedStatusCode : 422 } ,
2018-02-19 04:12:56 -06:00
{ SaveError : m . ErrDashboardFailedGenerateUniqueUid , ExpectedStatusCode : 500 } ,
{ SaveError : m . ErrDashboardTypeMismatch , ExpectedStatusCode : 400 } ,
{ SaveError : m . ErrDashboardFolderWithSameNameAsDashboard , ExpectedStatusCode : 400 } ,
{ SaveError : m . ErrDashboardWithSameNameAsFolder , ExpectedStatusCode : 400 } ,
{ SaveError : m . ErrDashboardFolderNameExists , ExpectedStatusCode : 400 } ,
{ SaveError : m . ErrDashboardUpdateAccessDenied , ExpectedStatusCode : 403 } ,
{ SaveError : m . ErrDashboardInvalidUid , ExpectedStatusCode : 400 } ,
{ SaveError : m . ErrDashboardUidToLong , ExpectedStatusCode : 400 } ,
2018-03-27 08:12:47 -05:00
{ SaveError : m . ErrDashboardCannotSaveProvisionedDashboard , ExpectedStatusCode : 400 } ,
2018-02-19 04:12:56 -06:00
{ SaveError : m . UpdatePluginDashboardError { PluginId : "plug" } , ExpectedStatusCode : 412 } ,
}
cmd := m . SaveDashboardCommand {
OrgId : 1 ,
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "" ,
} ) ,
}
for _ , tc := range testCases {
mock := & dashboards . FakeDashboardService {
SaveDashboardError : tc . SaveError ,
}
postDashboardScenario ( fmt . Sprintf ( "Expect '%s' error when calling POST on" , tc . SaveError . Error ( ) ) , "/api/dashboards" , "/api/dashboards" , mock , cmd , func ( sc * scenarioContext ) {
CallPostDashboard ( sc )
So ( sc . resp . Code , ShouldEqual , tc . ExpectedStatusCode )
} )
}
} )
} )
2018-02-27 10:53:30 -06:00
Convey ( "Given two dashboards being compared" , t , func ( ) {
mockResult := [ ] * m . DashboardAclInfoDTO { }
bus . AddHandler ( "test" , func ( query * m . GetDashboardAclInfoListQuery ) error {
query . Result = mockResult
return nil
} )
2018-04-10 05:30:37 -05:00
bus . AddHandler ( "test" , func ( query * m . IsDashboardProvisionedQuery ) error {
query . Result = false
2018-04-10 03:48:10 -05:00
return nil
} )
2018-02-27 10:53:30 -06:00
bus . AddHandler ( "test" , func ( query * m . GetDashboardVersionQuery ) error {
query . Result = & m . DashboardVersion {
Data : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "Dash" + string ( query . DashboardId ) ,
} ) ,
}
return nil
} )
cmd := dtos . CalculateDiffOptions {
Base : dtos . CalculateDiffTarget {
DashboardId : 1 ,
Version : 1 ,
} ,
New : dtos . CalculateDiffTarget {
DashboardId : 2 ,
Version : 2 ,
} ,
DiffType : "basic" ,
}
Convey ( "when user does not have permission" , func ( ) {
role := m . ROLE_VIEWER
postDiffScenario ( "When calling POST on" , "/api/dashboards/calculate-diff" , "/api/dashboards/calculate-diff" , cmd , role , func ( sc * scenarioContext ) {
CallPostDashboard ( sc )
So ( sc . resp . Code , ShouldEqual , 403 )
} )
} )
Convey ( "when user does have permission" , func ( ) {
role := m . ROLE_ADMIN
postDiffScenario ( "When calling POST on" , "/api/dashboards/calculate-diff" , "/api/dashboards/calculate-diff" , cmd , role , func ( sc * scenarioContext ) {
CallPostDashboard ( sc )
So ( sc . resp . Code , ShouldEqual , 200 )
} )
} )
} )
2017-06-12 08:48:55 -05:00
}
2017-06-12 16:05:32 -05:00
func GetDashboardShouldReturn200 ( sc * scenarioContext ) dtos . DashboardFullWithMeta {
2018-01-29 06:51:01 -06:00
CallGetDashboard ( sc )
2017-06-12 16:05:32 -05:00
So ( sc . resp . Code , ShouldEqual , 200 )
dash := dtos . DashboardFullWithMeta { }
err := json . NewDecoder ( sc . resp . Body ) . Decode ( & dash )
So ( err , ShouldBeNil )
return dash
}
2018-01-29 06:51:01 -06:00
func CallGetDashboard ( sc * scenarioContext ) {
sc . handlerFunc = GetDashboard
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
}
2017-06-13 17:28:34 -05:00
func CallGetDashboardVersion ( sc * scenarioContext ) {
2017-06-22 16:43:55 -05:00
bus . AddHandler ( "test" , func ( query * m . GetDashboardVersionQuery ) error {
query . Result = & m . DashboardVersion { }
2017-06-13 17:28:34 -05:00
return nil
} )
sc . handlerFunc = GetDashboardVersion
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
}
func CallGetDashboardVersions ( sc * scenarioContext ) {
2017-06-22 16:43:55 -05:00
bus . AddHandler ( "test" , func ( query * m . GetDashboardVersionsQuery ) error {
query . Result = [ ] * m . DashboardVersionDTO { }
2017-06-13 17:28:34 -05:00
return nil
} )
sc . handlerFunc = GetDashboardVersions
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
}
2017-06-12 16:05:32 -05:00
func CallDeleteDashboard ( sc * scenarioContext ) {
2017-06-22 16:43:55 -05:00
bus . AddHandler ( "test" , func ( cmd * m . DeleteDashboardCommand ) error {
2017-06-12 16:05:32 -05:00
return nil
} )
sc . handlerFunc = DeleteDashboard
sc . fakeReqWithParams ( "DELETE" , sc . url , map [ string ] string { } ) . exec ( )
}
2018-03-22 06:37:35 -05:00
func CallDeleteDashboardByUID ( sc * scenarioContext ) {
2018-01-31 09:46:31 -06:00
bus . AddHandler ( "test" , func ( cmd * m . DeleteDashboardCommand ) error {
return nil
} )
2018-03-22 06:37:35 -05:00
sc . handlerFunc = DeleteDashboardByUID
2018-01-31 09:46:31 -06:00
sc . fakeReqWithParams ( "DELETE" , sc . url , map [ string ] string { } ) . exec ( )
}
2017-06-12 16:05:32 -05:00
func CallPostDashboard ( sc * scenarioContext ) {
sc . fakeReqWithParams ( "POST" , sc . url , map [ string ] string { } ) . exec ( )
}
2018-01-30 16:37:54 -06:00
func CallPostDashboardShouldReturnSuccess ( sc * scenarioContext ) {
CallPostDashboard ( sc )
So ( sc . resp . Code , ShouldEqual , 200 )
}
2018-02-19 04:12:56 -06:00
func postDashboardScenario ( desc string , url string , routePattern string , mock * dashboards . FakeDashboardService , cmd m . SaveDashboardCommand , fn scenarioFunc ) {
2017-06-12 16:05:32 -05:00
Convey ( desc + " " + url , func ( ) {
defer bus . ClearBusHandlers ( )
2018-01-30 06:17:48 -06:00
sc := setupScenarioContext ( url )
2018-07-02 10:13:59 -05:00
sc . defaultHandler = Wrap ( func ( c * m . ReqContext ) Response {
2017-06-12 16:05:32 -05:00
sc . context = c
2018-02-19 04:12:56 -06:00
sc . context . SignedInUser = & m . SignedInUser { OrgId : cmd . OrgId , UserId : cmd . UserId }
2017-06-12 16:05:32 -05:00
return PostDashboard ( c , cmd )
} )
2018-02-19 04:12:56 -06:00
origNewDashboardService := dashboards . NewService
dashboards . MockDashboardService ( mock )
2017-12-12 09:15:24 -06:00
2017-06-12 16:05:32 -05:00
sc . m . Post ( routePattern , sc . defaultHandler )
2018-02-19 04:12:56 -06:00
defer func ( ) {
dashboards . NewService = origNewDashboardService
} ( )
2017-06-12 16:05:32 -05:00
fn ( sc )
} )
}
2018-01-29 12:27:53 -06:00
2018-02-27 10:53:30 -06:00
func postDiffScenario ( desc string , url string , routePattern string , cmd dtos . CalculateDiffOptions , role m . RoleType , fn scenarioFunc ) {
Convey ( desc + " " + url , func ( ) {
defer bus . ClearBusHandlers ( )
sc := setupScenarioContext ( url )
2018-07-02 10:13:59 -05:00
sc . defaultHandler = Wrap ( func ( c * m . ReqContext ) Response {
2018-02-27 10:53:30 -06:00
sc . context = c
sc . context . SignedInUser = & m . SignedInUser {
OrgId : TestOrgID ,
UserId : TestUserID ,
}
sc . context . OrgRole = role
return CalculateDashboardDiff ( c , cmd )
} )
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 )
So ( err , ShouldBeNil )
return result
}