2017-06-12 08:48:55 -05:00
package api
import (
2021-06-15 09:08:27 -05:00
"context"
2017-06-12 08:48:55 -05:00
"encoding/json"
2021-05-26 09:20:13 -05:00
"errors"
2018-02-19 04:12:56 -06:00
"fmt"
2021-07-16 05:40:03 -05:00
"net/http"
2022-08-10 08:37:51 -05:00
"os"
2017-06-12 08:48:55 -05:00
"testing"
2023-03-30 09:31:53 -05:00
"time"
2017-06-12 08:48:55 -05:00
2022-03-10 11:19:50 -06:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
2023-03-16 04:54:01 -05:00
"github.com/grafana/grafana/pkg/services/publicdashboards"
"github.com/grafana/grafana/pkg/services/publicdashboards/api"
2023-03-30 09:31:53 -05:00
"github.com/grafana/grafana/pkg/services/user/usertest"
2023-03-16 04:54:01 -05:00
2017-06-12 08:48:55 -05:00
"github.com/grafana/grafana/pkg/api/dtos"
2021-01-15 07:43:20 -06:00
"github.com/grafana/grafana/pkg/api/response"
"github.com/grafana/grafana/pkg/api/routing"
2023-01-26 02:21:10 -06:00
"github.com/grafana/grafana/pkg/bus"
2017-06-12 16:05:32 -05:00
"github.com/grafana/grafana/pkg/components/simplejson"
2022-10-19 08:02:15 -05:00
"github.com/grafana/grafana/pkg/infra/db"
2023-01-17 07:17:54 -06:00
"github.com/grafana/grafana/pkg/infra/db/dbtest"
2023-03-30 09:31:53 -05:00
"github.com/grafana/grafana/pkg/infra/localcache"
"github.com/grafana/grafana/pkg/infra/log"
2023-01-26 02:21:10 -06:00
"github.com/grafana/grafana/pkg/infra/tracing"
2021-09-22 09:28:40 -05:00
"github.com/grafana/grafana/pkg/infra/usagestats"
2022-09-28 01:29:35 -05: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-10 14:36:40 -06:00
"github.com/grafana/grafana/pkg/registry/corekind"
2023-01-23 03:54:29 -06:00
"github.com/grafana/grafana/pkg/services/accesscontrol/acimpl"
2022-11-24 08:38:55 -06:00
"github.com/grafana/grafana/pkg/services/accesscontrol/actest"
2022-03-10 05:58:18 -06:00
accesscontrolmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock"
2018-10-13 00:53:28 -05:00
"github.com/grafana/grafana/pkg/services/alerting"
2022-09-19 02:54:37 -05:00
"github.com/grafana/grafana/pkg/services/annotations/annotationstest"
2023-01-27 01:50:36 -06:00
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model"
2017-12-12 09:15:24 -06:00
"github.com/grafana/grafana/pkg/services/dashboards"
2022-02-16 07:15:44 -06:00
"github.com/grafana/grafana/pkg/services/dashboards/database"
2022-05-21 19:44:12 -05:00
"github.com/grafana/grafana/pkg/services/dashboards/service"
2022-05-25 03:41:51 -05:00
dashver "github.com/grafana/grafana/pkg/services/dashboardversion"
"github.com/grafana/grafana/pkg/services/dashboardversion/dashvertest"
2022-01-26 11:44:20 -06:00
"github.com/grafana/grafana/pkg/services/featuremgmt"
2022-10-10 14:47:53 -05:00
"github.com/grafana/grafana/pkg/services/folder"
2023-01-26 02:21:10 -06:00
"github.com/grafana/grafana/pkg/services/folder/folderimpl"
2022-11-11 07:28:24 -06:00
"github.com/grafana/grafana/pkg/services/folder/foldertest"
2022-03-21 04:49:49 -05:00
"github.com/grafana/grafana/pkg/services/guardian"
2023-02-01 10:32:05 -06:00
"github.com/grafana/grafana/pkg/services/libraryelements/model"
2020-10-28 03:36:57 -05:00
"github.com/grafana/grafana/pkg/services/live"
2022-08-10 04:56:48 -05:00
"github.com/grafana/grafana/pkg/services/org"
2022-04-21 08:03:17 -05:00
pref "github.com/grafana/grafana/pkg/services/preference"
"github.com/grafana/grafana/pkg/services/preference/preftest"
2019-04-30 06:32:18 -05:00
"github.com/grafana/grafana/pkg/services/provisioning"
2022-11-14 13:08:10 -06:00
"github.com/grafana/grafana/pkg/services/quota/quotatest"
2022-09-21 07:04:01 -05:00
"github.com/grafana/grafana/pkg/services/tag/tagimpl"
2022-09-20 11:58:04 -05:00
"github.com/grafana/grafana/pkg/services/team/teamtest"
2022-08-10 04:56:48 -05:00
"github.com/grafana/grafana/pkg/services/user"
2017-12-15 07:19:49 -06:00
"github.com/grafana/grafana/pkg/setting"
2021-10-11 07:30:59 -05:00
"github.com/grafana/grafana/pkg/web"
2017-06-12 08:48:55 -05:00
)
2020-06-22 11:00:39 -05:00
func TestGetHomeDashboard ( t * testing . T ) {
2021-07-16 05:40:03 -05:00
httpReq , err := http . NewRequest ( http . MethodGet , "" , nil )
require . NoError ( t , err )
2022-02-09 06:44:38 -06:00
httpReq . Header . Add ( "Content-Type" , "application/json" )
2023-01-27 01:50:36 -06:00
req := & contextmodel . ReqContext { SignedInUser : & user . SignedInUser { } , Context : & web . Context { Req : httpReq } }
2020-06-22 11:00:39 -05:00
cfg := setting . NewCfg ( )
cfg . StaticRootPath = "../../public/"
2022-04-21 08:03:17 -05:00
prefService := preftest . NewPreferenceServiceFake ( )
2022-05-25 03:41:51 -05:00
dashboardVersionService := dashvertest . NewDashboardVersionServiceFake ( )
2020-06-22 11:00:39 -05:00
2021-03-17 10:06:10 -05:00
hs := & HTTPServer {
2022-05-25 03:41:51 -05:00
Cfg : cfg ,
2022-09-28 01:29:35 -05:00
pluginStore : & plugins . FakePluginStore { } ,
2023-01-17 07:17:54 -06:00
SQLStore : dbtest . NewFakeDB ( ) ,
2022-05-25 03:41:51 -05: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-10 14:36:40 -06:00
Kinds : corekind . NewBase ( nil ) ,
2021-03-17 10:06:10 -05:00
}
2020-06-22 11:00:39 -05: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 08:37:51 -05:00
homeDashJSON , err := os . ReadFile ( tc . expectedDashboardPath )
2020-06-22 11:00:39 -05: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 08:03:17 -05:00
prefService . ExpectedPreference = & pref . Preference { }
2020-06-22 11:00:39 -05: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 07:43:20 -06:00
nr , ok := res . ( * response . NormalResponse )
2020-06-22 11:00:39 -05:00
require . True ( t , ok , "should return *NormalResponse" )
2021-01-15 07:43:20 -06:00
require . Equal ( t , b , nr . Body ( ) , "default home dashboard should equal content on disk" )
2020-06-22 11:00:39 -05:00
} )
}
}
2022-10-14 14:33:06 -05:00
func newTestLive ( t * testing . T , store db . DB ) * live . GrafanaLive {
2022-01-26 11:44:20 -06:00
features := featuremgmt . WithFeatures ( )
2021-08-25 08:11:22 -05:00
cfg := & setting . Cfg { AppURL : "http://localhost:3000/" }
2022-01-26 11:44:20 -06:00
cfg . IsFeatureToggleEnabled = features . IsEnabled
gLive , err := live . ProvideService ( nil , cfg ,
routing . NewRouteRegister ( ) ,
2023-02-27 12:48:18 -06:00
nil , nil , nil , nil ,
2022-03-22 14:48:32 -05:00
store ,
2022-01-26 11:44:20 -06:00
nil ,
& usagestats . UsageStatsMock { T : t } ,
nil ,
2023-01-23 03:54:29 -06:00
features , acimpl . ProvideAccessControl ( cfg ) , & dashboards . FakeDashboardService { } , annotationstest . NewFakeAnnotationsRepo ( ) , nil )
2021-05-04 10:44:55 -05:00
require . NoError ( t , err )
return gLive
}
2018-02-19 04:12:56 -06:00
// This tests three main scenarios.
// If a user has access to execute an action on a dashboard:
// 1. and the dashboard is in a folder which does not have an acl
// 2. and the dashboard is in a folder which does have an acl
// 3. Post dashboard response tests
2018-01-30 07:09:30 -06:00
2020-11-13 02:52:38 -06:00
func TestDashboardAPIEndpoint ( t * testing . T ) {
t . Run ( "Given a dashboard with a parent folder which does not have an ACL" , func ( t * testing . T ) {
2023-01-16 09:33:55 -06:00
fakeDash := dashboards . NewDashboard ( "Child dash" )
2023-04-14 18:50:10 -05:00
fakeDash . SetID ( 1 )
fakeDash . SetUID ( "test-uid-one" )
2023-01-16 09:33:55 -06:00
fakeDash . FolderID = 1
2022-07-18 08:14:58 -05:00
fakeDash . HasACL = false
2022-05-25 03:41:51 -05:00
fakeDashboardVersionService := dashvertest . NewDashboardVersionServiceFake ( )
2023-03-30 09:31:53 -05:00
fakeDashboardVersionService . ExpectedDashboardVersion = & dashver . DashboardVersionDTO { CreatedBy : 1 }
2022-09-20 11:58:04 -05:00
teamService := & teamtest . FakeService { }
2022-05-23 10:14:27 -05:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2023-01-25 03:36:26 -06:00
dashboardService . On ( "GetDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardQuery" ) ) . Return ( fakeDash , nil )
2023-01-17 07:17:54 -06:00
mockSQLStore := dbtest . NewFakeDB ( )
2018-01-31 09:51:06 -06:00
2022-02-07 05:43:43 -06:00
hs := & HTTPServer {
2022-05-25 03:41:51 -05:00
Cfg : setting . NewCfg ( ) ,
2022-09-28 01:29:35 -05:00
pluginStore : & plugins . FakePluginStore { } ,
2022-05-25 03:41:51 -05:00
SQLStore : mockSQLStore ,
AccessControl : accesscontrolmock . New ( ) ,
Features : featuremgmt . WithFeatures ( ) ,
2022-07-06 13:42:39 -05:00
DashboardService : dashboardService ,
2022-05-25 03:41:51 -05: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-10 14:36:40 -06:00
Kinds : corekind . NewBase ( nil ) ,
2022-11-14 13:08:10 -06:00
QuotaService : quotatest . New ( false , nil ) ,
2023-03-30 09:31:53 -05:00
userService : & usertest . FakeUserService {
ExpectedUser : & user . User { ID : 1 , Login : "test-user" } ,
} ,
2022-02-07 05:43:43 -06:00
}
2018-01-29 14:23:07 -06:00
2022-02-07 05:43:43 -06:00
setUp := func ( ) {
2022-08-10 04:56:48 -05:00
viewerRole := org . RoleViewer
editorRole := org . RoleEditor
2023-01-25 03:36:26 -06:00
qResult := [ ] * dashboards . DashboardACLInfoDTO {
2023-01-26 07:46:30 -06:00
{ Role : & viewerRole , Permission : dashboards . PERMISSION_VIEW } ,
{ Role : & editorRole , Permission : dashboards . PERMISSION_EDIT } ,
2023-01-25 03:36:26 -06:00
}
dashboardService . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Return ( qResult , nil )
2023-03-16 04:54:01 -05:00
guardian . InitLegacyGuardian ( hs . Cfg , mockSQLStore , dashboardService , teamService )
2020-11-13 02:52:38 -06:00
}
2017-06-22 16:43:55 -05:00
2018-01-30 07:09:30 -06:00
// This tests two scenarios:
// 1. user is an org viewer
// 2. user is an org editor
2020-11-13 02:52:38 -06:00
t . Run ( "When user is an Org Viewer" , func ( t * testing . T ) {
2022-08-10 04:56:48 -05:00
role := org . RoleViewer
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 05:43:43 -06:00
setUp ( )
sc . sqlStore = mockSQLStore
2023-01-19 10:38:07 -06:00
dash := getDashboardShouldReturn200WithConfig ( t , sc , nil , nil , dashboardService , nil )
2020-11-13 02:52:38 -06:00
2023-04-14 18:50:10 -05:00
assert . Equal ( t , fakeDash . UID , dash . Dashboard . Get ( "uid" ) . MustString ( ) )
2020-11-13 02:52:38 -06:00
assert . False ( t , dash . Meta . CanEdit )
assert . False ( t , dash . Meta . CanSave )
assert . False ( t , dash . Meta . CanAdmin )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions/1" ,
"/api/dashboards/id/:dashboardId/versions/:id" , role , func ( sc * scenarioContext ) {
setUp ( )
2022-02-07 05:43:43 -06:00
sc . sqlStore = mockSQLStore
2018-01-31 09:46:31 -06:00
2022-02-07 05:43:43 -06:00
hs . callGetDashboardVersion ( sc )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" ,
"/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
setUp ( )
2022-02-07 05:43:43 -06:00
sc . sqlStore = mockSQLStore
2017-06-13 17:28:34 -05:00
2022-02-07 05:43:43 -06:00
hs . callGetDashboardVersions ( sc )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-06-12 08:48:55 -05:00
} )
2020-11-13 02:52:38 -06:00
t . Run ( "When user is an Org Editor" , func ( t * testing . T ) {
2022-08-10 04:56:48 -05:00
role := org . RoleEditor
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 05:43:43 -06:00
setUp ( )
sc . sqlStore = mockSQLStore
2023-01-19 10:38:07 -06:00
dash := getDashboardShouldReturn200WithConfig ( t , sc , nil , nil , dashboardService , nil )
2017-06-12 08:48:55 -05:00
2020-11-13 02:52:38 -06:00
assert . True ( t , dash . Meta . CanEdit )
assert . True ( t , dash . Meta . CanSave )
assert . False ( t , dash . Meta . CanAdmin )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions/1" ,
"/api/dashboards/id/:dashboardId/versions/:id" , role , func ( sc * scenarioContext ) {
setUp ( )
2022-02-07 05:43:43 -06:00
sc . sqlStore = mockSQLStore
hs . callGetDashboardVersion ( sc )
2018-01-31 09:46:31 -06:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 200 , sc . resp . Code )
2023-03-30 09:31:53 -05:00
var version * dashver . DashboardVersionMeta
err := json . NewDecoder ( sc . resp . Body ) . Decode ( & version )
require . NoError ( t , err )
assert . NotEqual ( t , anonString , version . CreatedBy )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" ,
"/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
setUp ( )
2022-02-07 05:43:43 -06:00
hs . callGetDashboardVersions ( sc )
2017-06-13 17:28:34 -05:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 200 , sc . resp . Code )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-06-12 08:48:55 -05:00
} )
} )
2020-11-13 02:52:38 -06:00
t . Run ( "Given a dashboard with a parent folder which has an ACL" , func ( t * testing . T ) {
2023-01-16 09:33:55 -06:00
fakeDash := dashboards . NewDashboard ( "Child dash" )
fakeDash . ID = 1
fakeDash . FolderID = 1
2022-07-18 08:14:58 -05:00
fakeDash . HasACL = true
2022-05-25 03:41:51 -05:00
fakeDashboardVersionService := dashvertest . NewDashboardVersionServiceFake ( )
2022-12-27 10:17:24 -06:00
fakeDashboardVersionService . ExpectedDashboardVersion = & dashver . DashboardVersionDTO { }
2022-09-20 11:58:04 -05:00
teamService := & teamtest . FakeService { }
2022-05-23 10:14:27 -05:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2023-01-25 03:36:26 -06:00
dashboardService . On ( "GetDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardQuery" ) ) . Return ( fakeDash , nil )
qResult := [ ] * dashboards . DashboardACLInfoDTO {
{
DashboardID : 1 ,
2023-01-26 07:46:30 -06:00
Permission : dashboards . PERMISSION_EDIT ,
2023-01-25 03:36:26 -06:00
UserID : 200 ,
} ,
}
dashboardService . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Return ( qResult , nil )
2022-02-07 05:43:43 -06:00
2023-01-17 07:17:54 -06:00
mockSQLStore := dbtest . NewFakeDB ( )
2022-03-10 05:58:18 -06:00
cfg := setting . NewCfg ( )
2022-10-19 08:02:15 -05:00
sql := db . InitTestDB ( t )
2022-05-17 13:52:22 -05:00
2020-11-13 02:52:38 -06:00
hs := & HTTPServer {
2022-05-25 03:41:51 -05:00
Cfg : cfg ,
Live : newTestLive ( t , sql ) ,
LibraryPanelService : & mockLibraryPanelService { } ,
LibraryElementService : & mockLibraryElementService { } ,
SQLStore : mockSQLStore ,
AccessControl : accesscontrolmock . New ( ) ,
2022-07-06 13:42:39 -05:00
DashboardService : dashboardService ,
2022-05-25 03:41:51 -05:00
dashboardVersionService : fakeDashboardVersionService ,
2022-07-19 18:44:41 -05: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-10 14:36:40 -06:00
Kinds : corekind . NewBase ( nil ) ,
2020-11-13 02:52:38 -06:00
}
2017-06-12 08:48:55 -05:00
2022-02-07 05:43:43 -06:00
setUp := func ( ) {
2023-03-16 04:54:01 -05:00
cfg . ViewersCanEdit = false
guardian . InitLegacyGuardian ( cfg , mockSQLStore , dashboardService , teamService )
2019-04-30 06:32:18 -05:00
}
2018-01-30 07:09:30 -06:00
// This tests six scenarios:
// 1. user is an org viewer AND has no permissions for this dashboard
// 2. user is an org editor AND has no permissions for this dashboard
// 3. user is an org viewer AND has been granted edit permission for the dashboard
// 4. user is an org viewer AND all viewers have edit permission for this dashboard
// 5. user is an org viewer AND has been granted an admin permission
// 6. user is an org editor AND has been granted a view permission
2020-11-13 02:52:38 -06:00
t . Run ( "When user is an Org Viewer and has no permissions for this dashboard" , func ( t * testing . T ) {
2022-08-10 04:56:48 -05:00
role := org . RoleViewer
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 05:43:43 -06:00
setUp ( )
sc . sqlStore = mockSQLStore
2020-11-13 02:52:38 -06:00
sc . handlerFunc = hs . GetDashboard
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
2022-02-07 05:43:43 -06:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2018-01-29 14:23:07 -06:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 05:43:43 -06:00
setUp ( )
sc . sqlStore = mockSQLStore
2023-03-08 14:54:35 -06:00
hs . callDeleteDashboardByUID ( t , sc , dashboardService , nil )
2018-01-29 14:23:07 -06:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions/1" ,
"/api/dashboards/id/:dashboardId/versions/:id" , role , func ( sc * scenarioContext ) {
setUp ( )
2022-02-07 05:43:43 -06:00
sc . sqlStore = mockSQLStore
hs . callGetDashboardVersion ( sc )
2018-01-31 09:46:31 -06:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" ,
"/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
setUp ( )
2022-02-07 05:43:43 -06:00
hs . callGetDashboardVersions ( sc )
2017-06-13 17:28:34 -05:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-06-12 08:48:55 -05:00
} )
2020-11-13 02:52:38 -06:00
t . Run ( "When user is an Org Editor and has no permissions for this dashboard" , func ( t * testing . T ) {
2022-08-10 04:56:48 -05:00
role := org . RoleEditor
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 05:43:43 -06:00
setUp ( )
sc . sqlStore = mockSQLStore
2020-11-13 02:52:38 -06:00
sc . handlerFunc = hs . GetDashboard
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2018-01-29 14:23:07 -06:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 05:43:43 -06:00
setUp ( )
2023-03-08 14:54:35 -06:00
hs . callDeleteDashboardByUID ( t , sc , dashboardService , nil )
2018-01-29 14:23:07 -06:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions/1" ,
"/api/dashboards/id/:dashboardId/versions/:id" , role , func ( sc * scenarioContext ) {
setUp ( )
2022-02-07 05:43:43 -06:00
hs . callGetDashboardVersion ( sc )
2018-01-31 09:46:31 -06:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" ,
"/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
setUp ( )
2022-02-07 05:43:43 -06:00
hs . callGetDashboardVersions ( sc )
2017-06-13 17:28:34 -05:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-06-12 08:48:55 -05:00
} )
2020-11-13 02:52:38 -06:00
t . Run ( "When user is an Org Viewer but has an edit permission" , func ( t * testing . T ) {
2022-08-10 04:56:48 -05:00
role := org . RoleViewer
2017-06-12 16:05:32 -05:00
2022-02-07 05:43:43 -06:00
setUpInner := func ( ) {
2023-03-16 04:54:01 -05:00
cfg . ViewersCanEdit = false
2022-06-01 13:16:26 -05:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2023-01-25 03:36:26 -06:00
qResult := [ ] * dashboards . DashboardACLInfoDTO {
2023-01-26 07:46:30 -06:00
{ OrgID : 1 , DashboardID : 2 , UserID : 1 , Permission : dashboards . PERMISSION_EDIT } ,
2023-01-25 03:36:26 -06:00
}
dashboardService . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Return ( qResult , nil )
2023-03-16 04:54:01 -05:00
guardian . InitLegacyGuardian ( cfg , mockSQLStore , dashboardService , teamService )
2020-11-13 02:52:38 -06:00
}
2022-02-07 05:43:43 -06:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" ,
"/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 05:43:43 -06:00
setUpInner ( )
sc . sqlStore = mockSQLStore
2023-01-19 10:38:07 -06:00
dash := getDashboardShouldReturn200WithConfig ( t , sc , nil , nil , dashboardService , nil )
2018-01-29 14:23:07 -06:00
2020-11-13 02:52:38 -06:00
assert . True ( t , dash . Meta . CanEdit )
assert . True ( t , dash . Meta . CanSave )
assert . False ( t , dash . Meta . CanAdmin )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-06-22 17:34:19 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 05:43:43 -06:00
setUpInner ( )
2022-05-23 10:14:27 -05:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2023-01-25 03:36:26 -06:00
qResult := dashboards . NewDashboard ( "test" )
dashboardService . On ( "GetDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardQuery" ) ) . Return ( qResult , nil )
2022-05-23 10:14:27 -05:00
dashboardService . On ( "DeleteDashboard" , mock . Anything , mock . AnythingOfType ( "int64" ) , mock . AnythingOfType ( "int64" ) ) . Return ( nil )
2022-02-07 05:43:43 -06:00
2023-03-08 14:54:35 -06:00
pubdashService := publicdashboards . NewFakePublicDashboardService ( t )
pubdashService . On ( "DeleteByDashboard" , mock . Anything , mock . Anything ) . Return ( nil )
hs . callDeleteDashboardByUID ( t , sc , dashboardService , pubdashService )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 200 , sc . resp . Code )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-06-22 17:34:19 -05:00
} )
2020-11-13 02:52:38 -06:00
t . Run ( "When user is an Org Viewer and viewers can edit" , func ( t * testing . T ) {
2022-08-10 04:56:48 -05:00
role := org . RoleViewer
2017-12-15 07:19:49 -06:00
2022-02-07 05:43:43 -06:00
setUpInner := func ( ) {
2023-03-16 04:54:01 -05:00
cfg . ViewersCanEdit = true
2022-06-01 13:16:26 -05:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2023-01-25 03:36:26 -06:00
qResult := [ ] * dashboards . DashboardACLInfoDTO {
2023-01-26 07:46:30 -06:00
{ OrgID : 1 , DashboardID : 2 , UserID : 1 , Permission : dashboards . PERMISSION_VIEW } ,
2023-01-25 03:36:26 -06:00
}
dashboardService . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Return ( qResult , nil )
2023-03-16 04:54:01 -05:00
guardian . InitLegacyGuardian ( cfg , mockSQLStore , dashboardService , teamService )
2020-11-13 02:52:38 -06:00
}
2022-02-07 05:43:43 -06:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 05:43:43 -06:00
setUpInner ( )
2017-12-15 07:19:49 -06:00
2023-03-16 04:54:01 -05:00
require . True ( t , cfg . ViewersCanEdit )
2022-02-07 05:43:43 -06:00
sc . sqlStore = mockSQLStore
2023-01-19 10:38:07 -06:00
dash := getDashboardShouldReturn200WithConfig ( t , sc , nil , nil , dashboardService , nil )
2018-01-29 14:23:07 -06:00
2020-11-13 02:52:38 -06:00
assert . True ( t , dash . Meta . CanEdit )
assert . False ( t , dash . Meta . CanSave )
assert . False ( t , dash . Meta . CanAdmin )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-12-15 07:19:49 -06:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 05:43:43 -06:00
setUpInner ( )
2018-01-31 09:46:31 -06:00
2023-03-08 14:54:35 -06:00
hs . callDeleteDashboardByUID ( t , sc , dashboardService , nil )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-12-15 07:19:49 -06:00
} )
2020-11-13 02:52:38 -06:00
t . Run ( "When user is an Org Viewer but has an admin permission" , func ( t * testing . T ) {
2022-08-10 04:56:48 -05:00
role := org . RoleViewer
2017-06-22 17:34:19 -05:00
2022-02-07 05:43:43 -06:00
setUpInner := func ( ) {
2023-03-16 04:54:01 -05:00
cfg . ViewersCanEdit = true
2020-11-13 02:52:38 -06:00
2022-06-01 13:16:26 -05:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2023-01-25 03:36:26 -06:00
qResult := [ ] * dashboards . DashboardACLInfoDTO {
2023-01-26 07:46:30 -06:00
{ OrgID : 1 , DashboardID : 2 , UserID : 1 , Permission : dashboards . PERMISSION_ADMIN } ,
2023-01-25 03:36:26 -06:00
}
dashboardService . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Return ( qResult , nil )
2023-03-16 04:54:01 -05:00
guardian . InitLegacyGuardian ( cfg , mockSQLStore , dashboardService , teamService )
2017-06-22 17:34:19 -05:00
}
2022-02-07 05:43:43 -06: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-19 10:38:07 -06:00
dash := getDashboardShouldReturn200WithConfig ( t , sc , nil , nil , dashboardService , nil )
2018-01-29 14:23:07 -06:00
2020-11-13 02:52:38 -06:00
assert . True ( t , dash . Meta . CanEdit )
assert . True ( t , dash . Meta . CanSave )
assert . True ( t , dash . Meta . CanAdmin )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 05:43:43 -06:00
setUpInner ( )
sc . sqlStore = mockSQLStore
2022-05-23 10:14:27 -05:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2023-01-25 03:36:26 -06:00
qResult := dashboards . NewDashboard ( "test" )
dashboardService . On ( "GetDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardQuery" ) ) . Return ( qResult , nil )
2022-05-23 10:14:27 -05:00
dashboardService . On ( "DeleteDashboard" , mock . Anything , mock . AnythingOfType ( "int64" ) , mock . AnythingOfType ( "int64" ) ) . Return ( nil )
2023-03-08 14:54:35 -06:00
pubdashService := publicdashboards . NewFakePublicDashboardService ( t )
pubdashService . On ( "DeleteByDashboard" , mock . Anything , mock . Anything ) . Return ( nil )
hs . callDeleteDashboardByUID ( t , sc , dashboardService , pubdashService )
2018-01-29 14:23:07 -06:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 200 , sc . resp . Code )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions/1" , "/api/dashboards/id/:dashboardId/versions/:id" , role , func ( sc * scenarioContext ) {
setUpInner ( )
2018-01-31 09:46:31 -06:00
2022-02-07 05:43:43 -06:00
hs . callGetDashboardVersion ( sc )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 200 , sc . resp . Code )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" , "/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
setUpInner ( )
2017-06-13 17:28:34 -05:00
2022-02-07 05:43:43 -06:00
hs . callGetDashboardVersions ( sc )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 200 , sc . resp . Code )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-06-12 08:48:55 -05:00
} )
2020-11-13 02:52:38 -06:00
t . Run ( "When user is an Org Editor but has a view permission" , func ( t * testing . T ) {
2022-08-10 04:56:48 -05:00
role := org . RoleEditor
2017-06-12 16:05:32 -05:00
2022-02-07 05:43:43 -06:00
setUpInner := func ( ) {
2023-03-16 04:54:01 -05:00
cfg := setting . NewCfg ( )
2022-06-01 13:16:26 -05:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2023-01-25 03:36:26 -06:00
qResult := [ ] * dashboards . DashboardACLInfoDTO {
2023-01-26 07:46:30 -06:00
{ OrgID : 1 , DashboardID : 2 , UserID : 1 , Permission : dashboards . PERMISSION_VIEW } ,
2023-01-25 03:36:26 -06:00
}
dashboardService . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Return ( qResult , nil )
2023-03-16 04:54:01 -05:00
guardian . InitLegacyGuardian ( cfg , mockSQLStore , dashboardService , teamService )
2020-11-13 02:52:38 -06:00
}
2017-06-12 08:48:55 -05:00
2022-02-07 05:43:43 -06: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-19 10:38:07 -06:00
dash := getDashboardShouldReturn200WithConfig ( t , sc , nil , nil , dashboardService , nil )
2022-02-07 05:43:43 -06:00
2020-11-13 02:52:38 -06:00
assert . False ( t , dash . Meta . CanEdit )
assert . False ( t , dash . Meta . CanSave )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling DELETE on" , "DELETE" , "/api/dashboards/uid/abcdefghi" , "/api/dashboards/uid/:uid" , role , func ( sc * scenarioContext ) {
2022-02-07 05:43:43 -06:00
setUpInner ( )
2023-03-08 14:54:35 -06:00
hs . callDeleteDashboardByUID ( t , sc , dashboardService , nil )
2018-01-31 09:46:31 -06:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions/1" , "/api/dashboards/id/:dashboardId/versions/:id" , role , func ( sc * scenarioContext ) {
setUpInner ( )
2022-02-07 05:43:43 -06:00
hs . callGetDashboardVersion ( sc )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-06-13 17:28:34 -05:00
2020-11-13 02:52:38 -06:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/id/2/versions" , "/api/dashboards/id/:dashboardId/versions" , role , func ( sc * scenarioContext ) {
setUpInner ( )
2022-02-07 05:43:43 -06:00
hs . callGetDashboardVersions ( sc )
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 403 , sc . resp . Code )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2017-06-12 08:48:55 -05:00
} )
} )
2018-01-31 09:51:06 -06:00
2020-11-13 02:52:38 -06:00
t . Run ( "Given two dashboards with the same title in different folders" , func ( t * testing . T ) {
2023-01-18 06:52:41 -06:00
dashOne := dashboards . NewDashboard ( "dash" )
dashOne . ID = 2
dashOne . FolderID = 1
2022-07-18 08:14:58 -05:00
dashOne . HasACL = false
2018-01-31 09:51:06 -06:00
2023-01-18 06:52:41 -06:00
dashTwo := dashboards . NewDashboard ( "dash" )
dashTwo . ID = 4
dashTwo . FolderID = 3
2022-07-18 08:14:58 -05:00
dashTwo . HasACL = false
2018-01-31 09:51:06 -06:00
} )
2018-02-19 04:12:56 -06:00
2020-11-13 02:52:38 -06:00
t . Run ( "Post dashboard response tests" , func ( t * testing . T ) {
2022-03-10 11:19:50 -06:00
dashboardStore := & dashboards . FakeDashboardStore { }
2022-02-16 07:15:44 -06:00
defer dashboardStore . AssertExpectations ( t )
2018-02-19 04:12:56 -06:00
// This tests that a valid request returns correct response
2020-11-13 02:52:38 -06:00
t . Run ( "Given a correct request for creating a dashboard" , func ( t * testing . T ) {
const folderID int64 = 3
const dashID int64 = 2
2018-02-19 04:12:56 -06:00
2023-01-18 06:52:41 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
UserID : 5 ,
2018-02-19 04:12:56 -06:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "Dash" ,
} ) ,
Overwrite : true ,
2023-01-18 06:52:41 -06:00
FolderID : folderID ,
2018-02-19 04:12:56 -06:00
IsFolder : false ,
Message : "msg" ,
}
2022-05-23 10:14:27 -05:00
dashboardService := dashboards . NewFakeDashboardService ( t )
dashboardService . On ( "SaveDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.SaveDashboardDTO" ) , mock . AnythingOfType ( "bool" ) ) .
2023-01-16 09:33:55 -06:00
Return ( & dashboards . Dashboard { ID : dashID , UID : "uid" , Title : "Dash" , Slug : "dash" , Version : 2 } , nil )
2018-02-19 04:12:56 -06:00
2022-05-23 10:14:27 -05:00
postDashboardScenario ( t , "When calling POST on" , "/api/dashboards" , "/api/dashboards" , cmd , dashboardService , nil , func ( sc * scenarioContext ) {
2020-11-13 02:52:38 -06:00
callPostDashboardShouldReturnSuccess ( sc )
2018-02-19 04:12:56 -06:00
2020-11-13 02:52:38 -06: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 04:12:56 -06:00
} )
} )
2021-05-26 09:20:13 -05: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 06:52:41 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
UserID : 5 ,
2021-05-26 09:20:13 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "Dash" ,
} ) ,
Overwrite : true ,
2023-01-18 06:52:41 -06:00
FolderUID : folderUid ,
2021-05-26 09:20:13 -05:00
IsFolder : false ,
Message : "msg" ,
}
2022-05-23 10:14:27 -05:00
dashboardService := dashboards . NewFakeDashboardService ( t )
dashboardService . On ( "SaveDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.SaveDashboardDTO" ) , mock . AnythingOfType ( "bool" ) ) .
2023-01-16 09:33:55 -06:00
Return ( & dashboards . Dashboard { ID : dashID , UID : "uid" , Title : "Dash" , Slug : "dash" , Version : 2 } , nil )
2021-05-26 09:20:13 -05:00
2022-11-11 07:28:24 -06:00
mockFolder := & foldertest . FakeService {
ExpectedFolder : & folder . Folder { ID : 1 , UID : "folderUID" , Title : "Folder" } ,
2021-05-26 09:20:13 -05:00
}
2022-05-23 10:14:27 -05:00
postDashboardScenario ( t , "When calling POST on" , "/api/dashboards" , "/api/dashboards" , cmd , dashboardService , mockFolder , func ( sc * scenarioContext ) {
2021-05-26 09:20:13 -05: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 06:52:41 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
UserID : 5 ,
2021-05-26 09:20:13 -05:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "Dash" ,
} ) ,
Overwrite : true ,
2023-01-18 06:52:41 -06:00
FolderUID : "folderUID" ,
2021-05-26 09:20:13 -05:00
IsFolder : false ,
Message : "msg" ,
}
2022-05-23 10:14:27 -05:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2021-05-26 09:20:13 -05:00
2022-11-11 07:28:24 -06:00
mockFolder := & foldertest . FakeService {
ExpectedError : errors . New ( "Error while searching Folder ID" ) ,
2021-05-26 09:20:13 -05:00
}
2022-05-23 10:14:27 -05:00
postDashboardScenario ( t , "When calling POST on" , "/api/dashboards" , "/api/dashboards" , cmd , dashboardService , mockFolder , func ( sc * scenarioContext ) {
2021-05-26 09:20:13 -05:00
callPostDashboard ( sc )
assert . Equal ( t , 500 , sc . resp . Code )
} )
} )
2018-02-19 04:12:56 -06:00
// This tests that invalid requests returns expected error responses
2020-11-13 02:52:38 -06:00
t . Run ( "Given incorrect requests for creating a dashboard" , func ( t * testing . T ) {
2018-02-19 04:12:56 -06:00
testCases := [ ] struct {
SaveError error
ExpectedStatusCode int
} {
2022-06-30 08:31:54 -05: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 00:53:28 -05:00
{ SaveError : alerting . ValidationError { Reason : "Mu" } , ExpectedStatusCode : 422 } ,
2022-06-30 08:31:54 -05:00
{ 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 04:12:56 -06:00
}
2023-01-18 06:52:41 -06:00
cmd := dashboards . SaveDashboardCommand {
OrgID : 1 ,
2018-02-19 04:12:56 -06:00
Dashboard : simplejson . NewFromAny ( map [ string ] interface { } {
"title" : "" ,
} ) ,
}
for _ , tc := range testCases {
2022-05-23 10:14:27 -05:00
dashboardService := dashboards . NewFakeDashboardService ( t )
dashboardService . On ( "SaveDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.SaveDashboardDTO" ) , mock . AnythingOfType ( "bool" ) ) . Return ( nil , tc . SaveError )
2018-02-19 04:12:56 -06:00
2020-11-13 02:52:38 -06:00
postDashboardScenario ( t , fmt . Sprintf ( "Expect '%s' error when calling POST on" , tc . SaveError . Error ( ) ) ,
2022-05-23 10:14:27 -05:00
"/api/dashboards" , "/api/dashboards" , cmd , dashboardService , nil , func ( sc * scenarioContext ) {
2020-11-13 02:52:38 -06:00
callPostDashboard ( sc )
assert . Equal ( t , tc . ExpectedStatusCode , sc . resp . Code )
} )
2018-02-19 04:12:56 -06:00
}
} )
} )
2018-02-27 10:53:30 -06:00
2022-10-14 08:51:05 -05:00
t . Run ( "Given a dashboard to validate" , func ( t * testing . T ) {
2023-01-18 09:01:25 -06:00
sqlmock := dbtest . NewFakeDB ( )
2022-10-14 08:51:05 -05:00
t . Run ( "When an invalid dashboard json is posted" , func ( t * testing . T ) {
2023-01-18 06:52:41 -06:00
cmd := dashboards . ValidateDashboardCommand {
2022-10-14 08:51:05 -05: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 09:01:25 -06:00
} , sqlmock )
2022-10-14 08:51:05 -05:00
} )
t . Run ( "When a dashboard with a too-low schema version is posted" , func ( t * testing . T ) {
2023-01-18 06:52:41 -06:00
cmd := dashboards . ValidateDashboardCommand {
2022-10-14 08:51:05 -05: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 09:01:25 -06:00
} , sqlmock )
2022-10-14 08:51:05 -05: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 06:52:41 -06:00
cmd := dashboards . ValidateDashboardCommand {
2022-10-14 08:51:05 -05: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 09:01:25 -06:00
} , sqlmock )
2022-10-14 08:51:05 -05:00
} )
} )
2020-11-13 02:52:38 -06:00
t . Run ( "Given two dashboards being compared" , func ( t * testing . T ) {
2022-05-25 03:41:51 -05:00
fakeDashboardVersionService := dashvertest . NewDashboardVersionServiceFake ( )
2022-12-27 10:17:24 -06:00
fakeDashboardVersionService . ExpectedDashboardVersions = [ ] * dashver . DashboardVersionDTO {
2022-05-25 03:41:51 -05: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 07:17:54 -06:00
sqlmock := dbtest . NewFakeDB ( )
2020-11-13 02:52:38 -06:00
setUp := func ( ) {
2022-09-20 11:58:04 -05:00
teamSvc := & teamtest . FakeService { }
2022-06-01 13:16:26 -05:00
dashSvc := dashboards . NewFakeDashboardService ( t )
2023-01-25 03:36:26 -06:00
dashSvc . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Return ( nil , nil )
qResult := & dashboards . Dashboard { }
dashSvc . On ( "GetDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardQuery" ) ) . Return ( qResult , nil )
2023-03-16 04:54:01 -05:00
guardian . InitLegacyGuardian ( setting . NewCfg ( ) , sqlmock , dashSvc , teamSvc )
2020-11-13 02:52:38 -06:00
}
2018-02-27 10:53:30 -06:00
cmd := dtos . CalculateDiffOptions {
Base : dtos . CalculateDiffTarget {
DashboardId : 1 ,
Version : 1 ,
} ,
New : dtos . CalculateDiffTarget {
DashboardId : 2 ,
Version : 2 ,
} ,
DiffType : "basic" ,
}
2020-11-13 02:52:38 -06:00
t . Run ( "when user does not have permission" , func ( t * testing . T ) {
2022-08-10 04:56:48 -05:00
role := org . RoleViewer
2020-11-13 02:52:38 -06:00
postDiffScenario ( t , "When calling POST on" , "/api/dashboards/calculate-diff" , "/api/dashboards/calculate-diff" , cmd , role , func ( sc * scenarioContext ) {
setUp ( )
callPostDashboard ( sc )
assert . Equal ( t , 403 , sc . resp . Code )
2023-01-17 07:17:54 -06:00
} , sqlmock , fakeDashboardVersionService )
2018-02-27 10:53:30 -06:00
} )
2020-11-13 02:52:38 -06:00
t . Run ( "when user does have permission" , func ( t * testing . T ) {
2022-08-10 04:56:48 -05:00
role := org . RoleAdmin
2020-11-13 02:52:38 -06:00
postDiffScenario ( t , "When calling POST on" , "/api/dashboards/calculate-diff" , "/api/dashboards/calculate-diff" , cmd , role , func ( sc * scenarioContext ) {
2022-07-18 08:14:58 -05:00
// This test shouldn't hit GetDashboardACLInfoList, so no setup needed
2022-05-25 03:41:51 -05:00
sc . dashboardVersionService = fakeDashboardVersionService
2020-11-13 02:52:38 -06:00
callPostDashboard ( sc )
assert . Equal ( t , 200 , sc . resp . Code )
2023-01-17 07:17:54 -06:00
} , sqlmock , fakeDashboardVersionService )
2018-02-27 10:53:30 -06:00
} )
} )
2019-03-06 07:38:40 -06:00
2020-11-13 02:52:38 -06:00
t . Run ( "Given dashboard in folder being restored should restore to folder" , func ( t * testing . T ) {
const folderID int64 = 1
2023-01-16 09:33:55 -06:00
fakeDash := dashboards . NewDashboard ( "Child dash" )
fakeDash . ID = 2
fakeDash . FolderID = folderID
2022-07-18 08:14:58 -05:00
fakeDash . HasACL = false
2019-03-06 07:38:40 -06:00
2022-05-23 10:14:27 -05:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2023-01-25 03:36:26 -06:00
dashboardService . On ( "GetDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardQuery" ) ) . Return ( fakeDash , nil )
2022-05-23 10:14:27 -05:00
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 09:33:55 -06:00
cmd . Dashboard = & dashboards . Dashboard {
ID : 2 , UID : "uid" , Title : "Dash" , Slug : "dash" , Version : 1 ,
2022-05-23 10:14:27 -05:00
}
} ) . Return ( nil , nil )
2019-03-06 07:38:40 -06:00
cmd := dtos . RestoreDashboardVersionCommand {
Version : 1 ,
}
2022-05-25 03:41:51 -05:00
fakeDashboardVersionService := dashvertest . NewDashboardVersionServiceFake ( )
2022-12-27 10:17:24 -06:00
fakeDashboardVersionService . ExpectedDashboardVersions = [ ] * dashver . DashboardVersionDTO {
2022-02-10 02:58:52 -06:00
{
2022-05-25 03:41:51 -05:00
DashboardID : 2 ,
2022-02-10 02:58:52 -06:00
Version : 1 ,
Data : fakeDash . Data ,
} }
2023-01-18 09:01:25 -06:00
mockSQLStore := dbtest . NewFakeDB ( )
2020-11-13 02:52:38 -06:00
restoreDashboardVersionScenario ( t , "When calling POST on" , "/api/dashboards/id/1/restore" ,
2022-05-25 03:41:51 -05:00
"/api/dashboards/id/:dashboardId/restore" , dashboardService , fakeDashboardVersionService , cmd , func ( sc * scenarioContext ) {
sc . dashboardVersionService = fakeDashboardVersionService
2020-11-13 02:52:38 -06:00
callRestoreDashboardVersion ( sc )
assert . Equal ( t , 200 , sc . resp . Code )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2019-03-06 07:38:40 -06:00
} )
2020-11-13 02:52:38 -06:00
t . Run ( "Given dashboard in general folder being restored should restore to general folder" , func ( t * testing . T ) {
2023-01-16 09:33:55 -06:00
fakeDash := dashboards . NewDashboard ( "Child dash" )
fakeDash . ID = 2
2022-07-18 08:14:58 -05:00
fakeDash . HasACL = false
2019-03-06 07:38:40 -06:00
2022-05-23 10:14:27 -05:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2023-01-25 03:36:26 -06:00
dashboardService . On ( "GetDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardQuery" ) ) . Return ( fakeDash , nil )
2022-05-23 10:14:27 -05:00
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 09:33:55 -06:00
cmd . Dashboard = & dashboards . Dashboard {
ID : 2 , UID : "uid" , Title : "Dash" , Slug : "dash" , Version : 1 ,
2022-05-23 10:14:27 -05:00
}
} ) . Return ( nil , nil )
2019-03-06 07:38:40 -06:00
2022-05-25 03:41:51 -05:00
fakeDashboardVersionService := dashvertest . NewDashboardVersionServiceFake ( )
2022-12-27 10:17:24 -06:00
fakeDashboardVersionService . ExpectedDashboardVersions = [ ] * dashver . DashboardVersionDTO {
2022-05-25 03:41:51 -05:00
{
DashboardID : 2 ,
Version : 1 ,
Data : fakeDash . Data ,
} }
2019-03-06 07:38:40 -06:00
cmd := dtos . RestoreDashboardVersionCommand {
Version : 1 ,
}
2023-01-18 09:01:25 -06:00
mockSQLStore := dbtest . NewFakeDB ( )
2020-11-13 02:52:38 -06:00
restoreDashboardVersionScenario ( t , "When calling POST on" , "/api/dashboards/id/1/restore" ,
2022-05-25 03:41:51 -05:00
"/api/dashboards/id/:dashboardId/restore" , dashboardService , fakeDashboardVersionService , cmd , func ( sc * scenarioContext ) {
2020-11-13 02:52:38 -06:00
callRestoreDashboardVersion ( sc )
assert . Equal ( t , 200 , sc . resp . Code )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2019-03-06 07:38:40 -06:00
} )
2019-04-10 06:29:10 -05:00
2020-11-13 02:52:38 -06:00
t . Run ( "Given provisioned dashboard" , func ( t * testing . T ) {
2023-01-18 09:01:25 -06:00
mockSQLStore := dbtest . NewFakeDB ( )
2022-06-01 13:16:26 -05:00
dashboardStore := dashboards . NewFakeDashboardStore ( t )
2023-01-18 06:52:41 -06:00
dashboardStore . On ( "GetProvisionedDataByDashboardID" , mock . Anything , mock . AnythingOfType ( "int64" ) ) . Return ( & dashboards . DashboardProvisioning { ExternalID : "/dashboard1.json" } , nil ) . Once ( )
2022-06-01 13:16:26 -05:00
2022-09-20 11:58:04 -05:00
teamService := & teamtest . FakeService { }
2022-06-01 13:16:26 -05:00
dashboardService := dashboards . NewFakeDashboardService ( t )
2022-02-16 07:15:44 -06:00
2022-02-07 05:43:43 -06:00
dataValue , err := simplejson . NewJson ( [ ] byte ( ` { "id": 1, "editable": true, "style": "dark"} ` ) )
require . NoError ( t , err )
2023-01-25 03:36:26 -06:00
qResult := & dashboards . Dashboard { ID : 1 , Data : dataValue }
dashboardService . On ( "GetDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardQuery" ) ) . Return ( qResult , nil )
2023-01-26 07:46:30 -06:00
qResult2 := [ ] * dashboards . DashboardACLInfoDTO { { OrgID : testOrgID , DashboardID : 1 , UserID : testUserID , Permission : dashboards . PERMISSION_EDIT } }
2023-01-25 03:36:26 -06:00
dashboardService . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Return ( qResult2 , nil )
2023-03-16 04:54:01 -05:00
guardian . InitLegacyGuardian ( setting . NewCfg ( ) , mockSQLStore , dashboardService , teamService )
2019-04-30 06:32:18 -05:00
2022-08-10 04:56:48 -05:00
loggedInUserScenarioWithRole ( t , "When calling GET on" , "GET" , "/api/dashboards/uid/dash" , "/api/dashboards/uid/:uid" , org . RoleEditor , func ( sc * scenarioContext ) {
2022-02-16 07:15:44 -06:00
fakeProvisioningService := provisioning . NewProvisioningServiceMock ( context . Background ( ) )
fakeProvisioningService . GetDashboardProvisionerResolvedPathFunc = func ( name string ) string {
2019-04-30 06:32:18 -05:00
return "/tmp/grafana/dashboards"
}
2023-01-19 10:38:07 -06:00
dash := getDashboardShouldReturn200WithConfig ( t , sc , fakeProvisioningService , dashboardStore , dashboardService , nil )
2022-02-16 07:15:44 -06:00
assert . Equal ( t , "../../../dashboard1.json" , dash . Meta . ProvisionedExternalId , mockSQLStore )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2019-10-31 08:27:31 -05:00
2022-08-10 04:56:48 -05: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 07:15:44 -06:00
fakeProvisioningService := provisioning . NewProvisioningServiceMock ( context . Background ( ) )
fakeProvisioningService . GetDashboardProvisionerResolvedPathFunc = func ( name string ) string {
2019-10-31 08:27:31 -05:00
return "/tmp/grafana/dashboards"
}
2022-02-16 07:15:44 -06:00
fakeProvisioningService . GetAllowUIUpdatesFromConfigFunc = func ( name string ) bool {
2019-10-31 08:27:31 -05:00
return true
}
hs := & HTTPServer {
2022-02-16 07:15:44 -06:00
Cfg : setting . NewCfg ( ) ,
ProvisioningService : fakeProvisioningService ,
LibraryPanelService : & mockLibraryPanelService { } ,
LibraryElementService : & mockLibraryElementService { } ,
dashboardProvisioningService : mockDashboardProvisioningService { } ,
SQLStore : mockSQLStore ,
2022-04-04 09:57:43 -05:00
AccessControl : accesscontrolmock . New ( ) ,
2022-07-06 13:42:39 -05:00
DashboardService : dashboardService ,
2022-07-19 18:44:41 -05: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-10 14:36:40 -06:00
Kinds : corekind . NewBase ( nil ) ,
2019-10-31 08:27:31 -05:00
}
2022-02-07 05:43:43 -06:00
hs . callGetDashboard ( sc )
2019-10-31 08:27:31 -05:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , 200 , sc . resp . Code )
2019-10-31 08:27:31 -05:00
dash := dtos . DashboardFullWithMeta { }
err := json . NewDecoder ( sc . resp . Body ) . Decode ( & dash )
2020-11-13 02:52:38 -06:00
require . NoError ( t , err )
2019-10-31 08:27:31 -05:00
2020-11-13 02:52:38 -06:00
assert . Equal ( t , false , dash . Meta . Provisioned )
2022-02-07 05:43:43 -06:00
} , mockSQLStore )
2019-04-10 06:29:10 -05:00
} )
2017-06-12 08:48:55 -05:00
}
2017-06-12 16:05:32 -05:00
2023-03-30 09:31:53 -05:00
func TestDashboardVersionsAPIEndpoint ( t * testing . T ) {
fakeDash := dashboards . NewDashboard ( "Child dash" )
fakeDash . ID = 1
fakeDash . FolderID = 1
fakeDash . HasACL = false
fakeDashboardVersionService := dashvertest . NewDashboardVersionServiceFake ( )
dashboardService := dashboards . NewFakeDashboardService ( t )
dashboardService . On ( "GetDashboard" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardQuery" ) ) . Return ( fakeDash , nil )
teamService := & teamtest . FakeService { }
mockSQLStore := dbtest . NewFakeDB ( )
cfg := setting . NewCfg ( )
getHS := func ( userSvc * usertest . FakeUserService ) * HTTPServer {
return & HTTPServer {
Cfg : cfg ,
pluginStore : & plugins . FakePluginStore { } ,
SQLStore : mockSQLStore ,
AccessControl : accesscontrolmock . New ( ) ,
Features : featuremgmt . WithFeatures ( ) ,
DashboardService : dashboardService ,
dashboardVersionService : fakeDashboardVersionService ,
Kinds : corekind . NewBase ( nil ) ,
QuotaService : quotatest . New ( false , nil ) ,
userService : userSvc ,
CacheService : localcache . New ( 5 * time . Minute , 10 * time . Minute ) ,
log : log . New ( ) ,
}
}
setUp := func ( ) {
viewerRole := org . RoleViewer
editorRole := org . RoleEditor
qResult := [ ] * dashboards . DashboardACLInfoDTO {
{ Role : & viewerRole , Permission : dashboards . PERMISSION_VIEW } ,
{ Role : & editorRole , Permission : dashboards . PERMISSION_EDIT } ,
}
dashboardService . On ( "GetDashboardACLInfoList" , mock . Anything , mock . AnythingOfType ( "*dashboards.GetDashboardACLInfoListQuery" ) ) . Return ( qResult , nil )
guardian . InitLegacyGuardian ( cfg , mockSQLStore , dashboardService , teamService )
}
loggedInUserScenarioWithRole ( t , "When user exists and calling GET on" , "GET" , "/api/dashboards/id/2/versions" ,
"/api/dashboards/id/:dashboardId/versions" , org . RoleEditor , func ( sc * scenarioContext ) {
setUp ( )
fakeDashboardVersionService . ExpectedListDashboarVersions = [ ] * dashver . DashboardVersionDTO {
{
Version : 1 ,
CreatedBy : 1 ,
} ,
{
Version : 2 ,
CreatedBy : 1 ,
} ,
}
getHS ( & usertest . FakeUserService {
ExpectedUser : & user . User { ID : 1 , Login : "test-user" } ,
} ) . callGetDashboardVersions ( sc )
assert . Equal ( t , 200 , sc . resp . Code )
var versions [ ] dashver . DashboardVersionMeta
err := json . NewDecoder ( sc . resp . Body ) . Decode ( & versions )
require . NoError ( t , err )
for _ , v := range versions {
assert . Equal ( t , "test-user" , v . CreatedBy )
}
} , mockSQLStore )
loggedInUserScenarioWithRole ( t , "When user does not exist and calling GET on" , "GET" , "/api/dashboards/id/2/versions" ,
"/api/dashboards/id/:dashboardId/versions" , org . RoleEditor , func ( sc * scenarioContext ) {
setUp ( )
fakeDashboardVersionService . ExpectedListDashboarVersions = [ ] * dashver . DashboardVersionDTO {
{
Version : 1 ,
CreatedBy : 1 ,
} ,
{
Version : 2 ,
CreatedBy : 1 ,
} ,
}
getHS ( & usertest . FakeUserService {
ExpectedError : user . ErrUserNotFound ,
} ) . callGetDashboardVersions ( sc )
assert . Equal ( t , 200 , sc . resp . Code )
var versions [ ] dashver . DashboardVersionMeta
err := json . NewDecoder ( sc . resp . Body ) . Decode ( & versions )
require . NoError ( t , err )
for _ , v := range versions {
assert . Equal ( t , anonString , v . CreatedBy )
}
} , mockSQLStore )
loggedInUserScenarioWithRole ( t , "When failing to get user and calling GET on" , "GET" , "/api/dashboards/id/2/versions" ,
"/api/dashboards/id/:dashboardId/versions" , org . RoleEditor , func ( sc * scenarioContext ) {
setUp ( )
fakeDashboardVersionService . ExpectedListDashboarVersions = [ ] * dashver . DashboardVersionDTO {
{
Version : 1 ,
CreatedBy : 1 ,
} ,
{
Version : 2 ,
CreatedBy : 1 ,
} ,
}
getHS ( & usertest . FakeUserService {
ExpectedError : fmt . Errorf ( "some error" ) ,
} ) . callGetDashboardVersions ( sc )
assert . Equal ( t , 200 , sc . resp . Code )
var versions [ ] dashver . DashboardVersionMeta
err := json . NewDecoder ( sc . resp . Body ) . Decode ( & versions )
require . NoError ( t , err )
for _ , v := range versions {
assert . Equal ( t , anonString , v . CreatedBy )
}
} , mockSQLStore )
}
2023-02-01 07:43:21 -06:00
func getDashboardShouldReturn200WithConfig ( t * testing . T , sc * scenarioContext , provisioningService provisioning . ProvisioningService , dashboardStore dashboards . Store , dashboardService dashboards . DashboardService , folderStore folder . FolderStore ) dtos . DashboardFullWithMeta {
2022-02-16 07:15:44 -06:00
t . Helper ( )
2019-04-30 06:32:18 -05:00
if provisioningService == nil {
2021-11-03 05:31:56 -05:00
provisioningService = provisioning . NewProvisioningServiceMock ( context . Background ( ) )
2019-04-30 06:32:18 -05:00
}
2022-11-14 13:08:10 -06:00
var err error
2022-02-16 07:15:44 -06:00
if dashboardStore == nil {
2022-10-19 08:02:15 -05:00
sql := db . InitTestDB ( t )
2022-11-14 13:08:10 -06: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 07:15:44 -06:00
}
2021-05-12 01:48:17 -05:00
libraryPanelsService := mockLibraryPanelService { }
libraryElementsService := mockLibraryElementService { }
2022-03-10 05:58:18 -06:00
cfg := setting . NewCfg ( )
2022-06-07 04:02:20 -05:00
ac := accesscontrolmock . New ( )
folderPermissions := accesscontrolmock . NewMockedPermissionsService ( )
dashboardPermissions := accesscontrolmock . NewMockedPermissionsService ( )
2022-03-10 05:58:18 -06:00
features := featuremgmt . WithFeatures ( )
2021-05-12 01:48:17 -05:00
2023-03-02 07:09:57 -06:00
folderSvc := folderimpl . ProvideService ( ac , bus . ProvideBus ( tracing . InitializeTracerForTest ( ) ) ,
cfg , dashboardStore , folderStore , db . InitTestDB ( t ) , featuremgmt . WithFeatures ( ) )
2023-01-26 02:21:10 -06:00
2022-05-23 10:14:27 -05:00
if dashboardService == nil {
2023-04-14 04:17:23 -05:00
dashboardService , err = service . ProvideDashboardServiceImpl (
2023-03-03 09:56:33 -06:00
cfg , dashboardStore , folderStore , nil , features , folderPermissions , dashboardPermissions ,
2023-03-02 07:09:57 -06:00
ac , folderSvc ,
2022-06-07 04:02:20 -05:00
)
2023-04-14 04:17:23 -05:00
require . NoError ( t , err )
2022-05-23 10:14:27 -05:00
}
2023-04-14 04:17:23 -05:00
dashboardProvisioningService , err := service . ProvideDashboardServiceImpl (
cfg , dashboardStore , folderStore , nil , features , folderPermissions , dashboardPermissions ,
ac , folderSvc ,
)
require . NoError ( t , err )
2019-04-30 06:32:18 -05:00
hs := & HTTPServer {
2023-04-14 04:17:23 -05:00
Cfg : cfg ,
LibraryPanelService : & libraryPanelsService ,
LibraryElementService : & libraryElementsService ,
SQLStore : sc . sqlStore ,
ProvisioningService : provisioningService ,
AccessControl : accesscontrolmock . New ( ) ,
dashboardProvisioningService : dashboardProvisioningService ,
DashboardService : dashboardService ,
Features : featuremgmt . WithFeatures ( ) ,
Kinds : corekind . NewBase ( nil ) ,
2019-04-30 06:32:18 -05:00
}
2021-05-12 01:48:17 -05:00
2022-02-07 05:43:43 -06:00
hs . callGetDashboard ( sc )
2017-06-12 16:05:32 -05:00
2020-11-13 02:52:38 -06:00
require . Equal ( sc . t , 200 , sc . resp . Code )
2017-06-12 16:05:32 -05:00
dash := dtos . DashboardFullWithMeta { }
2022-11-14 13:08:10 -06:00
err = json . NewDecoder ( sc . resp . Body ) . Decode ( & dash )
2020-11-13 02:52:38 -06:00
require . NoError ( sc . t , err )
2017-06-12 16:05:32 -05:00
return dash
}
2022-02-07 05:43:43 -06:00
func ( hs * HTTPServer ) callGetDashboard ( sc * scenarioContext ) {
2019-04-30 06:32:18 -05:00
sc . handlerFunc = hs . GetDashboard
2018-01-29 06:51:01 -06:00
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
}
2022-02-07 05:43:43 -06:00
func ( hs * HTTPServer ) callGetDashboardVersion ( sc * scenarioContext ) {
sc . handlerFunc = hs . GetDashboardVersion
2017-06-13 17:28:34 -05:00
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
}
2022-02-07 05:43:43 -06:00
func ( hs * HTTPServer ) callGetDashboardVersions ( sc * scenarioContext ) {
sc . handlerFunc = hs . GetDashboardVersions
2017-06-13 17:28:34 -05:00
sc . fakeReqWithParams ( "GET" , sc . url , map [ string ] string { } ) . exec ( )
}
2022-02-16 07:15:44 -06:00
func ( hs * HTTPServer ) callDeleteDashboardByUID ( t * testing . T ,
2023-03-08 14:54:35 -06:00
sc * scenarioContext , mockDashboard * dashboards . FakeDashboardService , mockPubdashService * publicdashboards . FakePublicDashboardService ) {
2022-07-06 13:42:39 -05:00
hs . DashboardService = mockDashboard
2023-03-08 14:54:35 -06:00
pubdashApi := api . ProvideApi ( mockPubdashService , nil , nil , featuremgmt . WithFeatures ( ) )
hs . PublicDashboardsApi = pubdashApi
2021-01-20 02:28:10 -06:00
sc . handlerFunc = hs . DeleteDashboardByUID
2018-01-31 09:46:31 -06:00
sc . fakeReqWithParams ( "DELETE" , sc . url , map [ string ] string { } ) . exec ( )
}
2020-11-13 02:52:38 -06:00
func callPostDashboard ( sc * scenarioContext ) {
2017-06-12 16:05:32 -05:00
sc . fakeReqWithParams ( "POST" , sc . url , map [ string ] string { } ) . exec ( )
}
2020-11-13 02:52:38 -06:00
func callRestoreDashboardVersion ( sc * scenarioContext ) {
2019-03-06 07:38:40 -06:00
sc . fakeReqWithParams ( "POST" , sc . url , map [ string ] string { } ) . exec ( )
}
2020-11-13 02:52:38 -06:00
func callPostDashboardShouldReturnSuccess ( sc * scenarioContext ) {
callPostDashboard ( sc )
2018-01-30 16:37:54 -06:00
2020-11-13 02:52:38 -06:00
assert . Equal ( sc . t , 200 , sc . resp . Code )
2018-01-30 16:37:54 -06:00
}
2023-01-18 06:52:41 -06: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 02:52:38 -06:00
t . Run ( fmt . Sprintf ( "%s %s" , desc , url ) , func ( t * testing . T ) {
2020-12-15 12:09:04 -06:00
cfg := setting . NewCfg ( )
2019-02-11 14:12:01 -06:00
hs := HTTPServer {
2022-11-14 13:08:10 -06:00
Cfg : cfg ,
ProvisioningService : provisioning . NewProvisioningServiceMock ( context . Background ( ) ) ,
Live : newTestLive ( t , db . InitTestDB ( t ) ) ,
QuotaService : quotatest . New ( false , nil ) ,
2022-09-28 01:29:35 -05:00
pluginStore : & plugins . FakePluginStore { } ,
2021-05-12 01:48:17 -05:00
LibraryPanelService : & mockLibraryPanelService { } ,
LibraryElementService : & mockLibraryElementService { } ,
2022-07-06 13:42:39 -05:00
DashboardService : dashboardService ,
2022-02-16 07:15:44 -06:00
folderService : folderService ,
2022-03-03 08:05:47 -06: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-10 14:36:40 -06:00
Kinds : corekind . NewBase ( nil ) ,
2022-11-24 08:38:55 -06:00
accesscontrolService : actest . FakeService { } ,
2019-02-11 14:12:01 -06:00
}
2020-11-13 02:52:38 -06:00
sc := setupScenarioContext ( t , url )
2023-01-27 01:50:36 -06:00
sc . defaultHandler = routing . Wrap ( func ( c * contextmodel . ReqContext ) response . Response {
2021-11-29 03:18:01 -06:00
c . Req . Body = mockRequestBody ( cmd )
2022-02-09 06:44:38 -06:00
c . Req . Header . Add ( "Content-Type" , "application/json" )
2017-06-12 16:05:32 -05:00
sc . context = c
2023-01-18 06:52:41 -06:00
sc . context . SignedInUser = & user . SignedInUser { OrgID : cmd . OrgID , UserID : cmd . UserID }
2017-06-12 16:05:32 -05:00
2021-11-29 03:18:01 -06:00
return hs . PostDashboard ( c )
2017-06-12 16:05:32 -05:00
} )
sc . m . Post ( routePattern , sc . defaultHandler )
fn ( sc )
} )
}
2018-01-29 12:27:53 -06:00
2023-01-18 06:52:41 -06:00
func postValidateScenario ( t * testing . T , desc string , url string , routePattern string , cmd dashboards . ValidateDashboardCommand ,
2022-12-13 04:03:36 -06:00
role org . RoleType , fn scenarioFunc , sqlmock db . DB ) {
2022-10-14 08:51:05 -05: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 08:02:15 -05:00
Live : newTestLive ( t , db . InitTestDB ( t ) ) ,
2022-11-14 13:08:10 -06:00
QuotaService : quotatest . New ( false , nil ) ,
2022-10-14 08:51:05 -05: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-10 14:36:40 -06:00
Kinds : corekind . NewBase ( nil ) ,
2022-10-14 08:51:05 -05:00
}
sc := setupScenarioContext ( t , url )
2023-01-27 01:50:36 -06:00
sc . defaultHandler = routing . Wrap ( func ( c * contextmodel . ReqContext ) response . Response {
2022-10-14 08:51:05 -05:00
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 03:41:51 -05:00
func postDiffScenario ( t * testing . T , desc string , url string , routePattern string , cmd dtos . CalculateDiffOptions ,
2022-12-13 04:03:36 -06:00
role org . RoleType , fn scenarioFunc , sqlmock db . DB , fakeDashboardVersionService * dashvertest . FakeDashboardVersionService ) {
2020-11-13 02:52:38 -06:00
t . Run ( fmt . Sprintf ( "%s %s" , desc , url ) , func ( t * testing . T ) {
2022-02-10 02:58:52 -06:00
cfg := setting . NewCfg ( )
2022-12-15 08:34:17 -06:00
dashSvc := dashboards . NewFakeDashboardService ( t )
2022-02-10 02:58:52 -06:00
hs := HTTPServer {
2022-05-25 03:41:51 -05:00
Cfg : cfg ,
ProvisioningService : provisioning . NewProvisioningServiceMock ( context . Background ( ) ) ,
2022-10-19 08:02:15 -05:00
Live : newTestLive ( t , db . InitTestDB ( t ) ) ,
2022-11-14 13:08:10 -06:00
QuotaService : quotatest . New ( false , nil ) ,
2022-05-25 03:41:51 -05:00
LibraryPanelService : & mockLibraryPanelService { } ,
LibraryElementService : & mockLibraryElementService { } ,
SQLStore : sqlmock ,
dashboardVersionService : fakeDashboardVersionService ,
2022-07-19 18:44:41 -05: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-10 14:36:40 -06:00
Kinds : corekind . NewBase ( nil ) ,
2022-12-15 08:34:17 -06:00
DashboardService : dashSvc ,
2022-02-10 02:58:52 -06:00
}
2020-11-13 02:52:38 -06:00
sc := setupScenarioContext ( t , url )
2023-01-27 01:50:36 -06:00
sc . defaultHandler = routing . Wrap ( func ( c * contextmodel . ReqContext ) response . Response {
2021-11-29 03:18:01 -06:00
c . Req . Body = mockRequestBody ( cmd )
2022-02-09 06:44:38 -06:00
c . Req . Header . Add ( "Content-Type" , "application/json" )
2018-02-27 10:53:30 -06:00
sc . context = c
2022-08-10 04:56:48 -05:00
sc . context . SignedInUser = & user . SignedInUser {
2022-08-11 06:28:55 -05:00
OrgID : testOrgID ,
UserID : testUserID ,
2018-02-27 10:53:30 -06:00
}
sc . context . OrgRole = role
2022-02-10 02:58:52 -06:00
return hs . CalculateDashboardDiff ( c )
2018-02-27 10:53:30 -06:00
} )
sc . m . Post ( routePattern , sc . defaultHandler )
fn ( sc )
} )
}
2022-05-25 03:41:51 -05:00
func restoreDashboardVersionScenario ( t * testing . T , desc string , url string , routePattern string ,
mock * dashboards . FakeDashboardService , fakeDashboardVersionService * dashvertest . FakeDashboardVersionService ,
2022-12-13 04:03:36 -06:00
cmd dtos . RestoreDashboardVersionCommand , fn scenarioFunc , sqlStore db . DB ) {
2020-11-13 02:52:38 -06:00
t . Run ( fmt . Sprintf ( "%s %s" , desc , url ) , func ( t * testing . T ) {
2020-12-15 12:09:04 -06:00
cfg := setting . NewCfg ( )
2019-03-06 07:38:40 -06:00
hs := HTTPServer {
2022-05-25 03:41:51 -05:00
Cfg : cfg ,
ProvisioningService : provisioning . NewProvisioningServiceMock ( context . Background ( ) ) ,
2022-10-19 08:02:15 -05:00
Live : newTestLive ( t , db . InitTestDB ( t ) ) ,
2022-11-14 13:08:10 -06:00
QuotaService : quotatest . New ( false , nil ) ,
2022-05-25 03:41:51 -05:00
LibraryPanelService : & mockLibraryPanelService { } ,
LibraryElementService : & mockLibraryElementService { } ,
2022-07-06 13:42:39 -05:00
DashboardService : mock ,
2022-05-25 03:41:51 -05: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-10 14:36:40 -06:00
Kinds : corekind . NewBase ( nil ) ,
2022-11-24 08:38:55 -06:00
accesscontrolService : actest . FakeService { } ,
2019-03-06 07:38:40 -06:00
}
2020-11-13 02:52:38 -06:00
sc := setupScenarioContext ( t , url )
2022-05-17 11:57:27 -05:00
sc . sqlStore = sqlStore
2022-05-25 03:41:51 -05:00
sc . dashboardVersionService = fakeDashboardVersionService
2023-01-27 01:50:36 -06:00
sc . defaultHandler = routing . Wrap ( func ( c * contextmodel . ReqContext ) response . Response {
2021-11-29 03:18:01 -06:00
c . Req . Body = mockRequestBody ( cmd )
2022-02-09 06:44:38 -06:00
c . Req . Header . Add ( "Content-Type" , "application/json" )
2019-03-06 07:38:40 -06:00
sc . context = c
2022-08-10 04:56:48 -05:00
sc . context . SignedInUser = & user . SignedInUser {
2022-08-11 06:28:55 -05:00
OrgID : testOrgID ,
UserID : testUserID ,
2019-03-06 07:38:40 -06:00
}
2022-08-10 04:56:48 -05:00
sc . context . OrgRole = org . RoleAdmin
2019-03-06 07:38:40 -06:00
2021-11-29 03:18:01 -06:00
return hs . RestoreDashboardVersion ( c )
2019-03-06 07:38:40 -06:00
} )
sc . m . Post ( routePattern , sc . defaultHandler )
fn ( sc )
} )
}
2018-03-22 16:13:46 -05:00
func ( sc * scenarioContext ) ToJSON ( ) * simplejson . Json {
2022-04-12 02:30:34 -05:00
result := simplejson . New ( )
err := json . NewDecoder ( sc . resp . Body ) . Decode ( result )
2020-11-13 02:52:38 -06:00
require . NoError ( sc . t , err )
2018-01-29 12:27:53 -06:00
return result
}
2019-10-31 08:27:31 -05:00
type mockDashboardProvisioningService struct {
2021-03-12 04:51:02 -06:00
dashboards . DashboardProvisioningService
2019-10-31 08:27:31 -05:00
}
2022-09-21 07:04:01 -05:00
func ( s mockDashboardProvisioningService ) GetProvisionedDashboardDataByDashboardID ( ctx context . Context , dashboardID int64 ) (
2023-01-18 06:52:41 -06:00
* dashboards . DashboardProvisioning , error ) {
2021-03-17 10:06:10 -05:00
return nil , nil
2019-10-31 08:27:31 -05:00
}
2021-05-12 01:48:17 -05:00
type mockLibraryPanelService struct {
}
2023-01-16 09:33:55 -06:00
func ( m * mockLibraryPanelService ) ConnectLibraryPanelsForDashboard ( c context . Context , signedInUser * user . SignedInUser , dash * dashboards . Dashboard ) error {
2021-05-12 01:48:17 -05:00
return nil
}
2022-08-10 04:56:48 -05:00
func ( m * mockLibraryPanelService ) ImportLibraryPanelsForDashboard ( c context . Context , signedInUser * user . SignedInUser , libraryPanels * simplejson . Json , panels [ ] interface { } , folderID int64 ) error {
2021-09-20 03:58:24 -05:00
return nil
}
2021-05-12 01:48:17 -05:00
type mockLibraryElementService struct {
}
2023-02-01 10:32:05 -06:00
func ( l * mockLibraryElementService ) CreateElement ( c context . Context , signedInUser * user . SignedInUser , cmd model . CreateLibraryElementCommand ) ( model . LibraryElementDTO , error ) {
return model . LibraryElementDTO { } , nil
2021-09-20 03:58:24 -05:00
}
// GetElement gets an element from a UID.
2023-02-01 10:32:05 -06:00
func ( l * mockLibraryElementService ) GetElement ( c context . Context , signedInUser * user . SignedInUser , UID string ) ( model . LibraryElementDTO , error ) {
return model . LibraryElementDTO { } , nil
2021-05-12 01:48:17 -05:00
}
// GetElementsForDashboard gets all connected elements for a specific dashboard.
2023-02-01 10:32:05 -06:00
func ( l * mockLibraryElementService ) GetElementsForDashboard ( c context . Context , dashboardID int64 ) ( map [ string ] model . LibraryElementDTO , error ) {
return map [ string ] model . LibraryElementDTO { } , nil
2021-05-12 01:48:17 -05:00
}
// ConnectElementsToDashboard connects elements to a specific dashboard.
2022-08-10 04:56:48 -05:00
func ( l * mockLibraryElementService ) ConnectElementsToDashboard ( c context . Context , signedInUser * user . SignedInUser , elementUIDs [ ] string , dashboardID int64 ) error {
2021-05-12 01:48:17 -05:00
return nil
}
// DisconnectElementsFromDashboard disconnects elements from a specific dashboard.
2021-09-27 02:04:36 -05:00
func ( l * mockLibraryElementService ) DisconnectElementsFromDashboard ( c context . Context , dashboardID int64 ) error {
2021-05-12 01:48:17 -05:00
return nil
}
// DeleteLibraryElementsInFolder deletes all elements for a specific folder.
2022-08-10 04:56:48 -05:00
func ( l * mockLibraryElementService ) DeleteLibraryElementsInFolder ( c context . Context , signedInUser * user . SignedInUser , folderUID string ) error {
2021-05-12 01:48:17 -05:00
return nil
}