| 
									
										
										
										
											2023-01-25 18:45:52 +08:00
										 |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"encoding/json" | 
					
						
							|  |  |  | 	"errors" | 
					
						
							|  |  |  | 	"io" | 
					
						
							|  |  |  | 	"path/filepath" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana-azure-sdk-go/azsettings" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana-plugin-sdk-go/backend" | 
					
						
							| 
									
										
										
										
											2023-01-30 16:18:26 +08:00
										 |  |  | 	"github.com/stretchr/testify/require" | 
					
						
							| 
									
										
										
										
											2023-01-25 18:45:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/infra/db" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/infra/localcache" | 
					
						
							| 
									
										
										
										
											2023-04-28 20:02:27 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/log" | 
					
						
							| 
									
										
										
										
											2023-03-28 17:01:06 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/tracing" | 
					
						
							| 
									
										
										
										
											2023-01-25 18:45:52 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/plugins/backendplugin/coreplugin" | 
					
						
							|  |  |  | 	pluginClient "github.com/grafana/grafana/pkg/plugins/manager/client" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/plugins/manager/fakes" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/services/accesscontrol" | 
					
						
							| 
									
										
										
										
											2023-04-13 00:30:33 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/caching" | 
					
						
							| 
									
										
										
										
											2023-01-25 18:45:52 +08:00
										 |  |  | 	datasources "github.com/grafana/grafana/pkg/services/datasources/fakes" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/services/featuremgmt" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/services/oauthtoken/oauthtokentest" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/services/pluginsintegration" | 
					
						
							| 
									
										
										
										
											2023-03-27 17:15:37 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginaccesscontrol" | 
					
						
							| 
									
										
										
										
											2023-03-08 00:22:30 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/pluginsintegration/plugincontext" | 
					
						
							|  |  |  | 	pluginSettings "github.com/grafana/grafana/pkg/services/pluginsintegration/pluginsettings/service" | 
					
						
							| 
									
										
										
										
											2023-01-25 18:45:52 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/quota/quotatest" | 
					
						
							| 
									
										
										
										
											2023-06-08 19:59:51 +08:00
										 |  |  | 	fakeSecrets "github.com/grafana/grafana/pkg/services/secrets/fakes" | 
					
						
							| 
									
										
										
										
											2023-01-25 18:45:52 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/user" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/tsdb/cloudwatch" | 
					
						
							| 
									
										
										
										
											2023-09-22 20:00:40 +08:00
										 |  |  | 	testdatasource "github.com/grafana/grafana/pkg/tsdb/grafana-testdata-datasource" | 
					
						
							| 
									
										
										
										
											2023-01-25 18:45:52 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/web/webtest" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestCallResource(t *testing.T) { | 
					
						
							|  |  |  | 	staticRootPath, err := filepath.Abs("../../public/") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cfg := setting.NewCfg() | 
					
						
							|  |  |  | 	cfg.StaticRootPath = staticRootPath | 
					
						
							|  |  |  | 	cfg.IsFeatureToggleEnabled = func(_ string) bool { | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	cfg.Azure = &azsettings.AzureSettings{} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-26 20:46:31 +08:00
										 |  |  | 	coreRegistry := coreplugin.ProvideCoreRegistry(tracing.InitializeTracerForTest(), nil, &cloudwatch.CloudWatchService{}, nil, nil, nil, nil, | 
					
						
							| 
									
										
										
										
											2023-07-13 15:34:40 +08:00
										 |  |  | 		nil, nil, nil, nil, testdatasource.ProvideService(), nil, nil, nil, nil, nil, nil) | 
					
						
							| 
									
										
										
										
											2023-07-27 21:29:13 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-10 00:25:28 +08:00
										 |  |  | 	textCtx := pluginsintegration.CreateIntegrationTestCtx(t, cfg, coreRegistry) | 
					
						
							| 
									
										
										
										
											2023-01-25 18:45:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-21 17:33:31 +08:00
										 |  |  | 	pcp := plugincontext.ProvideService(cfg, localcache.ProvideService(), textCtx.PluginStore, &datasources.FakeDataSourceService{}, | 
					
						
							|  |  |  | 		pluginSettings.ProvideService(db.InitTestDB(t), fakeSecrets.NewFakeSecretsService()), nil, nil) | 
					
						
							| 
									
										
										
										
											2023-01-25 18:45:52 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	srv := SetupAPITestServer(t, func(hs *HTTPServer) { | 
					
						
							|  |  |  | 		hs.Cfg = cfg | 
					
						
							| 
									
										
										
										
											2023-06-08 19:59:51 +08:00
										 |  |  | 		hs.pluginContextProvider = pcp | 
					
						
							| 
									
										
										
										
											2023-01-25 18:45:52 +08:00
										 |  |  | 		hs.QuotaService = quotatest.New(false, nil) | 
					
						
							| 
									
										
										
										
											2023-08-10 00:25:28 +08:00
										 |  |  | 		hs.pluginStore = textCtx.PluginStore | 
					
						
							|  |  |  | 		hs.pluginClient = textCtx.PluginClient | 
					
						
							| 
									
										
										
										
											2023-04-28 20:02:27 +08:00
										 |  |  | 		hs.log = log.New("test") | 
					
						
							| 
									
										
										
										
											2023-01-25 18:45:52 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t.Run("Test successful response is received for valid request", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2023-09-22 20:00:40 +08:00
										 |  |  | 		req := srv.NewPostRequest("/api/plugins/grafana-testdata-datasource/resources/test", strings.NewReader("{ \"test\": true }")) | 
					
						
							| 
									
										
										
										
											2023-01-25 18:45:52 +08:00
										 |  |  | 		webtest.RequestWithSignedInUser(req, &user.SignedInUser{UserID: 1, OrgID: 1, Permissions: map[int64]map[string][]string{ | 
					
						
							|  |  |  | 			1: accesscontrol.GroupScopesByAction([]accesscontrol.Permission{ | 
					
						
							| 
									
										
										
										
											2023-03-27 17:15:37 +08:00
										 |  |  | 				{Action: pluginaccesscontrol.ActionAppAccess, Scope: pluginaccesscontrol.ScopeProvider.GetResourceAllScope()}, | 
					
						
							| 
									
										
										
										
											2023-01-25 18:45:52 +08:00
										 |  |  | 			}), | 
					
						
							|  |  |  | 		}}) | 
					
						
							|  |  |  | 		resp, err := srv.SendJSON(req) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		b, err := io.ReadAll(resp.Body) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-30 23:46:47 +08:00
										 |  |  | 		var body = make(map[string]any) | 
					
						
							| 
									
										
										
										
											2023-01-25 18:45:52 +08:00
										 |  |  | 		err = json.Unmarshal(b, &body) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		require.Equal(t, "Hello world from test datasource!", body["message"]) | 
					
						
							|  |  |  | 		require.NoError(t, resp.Body.Close()) | 
					
						
							|  |  |  | 		require.Equal(t, 200, resp.StatusCode) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pc, err := pluginClient.NewDecorator(&fakes.FakePluginClient{ | 
					
						
							|  |  |  | 		CallResourceHandlerFunc: backend.CallResourceHandlerFunc(func(ctx context.Context, | 
					
						
							|  |  |  | 			req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error { | 
					
						
							|  |  |  | 			return errors.New("something went wrong") | 
					
						
							|  |  |  | 		}), | 
					
						
							| 
									
										
										
										
											2023-04-13 00:30:33 +08:00
										 |  |  | 	}, pluginsintegration.CreateMiddlewares(cfg, &oauthtokentest.Service{}, tracing.InitializeTracerForTest(), &caching.OSSCachingService{}, &featuremgmt.FeatureManager{})...) | 
					
						
							| 
									
										
										
										
											2023-01-25 18:45:52 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	srv = SetupAPITestServer(t, func(hs *HTTPServer) { | 
					
						
							|  |  |  | 		hs.Cfg = cfg | 
					
						
							| 
									
										
										
										
											2023-06-08 19:59:51 +08:00
										 |  |  | 		hs.pluginContextProvider = pcp | 
					
						
							| 
									
										
										
										
											2023-01-25 18:45:52 +08:00
										 |  |  | 		hs.QuotaService = quotatest.New(false, nil) | 
					
						
							| 
									
										
										
										
											2023-08-10 00:25:28 +08:00
										 |  |  | 		hs.pluginStore = textCtx.PluginStore | 
					
						
							| 
									
										
										
										
											2023-01-25 18:45:52 +08:00
										 |  |  | 		hs.pluginClient = pc | 
					
						
							| 
									
										
										
										
											2023-04-28 20:02:27 +08:00
										 |  |  | 		hs.log = log.New("test") | 
					
						
							| 
									
										
										
										
											2023-01-25 18:45:52 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	t.Run("Test error is properly propagated to API response", func(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2023-09-22 20:00:40 +08:00
										 |  |  | 		req := srv.NewGetRequest("/api/plugins/grafana-testdata-datasource/resources/scenarios") | 
					
						
							| 
									
										
										
										
											2023-01-25 18:45:52 +08:00
										 |  |  | 		webtest.RequestWithSignedInUser(req, &user.SignedInUser{UserID: 1, OrgID: 1, Permissions: map[int64]map[string][]string{ | 
					
						
							|  |  |  | 			1: accesscontrol.GroupScopesByAction([]accesscontrol.Permission{ | 
					
						
							| 
									
										
										
										
											2023-03-27 17:15:37 +08:00
										 |  |  | 				{Action: pluginaccesscontrol.ActionAppAccess, Scope: pluginaccesscontrol.ScopeProvider.GetResourceAllScope()}, | 
					
						
							| 
									
										
										
										
											2023-01-25 18:45:52 +08:00
										 |  |  | 			}), | 
					
						
							|  |  |  | 		}}) | 
					
						
							|  |  |  | 		resp, err := srv.SendJSON(req) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		body := new(strings.Builder) | 
					
						
							|  |  |  | 		_, err = io.Copy(body, resp.Body) | 
					
						
							|  |  |  | 		require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		expectedBody := `{ "error": "something went wrong", "message": "Failed to call resource", "traceID": "" }` | 
					
						
							|  |  |  | 		require.JSONEq(t, expectedBody, body.String()) | 
					
						
							|  |  |  | 		require.NoError(t, resp.Body.Close()) | 
					
						
							|  |  |  | 		require.Equal(t, 500, resp.StatusCode) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } |