2017-06-12 21:48:55 +08:00
package api
import (
2021-06-15 22:08:27 +08:00
"context"
2017-06-12 21:48:55 +08:00
"encoding/json"
2021-05-26 22:20:13 +08:00
"errors"
2018-02-19 18:12:56 +08:00
"fmt"
2021-07-16 18:40:03 +08:00
"net/http"
2022-08-10 21:37:51 +08:00
"os"
2017-06-12 21:48:55 +08:00
"testing"
2022-03-11 01:19:50 +08:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
2017-06-12 21:48:55 +08:00
"github.com/grafana/grafana/pkg/api/dtos"
2021-01-15 21:43:20 +08:00
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/api/routing"
2017-06-13 05:05:32 +08:00
"github.com/grafana/grafana/pkg/components/simplejson"
2022-10-19 21:02:15 +08:00
"github.com/grafana/grafana/pkg/infra/db"
2023-01-17 21:17:54 +08:00
"github.com/grafana/grafana/pkg/infra/db/dbtest"
2021-09-22 22:28:40 +08:00
"github.com/grafana/grafana/pkg/infra/usagestats"
2020-03-04 19:57:20 +08:00
"github.com/grafana/grafana/pkg/models"
2022-09-28 14:29:35 +08:00
"github.com/grafana/grafana/pkg/plugins"
Reconcile coremodels, entities, objects under new kind framework (#56492)
* Update thema to latest
* Deal with s/Library/*Runtime/
* Commit new, working results of codegen
* We like pointers now
* Always take runtime arg for NewBase()
* Sketchy handwavy pass at entity meta framework
* Little nibbles
* Update pkg/framework/coremodel/entityframework.cue
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
* Move file into new framework location
* Introduce loaders, Go code
* Complete rename to kind
* Flesh out framework, add svg/dashboard examples
* Cruft removal
* Remove generated kind go files from gitignore
* Refine maturity concept, add SlotKind
* Update embed and go deps
* Export PrefixWithGrafanaCUE
* Make the loader actually work, holy crap
* Many small tweaks to type.cue
* Add Apache 2 licensing exceptions for kinds
* Add new kinds dir, start of generator
* Roll back to earlier oapi-codegen
* Introduce new grafana-specific CUE loaders
* Introduce new tidy code generators framework
* Catch up kind framework with tinkering
* Add slices for the generators
* Add write/verify step to main generator
* Many renames
* Split up kind framework cue files
* Use kind.Decl within generated kinds
* Create kind.SomeDecl wrapper type to cache lineages
* Better names again
* Get one generated implemented, hopefully
* Copy dashboard schema into new kind.cue
* Small fixes to make the initial gen work
* Put svg kind in its new home
* Add generated Go dashboard type
* More renames and cleanups
* Add base kind registry and generator
* Stop blacklisting *_gen.go files
This is not the Go best practice, anyway. All we actually want to ignore
for enterprise is generated wire files.
* Change codegen output directories
pkg/kind -> pkg/kinds
pkg/registry/kindreg -> pkg/registry/corekind
* Rename pkg/framework/kind to pkg/kindsys
* Add core structured kind generator
* Add plural and machine names to kind spec
* Copy playlist over to kind system
* Consolidate kindsys files
* Add raw kind generator
* Update CODEOWNERS for kind framework
* Touch up comments a bit
* More docs tweaks
* Remove generated types to reduce noise for review
* Split each generator into its own file
* Rename Slot kind to Composable kind
* Add handwavy types for customkind loading
* Guard against init calls to framework loader
* First pass at doc on extending the kind system
* Improve attribute example in docs
* Fix wire imports
* Add basic TS types generator
* Fix composable kind category def
* No need for a separate file with generate directive
* Catch dashboard schema up
* Rename generator types to something saner and generic
* Make version configurable in ts/go generators
* Add CommonMeta to ease property access
* Add kindsys prop indicating whether lineage is group
* Put all kind categories back in a single file
* Finish with kindsys group props
* Refactor maturity progression per discussion
- Replace "committed" with "merged"
- All kindcats can use all maturity levels, at least for now
* Convert ts veneer index generator to modular system
* Move over to new jennywrites framework
* Strip down old coremodel generator
* Use public version of jennywrites
* Pull latest thema
* Commit generated Go types
* Add header injection postprocessor
* Move sdboyer/jennywrites to grafana/codejen
* Tweak header output
* Remove dashboard and playlist coremodels
* Fix up backend dashboards devenv test
* Fix TS import patterns to new gen filename
* Update internal imports, remove coremodel registry
* Fix compilation errors, wire generation
* Export and replace the prefix dropper
* More Go struct and field name changes
* Last name fixes, hopefully
* Fix lint errors
* Last lint error
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
2022-11-11 04:36:40 +08:00
"github.com/grafana/grafana/pkg/registry/corekind"
2022-11-24 22:38:55 +08:00
"github.com/grafana/grafana/pkg/services/accesscontrol/actest"
2022-03-10 19:58:18 +08:00
accesscontrolmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
2018-10-13 13:53:28 +08:00
"github.com/grafana/grafana/pkg/services/alerting"
2022-09-19 15:54:37 +08:00
"github.com/grafana/grafana/pkg/services/annotations/annotationstest"
2017-12-12 23:15:24 +08:00
"github.com/grafana/grafana/pkg/services/dashboards"
2022-02-16 21:15:44 +08:00
"github.com/grafana/grafana/pkg/services/dashboards/database"
2022-05-22 08:44:12 +08:00
"github.com/grafana/grafana/pkg/services/dashboards/service"
2022-05-25 16:41:51 +08:00
dashver "github.com/grafana/grafana/pkg/services/dashboardversion"
"github.com/grafana/grafana/pkg/services/dashboardversion/dashvertest"
2022-01-27 01:44:20 +08:00
"github.com/grafana/grafana/pkg/services/featuremgmt"
2022-10-11 03:47:53 +08:00
"github.com/grafana/grafana/pkg/services/folder"
2022-11-11 21:28:24 +08:00
"github.com/grafana/grafana/pkg/services/folder/foldertest"
2022-03-21 17:49:49 +08:00
"github.com/grafana/grafana/pkg/services/guardian"
2021-05-12 14:48:17 +08:00
"github.com/grafana/grafana/pkg/services/libraryelements"
2020-10-28 16:36:57 +08:00
"github.com/grafana/grafana/pkg/services/live"
2022-08-10 17:56:48 +08:00
"github.com/grafana/grafana/pkg/services/org"
2022-04-21 21:03:17 +08:00
pref "github.com/grafana/grafana/pkg/services/preference"
"github.com/grafana/grafana/pkg/services/preference/preftest"
2019-04-30 19:32:18 +08:00
"github.com/grafana/grafana/pkg/services/provisioning"
2022-11-15 03:08:10 +08:00
"github.com/grafana/grafana/pkg/services/quota/quotatest"
2022-09-21 20:04:01 +08:00
"github.com/grafana/grafana/pkg/services/tag/tagimpl"
2022-09-21 00:58:04 +08:00
"github.com/grafana/grafana/pkg/services/team/teamtest"
2022-08-10 17:56:48 +08:00
"github.com/grafana/grafana/pkg/services/user"
2017-12-15 21:19:49 +08:00
"github.com/grafana/grafana/pkg/setting"
2021-10-11 20:30:59 +08:00
"github.com/grafana/grafana/pkg/web"
2017-06-12 21:48:55 +08:00
)
2020-06-23 00:00:39 +08:00
func TestGetHomeDashboard ( t * testing . T ) {
2021-07-16 18:40:03 +08:00
httpReq , err := http . NewRequest ( http . MethodGet , "" , nil )
require . NoError ( t , err )
2022-02-09 20:44:38 +08:00
httpReq . Header . Add ( "Content-Type" , "application/json" )
2022-08-10 17:56:48 +08:00
req := & models . ReqContext { SignedInUser : & user . SignedInUser { } , Context : & web . Context { Req : httpReq } }
2020-06-23 00:00:39 +08:00
cfg := setting . NewCfg ( )
cfg . StaticRootPath = "../../public/"
2022-04-21 21:03:17 +08:00
prefService := preftest . NewPreferenceServiceFake ( )
2022-05-25 16:41:51 +08:00
dashboardVersionService := dashvertest . NewDashboardVersionServiceFake ( )
2020-06-23 00:00:39 +08:00
2021-03-17 23:06:10 +08:00
hs := & HTTPServer {
2022-05-25 16:41:51 +08:00
Cfg : cfg ,
2022-09-28 14:29:35 +08:00
pluginStore : & plugins . FakePluginStore { } ,
2023-01-17 21:17:54 +08:00
SQLStore : dbtest . NewFakeDB ( ) ,
2022-05-25 16:41:51 +08:00
preferenceService : prefService ,
dashboardVersionService : dashboardVersionService ,
Reconcile coremodels, entities, objects under new kind framework (#56492)
* Update thema to latest
* Deal with s/Library/*Runtime/
* Commit new, working results of codegen
* We like pointers now
* Always take runtime arg for NewBase()
* Sketchy handwavy pass at entity meta framework
* Little nibbles
* Update pkg/framework/coremodel/entityframework.cue
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
* Move file into new framework location
* Introduce loaders, Go code
* Complete rename to kind
* Flesh out framework, add svg/dashboard examples
* Cruft removal
* Remove generated kind go files from gitignore
* Refine maturity concept, add SlotKind
* Update embed and go deps
* Export PrefixWithGrafanaCUE
* Make the loader actually work, holy crap
* Many small tweaks to type.cue
* Add Apache 2 licensing exceptions for kinds
* Add new kinds dir, start of generator
* Roll back to earlier oapi-codegen
* Introduce new grafana-specific CUE loaders
* Introduce new tidy code generators framework
* Catch up kind framework with tinkering
* Add slices for the generators
* Add write/verify step to main generator
* Many renames
* Split up kind framework cue files
* Use kind.Decl within generated kinds
* Create kind.SomeDecl wrapper type to cache lineages
* Better names again
* Get one generated implemented, hopefully
* Copy dashboard schema into new kind.cue
* Small fixes to make the initial gen work
* Put svg kind in its new home
* Add generated Go dashboard type
* More renames and cleanups
* Add base kind registry and generator
* Stop blacklisting *_gen.go files
This is not the Go best practice, anyway. All we actually want to ignore
for enterprise is generated wire files.
* Change codegen output directories
pkg/kind -> pkg/kinds
pkg/registry/kindreg -> pkg/registry/corekind
* Rename pkg/framework/kind to pkg/kindsys
* Add core structured kind generator
* Add plural and machine names to kind spec
* Copy playlist over to kind system
* Consolidate kindsys files
* Add raw kind generator
* Update CODEOWNERS for kind framework
* Touch up comments a bit
* More docs tweaks
* Remove generated types to reduce noise for review
* Split each generator into its own file
* Rename Slot kind to Composable kind
* Add handwavy types for customkind loading
* Guard against init calls to framework loader
* First pass at doc on extending the kind system
* Improve attribute example in docs
* Fix wire imports
* Add basic TS types generator
* Fix composable kind category def
* No need for a separate file with generate directive
* Catch dashboard schema up
* Rename generator types to something saner and generic
* Make version configurable in ts/go generators
* Add CommonMeta to ease property access
* Add kindsys prop indicating whether lineage is group
* Put all kind categories back in a single file
* Finish with kindsys group props
* Refactor maturity progression per discussion
- Replace "committed" with "merged"
- All kindcats can use all maturity levels, at least for now
* Convert ts veneer index generator to modular system
* Move over to new jennywrites framework
* Strip down old coremodel generator
* Use public version of jennywrites
* Pull latest thema
* Commit generated Go types
* Add header injection postprocessor
* Move sdboyer/jennywrites to grafana/codejen
* Tweak header output
* Remove dashboard and playlist coremodels
* Fix up backend dashboards devenv test
* Fix TS import patterns to new gen filename
* Update internal imports, remove coremodel registry
* Fix compilation errors, wire generation
* Export and replace the prefix dropper
* More Go struct and field name changes
* Last name fixes, hopefully
* Fix lint errors
* Last lint error
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
2022-11-11 04:36:40 +08:00
Kinds : corekind . NewBase ( nil ) ,
2021-03-17 23:06:10 +08:00
}
2020-06-23 00:00:39 +08:00
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 . FolderTitle = "General"
2022-08-10 21:37:51 +08:00
homeDashJSON , err := os . ReadFile ( tc . expectedDashboardPath )
2020-06-23 00:00:39 +08:00
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" )
2022-04-21 21:03:17 +08:00
prefService . ExpectedPreference = & pref . Preference { }
2020-06-23 00:00:39 +08:00
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 21:43:20 +08:00
nr , ok := res . ( * response . NormalResponse )
2020-06-23 00:00:39 +08:00
require . True ( t , ok , "should return *NormalResponse" )
2021-01-15 21:43:20 +08:00
require . Equal ( t , b , nr . Body ( ) , "default home dashboard should equal content on disk" )
2020-06-23 00:00:39 +08:00
} )
}
}
2022-10-15 03:33:06 +08:00
func newTestLive ( t * testing . T , store db . DB ) * live . GrafanaLive {
2022-01-27 01:44:20 +08:00
features := featuremgmt . WithFeatures ( )
2021-08-25 21:11:22 +08:00
cfg := & setting . Cfg { AppURL : "http://localhost:3000/" }
2022-01-27 01:44:20 +08:00
cfg . IsFeatureToggleEnabled = features . IsEnabled
gLive , err := live . ProvideService ( nil , cfg ,
routing . NewRouteRegister ( ) ,
nil , nil , nil ,
2022-03-23 03:48:32 +08:00
store ,
2022-01-27 01:44:20 +08:00
nil ,
& usagestats . UsageStatsMock { T : t } ,
nil ,
2022-09-20 15:55:40 +08:00
features , accesscontrolmock . New ( ) , & dashboards . FakeDashboardService { } , annotationstest . NewFakeAnnotationsRepo ( ) , nil )
2021-05-04 23:44:55 +08:00
require . NoError ( t , err )
return gLive
}
2018-02-19 18:12:56 +08: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 21:09:30 +08:00
2020-11-13 16:52:38 +08: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 ) {
2023-01-16 23:33:55 +08:00
fakeDash := dashboards . NewDashboard ( "Child dash" )
fakeDash . ID = 1
fakeDash . FolderID = 1
2022-07-18 21:14:58 +08:00
fakeDash . HasACL = false
2022-05-25 16:41:51 +08:00
fakeDashboardVersionService := dashvertest . NewDashboardVersionServiceFake ( )
2022-12-28 00:17:24 +08:00
fakeDashboardVersionService . ExpectedDashboardVersion = & dashver . DashboardVersionDTO { }
2022-09-21 00:58:04 +08:00
teamService := & teamtest . FakeService { }
2022-05-23 23:14:27 +08:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2023-01-16 23:33:55 +08:00
dashboardService . On ( "GetDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardQuery" ) ) . Run ( func ( args mock . Arguments ) {
q := args . Get ( 1 ) . ( * dashboards . GetDashboardQuery )
2022-05-23 23:14:27 +08:00
q . Result = fakeDash
} ) . Return ( nil )
2023-01-17 21:17:54 +08:00
mockSQLStore := dbtest . NewFakeDB ( )
2018-01-31 23:51:06 +08:00
2022-02-07 19:43:43 +08:00
hs := & HTTPServer {
2022-05-25 16:41:51 +08:00
Cfg : setting . NewCfg ( ) ,
2022-09-28 14:29:35 +08:00
pluginStore : & plugins . FakePluginStore { } ,
2022-05-25 16:41:51 +08:00
SQLStore : mockSQLStore ,
AccessControl : accesscontrolmock . New ( ) ,
Features : featuremgmt . WithFeatures ( ) ,
2022-07-07 02:42:39 +08:00
DashboardService : dashboardService ,
2022-05-25 16:41:51 +08:00
dashboardVersionService : fakeDashboardVersionService ,
Reconcile coremodels, entities, objects under new kind framework (#56492)
* Update thema to latest
* Deal with s/Library/*Runtime/
* Commit new, working results of codegen
* We like pointers now
* Always take runtime arg for NewBase()
* Sketchy handwavy pass at entity meta framework
* Little nibbles
* Update pkg/framework/coremodel/entityframework.cue
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
* Move file into new framework location
* Introduce loaders, Go code
* Complete rename to kind
* Flesh out framework, add svg/dashboard examples
* Cruft removal
* Remove generated kind go files from gitignore
* Refine maturity concept, add SlotKind
* Update embed and go deps
* Export PrefixWithGrafanaCUE
* Make the loader actually work, holy crap
* Many small tweaks to type.cue
* Add Apache 2 licensing exceptions for kinds
* Add new kinds dir, start of generator
* Roll back to earlier oapi-codegen
* Introduce new grafana-specific CUE loaders
* Introduce new tidy code generators framework
* Catch up kind framework with tinkering
* Add slices for the generators
* Add write/verify step to main generator
* Many renames
* Split up kind framework cue files
* Use kind.Decl within generated kinds
* Create kind.SomeDecl wrapper type to cache lineages
* Better names again
* Get one generated implemented, hopefully
* Copy dashboard schema into new kind.cue
* Small fixes to make the initial gen work
* Put svg kind in its new home
* Add generated Go dashboard type
* More renames and cleanups
* Add base kind registry and generator
* Stop blacklisting *_gen.go files
This is not the Go best practice, anyway. All we actually want to ignore
for enterprise is generated wire files.
* Change codegen output directories
pkg/kind -> pkg/kinds
pkg/registry/kindreg -> pkg/registry/corekind
* Rename pkg/framework/kind to pkg/kindsys
* Add core structured kind generator
* Add plural and machine names to kind spec
* Copy playlist over to kind system
* Consolidate kindsys files
* Add raw kind generator
* Update CODEOWNERS for kind framework
* Touch up comments a bit
* More docs tweaks
* Remove generated types to reduce noise for review
* Split each generator into its own file
* Rename Slot kind to Composable kind
* Add handwavy types for customkind loading
* Guard against init calls to framework loader
* First pass at doc on extending the kind system
* Improve attribute example in docs
* Fix wire imports
* Add basic TS types generator
* Fix composable kind category def
* No need for a separate file with generate directive
* Catch dashboard schema up
* Rename generator types to something saner and generic
* Make version configurable in ts/go generators
* Add CommonMeta to ease property access
* Add kindsys prop indicating whether lineage is group
* Put all kind categories back in a single file
* Finish with kindsys group props
* Refactor maturity progression per discussion
- Replace "committed" with "merged"
- All kindcats can use all maturity levels, at least for now
* Convert ts veneer index generator to modular system
* Move over to new jennywrites framework
* Strip down old coremodel generator
* Use public version of jennywrites
* Pull latest thema
* Commit generated Go types
* Add header injection postprocessor
* Move sdboyer/jennywrites to grafana/codejen
* Tweak header output
* Remove dashboard and playlist coremodels
* Fix up backend dashboards devenv test
* Fix TS import patterns to new gen filename
* Update internal imports, remove coremodel registry
* Fix compilation errors, wire generation
* Export and replace the prefix dropper
* More Go struct and field name changes
* Last name fixes, hopefully
* Fix lint errors
* Last lint error
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
2022-11-11 04:36:40 +08:00
Kinds : corekind . NewBase ( nil ) ,
2022-11-15 03:08:10 +08:00
QuotaService : quotatest . New ( false , nil ) ,
2022-02-07 19:43:43 +08:00
}
2018-01-30 04:23:07 +08:00
2022-02-07 19:43:43 +08:00
setUp := func ( ) {
2022-08-10 17:56:48 +08:00
viewerRole := org . RoleViewer
editorRole := org . RoleEditor
2023-01-20 21:58:47 +08:00
dashboardService . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Run ( func ( args mock . Arguments ) {
q := args . Get ( 1 ) . ( * dashboards . GetDashboardACLInfoListQuery )
q . Result = [ ] * dashboards . DashboardACLInfoDTO {
2022-06-02 02:16:26 +08:00
{ Role : & viewerRole , Permission : models . PERMISSION_VIEW } ,
{ Role : & editorRole , Permission : models . PERMISSION_EDIT } ,
}
} ) . Return ( nil )
2022-09-21 00:58:04 +08:00
guardian . InitLegacyGuardian ( mockSQLStore , dashboardService , teamService )
2020-11-13 16:52:38 +08:00
}
2017-06-23 05:43:55 +08:00
2018-01-30 21:09:30 +08:00
// This tests two scenarios:
// 1. user is an org viewer
// 2. user is an org editor
2020-11-13 16:52:38 +08:00
t . Run ( "When user is an Org Viewer" , func ( t * testing . T ) {
2022-08-10 17:56:48 +08:00
role := org . RoleViewer
2020-11-13 16:52:38 +08:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 19:43:43 +08:00
setUp ( )
sc . sqlStore = mockSQLStore
2023-01-20 00:38:07 +08:00
dash := getDashboardShouldReturn200WithConfig ( t , sc , nil , nil , dashboardService , nil )
2020-11-13 16:52:38 +08:00
assert . False ( t , dash . Meta . CanEdit )
assert . False ( t , dash . Meta . CanSave )
assert . False ( t , dash . Meta . CanAdmin )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-13 05:05:32 +08:00
2020-11-13 16:52:38 +08: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 ( )
2022-02-07 19:43:43 +08:00
sc . sqlStore = mockSQLStore
2018-01-31 23:46:31 +08:00
2022-02-07 19:43:43 +08:00
hs . callGetDashboardVersion ( sc )
2020-11-13 16:52:38 +08:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-13 05:05:32 +08:00
2020-11-13 16:52:38 +08:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" ,
"/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
setUp ( )
2022-02-07 19:43:43 +08:00
sc . sqlStore = mockSQLStore
2017-06-14 06:28:34 +08:00
2022-02-07 19:43:43 +08:00
hs . callGetDashboardVersions ( sc )
2020-11-13 16:52:38 +08:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-12 21:48:55 +08:00
} )
2020-11-13 16:52:38 +08:00
t . Run ( "When user is an Org Editor" , func ( t * testing . T ) {
2022-08-10 17:56:48 +08:00
role := org . RoleEditor
2020-11-13 16:52:38 +08:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 19:43:43 +08:00
setUp ( )
sc . sqlStore = mockSQLStore
2023-01-20 00:38:07 +08:00
dash := getDashboardShouldReturn200WithConfig ( t , sc , nil , nil , dashboardService , nil )
2017-06-12 21:48:55 +08:00
2020-11-13 16:52:38 +08:00
assert . True ( t , dash . Meta . CanEdit )
assert . True ( t , dash . Meta . CanSave )
assert . False ( t , dash . Meta . CanAdmin )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-13 05:05:32 +08:00
2020-11-13 16:52:38 +08: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 ( )
2022-02-07 19:43:43 +08:00
sc . sqlStore = mockSQLStore
hs . callGetDashboardVersion ( sc )
2018-01-31 23:46:31 +08:00
2020-11-13 16:52:38 +08:00
assert . Equal ( t , 200 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-13 05:05:32 +08:00
2020-11-13 16:52:38 +08:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" ,
"/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
setUp ( )
2022-02-07 19:43:43 +08:00
hs . callGetDashboardVersions ( sc )
2017-06-14 06:28:34 +08:00
2020-11-13 16:52:38 +08:00
assert . Equal ( t , 200 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-12 21:48:55 +08:00
} )
} )
2020-11-13 16:52:38 +08:00
t . Run ( "Given a dashboard with a parent folder which has an ACL" , func ( t * testing . T ) {
2023-01-16 23:33:55 +08:00
fakeDash := dashboards . NewDashboard ( "Child dash" )
fakeDash . ID = 1
fakeDash . FolderID = 1
2022-07-18 21:14:58 +08:00
fakeDash . HasACL = true
2022-05-25 16:41:51 +08:00
fakeDashboardVersionService := dashvertest . NewDashboardVersionServiceFake ( )
2022-12-28 00:17:24 +08:00
fakeDashboardVersionService . ExpectedDashboardVersion = & dashver . DashboardVersionDTO { }
2022-09-21 00:58:04 +08:00
teamService := & teamtest . FakeService { }
2022-05-23 23:14:27 +08:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2023-01-16 23:33:55 +08:00
dashboardService . On ( "GetDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardQuery" ) ) . Run ( func ( args mock . Arguments ) {
q := args . Get ( 1 ) . ( * dashboards . GetDashboardQuery )
2022-05-23 23:14:27 +08:00
q . Result = fakeDash
} ) . Return ( nil )
2023-01-20 21:58:47 +08:00
dashboardService . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Run ( func ( args mock . Arguments ) {
q := args . Get ( 1 ) . ( * dashboards . GetDashboardACLInfoListQuery )
q . Result = [ ] * dashboards . DashboardACLInfoDTO {
2022-06-02 02:16:26 +08:00
{
2023-01-20 21:58:47 +08:00
DashboardID : 1 ,
2022-06-02 02:16:26 +08:00
Permission : models . PERMISSION_EDIT ,
2023-01-20 21:58:47 +08:00
UserID : 200 ,
2022-06-02 02:16:26 +08:00
} ,
}
} ) . Return ( nil )
2022-02-07 19:43:43 +08:00
2023-01-17 21:17:54 +08:00
mockSQLStore := dbtest . NewFakeDB ( )
2022-03-10 19:58:18 +08:00
cfg := setting . NewCfg ( )
2022-10-19 21:02:15 +08:00
sql := db . InitTestDB ( t )
2022-05-18 02:52:22 +08:00
2020-11-13 16:52:38 +08:00
hs := & HTTPServer {
2022-05-25 16:41:51 +08:00
Cfg : cfg ,
Live : newTestLive ( t , sql ) ,
LibraryPanelService : & mockLibraryPanelService { } ,
LibraryElementService : & mockLibraryElementService { } ,
SQLStore : mockSQLStore ,
AccessControl : accesscontrolmock . New ( ) ,
2022-07-07 02:42:39 +08:00
DashboardService : dashboardService ,
2022-05-25 16:41:51 +08:00
dashboardVersionService : fakeDashboardVersionService ,
2022-07-20 07:44:41 +08:00
Features : featuremgmt . WithFeatures ( ) ,
Reconcile coremodels, entities, objects under new kind framework (#56492)
* Update thema to latest
* Deal with s/Library/*Runtime/
* Commit new, working results of codegen
* We like pointers now
* Always take runtime arg for NewBase()
* Sketchy handwavy pass at entity meta framework
* Little nibbles
* Update pkg/framework/coremodel/entityframework.cue
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
* Move file into new framework location
* Introduce loaders, Go code
* Complete rename to kind
* Flesh out framework, add svg/dashboard examples
* Cruft removal
* Remove generated kind go files from gitignore
* Refine maturity concept, add SlotKind
* Update embed and go deps
* Export PrefixWithGrafanaCUE
* Make the loader actually work, holy crap
* Many small tweaks to type.cue
* Add Apache 2 licensing exceptions for kinds
* Add new kinds dir, start of generator
* Roll back to earlier oapi-codegen
* Introduce new grafana-specific CUE loaders
* Introduce new tidy code generators framework
* Catch up kind framework with tinkering
* Add slices for the generators
* Add write/verify step to main generator
* Many renames
* Split up kind framework cue files
* Use kind.Decl within generated kinds
* Create kind.SomeDecl wrapper type to cache lineages
* Better names again
* Get one generated implemented, hopefully
* Copy dashboard schema into new kind.cue
* Small fixes to make the initial gen work
* Put svg kind in its new home
* Add generated Go dashboard type
* More renames and cleanups
* Add base kind registry and generator
* Stop blacklisting *_gen.go files
This is not the Go best practice, anyway. All we actually want to ignore
for enterprise is generated wire files.
* Change codegen output directories
pkg/kind -> pkg/kinds
pkg/registry/kindreg -> pkg/registry/corekind
* Rename pkg/framework/kind to pkg/kindsys
* Add core structured kind generator
* Add plural and machine names to kind spec
* Copy playlist over to kind system
* Consolidate kindsys files
* Add raw kind generator
* Update CODEOWNERS for kind framework
* Touch up comments a bit
* More docs tweaks
* Remove generated types to reduce noise for review
* Split each generator into its own file
* Rename Slot kind to Composable kind
* Add handwavy types for customkind loading
* Guard against init calls to framework loader
* First pass at doc on extending the kind system
* Improve attribute example in docs
* Fix wire imports
* Add basic TS types generator
* Fix composable kind category def
* No need for a separate file with generate directive
* Catch dashboard schema up
* Rename generator types to something saner and generic
* Make version configurable in ts/go generators
* Add CommonMeta to ease property access
* Add kindsys prop indicating whether lineage is group
* Put all kind categories back in a single file
* Finish with kindsys group props
* Refactor maturity progression per discussion
- Replace "committed" with "merged"
- All kindcats can use all maturity levels, at least for now
* Convert ts veneer index generator to modular system
* Move over to new jennywrites framework
* Strip down old coremodel generator
* Use public version of jennywrites
* Pull latest thema
* Commit generated Go types
* Add header injection postprocessor
* Move sdboyer/jennywrites to grafana/codejen
* Tweak header output
* Remove dashboard and playlist coremodels
* Fix up backend dashboards devenv test
* Fix TS import patterns to new gen filename
* Update internal imports, remove coremodel registry
* Fix compilation errors, wire generation
* Export and replace the prefix dropper
* More Go struct and field name changes
* Last name fixes, hopefully
* Fix lint errors
* Last lint error
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
2022-11-11 04:36:40 +08:00
Kinds : corekind . NewBase ( nil ) ,
2020-11-13 16:52:38 +08:00
}
2017-06-12 21:48:55 +08:00
2022-02-07 19:43:43 +08:00
setUp := func ( ) {
2020-11-13 16:52:38 +08:00
origCanEdit := setting . ViewersCanEdit
t . Cleanup ( func ( ) {
setting . ViewersCanEdit = origCanEdit
} )
setting . ViewersCanEdit = false
2022-09-21 00:58:04 +08:00
guardian . InitLegacyGuardian ( mockSQLStore , dashboardService , teamService )
2019-04-30 19:32:18 +08:00
}
2018-01-30 21:09:30 +08: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 16:52:38 +08:00
t . Run ( "When user is an Org Viewer and has no permissions for this dashboard" , func ( t * testing . T ) {
2022-08-10 17:56:48 +08:00
role := org . RoleViewer
2020-11-13 16:52:38 +08:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 19:43:43 +08:00
setUp ( )
sc . sqlStore = mockSQLStore
2020-11-13 16:52:38 +08:00
sc . handlerFunc = hs . GetDashboard
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
2022-02-07 19:43:43 +08:00
2020-11-13 16:52:38 +08:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2018-01-30 04:23:07 +08:00
2020-11-13 16:52:38 +08:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 19:43:43 +08:00
setUp ( )
sc . sqlStore = mockSQLStore
2022-05-23 23:14:27 +08:00
hs . callDeleteDashboardByUID ( t , sc , dashboardService )
2018-01-30 04:23:07 +08:00
2020-11-13 16:52:38 +08:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-13 05:05:32 +08:00
2020-11-13 16:52:38 +08: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 ( )
2022-02-07 19:43:43 +08:00
sc . sqlStore = mockSQLStore
hs . callGetDashboardVersion ( sc )
2018-01-31 23:46:31 +08:00
2020-11-13 16:52:38 +08:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-13 05:05:32 +08:00
2020-11-13 16:52:38 +08:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" ,
"/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
setUp ( )
2022-02-07 19:43:43 +08:00
hs . callGetDashboardVersions ( sc )
2017-06-14 06:28:34 +08:00
2020-11-13 16:52:38 +08:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-12 21:48:55 +08:00
} )
2020-11-13 16:52:38 +08:00
t . Run ( "When user is an Org Editor and has no permissions for this dashboard" , func ( t * testing . T ) {
2022-08-10 17:56:48 +08:00
role := org . RoleEditor
2020-11-13 16:52:38 +08:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 19:43:43 +08:00
setUp ( )
sc . sqlStore = mockSQLStore
2020-11-13 16:52:38 +08:00
sc . handlerFunc = hs . GetDashboard
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2018-01-30 04:23:07 +08:00
2020-11-13 16:52:38 +08:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 19:43:43 +08:00
setUp ( )
2022-05-23 23:14:27 +08:00
hs . callDeleteDashboardByUID ( t , sc , dashboardService )
2018-01-30 04:23:07 +08:00
2020-11-13 16:52:38 +08:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-13 05:05:32 +08:00
2020-11-13 16:52:38 +08: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 ( )
2022-02-07 19:43:43 +08:00
hs . callGetDashboardVersion ( sc )
2018-01-31 23:46:31 +08:00
2020-11-13 16:52:38 +08:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-13 05:05:32 +08:00
2020-11-13 16:52:38 +08:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" ,
"/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
setUp ( )
2022-02-07 19:43:43 +08:00
hs . callGetDashboardVersions ( sc )
2017-06-14 06:28:34 +08:00
2020-11-13 16:52:38 +08:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-12 21:48:55 +08:00
} )
2020-11-13 16:52:38 +08:00
t . Run ( "When user is an Org Viewer but has an edit permission" , func ( t * testing . T ) {
2022-08-10 17:56:48 +08:00
role := org . RoleViewer
2017-06-13 05:05:32 +08:00
2022-02-07 19:43:43 +08:00
setUpInner := func ( ) {
2022-06-02 02:16:26 +08:00
origCanEdit := setting . ViewersCanEdit
t . Cleanup ( func ( ) {
setting . ViewersCanEdit = origCanEdit
} )
setting . ViewersCanEdit = false
dashboardService := dashboards . NewFakeDashboardService ( t )
2023-01-20 21:58:47 +08:00
dashboardService . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Run ( func ( args mock . Arguments ) {
q := args . Get ( 1 ) . ( * dashboards . GetDashboardACLInfoListQuery )
q . Result = [ ] * dashboards . DashboardACLInfoDTO {
{ OrgID : 1 , DashboardID : 2 , UserID : 1 , Permission : models . PERMISSION_EDIT } ,
2022-06-02 02:16:26 +08:00
}
} ) . Return ( nil )
2022-09-21 00:58:04 +08:00
guardian . InitLegacyGuardian ( mockSQLStore , dashboardService , teamService )
2020-11-13 16:52:38 +08:00
}
2022-02-07 19:43:43 +08:00
2020-11-13 16:52:38 +08:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 19:43:43 +08:00
setUpInner ( )
sc . sqlStore = mockSQLStore
2023-01-20 00:38:07 +08:00
dash := getDashboardShouldReturn200WithConfig ( t , sc , nil , nil , dashboardService , nil )
2018-01-30 04:23:07 +08:00
2020-11-13 16:52:38 +08:00
assert . True ( t , dash . Meta . CanEdit )
assert . True ( t , dash . Meta . CanSave )
assert . False ( t , dash . Meta . CanAdmin )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-23 06:34:19 +08:00
2020-11-13 16:52:38 +08:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 19:43:43 +08:00
setUpInner ( )
2022-05-23 23:14:27 +08:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2023-01-16 23:33:55 +08:00
dashboardService . On ( "GetDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardQuery" ) ) . Run ( func ( args mock . Arguments ) {
q := args . Get ( 1 ) . ( * dashboards . GetDashboardQuery )
q . Result = dashboards . NewDashboard ( "test" )
2022-05-23 23:14:27 +08:00
} ) . Return ( nil )
dashboardService . On ( "DeleteDashboard" , mock . Anything , mock . AnythingOfType ( "int64" ) , mock . AnythingOfType ( "int64" ) ) . Return ( nil )
2022-02-07 19:43:43 +08:00
2022-05-23 23:14:27 +08:00
hs . callDeleteDashboardByUID ( t , sc , dashboardService )
2022-02-07 19:43:43 +08:00
2020-11-13 16:52:38 +08:00
assert . Equal ( t , 200 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-23 06:34:19 +08:00
2020-11-13 16:52:38 +08: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 ( )
2022-02-07 19:43:43 +08:00
sc . sqlStore = mockSQLStore
2022-05-25 16:41:51 +08:00
sc . dashboardVersionService = fakeDashboardVersionService
2022-02-07 19:43:43 +08:00
hs . callGetDashboardVersion ( sc )
2020-11-13 16:52:38 +08:00
assert . Equal ( t , 200 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-23 06:34:19 +08:00
2020-11-13 16:52:38 +08:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" , "/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
setUpInner ( )
2022-02-07 19:43:43 +08:00
hs . callGetDashboardVersions ( sc )
2020-11-13 16:52:38 +08:00
assert . Equal ( t , 200 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-23 06:34:19 +08:00
} )
2020-11-13 16:52:38 +08:00
t . Run ( "When user is an Org Viewer and viewers can edit" , func ( t * testing . T ) {
2022-08-10 17:56:48 +08:00
role := org . RoleViewer
2017-12-15 21:19:49 +08:00
2022-02-07 19:43:43 +08:00
setUpInner := func ( ) {
2020-11-13 16:52:38 +08:00
origCanEdit := setting . ViewersCanEdit
t . Cleanup ( func ( ) {
setting . ViewersCanEdit = origCanEdit
2017-12-15 21:19:49 +08:00
} )
2020-11-13 16:52:38 +08:00
setting . ViewersCanEdit = true
2022-06-02 02:16:26 +08:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2023-01-20 21:58:47 +08:00
dashboardService . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Run ( func ( args mock . Arguments ) {
q := args . Get ( 1 ) . ( * dashboards . GetDashboardACLInfoListQuery )
q . Result = [ ] * dashboards . DashboardACLInfoDTO {
{ OrgID : 1 , DashboardID : 2 , UserID : 1 , Permission : models . PERMISSION_VIEW } ,
2022-06-02 02:16:26 +08:00
}
} ) . Return ( nil )
2022-09-21 00:58:04 +08:00
guardian . InitLegacyGuardian ( mockSQLStore , dashboardService , teamService )
2020-11-13 16:52:38 +08:00
}
2022-02-07 19:43:43 +08:00
2020-11-13 16:52:38 +08:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 19:43:43 +08:00
setUpInner ( )
2017-12-15 21:19:49 +08:00
2020-11-13 16:52:38 +08:00
require . True ( t , setting . ViewersCanEdit )
2022-02-07 19:43:43 +08:00
sc . sqlStore = mockSQLStore
2023-01-20 00:38:07 +08:00
dash := getDashboardShouldReturn200WithConfig ( t , sc , nil , nil , dashboardService , nil )
2018-01-30 04:23:07 +08:00
2020-11-13 16:52:38 +08:00
assert . True ( t , dash . Meta . CanEdit )
assert . False ( t , dash . Meta . CanSave )
assert . False ( t , dash . Meta . CanAdmin )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-12-15 21:19:49 +08:00
2020-11-13 16:52:38 +08:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 19:43:43 +08:00
setUpInner ( )
2018-01-31 23:46:31 +08:00
2022-05-18 02:52:22 +08:00
hs . callDeleteDashboardByUID ( t , sc , dashboardService )
2020-11-13 16:52:38 +08:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-12-15 21:19:49 +08:00
} )
2020-11-13 16:52:38 +08:00
t . Run ( "When user is an Org Viewer but has an admin permission" , func ( t * testing . T ) {
2022-08-10 17:56:48 +08:00
role := org . RoleViewer
2017-06-23 06:34:19 +08:00
2022-02-07 19:43:43 +08:00
setUpInner := func ( ) {
2022-06-02 02:16:26 +08:00
origCanEdit := setting . ViewersCanEdit
t . Cleanup ( func ( ) {
setting . ViewersCanEdit = origCanEdit
} )
setting . ViewersCanEdit = true
2020-11-13 16:52:38 +08:00
2022-06-02 02:16:26 +08:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2023-01-20 21:58:47 +08:00
dashboardService . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Run ( func ( args mock . Arguments ) {
q := args . Get ( 1 ) . ( * dashboards . GetDashboardACLInfoListQuery )
q . Result = [ ] * dashboards . DashboardACLInfoDTO {
{ OrgID : 1 , DashboardID : 2 , UserID : 1 , Permission : models . PERMISSION_ADMIN } ,
2022-06-02 02:16:26 +08:00
}
} ) . Return ( nil )
2022-09-21 00:58:04 +08:00
guardian . InitLegacyGuardian ( mockSQLStore , dashboardService , teamService )
2017-06-23 06:34:19 +08:00
}
2022-02-07 19:43:43 +08:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
setUpInner ( )
sc . sqlStore = mockSQLStore
2023-01-20 00:38:07 +08:00
dash := getDashboardShouldReturn200WithConfig ( t , sc , nil , nil , dashboardService , nil )
2018-01-30 04:23:07 +08:00
2020-11-13 16:52:38 +08:00
assert . True ( t , dash . Meta . CanEdit )
assert . True ( t , dash . Meta . CanSave )
assert . True ( t , dash . Meta . CanAdmin )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-13 05:05:32 +08:00
2020-11-13 16:52:38 +08:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 19:43:43 +08:00
setUpInner ( )
sc . sqlStore = mockSQLStore
2022-05-23 23:14:27 +08:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2023-01-16 23:33:55 +08:00
dashboardService . On ( "GetDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardQuery" ) ) . Run ( func ( args mock . Arguments ) {
q := args . Get ( 1 ) . ( * dashboards . GetDashboardQuery )
q . Result = dashboards . NewDashboard ( "test" )
2022-05-23 23:14:27 +08:00
} ) . Return ( nil )
dashboardService . On ( "DeleteDashboard" , mock . Anything , mock . AnythingOfType ( "int64" ) , mock . AnythingOfType ( "int64" ) ) . Return ( nil )
hs . callDeleteDashboardByUID ( t , sc , dashboardService )
2018-01-30 04:23:07 +08:00
2020-11-13 16:52:38 +08:00
assert . Equal ( t , 200 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-13 05:05:32 +08:00
2020-11-13 16:52:38 +08: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 23:46:31 +08:00
2022-02-07 19:43:43 +08:00
hs . callGetDashboardVersion ( sc )
2020-11-13 16:52:38 +08:00
assert . Equal ( t , 200 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-13 05:05:32 +08:00
2020-11-13 16:52:38 +08: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-14 06:28:34 +08:00
2022-02-07 19:43:43 +08:00
hs . callGetDashboardVersions ( sc )
2020-11-13 16:52:38 +08:00
assert . Equal ( t , 200 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-12 21:48:55 +08:00
} )
2020-11-13 16:52:38 +08:00
t . Run ( "When user is an Org Editor but has a view permission" , func ( t * testing . T ) {
2022-08-10 17:56:48 +08:00
role := org . RoleEditor
2017-06-13 05:05:32 +08:00
2022-02-07 19:43:43 +08:00
setUpInner := func ( ) {
2022-06-02 02:16:26 +08:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2023-01-20 21:58:47 +08:00
dashboardService . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Run ( func ( args mock . Arguments ) {
q := args . Get ( 1 ) . ( * dashboards . GetDashboardACLInfoListQuery )
q . Result = [ ] * dashboards . DashboardACLInfoDTO {
{ OrgID : 1 , DashboardID : 2 , UserID : 1 , Permission : models . PERMISSION_VIEW } ,
2022-06-02 02:16:26 +08:00
}
} ) . Return ( nil )
2022-09-21 00:58:04 +08:00
guardian . InitLegacyGuardian ( mockSQLStore , dashboardService , teamService )
2020-11-13 16:52:38 +08:00
}
2017-06-12 21:48:55 +08:00
2022-02-07 19:43:43 +08:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
setUpInner ( )
sc . sqlStore = mockSQLStore
2023-01-20 00:38:07 +08:00
dash := getDashboardShouldReturn200WithConfig ( t , sc , nil , nil , dashboardService , nil )
2022-02-07 19:43:43 +08:00
2020-11-13 16:52:38 +08:00
assert . False ( t , dash . Meta . CanEdit )
assert . False ( t , dash . Meta . CanSave )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-13 05:05:32 +08:00
2020-11-13 16:52:38 +08:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 19:43:43 +08:00
setUpInner ( )
2022-05-18 02:52:22 +08:00
hs . callDeleteDashboardByUID ( t , sc , dashboardService )
2018-01-31 23:46:31 +08:00
2020-11-13 16:52:38 +08:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-13 05:05:32 +08:00
2020-11-13 16:52:38 +08: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 ( )
2022-02-07 19:43:43 +08:00
hs . callGetDashboardVersion ( sc )
2020-11-13 16:52:38 +08:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-14 06:28:34 +08:00
2020-11-13 16:52:38 +08:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" , "/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
setUpInner ( )
2022-02-07 19:43:43 +08:00
hs . callGetDashboardVersions ( sc )
2020-11-13 16:52:38 +08:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2017-06-12 21:48:55 +08:00
} )
} )
2018-01-31 23:51:06 +08:00
2020-11-13 16:52:38 +08:00
t . Run ( "Given two dashboards with the same title in different folders" , func ( t * testing . T ) {
2023-01-18 20:52:41 +08:00
dashOne := dashboards . NewDashboard ( "dash" )
dashOne . ID = 2
dashOne . FolderID = 1
2022-07-18 21:14:58 +08:00
dashOne . HasACL = false
2018-01-31 23:51:06 +08:00
2023-01-18 20:52:41 +08:00
dashTwo := dashboards . NewDashboard ( "dash" )
dashTwo . ID = 4
dashTwo . FolderID = 3
2022-07-18 21:14:58 +08:00
dashTwo . HasACL = false
2018-01-31 23:51:06 +08:00
} )
2018-02-19 18:12:56 +08:00
2020-11-13 16:52:38 +08:00
t . Run ( "Post dashboard response tests" , func ( t * testing . T ) {
2022-03-11 01:19:50 +08:00
dashboardStore := & dashboards . FakeDashboardStore { }
2022-02-16 21:15:44 +08:00
defer dashboardStore . AssertExpectations ( t )
2018-02-19 18:12:56 +08:00
// This tests that a valid request returns correct response
2020-11-13 16:52:38 +08: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 18:12:56 +08:00
2023-01-18 20:52:41 +08:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
UserID : 5 ,
2018-02-19 18:12:56 +08:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "Dash" ,
} ) ,
Overwrite : true ,
2023-01-18 20:52:41 +08:00
FolderID : folderID ,
2018-02-19 18:12:56 +08:00
IsFolder : false ,
Message : "msg" ,
}
2022-05-23 23:14:27 +08:00
dashboardService := dashboards . NewFakeDashboardService ( t )
dashboardService . On ( "SaveDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.SaveDashboardDTO" ) , mock . AnythingOfType ( "bool" ) ) .
2023-01-16 23:33:55 +08:00
Return ( & dashboards . Dashboard { ID : dashID , UID : "uid" , Title : "Dash" , Slug : "dash" , Version : 2 } , nil )
2018-02-19 18:12:56 +08:00
2022-05-23 23:14:27 +08:00
postDashboardScenario ( t , "When calling POST on" , "/api/dashboards" , "/api/dashboards" , cmd , dashboardService , nil , func ( sc * scenarioContext ) {
2020-11-13 16:52:38 +08:00
callPostDashboardShouldReturnSuccess ( sc )
2018-02-19 18:12:56 +08:00
2020-11-13 16:52:38 +08:00
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 18:12:56 +08:00
} )
} )
2021-05-26 22:20:13 +08:00
t . Run ( "Given a correct request for creating a dashboard with folder uid" , func ( t * testing . T ) {
const folderUid string = "folderUID"
const dashID int64 = 2
2023-01-18 20:52:41 +08:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
UserID : 5 ,
2021-05-26 22:20:13 +08:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "Dash" ,
} ) ,
Overwrite : true ,
2023-01-18 20:52:41 +08:00
FolderUID : folderUid ,
2021-05-26 22:20:13 +08:00
IsFolder : false ,
Message : "msg" ,
}
2022-05-23 23:14:27 +08:00
dashboardService := dashboards . NewFakeDashboardService ( t )
dashboardService . On ( "SaveDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.SaveDashboardDTO" ) , mock . AnythingOfType ( "bool" ) ) .
2023-01-16 23:33:55 +08:00
Return ( & dashboards . Dashboard { ID : dashID , UID : "uid" , Title : "Dash" , Slug : "dash" , Version : 2 } , nil )
2021-05-26 22:20:13 +08:00
2022-11-11 21:28:24 +08:00
mockFolder := & foldertest . FakeService {
ExpectedFolder : & folder . Folder { ID : 1 , UID : "folderUID" , Title : "Folder" } ,
2021-05-26 22:20:13 +08:00
}
2022-05-23 23:14:27 +08:00
postDashboardScenario ( t , "When calling POST on" , "/api/dashboards" , "/api/dashboards" , cmd , dashboardService , mockFolder , func ( sc * scenarioContext ) {
2021-05-26 22:20:13 +08:00
callPostDashboardShouldReturnSuccess ( sc )
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 ( ) )
} )
} )
t . Run ( "Given a request with incorrect folder uid for creating a dashboard with" , func ( t * testing . T ) {
2023-01-18 20:52:41 +08:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
UserID : 5 ,
2021-05-26 22:20:13 +08:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "Dash" ,
} ) ,
Overwrite : true ,
2023-01-18 20:52:41 +08:00
FolderUID : "folderUID" ,
2021-05-26 22:20:13 +08:00
IsFolder : false ,
Message : "msg" ,
}
2022-05-23 23:14:27 +08:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2021-05-26 22:20:13 +08:00
2022-11-11 21:28:24 +08:00
mockFolder := & foldertest . FakeService {
ExpectedError : errors . New ( "Error while searching Folder ID" ) ,
2021-05-26 22:20:13 +08:00
}
2022-05-23 23:14:27 +08:00
postDashboardScenario ( t , "When calling POST on" , "/api/dashboards" , "/api/dashboards" , cmd , dashboardService , mockFolder , func ( sc * scenarioContext ) {
2021-05-26 22:20:13 +08:00
callPostDashboard ( sc )
assert . Equal ( t , 500 , sc . resp . Code )
} )
} )
2018-02-19 18:12:56 +08:00
// This tests that invalid requests returns expected error responses
2020-11-13 16:52:38 +08:00
t . Run ( "Given incorrect requests for creating a dashboard" , func ( t * testing . T ) {
2018-02-19 18:12:56 +08:00
testCases := [ ] struct {
SaveError error
ExpectedStatusCode int
} {
2022-06-30 21:31:54 +08:00
{ SaveError : dashboards . ErrDashboardNotFound , ExpectedStatusCode : 404 } ,
{ SaveError : dashboards . ErrFolderNotFound , ExpectedStatusCode : 400 } ,
{ SaveError : dashboards . ErrDashboardWithSameUIDExists , ExpectedStatusCode : 400 } ,
{ SaveError : dashboards . ErrDashboardWithSameNameInFolderExists , ExpectedStatusCode : 412 } ,
{ SaveError : dashboards . ErrDashboardVersionMismatch , ExpectedStatusCode : 412 } ,
{ SaveError : dashboards . ErrDashboardTitleEmpty , ExpectedStatusCode : 400 } ,
{ SaveError : dashboards . ErrDashboardFolderCannotHaveParent , ExpectedStatusCode : 400 } ,
2018-10-13 13:53:28 +08:00
{ SaveError : alerting . ValidationError { Reason : "Mu" } , ExpectedStatusCode : 422 } ,
2022-06-30 21:31:54 +08:00
{ SaveError : dashboards . ErrDashboardFailedGenerateUniqueUid , ExpectedStatusCode : 500 } ,
{ SaveError : dashboards . ErrDashboardTypeMismatch , ExpectedStatusCode : 400 } ,
{ SaveError : dashboards . ErrDashboardFolderWithSameNameAsDashboard , ExpectedStatusCode : 400 } ,
{ SaveError : dashboards . ErrDashboardWithSameNameAsFolder , ExpectedStatusCode : 400 } ,
{ SaveError : dashboards . ErrDashboardFolderNameExists , ExpectedStatusCode : 400 } ,
{ SaveError : dashboards . ErrDashboardUpdateAccessDenied , ExpectedStatusCode : 403 } ,
{ SaveError : dashboards . ErrDashboardInvalidUid , ExpectedStatusCode : 400 } ,
{ SaveError : dashboards . ErrDashboardUidTooLong , ExpectedStatusCode : 400 } ,
{ SaveError : dashboards . ErrDashboardCannotSaveProvisionedDashboard , ExpectedStatusCode : 400 } ,
{ SaveError : dashboards . UpdatePluginDashboardError { PluginId : "plug" } , ExpectedStatusCode : 412 } ,
2018-02-19 18:12:56 +08:00
}
2023-01-18 20:52:41 +08:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
2018-02-19 18:12:56 +08:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "" ,
} ) ,
}
for _ , tc := range testCases {
2022-05-23 23:14:27 +08:00
dashboardService := dashboards . NewFakeDashboardService ( t )
dashboardService . On ( "SaveDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.SaveDashboardDTO" ) , mock . AnythingOfType ( "bool" ) ) . Return ( nil , tc . SaveError )
2018-02-19 18:12:56 +08:00
2020-11-13 16:52:38 +08:00
postDashboardScenario ( t , fmt . Sprintf ( "Expect '%s' error when calling POST on" , tc . SaveError . Error ( ) ) ,
2022-05-23 23:14:27 +08:00
"/api/dashboards" , "/api/dashboards" , cmd , dashboardService , nil , func ( sc * scenarioContext ) {
2020-11-13 16:52:38 +08:00
callPostDashboard ( sc )
assert . Equal ( t , tc . ExpectedStatusCode , sc . resp . Code )
} )
2018-02-19 18:12:56 +08:00
}
} )
} )
2018-02-28 00:53:30 +08:00
2022-10-14 21:51:05 +08:00
t . Run ( "Given a dashboard to validate" , func ( t * testing . T ) {
2023-01-18 23:01:25 +08:00
sqlmock := dbtest . NewFakeDB ( )
2022-10-14 21:51:05 +08:00
t . Run ( "When an invalid dashboard json is posted" , func ( t * testing . T ) {
2023-01-18 20:52:41 +08:00
cmd := dashboards . ValidateDashboardCommand {
2022-10-14 21:51:05 +08:00
Dashboard : "{\"hello\": \"world\"}" ,
}
role := org . RoleAdmin
postValidateScenario ( t , "When calling POST on" , "/api/dashboards/validate" , "/api/dashboards/validate" , cmd , role , func ( sc * scenarioContext ) {
callPostDashboard ( sc )
result := sc . ToJSON ( )
assert . Equal ( t , 422 , sc . resp . Code )
assert . False ( t , result . Get ( "isValid" ) . MustBool ( ) )
assert . NotEmpty ( t , result . Get ( "message" ) . MustString ( ) )
2023-01-18 23:01:25 +08:00
} , sqlmock )
2022-10-14 21:51:05 +08:00
} )
t . Run ( "When a dashboard with a too-low schema version is posted" , func ( t * testing . T ) {
2023-01-18 20:52:41 +08:00
cmd := dashboards . ValidateDashboardCommand {
2022-10-14 21:51:05 +08:00
Dashboard : "{\"schemaVersion\": 1}" ,
}
role := org . RoleAdmin
postValidateScenario ( t , "When calling POST on" , "/api/dashboards/validate" , "/api/dashboards/validate" , cmd , role , func ( sc * scenarioContext ) {
callPostDashboard ( sc )
result := sc . ToJSON ( )
assert . Equal ( t , 412 , sc . resp . Code )
assert . False ( t , result . Get ( "isValid" ) . MustBool ( ) )
assert . Equal ( t , "invalid schema version" , result . Get ( "message" ) . MustString ( ) )
2023-01-18 23:01:25 +08:00
} , sqlmock )
2022-10-14 21:51:05 +08:00
} )
t . Run ( "When a valid dashboard is posted" , func ( t * testing . T ) {
devenvDashboard , readErr := os . ReadFile ( "../../devenv/dev-dashboards/home.json" )
assert . Empty ( t , readErr )
2023-01-18 20:52:41 +08:00
cmd := dashboards . ValidateDashboardCommand {
2022-10-14 21:51:05 +08:00
Dashboard : string ( devenvDashboard ) ,
}
role := org . RoleAdmin
postValidateScenario ( t , "When calling POST on" , "/api/dashboards/validate" , "/api/dashboards/validate" , cmd , role , func ( sc * scenarioContext ) {
callPostDashboard ( sc )
result := sc . ToJSON ( )
assert . Equal ( t , 200 , sc . resp . Code )
assert . True ( t , result . Get ( "isValid" ) . MustBool ( ) )
2023-01-18 23:01:25 +08:00
} , sqlmock )
2022-10-14 21:51:05 +08:00
} )
} )
2020-11-13 16:52:38 +08:00
t . Run ( "Given two dashboards being compared" , func ( t * testing . T ) {
2022-05-25 16:41:51 +08:00
fakeDashboardVersionService := dashvertest . NewDashboardVersionServiceFake ( )
2022-12-28 00:17:24 +08:00
fakeDashboardVersionService . ExpectedDashboardVersions = [ ] * dashver . DashboardVersionDTO {
2022-05-25 16:41:51 +08:00
{
DashboardID : 1 ,
Version : 1 ,
Data : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "Dash1" ,
} ) ,
} ,
{
DashboardID : 2 ,
Version : 2 ,
Data : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "Dash2" ,
} ) ,
} ,
}
2023-01-17 21:17:54 +08:00
sqlmock := dbtest . NewFakeDB ( )
2020-11-13 16:52:38 +08:00
setUp := func ( ) {
2022-09-21 00:58:04 +08:00
teamSvc := & teamtest . FakeService { }
2022-06-02 02:16:26 +08:00
dashSvc := dashboards . NewFakeDashboardService ( t )
2023-01-20 21:58:47 +08:00
dashSvc . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Return ( nil )
2023-01-16 23:33:55 +08:00
dashSvc . On ( "GetDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardQuery" ) ) . Run ( func ( args mock . Arguments ) {
q := args . Get ( 1 ) . ( * dashboards . GetDashboardQuery )
q . Result = & dashboards . Dashboard {
OrgID : q . OrgID ,
ID : q . ID ,
2022-12-15 22:34:17 +08:00
}
} ) . Return ( nil )
2023-01-17 21:17:54 +08:00
guardian . InitLegacyGuardian ( sqlmock , dashSvc , teamSvc )
2020-11-13 16:52:38 +08:00
}
2018-02-28 00:53:30 +08:00
cmd := dtos . CalculateDiffOptions {
Base : dtos . CalculateDiffTarget {
DashboardId : 1 ,
Version : 1 ,
} ,
New : dtos . CalculateDiffTarget {
DashboardId : 2 ,
Version : 2 ,
} ,
DiffType : "basic" ,
}
2020-11-13 16:52:38 +08:00
t . Run ( "when user does not have permission" , func ( t * testing . T ) {
2022-08-10 17:56:48 +08:00
role := org . RoleViewer
2020-11-13 16:52:38 +08: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 )
2023-01-17 21:17:54 +08:00
} , sqlmock , fakeDashboardVersionService )
2018-02-28 00:53:30 +08:00
} )
2020-11-13 16:52:38 +08:00
t . Run ( "when user does have permission" , func ( t * testing . T ) {
2022-08-10 17:56:48 +08:00
role := org . RoleAdmin
2020-11-13 16:52:38 +08:00
postDiffScenario ( t , "When calling POST on" , "/api/dashboards/calculate-diff" , "/api/dashboards/calculate-diff" , cmd , role , func ( sc * scenarioContext ) {
2022-07-18 21:14:58 +08:00
// This test shouldn't hit GetDashboardACLInfoList, so no setup needed
2022-05-25 16:41:51 +08:00
sc . dashboardVersionService = fakeDashboardVersionService
2020-11-13 16:52:38 +08:00
callPostDashboard ( sc )
assert . Equal ( t , 200 , sc . resp . Code )
2023-01-17 21:17:54 +08:00
} , sqlmock , fakeDashboardVersionService )
2018-02-28 00:53:30 +08:00
} )
} )
2019-03-06 21:38:40 +08:00
2020-11-13 16:52:38 +08:00
t . Run ( "Given dashboard in folder being restored should restore to folder" , func ( t * testing . T ) {
const folderID int64 = 1
2023-01-16 23:33:55 +08:00
fakeDash := dashboards . NewDashboard ( "Child dash" )
fakeDash . ID = 2
fakeDash . FolderID = folderID
2022-07-18 21:14:58 +08:00
fakeDash . HasACL = false
2019-03-06 21:38:40 +08:00
2022-05-23 23:14:27 +08:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2023-01-16 23:33:55 +08:00
dashboardService . On ( "GetDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardQuery" ) ) . Run ( func ( args mock . Arguments ) {
q := args . Get ( 1 ) . ( * dashboards . GetDashboardQuery )
2022-05-23 23:14:27 +08:00
q . Result = fakeDash
} ) . Return ( nil )
dashboardService . On ( "SaveDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.SaveDashboardDTO" ) , mock . AnythingOfType ( "bool" ) ) . Run ( func ( args mock . Arguments ) {
cmd := args . Get ( 1 ) . ( * dashboards . SaveDashboardDTO )
2023-01-16 23:33:55 +08:00
cmd . Dashboard = & dashboards . Dashboard {
ID : 2 , UID : "uid" , Title : "Dash" , Slug : "dash" , Version : 1 ,
2022-05-23 23:14:27 +08:00
}
} ) . Return ( nil , nil )
2019-03-06 21:38:40 +08:00
cmd := dtos . RestoreDashboardVersionCommand {
Version : 1 ,
}
2022-05-25 16:41:51 +08:00
fakeDashboardVersionService := dashvertest . NewDashboardVersionServiceFake ( )
2022-12-28 00:17:24 +08:00
fakeDashboardVersionService . ExpectedDashboardVersions = [ ] * dashver . DashboardVersionDTO {
2022-02-10 16:58:52 +08:00
{
2022-05-25 16:41:51 +08:00
DashboardID : 2 ,
2022-02-10 16:58:52 +08:00
Version : 1 ,
Data : fakeDash . Data ,
} }
2023-01-18 23:01:25 +08:00
mockSQLStore := dbtest . NewFakeDB ( )
2020-11-13 16:52:38 +08:00
restoreDashboardVersionScenario ( t , "When calling POST on" , "/api/dashboards/id/1/restore" ,
2022-05-25 16:41:51 +08:00
"/api/dashboards/id/:dashboardId/restore" , dashboardService , fakeDashboardVersionService , cmd , func ( sc * scenarioContext ) {
sc . dashboardVersionService = fakeDashboardVersionService
2020-11-13 16:52:38 +08:00
callRestoreDashboardVersion ( sc )
assert . Equal ( t , 200 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2019-03-06 21:38:40 +08:00
} )
2020-11-13 16:52:38 +08:00
t . Run ( "Given dashboard in general folder being restored should restore to general folder" , func ( t * testing . T ) {
2023-01-16 23:33:55 +08:00
fakeDash := dashboards . NewDashboard ( "Child dash" )
fakeDash . ID = 2
2022-07-18 21:14:58 +08:00
fakeDash . HasACL = false
2019-03-06 21:38:40 +08:00
2022-05-23 23:14:27 +08:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2023-01-16 23:33:55 +08:00
dashboardService . On ( "GetDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardQuery" ) ) . Run ( func ( args mock . Arguments ) {
q := args . Get ( 1 ) . ( * dashboards . GetDashboardQuery )
2022-05-23 23:14:27 +08:00
q . Result = fakeDash
} ) . Return ( nil )
dashboardService . On ( "SaveDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.SaveDashboardDTO" ) , mock . AnythingOfType ( "bool" ) ) . Run ( func ( args mock . Arguments ) {
cmd := args . Get ( 1 ) . ( * dashboards . SaveDashboardDTO )
2023-01-16 23:33:55 +08:00
cmd . Dashboard = & dashboards . Dashboard {
ID : 2 , UID : "uid" , Title : "Dash" , Slug : "dash" , Version : 1 ,
2022-05-23 23:14:27 +08:00
}
} ) . Return ( nil , nil )
2019-03-06 21:38:40 +08:00
2022-05-25 16:41:51 +08:00
fakeDashboardVersionService := dashvertest . NewDashboardVersionServiceFake ( )
2022-12-28 00:17:24 +08:00
fakeDashboardVersionService . ExpectedDashboardVersions = [ ] * dashver . DashboardVersionDTO {
2022-05-25 16:41:51 +08:00
{
DashboardID : 2 ,
Version : 1 ,
Data : fakeDash . Data ,
} }
2019-03-06 21:38:40 +08:00
cmd := dtos . RestoreDashboardVersionCommand {
Version : 1 ,
}
2023-01-18 23:01:25 +08:00
mockSQLStore := dbtest . NewFakeDB ( )
2020-11-13 16:52:38 +08:00
restoreDashboardVersionScenario ( t , "When calling POST on" , "/api/dashboards/id/1/restore" ,
2022-05-25 16:41:51 +08:00
"/api/dashboards/id/:dashboardId/restore" , dashboardService , fakeDashboardVersionService , cmd , func ( sc * scenarioContext ) {
2020-11-13 16:52:38 +08:00
callRestoreDashboardVersion ( sc )
assert . Equal ( t , 200 , sc . resp . Code )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2019-03-06 21:38:40 +08:00
} )
2019-04-10 19:29:10 +08:00
2020-11-13 16:52:38 +08:00
t . Run ( "Given provisioned dashboard" , func ( t * testing . T ) {
2023-01-18 23:01:25 +08:00
mockSQLStore := dbtest . NewFakeDB ( )
2022-06-02 02:16:26 +08:00
dashboardStore := dashboards . NewFakeDashboardStore ( t )
2023-01-18 20:52:41 +08:00
dashboardStore . On ( "GetProvisionedDataByDashboardID" , mock . Anything , mock . AnythingOfType ( "int64" ) ) . Return ( & dashboards . DashboardProvisioning { ExternalID : "/dashboard1.json" } , nil ) . Once ( )
2022-06-02 02:16:26 +08:00
2022-09-21 00:58:04 +08:00
teamService := & teamtest . FakeService { }
2022-06-02 02:16:26 +08:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2022-02-16 21:15:44 +08:00
2022-02-07 19:43:43 +08:00
dataValue , err := simplejson . NewJson ( [ ] byte ( ` { "id": 1, "editable": true, "style": "dark"} ` ) )
require . NoError ( t , err )
2023-01-16 23:33:55 +08:00
dashboardService . On ( "GetDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardQuery" ) ) . Run ( func ( args mock . Arguments ) {
q := args . Get ( 1 ) . ( * dashboards . GetDashboardQuery )
q . Result = & dashboards . Dashboard { ID : 1 , Data : dataValue }
2022-05-23 23:14:27 +08:00
} ) . Return ( nil )
2023-01-20 21:58:47 +08:00
dashboardService . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Run ( func ( args mock . Arguments ) {
q := args . Get ( 1 ) . ( * dashboards . GetDashboardACLInfoListQuery )
q . Result = [ ] * dashboards . DashboardACLInfoDTO { { OrgID : testOrgID , DashboardID : 1 , UserID : testUserID , Permission : models . PERMISSION_EDIT } }
2022-06-02 02:16:26 +08:00
} ) . Return ( nil )
2022-09-21 00:58:04 +08:00
guardian . InitLegacyGuardian ( mockSQLStore , dashboardService , teamService )
2019-04-30 19:32:18 +08:00
2022-08-10 17:56:48 +08:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/dash" , "/api/dashboards/uid/:uid" , org . RoleEditor , func ( sc * scenarioContext ) {
2022-02-16 21:15:44 +08:00
fakeProvisioningService := provisioning . NewProvisioningServiceMock ( context . Background ( ) )
fakeProvisioningService . GetDashboardProvisionerResolvedPathFunc = func ( name string ) string {
2019-04-30 19:32:18 +08:00
return "/tmp/grafana/dashboards"
}
2023-01-20 00:38:07 +08:00
dash := getDashboardShouldReturn200WithConfig ( t , sc , fakeProvisioningService , dashboardStore , dashboardService , nil )
2022-02-16 21:15:44 +08:00
assert . Equal ( t , "../../../dashboard1.json" , dash . Meta . ProvisionedExternalId , mockSQLStore )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2019-10-31 21:27:31 +08:00
2022-08-10 17:56:48 +08:00
loggedInUserScenarioWithRole ( t , "When allowUiUpdates is true and calling GET on" , "GET" , "/api/dashboards/uid/dash" , "/api/dashboards/uid/:uid" , org . RoleEditor , func ( sc * scenarioContext ) {
2022-02-16 21:15:44 +08:00
fakeProvisioningService := provisioning . NewProvisioningServiceMock ( context . Background ( ) )
fakeProvisioningService . GetDashboardProvisionerResolvedPathFunc = func ( name string ) string {
2019-10-31 21:27:31 +08:00
return "/tmp/grafana/dashboards"
}
2022-02-16 21:15:44 +08:00
fakeProvisioningService . GetAllowUIUpdatesFromConfigFunc = func ( name string ) bool {
2019-10-31 21:27:31 +08:00
return true
}
hs := & HTTPServer {
2022-02-16 21:15:44 +08:00
Cfg : setting . NewCfg ( ) ,
ProvisioningService : fakeProvisioningService ,
LibraryPanelService : & mockLibraryPanelService { } ,
LibraryElementService : & mockLibraryElementService { } ,
dashboardProvisioningService : mockDashboardProvisioningService { } ,
SQLStore : mockSQLStore ,
2022-04-04 22:57:43 +08:00
AccessControl : accesscontrolmock . New ( ) ,
2022-07-07 02:42:39 +08:00
DashboardService : dashboardService ,
2022-07-20 07:44:41 +08:00
Features : featuremgmt . WithFeatures ( ) ,
Reconcile coremodels, entities, objects under new kind framework (#56492)
* Update thema to latest
* Deal with s/Library/*Runtime/
* Commit new, working results of codegen
* We like pointers now
* Always take runtime arg for NewBase()
* Sketchy handwavy pass at entity meta framework
* Little nibbles
* Update pkg/framework/coremodel/entityframework.cue
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
* Move file into new framework location
* Introduce loaders, Go code
* Complete rename to kind
* Flesh out framework, add svg/dashboard examples
* Cruft removal
* Remove generated kind go files from gitignore
* Refine maturity concept, add SlotKind
* Update embed and go deps
* Export PrefixWithGrafanaCUE
* Make the loader actually work, holy crap
* Many small tweaks to type.cue
* Add Apache 2 licensing exceptions for kinds
* Add new kinds dir, start of generator
* Roll back to earlier oapi-codegen
* Introduce new grafana-specific CUE loaders
* Introduce new tidy code generators framework
* Catch up kind framework with tinkering
* Add slices for the generators
* Add write/verify step to main generator
* Many renames
* Split up kind framework cue files
* Use kind.Decl within generated kinds
* Create kind.SomeDecl wrapper type to cache lineages
* Better names again
* Get one generated implemented, hopefully
* Copy dashboard schema into new kind.cue
* Small fixes to make the initial gen work
* Put svg kind in its new home
* Add generated Go dashboard type
* More renames and cleanups
* Add base kind registry and generator
* Stop blacklisting *_gen.go files
This is not the Go best practice, anyway. All we actually want to ignore
for enterprise is generated wire files.
* Change codegen output directories
pkg/kind -> pkg/kinds
pkg/registry/kindreg -> pkg/registry/corekind
* Rename pkg/framework/kind to pkg/kindsys
* Add core structured kind generator
* Add plural and machine names to kind spec
* Copy playlist over to kind system
* Consolidate kindsys files
* Add raw kind generator
* Update CODEOWNERS for kind framework
* Touch up comments a bit
* More docs tweaks
* Remove generated types to reduce noise for review
* Split each generator into its own file
* Rename Slot kind to Composable kind
* Add handwavy types for customkind loading
* Guard against init calls to framework loader
* First pass at doc on extending the kind system
* Improve attribute example in docs
* Fix wire imports
* Add basic TS types generator
* Fix composable kind category def
* No need for a separate file with generate directive
* Catch dashboard schema up
* Rename generator types to something saner and generic
* Make version configurable in ts/go generators
* Add CommonMeta to ease property access
* Add kindsys prop indicating whether lineage is group
* Put all kind categories back in a single file
* Finish with kindsys group props
* Refactor maturity progression per discussion
- Replace "committed" with "merged"
- All kindcats can use all maturity levels, at least for now
* Convert ts veneer index generator to modular system
* Move over to new jennywrites framework
* Strip down old coremodel generator
* Use public version of jennywrites
* Pull latest thema
* Commit generated Go types
* Add header injection postprocessor
* Move sdboyer/jennywrites to grafana/codejen
* Tweak header output
* Remove dashboard and playlist coremodels
* Fix up backend dashboards devenv test
* Fix TS import patterns to new gen filename
* Update internal imports, remove coremodel registry
* Fix compilation errors, wire generation
* Export and replace the prefix dropper
* More Go struct and field name changes
* Last name fixes, hopefully
* Fix lint errors
* Last lint error
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
2022-11-11 04:36:40 +08:00
Kinds : corekind . NewBase ( nil ) ,
2019-10-31 21:27:31 +08:00
}
2022-02-07 19:43:43 +08:00
hs . callGetDashboard ( sc )
2019-10-31 21:27:31 +08:00
2020-11-13 16:52:38 +08:00
assert . Equal ( t , 200 , sc . resp . Code )
2019-10-31 21:27:31 +08:00
dash := dtos . DashboardFullWithMeta { }
err := json . NewDecoder ( sc . resp . Body ) . Decode ( & dash )
2020-11-13 16:52:38 +08:00
require . NoError ( t , err )
2019-10-31 21:27:31 +08:00
2020-11-13 16:52:38 +08:00
assert . Equal ( t , false , dash . Meta . Provisioned )
2022-02-07 19:43:43 +08:00
} , mockSQLStore )
2019-04-10 19:29:10 +08:00
} )
2017-06-12 21:48:55 +08:00
}
2017-06-13 05:05:32 +08:00
2023-01-20 00:38:07 +08:00
func getDashboardShouldReturn200WithConfig ( t * testing . T , sc * scenarioContext , provisioningService provisioning . ProvisioningService , dashboardStore dashboards . Store , dashboardService dashboards . DashboardService , folderStore dashboards . FolderStore ) dtos . DashboardFullWithMeta {
2022-02-16 21:15:44 +08:00
t . Helper ( )
2019-04-30 19:32:18 +08:00
if provisioningService == nil {
2021-11-03 18:31:56 +08:00
provisioningService = provisioning . NewProvisioningServiceMock ( context . Background ( ) )
2019-04-30 19:32:18 +08:00
}
2022-11-15 03:08:10 +08:00
var err error
2022-02-16 21:15:44 +08:00
if dashboardStore == nil {
2022-10-19 21:02:15 +08:00
sql := db . InitTestDB ( t )
2022-11-15 03:08:10 +08:00
quotaService := quotatest . New ( false , nil )
dashboardStore , err = database . ProvideDashboardStore ( sql , sql . Cfg , featuremgmt . WithFeatures ( ) , tagimpl . ProvideService ( sql , sql . Cfg ) , quotaService )
require . NoError ( t , err )
2022-02-16 21:15:44 +08:00
}
2021-05-12 14:48:17 +08:00
libraryPanelsService := mockLibraryPanelService { }
libraryElementsService := mockLibraryElementService { }
2022-03-10 19:58:18 +08:00
cfg := setting . NewCfg ( )
2022-06-07 17:02:20 +08:00
ac := accesscontrolmock . New ( )
folderPermissions := accesscontrolmock . NewMockedPermissionsService ( )
dashboardPermissions := accesscontrolmock . NewMockedPermissionsService ( )
2022-03-10 19:58:18 +08:00
features := featuremgmt . WithFeatures ( )
2021-05-12 14:48:17 +08:00
2022-05-23 23:14:27 +08:00
if dashboardService == nil {
2022-06-07 17:02:20 +08:00
dashboardService = service . ProvideDashboardService (
2023-01-20 00:38:07 +08:00
cfg , dashboardStore , folderStore , nil , features ,
2022-06-07 17:02:20 +08:00
folderPermissions , dashboardPermissions , ac ,
)
2022-05-23 23:14:27 +08:00
}
2019-04-30 19:32:18 +08:00
hs := & HTTPServer {
2022-03-10 19:58:18 +08:00
Cfg : cfg ,
LibraryPanelService : & libraryPanelsService ,
LibraryElementService : & libraryElementsService ,
SQLStore : sc . sqlStore ,
ProvisioningService : provisioningService ,
2022-04-04 22:57:43 +08:00
AccessControl : accesscontrolmock . New ( ) ,
2022-03-10 19:58:18 +08:00
dashboardProvisioningService : service . ProvideDashboardService (
2023-01-20 00:38:07 +08:00
cfg , dashboardStore , folderStore , nil , features ,
2022-06-07 17:02:20 +08:00
folderPermissions , dashboardPermissions , ac ,
2022-03-10 19:58:18 +08:00
) ,
2022-07-07 02:42:39 +08:00
DashboardService : dashboardService ,
2022-07-20 07:44:41 +08:00
Features : featuremgmt . WithFeatures ( ) ,
Reconcile coremodels, entities, objects under new kind framework (#56492)
* Update thema to latest
* Deal with s/Library/*Runtime/
* Commit new, working results of codegen
* We like pointers now
* Always take runtime arg for NewBase()
* Sketchy handwavy pass at entity meta framework
* Little nibbles
* Update pkg/framework/coremodel/entityframework.cue
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
* Move file into new framework location
* Introduce loaders, Go code
* Complete rename to kind
* Flesh out framework, add svg/dashboard examples
* Cruft removal
* Remove generated kind go files from gitignore
* Refine maturity concept, add SlotKind
* Update embed and go deps
* Export PrefixWithGrafanaCUE
* Make the loader actually work, holy crap
* Many small tweaks to type.cue
* Add Apache 2 licensing exceptions for kinds
* Add new kinds dir, start of generator
* Roll back to earlier oapi-codegen
* Introduce new grafana-specific CUE loaders
* Introduce new tidy code generators framework
* Catch up kind framework with tinkering
* Add slices for the generators
* Add write/verify step to main generator
* Many renames
* Split up kind framework cue files
* Use kind.Decl within generated kinds
* Create kind.SomeDecl wrapper type to cache lineages
* Better names again
* Get one generated implemented, hopefully
* Copy dashboard schema into new kind.cue
* Small fixes to make the initial gen work
* Put svg kind in its new home
* Add generated Go dashboard type
* More renames and cleanups
* Add base kind registry and generator
* Stop blacklisting *_gen.go files
This is not the Go best practice, anyway. All we actually want to ignore
for enterprise is generated wire files.
* Change codegen output directories
pkg/kind -> pkg/kinds
pkg/registry/kindreg -> pkg/registry/corekind
* Rename pkg/framework/kind to pkg/kindsys
* Add core structured kind generator
* Add plural and machine names to kind spec
* Copy playlist over to kind system
* Consolidate kindsys files
* Add raw kind generator
* Update CODEOWNERS for kind framework
* Touch up comments a bit
* More docs tweaks
* Remove generated types to reduce noise for review
* Split each generator into its own file
* Rename Slot kind to Composable kind
* Add handwavy types for customkind loading
* Guard against init calls to framework loader
* First pass at doc on extending the kind system
* Improve attribute example in docs
* Fix wire imports
* Add basic TS types generator
* Fix composable kind category def
* No need for a separate file with generate directive
* Catch dashboard schema up
* Rename generator types to something saner and generic
* Make version configurable in ts/go generators
* Add CommonMeta to ease property access
* Add kindsys prop indicating whether lineage is group
* Put all kind categories back in a single file
* Finish with kindsys group props
* Refactor maturity progression per discussion
- Replace "committed" with "merged"
- All kindcats can use all maturity levels, at least for now
* Convert ts veneer index generator to modular system
* Move over to new jennywrites framework
* Strip down old coremodel generator
* Use public version of jennywrites
* Pull latest thema
* Commit generated Go types
* Add header injection postprocessor
* Move sdboyer/jennywrites to grafana/codejen
* Tweak header output
* Remove dashboard and playlist coremodels
* Fix up backend dashboards devenv test
* Fix TS import patterns to new gen filename
* Update internal imports, remove coremodel registry
* Fix compilation errors, wire generation
* Export and replace the prefix dropper
* More Go struct and field name changes
* Last name fixes, hopefully
* Fix lint errors
* Last lint error
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
2022-11-11 04:36:40 +08:00
Kinds : corekind . NewBase ( nil ) ,
2019-04-30 19:32:18 +08:00
}
2021-05-12 14:48:17 +08:00
2022-02-07 19:43:43 +08:00
hs . callGetDashboard ( sc )
2017-06-13 05:05:32 +08:00
2020-11-13 16:52:38 +08:00
require . Equal ( sc . t , 200 , sc . resp . Code )
2017-06-13 05:05:32 +08:00
dash := dtos . DashboardFullWithMeta { }
2022-11-15 03:08:10 +08:00
err = json . NewDecoder ( sc . resp . Body ) . Decode ( & dash )
2020-11-13 16:52:38 +08:00
require . NoError ( sc . t , err )
2017-06-13 05:05:32 +08:00
return dash
}
2022-02-07 19:43:43 +08:00
func ( hs * HTTPServer ) callGetDashboard ( sc * scenarioContext ) {
2019-04-30 19:32:18 +08:00
sc . handlerFunc = hs . GetDashboard
2018-01-29 20:51:01 +08:00
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
}
2022-02-07 19:43:43 +08:00
func ( hs * HTTPServer ) callGetDashboardVersion ( sc * scenarioContext ) {
sc . handlerFunc = hs . GetDashboardVersion
2017-06-14 06:28:34 +08:00
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
}
2022-02-07 19:43:43 +08:00
func ( hs * HTTPServer ) callGetDashboardVersions ( sc * scenarioContext ) {
sc . handlerFunc = hs . GetDashboardVersions
2017-06-14 06:28:34 +08:00
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
}
2022-02-16 21:15:44 +08:00
func ( hs * HTTPServer ) callDeleteDashboardByUID ( t * testing . T ,
sc * scenarioContext , mockDashboard * dashboards . FakeDashboardService ) {
2022-07-07 02:42:39 +08:00
hs . DashboardService = mockDashboard
2021-01-20 16:28:10 +08:00
sc . handlerFunc = hs . DeleteDashboardByUID
2018-01-31 23:46:31 +08:00
sc . fakeReqWithParams ( "DELETE" , sc . url , map [ string ] string { } ) . exec ( )
}
2020-11-13 16:52:38 +08:00
func callPostDashboard ( sc * scenarioContext ) {
2017-06-13 05:05:32 +08:00
sc . fakeReqWithParams ( "POST" , sc . url , map [ string ] string { } ) . exec ( )
}
2020-11-13 16:52:38 +08:00
func callRestoreDashboardVersion ( sc * scenarioContext ) {
2019-03-06 21:38:40 +08:00
sc . fakeReqWithParams ( "POST" , sc . url , map [ string ] string { } ) . exec ( )
}
2020-11-13 16:52:38 +08:00
func callPostDashboardShouldReturnSuccess ( sc * scenarioContext ) {
callPostDashboard ( sc )
2018-01-31 06:37:54 +08:00
2020-11-13 16:52:38 +08:00
assert . Equal ( sc . t , 200 , sc . resp . Code )
2018-01-31 06:37:54 +08:00
}
2023-01-18 20:52:41 +08:00
func postDashboardScenario ( t * testing . T , desc string , url string , routePattern string , cmd dashboards . SaveDashboardCommand , dashboardService dashboards . DashboardService , folderService folder . Service , fn scenarioFunc ) {
2020-11-13 16:52:38 +08:00
t . Run ( fmt . Sprintf ( "%s %s" , desc , url ) , func ( t * testing . T ) {
2020-12-16 02:09:04 +08:00
cfg := setting . NewCfg ( )
2019-02-12 04:12:01 +08:00
hs := HTTPServer {
2022-11-15 03:08:10 +08:00
Cfg : cfg ,
ProvisioningService : provisioning . NewProvisioningServiceMock ( context . Background ( ) ) ,
Live : newTestLive ( t , db . InitTestDB ( t ) ) ,
QuotaService : quotatest . New ( false , nil ) ,
2022-09-28 14:29:35 +08:00
pluginStore : & plugins . FakePluginStore { } ,
2021-05-12 14:48:17 +08:00
LibraryPanelService : & mockLibraryPanelService { } ,
LibraryElementService : & mockLibraryElementService { } ,
2022-07-07 02:42:39 +08:00
DashboardService : dashboardService ,
2022-02-16 21:15:44 +08:00
folderService : folderService ,
2022-03-03 22:05:47 +08:00
Features : featuremgmt . WithFeatures ( ) ,
Reconcile coremodels, entities, objects under new kind framework (#56492)
* Update thema to latest
* Deal with s/Library/*Runtime/
* Commit new, working results of codegen
* We like pointers now
* Always take runtime arg for NewBase()
* Sketchy handwavy pass at entity meta framework
* Little nibbles
* Update pkg/framework/coremodel/entityframework.cue
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
* Move file into new framework location
* Introduce loaders, Go code
* Complete rename to kind
* Flesh out framework, add svg/dashboard examples
* Cruft removal
* Remove generated kind go files from gitignore
* Refine maturity concept, add SlotKind
* Update embed and go deps
* Export PrefixWithGrafanaCUE
* Make the loader actually work, holy crap
* Many small tweaks to type.cue
* Add Apache 2 licensing exceptions for kinds
* Add new kinds dir, start of generator
* Roll back to earlier oapi-codegen
* Introduce new grafana-specific CUE loaders
* Introduce new tidy code generators framework
* Catch up kind framework with tinkering
* Add slices for the generators
* Add write/verify step to main generator
* Many renames
* Split up kind framework cue files
* Use kind.Decl within generated kinds
* Create kind.SomeDecl wrapper type to cache lineages
* Better names again
* Get one generated implemented, hopefully
* Copy dashboard schema into new kind.cue
* Small fixes to make the initial gen work
* Put svg kind in its new home
* Add generated Go dashboard type
* More renames and cleanups
* Add base kind registry and generator
* Stop blacklisting *_gen.go files
This is not the Go best practice, anyway. All we actually want to ignore
for enterprise is generated wire files.
* Change codegen output directories
pkg/kind -> pkg/kinds
pkg/registry/kindreg -> pkg/registry/corekind
* Rename pkg/framework/kind to pkg/kindsys
* Add core structured kind generator
* Add plural and machine names to kind spec
* Copy playlist over to kind system
* Consolidate kindsys files
* Add raw kind generator
* Update CODEOWNERS for kind framework
* Touch up comments a bit
* More docs tweaks
* Remove generated types to reduce noise for review
* Split each generator into its own file
* Rename Slot kind to Composable kind
* Add handwavy types for customkind loading
* Guard against init calls to framework loader
* First pass at doc on extending the kind system
* Improve attribute example in docs
* Fix wire imports
* Add basic TS types generator
* Fix composable kind category def
* No need for a separate file with generate directive
* Catch dashboard schema up
* Rename generator types to something saner and generic
* Make version configurable in ts/go generators
* Add CommonMeta to ease property access
* Add kindsys prop indicating whether lineage is group
* Put all kind categories back in a single file
* Finish with kindsys group props
* Refactor maturity progression per discussion
- Replace "committed" with "merged"
- All kindcats can use all maturity levels, at least for now
* Convert ts veneer index generator to modular system
* Move over to new jennywrites framework
* Strip down old coremodel generator
* Use public version of jennywrites
* Pull latest thema
* Commit generated Go types
* Add header injection postprocessor
* Move sdboyer/jennywrites to grafana/codejen
* Tweak header output
* Remove dashboard and playlist coremodels
* Fix up backend dashboards devenv test
* Fix TS import patterns to new gen filename
* Update internal imports, remove coremodel registry
* Fix compilation errors, wire generation
* Export and replace the prefix dropper
* More Go struct and field name changes
* Last name fixes, hopefully
* Fix lint errors
* Last lint error
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
2022-11-11 04:36:40 +08:00
Kinds : corekind . NewBase ( nil ) ,
2022-11-24 22:38:55 +08:00
accesscontrolService : actest . FakeService { } ,
2019-02-12 04:12:01 +08:00
}
2020-11-13 16:52:38 +08:00
sc := setupScenarioContext ( t , url )
2021-01-15 21:43:20 +08:00
sc . defaultHandler = routing . Wrap ( func ( c * models . ReqContext ) response . Response {
2021-11-29 17:18:01 +08:00
c . Req . Body = mockRequestBody ( cmd )
2022-02-09 20:44:38 +08:00
c . Req . Header . Add ( "Content-Type" , "application/json" )
2017-06-13 05:05:32 +08:00
sc . context = c
2023-01-18 20:52:41 +08:00
sc . context . SignedInUser = & user . SignedInUser { OrgID : cmd . OrgID , UserID : cmd . UserID }
2017-06-13 05:05:32 +08:00
2021-11-29 17:18:01 +08:00
return hs . PostDashboard ( c )
2017-06-13 05:05:32 +08:00
} )
sc . m . Post ( routePattern , sc . defaultHandler )
fn ( sc )
} )
}
2018-01-30 02:27:53 +08:00
2023-01-18 20:52:41 +08:00
func postValidateScenario ( t * testing . T , desc string , url string , routePattern string , cmd dashboards . ValidateDashboardCommand ,
2022-12-13 18:03:36 +08:00
role org . RoleType , fn scenarioFunc , sqlmock db . DB ) {
2022-10-14 21:51:05 +08:00
t . Run ( fmt . Sprintf ( "%s %s" , desc , url ) , func ( t * testing . T ) {
cfg := setting . NewCfg ( )
hs := HTTPServer {
Cfg : cfg ,
ProvisioningService : provisioning . NewProvisioningServiceMock ( context . Background ( ) ) ,
2022-10-19 21:02:15 +08:00
Live : newTestLive ( t , db . InitTestDB ( t ) ) ,
2022-11-15 03:08:10 +08:00
QuotaService : quotatest . New ( false , nil ) ,
2022-10-14 21:51:05 +08:00
LibraryPanelService : & mockLibraryPanelService { } ,
LibraryElementService : & mockLibraryElementService { } ,
SQLStore : sqlmock ,
Features : featuremgmt . WithFeatures ( ) ,
Reconcile coremodels, entities, objects under new kind framework (#56492)
* Update thema to latest
* Deal with s/Library/*Runtime/
* Commit new, working results of codegen
* We like pointers now
* Always take runtime arg for NewBase()
* Sketchy handwavy pass at entity meta framework
* Little nibbles
* Update pkg/framework/coremodel/entityframework.cue
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
* Move file into new framework location
* Introduce loaders, Go code
* Complete rename to kind
* Flesh out framework, add svg/dashboard examples
* Cruft removal
* Remove generated kind go files from gitignore
* Refine maturity concept, add SlotKind
* Update embed and go deps
* Export PrefixWithGrafanaCUE
* Make the loader actually work, holy crap
* Many small tweaks to type.cue
* Add Apache 2 licensing exceptions for kinds
* Add new kinds dir, start of generator
* Roll back to earlier oapi-codegen
* Introduce new grafana-specific CUE loaders
* Introduce new tidy code generators framework
* Catch up kind framework with tinkering
* Add slices for the generators
* Add write/verify step to main generator
* Many renames
* Split up kind framework cue files
* Use kind.Decl within generated kinds
* Create kind.SomeDecl wrapper type to cache lineages
* Better names again
* Get one generated implemented, hopefully
* Copy dashboard schema into new kind.cue
* Small fixes to make the initial gen work
* Put svg kind in its new home
* Add generated Go dashboard type
* More renames and cleanups
* Add base kind registry and generator
* Stop blacklisting *_gen.go files
This is not the Go best practice, anyway. All we actually want to ignore
for enterprise is generated wire files.
* Change codegen output directories
pkg/kind -> pkg/kinds
pkg/registry/kindreg -> pkg/registry/corekind
* Rename pkg/framework/kind to pkg/kindsys
* Add core structured kind generator
* Add plural and machine names to kind spec
* Copy playlist over to kind system
* Consolidate kindsys files
* Add raw kind generator
* Update CODEOWNERS for kind framework
* Touch up comments a bit
* More docs tweaks
* Remove generated types to reduce noise for review
* Split each generator into its own file
* Rename Slot kind to Composable kind
* Add handwavy types for customkind loading
* Guard against init calls to framework loader
* First pass at doc on extending the kind system
* Improve attribute example in docs
* Fix wire imports
* Add basic TS types generator
* Fix composable kind category def
* No need for a separate file with generate directive
* Catch dashboard schema up
* Rename generator types to something saner and generic
* Make version configurable in ts/go generators
* Add CommonMeta to ease property access
* Add kindsys prop indicating whether lineage is group
* Put all kind categories back in a single file
* Finish with kindsys group props
* Refactor maturity progression per discussion
- Replace "committed" with "merged"
- All kindcats can use all maturity levels, at least for now
* Convert ts veneer index generator to modular system
* Move over to new jennywrites framework
* Strip down old coremodel generator
* Use public version of jennywrites
* Pull latest thema
* Commit generated Go types
* Add header injection postprocessor
* Move sdboyer/jennywrites to grafana/codejen
* Tweak header output
* Remove dashboard and playlist coremodels
* Fix up backend dashboards devenv test
* Fix TS import patterns to new gen filename
* Update internal imports, remove coremodel registry
* Fix compilation errors, wire generation
* Export and replace the prefix dropper
* More Go struct and field name changes
* Last name fixes, hopefully
* Fix lint errors
* Last lint error
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
2022-11-11 04:36:40 +08:00
Kinds : corekind . NewBase ( nil ) ,
2022-10-14 21:51:05 +08:00
}
sc := setupScenarioContext ( t , url )
sc . defaultHandler = routing . Wrap ( func ( c * models . ReqContext ) response . Response {
c . Req . Body = mockRequestBody ( cmd )
c . Req . Header . Add ( "Content-Type" , "application/json" )
sc . context = c
sc . context . SignedInUser = & user . SignedInUser {
OrgID : testOrgID ,
UserID : testUserID ,
}
sc . context . OrgRole = role
return hs . ValidateDashboard ( c )
} )
sc . m . Post ( routePattern , sc . defaultHandler )
fn ( sc )
} )
}
2022-05-25 16:41:51 +08:00
func postDiffScenario ( t * testing . T , desc string , url string , routePattern string , cmd dtos . CalculateDiffOptions ,
2022-12-13 18:03:36 +08:00
role org . RoleType , fn scenarioFunc , sqlmock db . DB , fakeDashboardVersionService * dashvertest . FakeDashboardVersionService ) {
2020-11-13 16:52:38 +08:00
t . Run ( fmt . Sprintf ( "%s %s" , desc , url ) , func ( t * testing . T ) {
2022-02-10 16:58:52 +08:00
cfg := setting . NewCfg ( )
2022-12-15 22:34:17 +08:00
dashSvc := dashboards . NewFakeDashboardService ( t )
2022-02-10 16:58:52 +08:00
hs := HTTPServer {
2022-05-25 16:41:51 +08:00
Cfg : cfg ,
ProvisioningService : provisioning . NewProvisioningServiceMock ( context . Background ( ) ) ,
2022-10-19 21:02:15 +08:00
Live : newTestLive ( t , db . InitTestDB ( t ) ) ,
2022-11-15 03:08:10 +08:00
QuotaService : quotatest . New ( false , nil ) ,
2022-05-25 16:41:51 +08:00
LibraryPanelService : & mockLibraryPanelService { } ,
LibraryElementService : & mockLibraryElementService { } ,
SQLStore : sqlmock ,
dashboardVersionService : fakeDashboardVersionService ,
2022-07-20 07:44:41 +08:00
Features : featuremgmt . WithFeatures ( ) ,
Reconcile coremodels, entities, objects under new kind framework (#56492)
* Update thema to latest
* Deal with s/Library/*Runtime/
* Commit new, working results of codegen
* We like pointers now
* Always take runtime arg for NewBase()
* Sketchy handwavy pass at entity meta framework
* Little nibbles
* Update pkg/framework/coremodel/entityframework.cue
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
* Move file into new framework location
* Introduce loaders, Go code
* Complete rename to kind
* Flesh out framework, add svg/dashboard examples
* Cruft removal
* Remove generated kind go files from gitignore
* Refine maturity concept, add SlotKind
* Update embed and go deps
* Export PrefixWithGrafanaCUE
* Make the loader actually work, holy crap
* Many small tweaks to type.cue
* Add Apache 2 licensing exceptions for kinds
* Add new kinds dir, start of generator
* Roll back to earlier oapi-codegen
* Introduce new grafana-specific CUE loaders
* Introduce new tidy code generators framework
* Catch up kind framework with tinkering
* Add slices for the generators
* Add write/verify step to main generator
* Many renames
* Split up kind framework cue files
* Use kind.Decl within generated kinds
* Create kind.SomeDecl wrapper type to cache lineages
* Better names again
* Get one generated implemented, hopefully
* Copy dashboard schema into new kind.cue
* Small fixes to make the initial gen work
* Put svg kind in its new home
* Add generated Go dashboard type
* More renames and cleanups
* Add base kind registry and generator
* Stop blacklisting *_gen.go files
This is not the Go best practice, anyway. All we actually want to ignore
for enterprise is generated wire files.
* Change codegen output directories
pkg/kind -> pkg/kinds
pkg/registry/kindreg -> pkg/registry/corekind
* Rename pkg/framework/kind to pkg/kindsys
* Add core structured kind generator
* Add plural and machine names to kind spec
* Copy playlist over to kind system
* Consolidate kindsys files
* Add raw kind generator
* Update CODEOWNERS for kind framework
* Touch up comments a bit
* More docs tweaks
* Remove generated types to reduce noise for review
* Split each generator into its own file
* Rename Slot kind to Composable kind
* Add handwavy types for customkind loading
* Guard against init calls to framework loader
* First pass at doc on extending the kind system
* Improve attribute example in docs
* Fix wire imports
* Add basic TS types generator
* Fix composable kind category def
* No need for a separate file with generate directive
* Catch dashboard schema up
* Rename generator types to something saner and generic
* Make version configurable in ts/go generators
* Add CommonMeta to ease property access
* Add kindsys prop indicating whether lineage is group
* Put all kind categories back in a single file
* Finish with kindsys group props
* Refactor maturity progression per discussion
- Replace "committed" with "merged"
- All kindcats can use all maturity levels, at least for now
* Convert ts veneer index generator to modular system
* Move over to new jennywrites framework
* Strip down old coremodel generator
* Use public version of jennywrites
* Pull latest thema
* Commit generated Go types
* Add header injection postprocessor
* Move sdboyer/jennywrites to grafana/codejen
* Tweak header output
* Remove dashboard and playlist coremodels
* Fix up backend dashboards devenv test
* Fix TS import patterns to new gen filename
* Update internal imports, remove coremodel registry
* Fix compilation errors, wire generation
* Export and replace the prefix dropper
* More Go struct and field name changes
* Last name fixes, hopefully
* Fix lint errors
* Last lint error
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
2022-11-11 04:36:40 +08:00
Kinds : corekind . NewBase ( nil ) ,
2022-12-15 22:34:17 +08:00
DashboardService : dashSvc ,
2022-02-10 16:58:52 +08:00
}
2020-11-13 16:52:38 +08:00
sc := setupScenarioContext ( t , url )
2021-01-15 21:43:20 +08:00
sc . defaultHandler = routing . Wrap ( func ( c * models . ReqContext ) response . Response {
2021-11-29 17:18:01 +08:00
c . Req . Body = mockRequestBody ( cmd )
2022-02-09 20:44:38 +08:00
c . Req . Header . Add ( "Content-Type" , "application/json" )
2018-02-28 00:53:30 +08:00
sc . context = c
2022-08-10 17:56:48 +08:00
sc . context . SignedInUser = & user . SignedInUser {
2022-08-11 19:28:55 +08:00
OrgID : testOrgID ,
UserID : testUserID ,
2018-02-28 00:53:30 +08:00
}
sc . context . OrgRole = role
2022-02-10 16:58:52 +08:00
return hs . CalculateDashboardDiff ( c )
2018-02-28 00:53:30 +08:00
} )
sc . m . Post ( routePattern , sc . defaultHandler )
fn ( sc )
} )
}
2022-05-25 16:41:51 +08:00
func restoreDashboardVersionScenario ( t * testing . T , desc string , url string , routePattern string ,
mock * dashboards . FakeDashboardService , fakeDashboardVersionService * dashvertest . FakeDashboardVersionService ,
2022-12-13 18:03:36 +08:00
cmd dtos . RestoreDashboardVersionCommand , fn scenarioFunc , sqlStore db . DB ) {
2020-11-13 16:52:38 +08:00
t . Run ( fmt . Sprintf ( "%s %s" , desc , url ) , func ( t * testing . T ) {
2020-12-16 02:09:04 +08:00
cfg := setting . NewCfg ( )
2019-03-06 21:38:40 +08:00
hs := HTTPServer {
2022-05-25 16:41:51 +08:00
Cfg : cfg ,
ProvisioningService : provisioning . NewProvisioningServiceMock ( context . Background ( ) ) ,
2022-10-19 21:02:15 +08:00
Live : newTestLive ( t , db . InitTestDB ( t ) ) ,
2022-11-15 03:08:10 +08:00
QuotaService : quotatest . New ( false , nil ) ,
2022-05-25 16:41:51 +08:00
LibraryPanelService : & mockLibraryPanelService { } ,
LibraryElementService : & mockLibraryElementService { } ,
2022-07-07 02:42:39 +08:00
DashboardService : mock ,
2022-05-25 16:41:51 +08:00
SQLStore : sqlStore ,
Features : featuremgmt . WithFeatures ( ) ,
dashboardVersionService : fakeDashboardVersionService ,
Reconcile coremodels, entities, objects under new kind framework (#56492)
* Update thema to latest
* Deal with s/Library/*Runtime/
* Commit new, working results of codegen
* We like pointers now
* Always take runtime arg for NewBase()
* Sketchy handwavy pass at entity meta framework
* Little nibbles
* Update pkg/framework/coremodel/entityframework.cue
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
* Move file into new framework location
* Introduce loaders, Go code
* Complete rename to kind
* Flesh out framework, add svg/dashboard examples
* Cruft removal
* Remove generated kind go files from gitignore
* Refine maturity concept, add SlotKind
* Update embed and go deps
* Export PrefixWithGrafanaCUE
* Make the loader actually work, holy crap
* Many small tweaks to type.cue
* Add Apache 2 licensing exceptions for kinds
* Add new kinds dir, start of generator
* Roll back to earlier oapi-codegen
* Introduce new grafana-specific CUE loaders
* Introduce new tidy code generators framework
* Catch up kind framework with tinkering
* Add slices for the generators
* Add write/verify step to main generator
* Many renames
* Split up kind framework cue files
* Use kind.Decl within generated kinds
* Create kind.SomeDecl wrapper type to cache lineages
* Better names again
* Get one generated implemented, hopefully
* Copy dashboard schema into new kind.cue
* Small fixes to make the initial gen work
* Put svg kind in its new home
* Add generated Go dashboard type
* More renames and cleanups
* Add base kind registry and generator
* Stop blacklisting *_gen.go files
This is not the Go best practice, anyway. All we actually want to ignore
for enterprise is generated wire files.
* Change codegen output directories
pkg/kind -> pkg/kinds
pkg/registry/kindreg -> pkg/registry/corekind
* Rename pkg/framework/kind to pkg/kindsys
* Add core structured kind generator
* Add plural and machine names to kind spec
* Copy playlist over to kind system
* Consolidate kindsys files
* Add raw kind generator
* Update CODEOWNERS for kind framework
* Touch up comments a bit
* More docs tweaks
* Remove generated types to reduce noise for review
* Split each generator into its own file
* Rename Slot kind to Composable kind
* Add handwavy types for customkind loading
* Guard against init calls to framework loader
* First pass at doc on extending the kind system
* Improve attribute example in docs
* Fix wire imports
* Add basic TS types generator
* Fix composable kind category def
* No need for a separate file with generate directive
* Catch dashboard schema up
* Rename generator types to something saner and generic
* Make version configurable in ts/go generators
* Add CommonMeta to ease property access
* Add kindsys prop indicating whether lineage is group
* Put all kind categories back in a single file
* Finish with kindsys group props
* Refactor maturity progression per discussion
- Replace "committed" with "merged"
- All kindcats can use all maturity levels, at least for now
* Convert ts veneer index generator to modular system
* Move over to new jennywrites framework
* Strip down old coremodel generator
* Use public version of jennywrites
* Pull latest thema
* Commit generated Go types
* Add header injection postprocessor
* Move sdboyer/jennywrites to grafana/codejen
* Tweak header output
* Remove dashboard and playlist coremodels
* Fix up backend dashboards devenv test
* Fix TS import patterns to new gen filename
* Update internal imports, remove coremodel registry
* Fix compilation errors, wire generation
* Export and replace the prefix dropper
* More Go struct and field name changes
* Last name fixes, hopefully
* Fix lint errors
* Last lint error
Co-authored-by: Artur Wierzbicki <wierzbicki.artur.94@gmail.com>
2022-11-11 04:36:40 +08:00
Kinds : corekind . NewBase ( nil ) ,
2022-11-24 22:38:55 +08:00
accesscontrolService : actest . FakeService { } ,
2019-03-06 21:38:40 +08:00
}
2020-11-13 16:52:38 +08:00
sc := setupScenarioContext ( t , url )
2022-05-18 00:57:27 +08:00
sc . sqlStore = sqlStore
2022-05-25 16:41:51 +08:00
sc . dashboardVersionService = fakeDashboardVersionService
2021-01-15 21:43:20 +08:00
sc . defaultHandler = routing . Wrap ( func ( c * models . ReqContext ) response . Response {
2021-11-29 17:18:01 +08:00
c . Req . Body = mockRequestBody ( cmd )
2022-02-09 20:44:38 +08:00
c . Req . Header . Add ( "Content-Type" , "application/json" )
2019-03-06 21:38:40 +08:00
sc . context = c
2022-08-10 17:56:48 +08:00
sc . context . SignedInUser = & user . SignedInUser {
2022-08-11 19:28:55 +08:00
OrgID : testOrgID ,
UserID : testUserID ,
2019-03-06 21:38:40 +08:00
}
2022-08-10 17:56:48 +08:00
sc . context . OrgRole = org . RoleAdmin
2019-03-06 21:38:40 +08:00
2021-11-29 17:18:01 +08:00
return hs . RestoreDashboardVersion ( c )
2019-03-06 21:38:40 +08:00
} )
sc . m . Post ( routePattern , sc . defaultHandler )
fn ( sc )
} )
}
2018-03-23 05:13:46 +08:00
func ( sc * scenarioContext ) ToJSON ( ) * simplejson . Json {
2022-04-12 15:30:34 +08:00
result := simplejson . New ( )
err := json . NewDecoder ( sc . resp . Body ) . Decode ( result )
2020-11-13 16:52:38 +08:00
require . NoError ( sc . t , err )
2018-01-30 02:27:53 +08:00
return result
}
2019-10-31 21:27:31 +08:00
type mockDashboardProvisioningService struct {
2021-03-12 18:51:02 +08:00
dashboards . DashboardProvisioningService
2019-10-31 21:27:31 +08:00
}
2022-09-21 20:04:01 +08:00
func ( s mockDashboardProvisioningService ) GetProvisionedDashboardDataByDashboardID ( ctx context . Context , dashboardID int64 ) (
2023-01-18 20:52:41 +08:00
* dashboards . DashboardProvisioning , error ) {
2021-03-17 23:06:10 +08:00
return nil , nil
2019-10-31 21:27:31 +08:00
}
2021-05-12 14:48:17 +08:00
type mockLibraryPanelService struct {
}
2023-01-16 23:33:55 +08:00
func ( m * mockLibraryPanelService ) ConnectLibraryPanelsForDashboard ( c context . Context , signedInUser * user . SignedInUser , dash * dashboards . Dashboard ) error {
2021-05-12 14:48:17 +08:00
return nil
}
2022-08-10 17:56:48 +08:00
func ( m * mockLibraryPanelService ) ImportLibraryPanelsForDashboard ( c context . Context , signedInUser * user . SignedInUser , libraryPanels * simplejson . Json , panels [ ] interface { } , folderID int64 ) error {
2021-09-20 16:58:24 +08:00
return nil
}
2021-05-12 14:48:17 +08:00
type mockLibraryElementService struct {
}
2022-08-10 17:56:48 +08:00
func ( l * mockLibraryElementService ) CreateElement ( c context . Context , signedInUser * user . SignedInUser , cmd libraryelements . CreateLibraryElementCommand ) ( libraryelements . LibraryElementDTO , error ) {
2021-09-20 16:58:24 +08:00
return libraryelements . LibraryElementDTO { } , nil
}
// GetElement gets an element from a UID.
2022-08-10 17:56:48 +08:00
func ( l * mockLibraryElementService ) GetElement ( c context . Context , signedInUser * user . SignedInUser , UID string ) ( libraryelements . LibraryElementDTO , error ) {
2021-05-12 14:48:17 +08:00
return libraryelements . LibraryElementDTO { } , nil
}
// GetElementsForDashboard gets all connected elements for a specific dashboard.
2021-09-27 15:04:36 +08:00
func ( l * mockLibraryElementService ) GetElementsForDashboard ( c context . Context , dashboardID int64 ) ( map [ string ] libraryelements . LibraryElementDTO , error ) {
2021-05-12 14:48:17 +08:00
return map [ string ] libraryelements . LibraryElementDTO { } , nil
}
// ConnectElementsToDashboard connects elements to a specific dashboard.
2022-08-10 17:56:48 +08:00
func ( l * mockLibraryElementService ) ConnectElementsToDashboard ( c context . Context , signedInUser * user . SignedInUser , elementUIDs [ ] string , dashboardID int64 ) error {
2021-05-12 14:48:17 +08:00
return nil
}
// DisconnectElementsFromDashboard disconnects elements from a specific dashboard.
2021-09-27 15:04:36 +08:00
func ( l * mockLibraryElementService ) DisconnectElementsFromDashboard ( c context . Context , dashboardID int64 ) error {
2021-05-12 14:48:17 +08:00
return nil
}
// DeleteLibraryElementsInFolder deletes all elements for a specific folder.
2022-08-10 17:56:48 +08:00
func ( l * mockLibraryElementService ) DeleteLibraryElementsInFolder ( c context . Context , signedInUser * user . SignedInUser , folderUID string ) error {
2021-05-12 14:48:17 +08:00
return nil
}