* Only define plugin install endpoints when catalog enabled
* add external check
(cherry picked from commit 40dff288cd)
Co-authored-by: Will Browne <wbrowne@users.noreply.github.com>
This commit is contained in:
co-authored by
Will Browne
parent
e347be769f
commit
14e622414b
@@ -4,11 +4,13 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -19,8 +21,85 @@ import (
|
||||
"github.com/grafana/grafana/pkg/models"
|
||||
"github.com/grafana/grafana/pkg/plugins"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
"github.com/grafana/grafana/pkg/web/webtest"
|
||||
)
|
||||
|
||||
func Test_PluginsInstallAndUninstall(t *testing.T) {
|
||||
type tc struct {
|
||||
pluginAdminEnabled bool
|
||||
pluginAdminExternalManageEnabled bool
|
||||
expectedHTTPStatus int
|
||||
expectedHTTPBody string
|
||||
}
|
||||
tcs := []tc{
|
||||
{pluginAdminEnabled: true, pluginAdminExternalManageEnabled: true, expectedHTTPStatus: 404, expectedHTTPBody: "404 page not found\n"},
|
||||
{pluginAdminEnabled: true, pluginAdminExternalManageEnabled: false, expectedHTTPStatus: 200, expectedHTTPBody: ""},
|
||||
{pluginAdminEnabled: false, pluginAdminExternalManageEnabled: true, expectedHTTPStatus: 404, expectedHTTPBody: "404 page not found\n"},
|
||||
{pluginAdminEnabled: false, pluginAdminExternalManageEnabled: false, expectedHTTPStatus: 404, expectedHTTPBody: "404 page not found\n"},
|
||||
}
|
||||
|
||||
testName := func(action string, testCase tc) string {
|
||||
return fmt.Sprintf("%s request returns %d when adminEnabled: %t and externalEnabled: %t",
|
||||
action, testCase.expectedHTTPStatus, testCase.pluginAdminEnabled, testCase.pluginAdminExternalManageEnabled)
|
||||
}
|
||||
|
||||
ps := fakePluginStore{
|
||||
plugins: make(map[string]plugins.PluginDTO),
|
||||
}
|
||||
for _, tc := range tcs {
|
||||
srv := SetupAPITestServer(t, func(hs *HTTPServer) {
|
||||
hs.Cfg = &setting.Cfg{
|
||||
PluginAdminEnabled: tc.pluginAdminEnabled,
|
||||
PluginAdminExternalManageEnabled: tc.pluginAdminExternalManageEnabled,
|
||||
}
|
||||
hs.pluginStore = ps
|
||||
})
|
||||
|
||||
t.Run(testName("Install", tc), func(t *testing.T) {
|
||||
req := srv.NewPostRequest("/api/plugins/test/install", strings.NewReader("{ \"version\": \"1.0.2\" }"))
|
||||
webtest.RequestWithSignedInUser(req, &models.SignedInUser{UserId: 1, OrgId: 1, OrgRole: models.ROLE_EDITOR, IsGrafanaAdmin: true})
|
||||
resp, err := srv.SendJSON(req)
|
||||
require.NoError(t, err)
|
||||
|
||||
body := new(strings.Builder)
|
||||
_, err = io.Copy(body, resp.Body)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tc.expectedHTTPBody, body.String())
|
||||
require.NoError(t, resp.Body.Close())
|
||||
require.Equal(t, tc.expectedHTTPStatus, resp.StatusCode)
|
||||
|
||||
if tc.expectedHTTPStatus == 200 {
|
||||
require.Equal(t, plugins.PluginDTO{
|
||||
JSONData: plugins.JSONData{
|
||||
ID: "test",
|
||||
Info: plugins.Info{
|
||||
Version: "1.0.2",
|
||||
},
|
||||
},
|
||||
}, ps.plugins["test"])
|
||||
}
|
||||
})
|
||||
|
||||
t.Run(testName("Uninstall", tc), func(t *testing.T) {
|
||||
req := srv.NewPostRequest("/api/plugins/test/uninstall", strings.NewReader("{}"))
|
||||
webtest.RequestWithSignedInUser(req, &models.SignedInUser{UserId: 1, OrgId: 1, OrgRole: models.ROLE_VIEWER, IsGrafanaAdmin: true})
|
||||
resp, err := srv.SendJSON(req)
|
||||
require.NoError(t, err)
|
||||
|
||||
body := new(strings.Builder)
|
||||
_, err = io.Copy(body, resp.Body)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, tc.expectedHTTPBody, body.String())
|
||||
require.NoError(t, resp.Body.Close())
|
||||
require.Equal(t, tc.expectedHTTPStatus, resp.StatusCode)
|
||||
|
||||
if tc.expectedHTTPStatus == 200 {
|
||||
require.Empty(t, ps.plugins)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_GetPluginAssets(t *testing.T) {
|
||||
pluginID := "test-plugin"
|
||||
pluginDir := "."
|
||||
|
||||
Reference in New Issue
Block a user