[MM-41290] Add Endpoint to complete onboarding (#19435)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
a733fb840d
Коммит
321b19e3db
@@ -1472,8 +1472,7 @@ func TestInstallMarketplacePlugin(t *testing.T) {
|
||||
*cfg.PluginSettings.MarketplaceURL = testServer.URL
|
||||
})
|
||||
|
||||
// The content of the request is irrelevant. This test only cares about enterprise_plugins.
|
||||
pRequest := &model.InstallMarketplacePluginRequest{}
|
||||
pRequest := &model.InstallMarketplacePluginRequest{Id: "testplugin"}
|
||||
manifest, resp, err := client.InstallMarketplacePlugin(pRequest)
|
||||
require.Error(t, err)
|
||||
CheckInternalErrorStatus(t, resp)
|
||||
@@ -1511,8 +1510,7 @@ func TestInstallMarketplacePlugin(t *testing.T) {
|
||||
*l.Features.EnterprisePlugins = false
|
||||
th.App.Srv().SetLicense(l)
|
||||
|
||||
// The content of the request is irrelevant. This test only cares about enterprise_plugins.
|
||||
pRequest := &model.InstallMarketplacePluginRequest{}
|
||||
pRequest := &model.InstallMarketplacePluginRequest{Id: "testplugin"}
|
||||
manifest, resp, err := client.InstallMarketplacePlugin(pRequest)
|
||||
require.Error(t, err)
|
||||
CheckInternalErrorStatus(t, resp)
|
||||
@@ -1546,8 +1544,7 @@ func TestInstallMarketplacePlugin(t *testing.T) {
|
||||
|
||||
th.App.Srv().SetLicense(model.NewTestLicense("enterprise_plugins"))
|
||||
|
||||
// The content of the request is irrelevant. This test only cares about enterprise_plugins.
|
||||
pRequest := &model.InstallMarketplacePluginRequest{}
|
||||
pRequest := &model.InstallMarketplacePluginRequest{Id: "testplugin"}
|
||||
manifest, resp, err := client.InstallMarketplacePlugin(pRequest)
|
||||
require.Error(t, err)
|
||||
CheckInternalErrorStatus(t, resp)
|
||||
|
||||
@@ -67,8 +67,8 @@ func (api *API) InitSystem() {
|
||||
api.BaseRoutes.APIRoot.Handle("/warn_metrics/trial-license-ack/{warn_metric_id:[A-Za-z0-9-_]+}", api.APIHandler(requestTrialLicenseAndAckWarnMetric)).Methods("POST")
|
||||
api.BaseRoutes.System.Handle("/notices/{team_id:[A-Za-z0-9]+}", api.APISessionRequired(getProductNotices)).Methods("GET")
|
||||
api.BaseRoutes.System.Handle("/notices/view", api.APISessionRequired(updateViewedProductNotices)).Methods("PUT")
|
||||
|
||||
api.BaseRoutes.System.Handle("/support_packet", api.APISessionRequired(generateSupportPacket)).Methods("GET")
|
||||
api.BaseRoutes.System.Handle("/onboarding/complete", api.APIHandler(completeOnboarding)).Methods("POST")
|
||||
}
|
||||
|
||||
func generateSupportPacket(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
@@ -880,3 +880,29 @@ func updateViewedProductNotices(c *Context, w http.ResponseWriter, r *http.Reque
|
||||
auditRec.Success()
|
||||
ReturnStatusOK(w)
|
||||
}
|
||||
|
||||
func completeOnboarding(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
if !c.App.SessionHasPermissionTo(*c.AppContext.Session(), model.PermissionManageSystem) {
|
||||
c.Err = model.NewAppError("completeOnboarding", "app.system.complete_onboarding_request.no_first_user", nil, "", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
auditRec := c.MakeAuditRecord("completeOnboarding", audit.Fail)
|
||||
defer c.LogAuditRec(auditRec)
|
||||
|
||||
onboardingRequest, err := model.CompleteOnboardingRequestFromReader(r.Body)
|
||||
if err != nil {
|
||||
c.Err = model.NewAppError("completeOnboarding", "app.system.complete_onboarding_request.app_error", nil, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
auditRec.AddMeta("install_plugin", onboardingRequest.InstallPlugins)
|
||||
|
||||
appErr := c.App.CompleteOnboarding(onboardingRequest)
|
||||
if appErr != nil {
|
||||
c.Err = appErr
|
||||
return
|
||||
}
|
||||
|
||||
auditRec.Success()
|
||||
ReturnStatusOK(w)
|
||||
}
|
||||
|
||||
@@ -5,11 +5,14 @@ package api4
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
@@ -20,6 +23,7 @@ import (
|
||||
|
||||
"github.com/mattermost/mattermost-server/v6/model"
|
||||
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
||||
"github.com/mattermost/mattermost-server/v6/utils/fileutils"
|
||||
)
|
||||
|
||||
func TestGetPing(t *testing.T) {
|
||||
@@ -778,3 +782,103 @@ func TestPushNotificationAck(t *testing.T) {
|
||||
assert.NotNil(t, resp.Body)
|
||||
})
|
||||
}
|
||||
|
||||
func TestCompleteOnboarding(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
|
||||
path, _ := fileutils.FindDir("tests")
|
||||
signatureFilename := "testplugin2.tar.gz.sig"
|
||||
signatureFileReader, err := os.Open(filepath.Join(path, signatureFilename))
|
||||
require.NoError(t, err)
|
||||
sigFile, err := ioutil.ReadAll(signatureFileReader)
|
||||
require.NoError(t, err)
|
||||
pluginSignature := base64.StdEncoding.EncodeToString(sigFile)
|
||||
|
||||
tarData, err := ioutil.ReadFile(filepath.Join(path, "testplugin2.tar.gz"))
|
||||
require.NoError(t, err)
|
||||
pluginServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
res.WriteHeader(http.StatusOK)
|
||||
res.Write(tarData)
|
||||
}))
|
||||
defer pluginServer.Close()
|
||||
|
||||
samplePlugins := []*model.MarketplacePlugin{{
|
||||
BaseMarketplacePlugin: &model.BaseMarketplacePlugin{
|
||||
HomepageURL: "https://example.com/mattermost/mattermost-plugin-nps",
|
||||
IconData: "https://example.com/icon.svg",
|
||||
DownloadURL: pluginServer.URL,
|
||||
Manifest: &model.Manifest{
|
||||
Id: "testplugin2",
|
||||
Name: "testplugin2",
|
||||
Description: "a second plugin",
|
||||
Version: "1.2.3",
|
||||
MinServerVersion: "",
|
||||
},
|
||||
Signature: pluginSignature,
|
||||
},
|
||||
InstalledVersion: "",
|
||||
}}
|
||||
|
||||
marketplaceServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
||||
res.WriteHeader(http.StatusOK)
|
||||
var data []byte
|
||||
data, err = json.Marshal(samplePlugins)
|
||||
require.NoError(t, err)
|
||||
res.Write(data)
|
||||
}))
|
||||
defer marketplaceServer.Close()
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||
*cfg.PluginSettings.Enable = true
|
||||
*cfg.PluginSettings.EnableMarketplace = false
|
||||
*cfg.PluginSettings.EnableRemoteMarketplace = true
|
||||
*cfg.PluginSettings.MarketplaceURL = marketplaceServer.URL
|
||||
*cfg.PluginSettings.AllowInsecureDownloadURL = true
|
||||
})
|
||||
|
||||
key, err := os.Open(filepath.Join(path, "development-private-key.asc"))
|
||||
require.NoError(t, err)
|
||||
appErr := th.App.AddPublicKey("pub_key", key)
|
||||
require.Nil(t, appErr)
|
||||
|
||||
t.Cleanup(func() {
|
||||
appErr = th.App.DeletePublicKey("pub_key")
|
||||
require.Nil(t, appErr)
|
||||
})
|
||||
|
||||
req := &model.CompleteOnboardingRequest{
|
||||
InstallPlugins: []string{"testplugin2"},
|
||||
}
|
||||
|
||||
t.Run("as a regular user", func(t *testing.T) {
|
||||
resp, err := th.Client.CompleteOnboarding(req)
|
||||
require.Error(t, err)
|
||||
CheckForbiddenStatus(t, resp)
|
||||
})
|
||||
|
||||
t.Run("as a system admin", func(t *testing.T) {
|
||||
resp, err := th.SystemAdminClient.CompleteOnboarding(req)
|
||||
require.NoError(t, err)
|
||||
CheckOKStatus(t, resp)
|
||||
|
||||
t.Cleanup(func() {
|
||||
resp, err = th.SystemAdminClient.RemovePlugin("testplugin2")
|
||||
require.NoError(t, err)
|
||||
CheckOKStatus(t, resp)
|
||||
})
|
||||
|
||||
installedPlugins, resp, err := th.SystemAdminClient.GetPlugins()
|
||||
require.NoError(t, err)
|
||||
CheckOKStatus(t, resp)
|
||||
|
||||
found := false
|
||||
for _, p := range installedPlugins.Active {
|
||||
if p.Id == "testplugin2" {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
|
||||
require.True(t, found, "testplugin2 should have been installed and enabled")
|
||||
})
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user