| 
									
										
										
										
											2020-06-17 13:39:50 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							| 
									
										
										
										
											2023-02-08 00:20:05 +08:00
										 |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2020-06-17 13:39:50 +08:00
										 |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"net/http/httptest" | 
					
						
							| 
									
										
										
										
											2020-10-19 23:35:31 +08:00
										 |  |  | 	"path/filepath" | 
					
						
							| 
									
										
										
										
											2020-06-17 13:39:50 +08:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-19 21:02:15 +08:00
										 |  |  | 	"github.com/stretchr/testify/assert" | 
					
						
							|  |  |  | 	"github.com/stretchr/testify/require" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/infra/db" | 
					
						
							| 
									
										
										
										
											2023-06-17 00:29:09 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/remotecache" | 
					
						
							| 
									
										
										
										
											2023-02-07 00:23:53 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/usagestats" | 
					
						
							| 
									
										
										
										
											2023-01-30 16:18:26 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/login/social" | 
					
						
							| 
									
										
										
										
											2022-09-28 14:29:35 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/plugins" | 
					
						
							| 
									
										
										
										
											2023-01-30 16:18:26 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/plugins/config" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/plugins/pluginscdn" | 
					
						
							| 
									
										
										
										
											2021-10-05 21:54:26 +08:00
										 |  |  | 	accesscontrolmock "github.com/grafana/grafana/pkg/services/accesscontrol/mock" | 
					
						
							| 
									
										
										
										
											2022-01-27 01:44:20 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/featuremgmt" | 
					
						
							| 
									
										
										
										
											2020-06-17 13:39:50 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/licensing" | 
					
						
							| 
									
										
										
										
											2023-03-08 00:22:30 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginsettings" | 
					
						
							| 
									
										
										
										
											2023-09-11 19:59:24 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore" | 
					
						
							| 
									
										
										
										
											2021-10-05 21:54:26 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/rendering" | 
					
						
							| 
									
										
										
										
											2023-03-16 15:46:25 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/supportbundles/supportbundlestest" | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/updatechecker" | 
					
						
							| 
									
										
										
										
											2020-06-17 13:39:50 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/web" | 
					
						
							| 
									
										
										
										
											2020-06-17 13:39:50 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-11 19:59:24 +08:00
										 |  |  | func setupTestEnvironment(t *testing.T, cfg *setting.Cfg, features *featuremgmt.FeatureManager, pstore pluginstore.Store, psettings pluginsettings.Service) (*web.Mux, *HTTPServer) { | 
					
						
							| 
									
										
										
										
											2020-06-17 13:39:50 +08:00
										 |  |  | 	t.Helper() | 
					
						
							| 
									
										
										
										
											2022-10-19 21:02:15 +08:00
										 |  |  | 	db.InitTestDB(t) | 
					
						
							| 
									
										
										
										
											2022-01-27 01:44:20 +08:00
										 |  |  | 	cfg.IsFeatureToggleEnabled = features.IsEnabled | 
					
						
							| 
									
										
										
										
											2020-06-17 13:39:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		oldVersion := setting.BuildVersion | 
					
						
							|  |  |  | 		oldCommit := setting.BuildCommit | 
					
						
							|  |  |  | 		oldEnv := setting.Env | 
					
						
							|  |  |  | 		t.Cleanup(func() { | 
					
						
							|  |  |  | 			setting.BuildVersion = oldVersion | 
					
						
							|  |  |  | 			setting.BuildCommit = oldCommit | 
					
						
							|  |  |  | 			setting.Env = oldEnv | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-08 00:20:05 +08:00
										 |  |  | 	var pluginStore = pstore | 
					
						
							|  |  |  | 	if pluginStore == nil { | 
					
						
							| 
									
										
										
										
											2023-09-11 19:59:24 +08:00
										 |  |  | 		pluginStore = &pluginstore.FakePluginStore{} | 
					
						
							| 
									
										
										
										
											2023-02-08 00:20:05 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var pluginsSettings = psettings | 
					
						
							|  |  |  | 	if pluginsSettings == nil { | 
					
						
							| 
									
										
										
										
											2023-03-08 00:22:30 +08:00
										 |  |  | 		pluginsSettings = &pluginsettings.FakePluginSettings{} | 
					
						
							| 
									
										
										
										
											2023-02-08 00:20:05 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2020-06-17 13:39:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	hs := &HTTPServer{ | 
					
						
							| 
									
										
										
										
											2022-01-27 01:44:20 +08:00
										 |  |  | 		Cfg:      cfg, | 
					
						
							|  |  |  | 		Features: features, | 
					
						
							|  |  |  | 		License:  &licensing.OSSLicensingService{Cfg: cfg}, | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 		RenderService: &rendering.RenderingService{ | 
					
						
							|  |  |  | 			Cfg:                   cfg, | 
					
						
							|  |  |  | 			RendererPluginManager: &fakeRendererManager{}, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2023-02-08 00:20:05 +08:00
										 |  |  | 		SQLStore:             db.InitTestDB(t), | 
					
						
							| 
									
										
										
										
											2022-01-31 23:06:16 +08:00
										 |  |  | 		SettingsProvider:     setting.ProvideProvider(cfg), | 
					
						
							| 
									
										
										
										
											2023-02-08 00:20:05 +08:00
										 |  |  | 		pluginStore:          pluginStore, | 
					
						
							| 
									
										
										
										
											2022-01-31 23:06:16 +08:00
										 |  |  | 		grafanaUpdateChecker: &updatechecker.GrafanaService{}, | 
					
						
							| 
									
										
										
										
											2023-09-05 18:04:39 +08:00
										 |  |  | 		AccessControl:        accesscontrolmock.New(), | 
					
						
							| 
									
										
										
										
											2023-02-08 00:20:05 +08:00
										 |  |  | 		PluginSettings:       pluginsSettings, | 
					
						
							| 
									
										
										
										
											2023-01-27 22:08:17 +08:00
										 |  |  | 		pluginsCDNService: pluginscdn.ProvideService(&config.Cfg{ | 
					
						
							|  |  |  | 			PluginsCDNURLTemplate: cfg.PluginsCDNURLTemplate, | 
					
						
							|  |  |  | 			PluginSettings:        cfg.PluginSettings, | 
					
						
							|  |  |  | 		}), | 
					
						
							| 
									
										
										
										
											2023-06-17 00:29:09 +08:00
										 |  |  | 		SocialService: social.ProvideService(cfg, features, &usagestats.UsageStatsMock{}, supportbundlestest.NewFakeBundleService(), remotecache.NewFakeCacheStorage()), | 
					
						
							| 
									
										
										
										
											2020-06-17 13:39:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	m := web.New() | 
					
						
							| 
									
										
										
										
											2020-12-16 02:09:04 +08:00
										 |  |  | 	m.Use(getContextHandler(t, cfg).Middleware) | 
					
						
							| 
									
										
										
										
											2021-10-11 20:30:59 +08:00
										 |  |  | 	m.UseMiddleware(web.Renderer(filepath.Join(setting.StaticRootPath, "views"), "[[", "]]")) | 
					
						
							| 
									
										
										
										
											2020-06-17 13:39:50 +08:00
										 |  |  | 	m.Get("/api/frontend/settings/", hs.GetFrontendSettings) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return m, hs | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | func TestHTTPServer_GetFrontendSettings_hideVersionAnonymous(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2020-06-17 13:39:50 +08:00
										 |  |  | 	type buildInfo struct { | 
					
						
							|  |  |  | 		Version string `json:"version"` | 
					
						
							|  |  |  | 		Commit  string `json:"commit"` | 
					
						
							|  |  |  | 		Env     string `json:"env"` | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	type settings struct { | 
					
						
							|  |  |  | 		BuildInfo buildInfo `json:"buildInfo"` | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cfg := setting.NewCfg() | 
					
						
							| 
									
										
										
										
											2021-02-10 20:31:47 +08:00
										 |  |  | 	cfg.Env = "testing" | 
					
						
							|  |  |  | 	cfg.BuildVersion = "7.8.9" | 
					
						
							|  |  |  | 	cfg.BuildCommit = "01234567" | 
					
						
							| 
									
										
										
										
											2022-01-27 01:44:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-08 00:20:05 +08:00
										 |  |  | 	m, hs := setupTestEnvironment(t, cfg, featuremgmt.WithFeatures(), nil, nil) | 
					
						
							| 
									
										
										
										
											2020-06-17 13:39:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	req := httptest.NewRequest(http.MethodGet, "/api/frontend/settings", nil) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-10 20:31:47 +08:00
										 |  |  | 	// TODO: Remove
 | 
					
						
							|  |  |  | 	setting.BuildVersion = cfg.BuildVersion | 
					
						
							|  |  |  | 	setting.BuildCommit = cfg.BuildCommit | 
					
						
							|  |  |  | 	setting.Env = cfg.Env | 
					
						
							| 
									
										
										
										
											2020-06-17 13:39:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 		desc        string | 
					
						
							| 
									
										
										
										
											2020-06-17 13:39:50 +08:00
										 |  |  | 		hideVersion bool | 
					
						
							|  |  |  | 		expected    settings | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 			desc:        "Not hiding version", | 
					
						
							| 
									
										
										
										
											2020-06-17 13:39:50 +08:00
										 |  |  | 			hideVersion: false, | 
					
						
							|  |  |  | 			expected: settings{ | 
					
						
							|  |  |  | 				BuildInfo: buildInfo{ | 
					
						
							|  |  |  | 					Version: setting.BuildVersion, | 
					
						
							|  |  |  | 					Commit:  setting.BuildCommit, | 
					
						
							|  |  |  | 					Env:     setting.Env, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 			desc:        "Hiding version", | 
					
						
							| 
									
										
										
										
											2020-06-17 13:39:50 +08:00
										 |  |  | 			hideVersion: true, | 
					
						
							|  |  |  | 			expected: settings{ | 
					
						
							|  |  |  | 				BuildInfo: buildInfo{ | 
					
						
							|  |  |  | 					Version: "", | 
					
						
							|  |  |  | 					Commit:  "", | 
					
						
							|  |  |  | 					Env:     setting.Env, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, test := range tests { | 
					
						
							| 
									
										
										
										
											2020-12-11 18:44:44 +08:00
										 |  |  | 		t.Run(test.desc, func(t *testing.T) { | 
					
						
							|  |  |  | 			hs.Cfg.AnonymousHideVersion = test.hideVersion | 
					
						
							|  |  |  | 			expected := test.expected | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			recorder := httptest.NewRecorder() | 
					
						
							|  |  |  | 			m.ServeHTTP(recorder, req) | 
					
						
							|  |  |  | 			got := settings{} | 
					
						
							|  |  |  | 			err := json.Unmarshal(recorder.Body.Bytes(), &got) | 
					
						
							|  |  |  | 			require.NoError(t, err) | 
					
						
							|  |  |  | 			require.GreaterOrEqual(t, 400, recorder.Code, "status codes higher than 400 indicate a failure") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert.EqualValues(t, expected, got) | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2020-06-17 13:39:50 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-01-27 22:08:17 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestHTTPServer_GetFrontendSettings_pluginsCDNBaseURL(t *testing.T) { | 
					
						
							|  |  |  | 	type settings struct { | 
					
						
							|  |  |  | 		PluginsCDNBaseURL string `json:"pluginsCDNBaseURL"` | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		desc      string | 
					
						
							|  |  |  | 		mutateCfg func(*setting.Cfg) | 
					
						
							|  |  |  | 		expected  settings | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			desc: "With CDN", | 
					
						
							|  |  |  | 			mutateCfg: func(cfg *setting.Cfg) { | 
					
						
							| 
									
										
										
										
											2023-02-24 21:28:13 +08:00
										 |  |  | 				cfg.PluginsCDNURLTemplate = "https://cdn.example.com" | 
					
						
							| 
									
										
										
										
											2023-01-27 22:08:17 +08:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			expected: settings{PluginsCDNBaseURL: "https://cdn.example.com"}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			desc: "Without CDN", | 
					
						
							|  |  |  | 			mutateCfg: func(cfg *setting.Cfg) { | 
					
						
							|  |  |  | 				cfg.PluginsCDNURLTemplate = "" | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			expected: settings{PluginsCDNBaseURL: ""}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			desc:     "CDN is disabled by default", | 
					
						
							|  |  |  | 			expected: settings{PluginsCDNBaseURL: ""}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, test := range tests { | 
					
						
							|  |  |  | 		t.Run(test.desc, func(t *testing.T) { | 
					
						
							|  |  |  | 			cfg := setting.NewCfg() | 
					
						
							|  |  |  | 			if test.mutateCfg != nil { | 
					
						
							|  |  |  | 				test.mutateCfg(cfg) | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2023-02-08 00:20:05 +08:00
										 |  |  | 			m, _ := setupTestEnvironment(t, cfg, featuremgmt.WithFeatures(), nil, nil) | 
					
						
							| 
									
										
										
										
											2023-01-27 22:08:17 +08:00
										 |  |  | 			req := httptest.NewRequest(http.MethodGet, "/api/frontend/settings", nil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			recorder := httptest.NewRecorder() | 
					
						
							|  |  |  | 			m.ServeHTTP(recorder, req) | 
					
						
							|  |  |  | 			var got settings | 
					
						
							|  |  |  | 			err := json.Unmarshal(recorder.Body.Bytes(), &got) | 
					
						
							|  |  |  | 			require.NoError(t, err) | 
					
						
							|  |  |  | 			require.Equal(t, http.StatusOK, recorder.Code) | 
					
						
							|  |  |  | 			require.EqualValues(t, test.expected, got) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-02-08 00:20:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestHTTPServer_GetFrontendSettings_apps(t *testing.T) { | 
					
						
							|  |  |  | 	type settings struct { | 
					
						
							|  |  |  | 		Apps map[string]*plugins.AppDTO `json:"apps"` | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tests := []struct { | 
					
						
							|  |  |  | 		desc           string | 
					
						
							| 
									
										
										
										
											2023-09-11 19:59:24 +08:00
										 |  |  | 		pluginStore    func() pluginstore.Store | 
					
						
							| 
									
										
										
										
											2023-03-08 00:22:30 +08:00
										 |  |  | 		pluginSettings func() pluginsettings.Service | 
					
						
							| 
									
										
										
										
											2023-02-08 00:20:05 +08:00
										 |  |  | 		expected       settings | 
					
						
							|  |  |  | 	}{ | 
					
						
							| 
									
										
										
										
											2023-02-08 21:41:49 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			desc: "disabled app with preload", | 
					
						
							| 
									
										
										
										
											2023-09-11 19:59:24 +08:00
										 |  |  | 			pluginStore: func() pluginstore.Store { | 
					
						
							|  |  |  | 				return &pluginstore.FakePluginStore{ | 
					
						
							|  |  |  | 					PluginList: []pluginstore.Plugin{ | 
					
						
							| 
									
										
										
										
											2023-02-08 21:41:49 +08:00
										 |  |  | 						{ | 
					
						
							|  |  |  | 							Module: fmt.Sprintf("/%s/module.js", "test-app"), | 
					
						
							|  |  |  | 							JSONData: plugins.JSONData{ | 
					
						
							| 
									
										
										
										
											2023-03-02 22:42:00 +08:00
										 |  |  | 								ID:      "test-app", | 
					
						
							|  |  |  | 								Info:    plugins.Info{Version: "0.5.0"}, | 
					
						
							| 
									
										
										
										
											2023-06-08 18:21:19 +08:00
										 |  |  | 								Type:    plugins.TypeApp, | 
					
						
							| 
									
										
										
										
											2023-03-02 22:42:00 +08:00
										 |  |  | 								Preload: true, | 
					
						
							| 
									
										
										
										
											2023-02-08 21:41:49 +08:00
										 |  |  | 							}, | 
					
						
							|  |  |  | 						}, | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2023-03-08 00:22:30 +08:00
										 |  |  | 			pluginSettings: func() pluginsettings.Service { | 
					
						
							|  |  |  | 				return &pluginsettings.FakePluginSettings{ | 
					
						
							| 
									
										
										
										
											2023-02-08 21:41:49 +08:00
										 |  |  | 					Plugins: newAppSettings("test-app", false), | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			expected: settings{ | 
					
						
							|  |  |  | 				Apps: map[string]*plugins.AppDTO{ | 
					
						
							|  |  |  | 					"test-app": { | 
					
						
							| 
									
										
										
										
											2023-03-02 22:42:00 +08:00
										 |  |  | 						ID:      "test-app", | 
					
						
							|  |  |  | 						Preload: false, | 
					
						
							|  |  |  | 						Path:    "/test-app/module.js", | 
					
						
							|  |  |  | 						Version: "0.5.0", | 
					
						
							| 
									
										
										
										
											2023-02-08 21:41:49 +08:00
										 |  |  | 					}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2023-06-22 16:43:38 +08:00
										 |  |  | 			desc: "enabled app with preload", | 
					
						
							| 
									
										
										
										
											2023-09-11 19:59:24 +08:00
										 |  |  | 			pluginStore: func() pluginstore.Store { | 
					
						
							|  |  |  | 				return &pluginstore.FakePluginStore{ | 
					
						
							|  |  |  | 					PluginList: []pluginstore.Plugin{ | 
					
						
							| 
									
										
										
										
											2023-02-08 21:41:49 +08:00
										 |  |  | 						{ | 
					
						
							|  |  |  | 							Module: fmt.Sprintf("/%s/module.js", "test-app"), | 
					
						
							|  |  |  | 							JSONData: plugins.JSONData{ | 
					
						
							| 
									
										
										
										
											2023-03-02 22:42:00 +08:00
										 |  |  | 								ID:      "test-app", | 
					
						
							|  |  |  | 								Info:    plugins.Info{Version: "0.5.0"}, | 
					
						
							| 
									
										
										
										
											2023-06-08 18:21:19 +08:00
										 |  |  | 								Type:    plugins.TypeApp, | 
					
						
							| 
									
										
										
										
											2023-03-02 22:42:00 +08:00
										 |  |  | 								Preload: true, | 
					
						
							| 
									
										
										
										
											2023-02-08 21:41:49 +08:00
										 |  |  | 							}, | 
					
						
							|  |  |  | 						}, | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2023-03-08 00:22:30 +08:00
										 |  |  | 			pluginSettings: func() pluginsettings.Service { | 
					
						
							|  |  |  | 				return &pluginsettings.FakePluginSettings{ | 
					
						
							| 
									
										
										
										
											2023-02-08 21:41:49 +08:00
										 |  |  | 					Plugins: newAppSettings("test-app", true), | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			expected: settings{ | 
					
						
							|  |  |  | 				Apps: map[string]*plugins.AppDTO{ | 
					
						
							|  |  |  | 					"test-app": { | 
					
						
							| 
									
										
										
										
											2023-03-02 22:42:00 +08:00
										 |  |  | 						ID:      "test-app", | 
					
						
							|  |  |  | 						Preload: true, | 
					
						
							|  |  |  | 						Path:    "/test-app/module.js", | 
					
						
							|  |  |  | 						Version: "0.5.0", | 
					
						
							| 
									
										
										
										
											2023-02-08 21:41:49 +08:00
										 |  |  | 					}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2023-05-12 18:51:11 +08:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			desc: "angular app plugin", | 
					
						
							| 
									
										
										
										
											2023-09-11 19:59:24 +08:00
										 |  |  | 			pluginStore: func() pluginstore.Store { | 
					
						
							|  |  |  | 				return &pluginstore.FakePluginStore{ | 
					
						
							|  |  |  | 					PluginList: []pluginstore.Plugin{ | 
					
						
							| 
									
										
										
										
											2023-05-12 18:51:11 +08:00
										 |  |  | 						{ | 
					
						
							|  |  |  | 							Module: fmt.Sprintf("/%s/module.js", "test-app"), | 
					
						
							|  |  |  | 							JSONData: plugins.JSONData{ | 
					
						
							|  |  |  | 								ID:      "test-app", | 
					
						
							|  |  |  | 								Info:    plugins.Info{Version: "0.5.0"}, | 
					
						
							| 
									
										
										
										
											2023-06-08 18:21:19 +08:00
										 |  |  | 								Type:    plugins.TypeApp, | 
					
						
							| 
									
										
										
										
											2023-05-12 18:51:11 +08:00
										 |  |  | 								Preload: true, | 
					
						
							|  |  |  | 							}, | 
					
						
							|  |  |  | 							AngularDetected: true, | 
					
						
							|  |  |  | 						}, | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			pluginSettings: func() pluginsettings.Service { | 
					
						
							|  |  |  | 				return &pluginsettings.FakePluginSettings{ | 
					
						
							|  |  |  | 					Plugins: newAppSettings("test-app", true), | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			expected: settings{ | 
					
						
							|  |  |  | 				Apps: map[string]*plugins.AppDTO{ | 
					
						
							|  |  |  | 					"test-app": { | 
					
						
							|  |  |  | 						ID:              "test-app", | 
					
						
							|  |  |  | 						Preload:         true, | 
					
						
							|  |  |  | 						Path:            "/test-app/module.js", | 
					
						
							|  |  |  | 						Version:         "0.5.0", | 
					
						
							|  |  |  | 						AngularDetected: true, | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2023-02-08 00:20:05 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, test := range tests { | 
					
						
							|  |  |  | 		t.Run(test.desc, func(t *testing.T) { | 
					
						
							|  |  |  | 			cfg := setting.NewCfg() | 
					
						
							|  |  |  | 			m, _ := setupTestEnvironment(t, cfg, featuremgmt.WithFeatures(), test.pluginStore(), test.pluginSettings()) | 
					
						
							|  |  |  | 			req := httptest.NewRequest(http.MethodGet, "/api/frontend/settings", nil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			recorder := httptest.NewRecorder() | 
					
						
							|  |  |  | 			m.ServeHTTP(recorder, req) | 
					
						
							|  |  |  | 			var got settings | 
					
						
							|  |  |  | 			err := json.Unmarshal(recorder.Body.Bytes(), &got) | 
					
						
							|  |  |  | 			require.NoError(t, err) | 
					
						
							|  |  |  | 			require.Equal(t, http.StatusOK, recorder.Code) | 
					
						
							|  |  |  | 			require.EqualValues(t, test.expected, got) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 00:22:30 +08:00
										 |  |  | func newAppSettings(id string, enabled bool) map[string]*pluginsettings.DTO { | 
					
						
							|  |  |  | 	return map[string]*pluginsettings.DTO{ | 
					
						
							| 
									
										
										
										
											2023-02-08 00:20:05 +08:00
										 |  |  | 		id: { | 
					
						
							|  |  |  | 			ID:       0, | 
					
						
							|  |  |  | 			OrgID:    1, | 
					
						
							|  |  |  | 			PluginID: id, | 
					
						
							|  |  |  | 			Enabled:  enabled, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |