MMCTL: Add import delete cmd for removing the import files (#29764)
Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com>
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
0cf6361139
Коммит
09a2037b61
@@ -126,6 +126,7 @@ type Routes struct {
|
||||
Cloud *mux.Router // 'api/v4/cloud'
|
||||
|
||||
Imports *mux.Router // 'api/v4/imports'
|
||||
Import *mux.Router // 'api/v4/imports/{import_name:.+\\.zip}'
|
||||
|
||||
Exports *mux.Router // 'api/v4/exports'
|
||||
Export *mux.Router // 'api/v4/exports/{export_name:.+\\.zip}'
|
||||
@@ -273,6 +274,7 @@ func Init(srv *app.Server) (*API, error) {
|
||||
api.BaseRoutes.Cloud = api.BaseRoutes.APIRoot.PathPrefix("/cloud").Subrouter()
|
||||
|
||||
api.BaseRoutes.Imports = api.BaseRoutes.APIRoot.PathPrefix("/imports").Subrouter()
|
||||
api.BaseRoutes.Import = api.BaseRoutes.Imports.PathPrefix("/{import_name:.+\\.zip}").Subrouter()
|
||||
api.BaseRoutes.Exports = api.BaseRoutes.APIRoot.PathPrefix("/exports").Subrouter()
|
||||
api.BaseRoutes.Export = api.BaseRoutes.Exports.PathPrefix("/{export_name:.+\\.zip}").Subrouter()
|
||||
|
||||
@@ -436,6 +438,7 @@ func InitLocal(srv *app.Server) *API {
|
||||
api.BaseRoutes.Upload = api.BaseRoutes.Uploads.PathPrefix("/{upload_id:[A-Za-z0-9]+}").Subrouter()
|
||||
|
||||
api.BaseRoutes.Imports = api.BaseRoutes.APIRoot.PathPrefix("/imports").Subrouter()
|
||||
api.BaseRoutes.Import = api.BaseRoutes.Imports.PathPrefix("/{import_name:.+\\.zip}").Subrouter()
|
||||
api.BaseRoutes.Exports = api.BaseRoutes.APIRoot.PathPrefix("/exports").Subrouter()
|
||||
api.BaseRoutes.Export = api.BaseRoutes.Exports.PathPrefix("/{export_name:.+\\.zip}").Subrouter()
|
||||
|
||||
|
||||
@@ -9,10 +9,12 @@ import (
|
||||
|
||||
"github.com/mattermost/mattermost/server/public/model"
|
||||
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
||||
"github.com/mattermost/mattermost/server/v8/channels/audit"
|
||||
)
|
||||
|
||||
func (api *API) InitImport() {
|
||||
api.BaseRoutes.Imports.Handle("", api.APISessionRequired(listImports)).Methods(http.MethodGet)
|
||||
api.BaseRoutes.Import.Handle("", api.APISessionRequired(deleteImport)).Methods(http.MethodDelete)
|
||||
}
|
||||
|
||||
func listImports(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
@@ -31,3 +33,23 @@ func listImports(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
c.Logger.Warn("Error writing imports", mlog.Err(err))
|
||||
}
|
||||
}
|
||||
|
||||
func deleteImport(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||
importName := c.Params.ImportName
|
||||
auditRec := c.MakeAuditRecord("deleteImport", audit.Fail)
|
||||
defer c.LogAuditRec(auditRec)
|
||||
auditRec.AddMeta("import_name", importName)
|
||||
|
||||
if !c.App.SessionHasPermissionTo(*c.AppContext.Session(), model.PermissionManageSystem) {
|
||||
c.SetPermissionError(model.PermissionManageSystem)
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.App.DeleteImport(importName); err != nil {
|
||||
c.Err = err
|
||||
return
|
||||
}
|
||||
|
||||
auditRec.Success()
|
||||
ReturnStatusOK(w)
|
||||
}
|
||||
|
||||
@@ -7,4 +7,5 @@ import "net/http"
|
||||
|
||||
func (api *API) InitImportLocal() {
|
||||
api.BaseRoutes.Imports.Handle("", api.APILocal(listImports)).Methods(http.MethodGet)
|
||||
api.BaseRoutes.Import.Handle("", api.APILocal(deleteImport)).Methods(http.MethodDelete)
|
||||
}
|
||||
|
||||
@@ -19,42 +19,43 @@ import (
|
||||
"github.com/mattermost/mattermost/server/v8/channels/utils/fileutils"
|
||||
)
|
||||
|
||||
// Helper function to upload a new import file
|
||||
func uploadNewImport(th *TestHelper, c *model.Client4, t *testing.T) string {
|
||||
testsDir, _ := fileutils.FindDir("tests")
|
||||
require.NotEmpty(t, testsDir)
|
||||
|
||||
file, err := os.Open(testsDir + "/import_test.zip")
|
||||
require.NoError(t, err)
|
||||
|
||||
info, err := file.Stat()
|
||||
require.NoError(t, err)
|
||||
|
||||
us := &model.UploadSession{
|
||||
Filename: info.Name(),
|
||||
FileSize: info.Size(),
|
||||
Type: model.UploadTypeImport,
|
||||
}
|
||||
|
||||
if c == th.LocalClient {
|
||||
us.UserId = model.UploadNoUserID
|
||||
}
|
||||
|
||||
u, _, err := c.CreateUpload(context.Background(), us)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, u)
|
||||
|
||||
finfo, _, err := c.UploadData(context.Background(), u.Id, file)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, finfo)
|
||||
|
||||
return u.Id
|
||||
}
|
||||
|
||||
func TestListImports(t *testing.T) {
|
||||
mainHelper.Parallel(t)
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
|
||||
testsDir, _ := fileutils.FindDir("tests")
|
||||
require.NotEmpty(t, testsDir)
|
||||
|
||||
uploadNewImport := func(c *model.Client4, t *testing.T) string {
|
||||
file, err := os.Open(testsDir + "/import_test.zip")
|
||||
require.NoError(t, err)
|
||||
|
||||
info, err := file.Stat()
|
||||
require.NoError(t, err)
|
||||
|
||||
us := &model.UploadSession{
|
||||
Filename: info.Name(),
|
||||
FileSize: info.Size(),
|
||||
Type: model.UploadTypeImport,
|
||||
}
|
||||
|
||||
if c == th.LocalClient {
|
||||
us.UserId = model.UploadNoUserID
|
||||
}
|
||||
|
||||
u, _, err := c.CreateUpload(context.Background(), us)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, u)
|
||||
|
||||
finfo, _, err := c.UploadData(context.Background(), u.Id, file)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, finfo)
|
||||
|
||||
return u.Id
|
||||
}
|
||||
|
||||
t.Run("no permissions", func(t *testing.T) {
|
||||
imports, _, err := th.Client.ListImports(context.Background())
|
||||
require.Error(t, err)
|
||||
@@ -71,8 +72,8 @@ func TestListImports(t *testing.T) {
|
||||
}, "no imports")
|
||||
|
||||
th.TestForSystemAdminAndLocal(t, func(t *testing.T, c *model.Client4) {
|
||||
id := uploadNewImport(c, t)
|
||||
id2 := uploadNewImport(c, t)
|
||||
id := uploadNewImport(th, c, t)
|
||||
id2 := uploadNewImport(th, c, t)
|
||||
|
||||
importDir := filepath.Join(dataDir, "import")
|
||||
f, err := os.Create(filepath.Join(importDir, "import.zip.tmp"))
|
||||
@@ -99,7 +100,7 @@ func TestListImports(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Empty(t, imports)
|
||||
|
||||
id := uploadNewImport(c, t)
|
||||
id := uploadNewImport(th, c, t)
|
||||
imports, _, err = c.ListImports(context.Background())
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, imports)
|
||||
@@ -151,3 +152,36 @@ func TestImportInLocalMode(t *testing.T) {
|
||||
// Just a sanity check to ensure new posts are actually added in the system.
|
||||
require.Greater(t, cnt2, cnt1)
|
||||
}
|
||||
|
||||
func TestDeleteImport(t *testing.T) {
|
||||
th := Setup(t)
|
||||
defer th.TearDown()
|
||||
|
||||
t.Run("no delete permissions", func(t *testing.T) {
|
||||
response, err := th.Client.DeleteImport(context.Background(), "import_test.zip")
|
||||
require.Error(t, err)
|
||||
CheckErrorID(t, err, "api.context.permissions.app_error")
|
||||
require.Equal(t, 403, response.StatusCode)
|
||||
})
|
||||
|
||||
th.TestForSystemAdminAndLocal(t, func(t *testing.T, c *model.Client4) {
|
||||
id := uploadNewImport(th, th.SystemAdminClient, t)
|
||||
id2 := uploadNewImport(th, th.SystemAdminClient, t)
|
||||
response, delErr := th.SystemAdminClient.DeleteImport(context.Background(), id+"_import_test.zip")
|
||||
require.Equal(t, 200, response.StatusCode)
|
||||
require.NoError(t, delErr)
|
||||
imports, _, err := th.SystemAdminClient.ListImports(context.Background())
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, imports)
|
||||
require.Len(t, imports, 1)
|
||||
require.Contains(t, imports, id2+"_import_test.zip")
|
||||
require.NotContains(t, imports, id+"_import_test.zip")
|
||||
|
||||
_, err = th.SystemAdminClient.DeleteImport(context.Background(), id2+"_import_test.zip")
|
||||
require.NoError(t, err)
|
||||
|
||||
//idempotency check
|
||||
_, err = th.SystemAdminClient.DeleteImport(context.Background(), id2+"_import_test.zip")
|
||||
require.NoError(t, err)
|
||||
}, "successful deletion")
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user