From 7eb09dbffdd87f36eff0d781eaeb0e816bbdac21 Mon Sep 17 00:00:00 2001 From: Carlos Tadeu Panato Junior Date: Mon, 3 Apr 2017 18:38:26 +0200 Subject: [PATCH] [APIV4] POST /teams/{team_id}/import for apiv4 (#5920) --- api4/team.go | 79 +++++++++++++++++++++++++++++++++++++ api4/team_test.go | 76 +++++++++++++++++++++++++++++++++++ i18n/en.json | 20 ++++++++++ model/client4.go | 55 ++++++++++++++++++++++++++ tests/Fake_Team_Import.zip | Bin 0 -> 3478 bytes 5 files changed, 230 insertions(+) create mode 100644 tests/Fake_Team_Import.zip diff --git a/api4/team.go b/api4/team.go index 4bca569945..85a083ee1e 100644 --- a/api4/team.go +++ b/api4/team.go @@ -4,7 +4,10 @@ package api4 import ( + "bytes" + "io" "net/http" + "strconv" l4g "github.com/alecthomas/log4go" "github.com/mattermost/platform/app" @@ -36,6 +39,8 @@ func InitTeam() { BaseRoutes.TeamMember.Handle("", ApiSessionRequired(getTeamMember)).Methods("GET") BaseRoutes.TeamByName.Handle("/exists", ApiSessionRequired(teamExists)).Methods("GET") BaseRoutes.TeamMember.Handle("/roles", ApiSessionRequired(updateTeamMemberRoles)).Methods("PUT") + + BaseRoutes.Team.Handle("/import", ApiSessionRequired(importTeam)).Methods("POST") } func createTeam(c *Context, w http.ResponseWriter, r *http.Request) { @@ -468,3 +473,77 @@ func teamExists(c *Context, w http.ResponseWriter, r *http.Request) { w.Write([]byte(model.MapBoolToJson(resp))) return } + +func importTeam(c *Context, w http.ResponseWriter, r *http.Request) { + c.RequireTeamId() + if c.Err != nil { + return + } + + if !app.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_IMPORT_TEAM) { + c.SetPermissionError(model.PERMISSION_IMPORT_TEAM) + return + } + + if err := r.ParseMultipartForm(10000000); err != nil { + c.Err = model.NewLocAppError("importTeam", "api.team.import_team.parse.app_error", nil, err.Error()) + return + } + + importFromArray, ok := r.MultipartForm.Value["importFrom"] + importFrom := importFromArray[0] + + fileSizeStr, ok := r.MultipartForm.Value["filesize"] + if !ok { + c.Err = model.NewLocAppError("importTeam", "api.team.import_team.unavailable.app_error", nil, "") + c.Err.StatusCode = http.StatusBadRequest + return + } + + fileSize, err := strconv.ParseInt(fileSizeStr[0], 10, 64) + if err != nil { + c.Err = model.NewLocAppError("importTeam", "api.team.import_team.integer.app_error", nil, "") + c.Err.StatusCode = http.StatusBadRequest + return + } + + fileInfoArray, ok := r.MultipartForm.File["file"] + if !ok { + c.Err = model.NewLocAppError("importTeam", "api.team.import_team.no_file.app_error", nil, "") + c.Err.StatusCode = http.StatusBadRequest + return + } + + if len(fileInfoArray) <= 0 { + c.Err = model.NewLocAppError("importTeam", "api.team.import_team.array.app_error", nil, "") + c.Err.StatusCode = http.StatusBadRequest + return + } + + fileInfo := fileInfoArray[0] + + fileData, err := fileInfo.Open() + defer fileData.Close() + if err != nil { + c.Err = model.NewLocAppError("importTeam", "api.team.import_team.open.app_error", nil, err.Error()) + c.Err.StatusCode = http.StatusBadRequest + return + } + + var log *bytes.Buffer + switch importFrom { + case "slack": + var err *model.AppError + if err, log = app.SlackImport(fileData, fileSize, c.Params.TeamId); err != nil { + c.Err = err + c.Err.StatusCode = http.StatusBadRequest + } + } + + w.Header().Set("Content-Disposition", "attachment; filename=MattermostImportLog.txt") + w.Header().Set("Content-Type", "application/octet-stream") + if c.Err != nil { + w.WriteHeader(c.Err.StatusCode) + } + io.Copy(w, bytes.NewReader(log.Bytes())) +} diff --git a/api4/team_test.go b/api4/team_test.go index a38acf2e6d..6127919c1b 100644 --- a/api4/team_test.go +++ b/api4/team_test.go @@ -4,9 +4,11 @@ package api4 import ( + "encoding/binary" "fmt" "net/http" "strconv" + "strings" "testing" "github.com/mattermost/platform/app" @@ -1047,3 +1049,77 @@ func TestTeamExists(t *testing.T) { _, resp = Client.TeamExists(team.Name, "") CheckUnauthorizedStatus(t, resp) } + +func TestImportTeam(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + + t.Run("ImportTeam", func(t *testing.T) { + var data []byte + var err error + data, err = readTestFile("Fake_Team_Import.zip") + if err != nil && len(data) == 0 { + t.Fatal("Error while reading the test file.") + } + + // Import the channels/users/posts + fileResp, resp := th.SystemAdminClient.ImportTeam(data, binary.Size(data), "slack", "Fake_Team_Import.zip", th.BasicTeam.Id) + CheckNoError(t, resp) + + fileReturned := fmt.Sprintf("%s", fileResp) + if !strings.Contains(fileReturned, "darth.vader@stardeath.com") { + t.Log(fileReturned) + t.Fatal("failed to report the user was imported") + } + + // Checking the imported users + importedUser, resp := th.SystemAdminClient.GetUserByUsername("bot_test", "") + CheckNoError(t, resp) + if importedUser.Username != "bot_test" { + t.Fatal("username should match with the imported user") + } + + importedUser, resp = th.SystemAdminClient.GetUserByUsername("lordvader", "") + CheckNoError(t, resp) + if importedUser.Username != "lordvader" { + t.Fatal("username should match with the imported user") + } + + // Checking the imported Channels + importedChannel, resp := th.SystemAdminClient.GetChannelByName("testchannel", th.BasicTeam.Id, "") + CheckNoError(t, resp) + if importedChannel.Name != "testchannel" { + t.Fatal("names did not match expected: testchannel") + } + + importedChannel, resp = th.SystemAdminClient.GetChannelByName("general", th.BasicTeam.Id, "") + CheckNoError(t, resp) + if importedChannel.Name != "general" { + t.Fatal("names did not match expected: general") + } + + posts, resp := th.SystemAdminClient.GetPostsForChannel(importedChannel.Id, 0, 60, "") + CheckNoError(t, resp) + if posts.Posts[posts.Order[3]].Message != "This is a test post to test the import process" { + t.Fatal("missing posts in the import process") + } + }) + + t.Run("MissingFile", func(t *testing.T) { + _, resp := th.SystemAdminClient.ImportTeam(nil, 4343, "slack", "Fake_Team_Import.zip", th.BasicTeam.Id) + CheckBadRequestStatus(t, resp) + }) + + t.Run("WrongPermission", func(t *testing.T) { + var data []byte + var err error + data, err = readTestFile("Fake_Team_Import.zip") + if err != nil && len(data) == 0 { + t.Fatal("Error while reading the test file.") + } + + // Import the channels/users/posts + _, resp := th.Client.ImportTeam(data, binary.Size(data), "slack", "Fake_Team_Import.zip", th.BasicTeam.Id) + CheckForbiddenStatus(t, resp) + }) +} diff --git a/i18n/en.json b/i18n/en.json index cd10197ae7..2a9c8309a7 100644 --- a/i18n/en.json +++ b/i18n/en.json @@ -3739,6 +3739,26 @@ "id": "model.client.upload_saml_cert.app_error", "translation": "Error creating SAML certificate multipart form request" }, + { + "id": "model.client.upload_post_attachment.writer.app_error", + "translation": "Error closing multipart writer" + }, + { + "id": "model.client.upload_post_attachment.file.app_error", + "translation": "Error writing file to multipart form" + }, + { + "id": "model.client.upload_post_attachment.channel_id.app_error", + "translation": "Error writing channel id to multipart form" + }, + { + "id": "model.client.upload_post_attachment.import_from.app_error", + "translation": "Error writing importFrom to multipart form" + }, + { + "id": "model.client.upload_post_attachment.file_size.app_error", + "translation": "Error writing fileSize to multipart form" + }, { "id": "model.command.is_valid.create_at.app_error", "translation": "Create at must be a valid time" diff --git a/model/client4.go b/model/client4.go index c542851e9c..0123b4252f 100644 --- a/model/client4.go +++ b/model/client4.go @@ -94,6 +94,10 @@ func (c *Client4) GetTeamStatsRoute(teamId string) string { return fmt.Sprintf(c.GetTeamRoute(teamId) + "/stats") } +func (c *Client4) GetTeamImportRoute(teamId string) string { + return fmt.Sprintf(c.GetTeamRoute(teamId) + "/import") +} + func (c *Client4) GetChannelsRoute() string { return fmt.Sprintf("/channels") } @@ -277,6 +281,27 @@ func (c *Client4) DoUploadFile(url string, data []byte, contentType string) (*Fi } } +func (c *Client4) DoUploadImportTeam(url string, data []byte, contentType string) ([]byte, *Response) { + rq, _ := http.NewRequest("POST", c.ApiUrl+url, bytes.NewReader(data)) + rq.Header.Set("Content-Type", contentType) + rq.Close = true + + if len(c.AuthToken) > 0 { + rq.Header.Set(HEADER_AUTH, c.AuthType+" "+c.AuthToken) + } + + if rp, err := c.HttpClient.Do(rq); err != nil { + return nil, &Response{Error: NewAppError(url, "model.client.connecting.app_error", nil, err.Error(), 0)} + } else if rp.StatusCode >= 300 { + return nil, &Response{StatusCode: rp.StatusCode, Error: AppErrorFromJson(rp.Body)} + } else if data, err := ioutil.ReadAll(rp.Body); err != nil { + return nil, &Response{StatusCode: rp.StatusCode, Error: NewAppError("UploadImportTeam", "model.client.read_file.app_error", nil, err.Error(), rp.StatusCode)} + } else { + defer closeBody(rp) + return data, BuildResponse(rp) + } +} + // CheckStatusOK is a convenience function for checking the standard OK response // from the web service. func CheckStatusOK(r *http.Response) bool { @@ -966,6 +991,36 @@ func (c *Client4) GetTeamUnread(teamId, userId string) (*TeamUnread, *Response) } } +// ImportTeam will import an exported team from other app into a existing team. +func (c *Client4) ImportTeam(data []byte, filesize int, importFrom, filename, teamId string) ([]byte, *Response) { + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + + if part, err := writer.CreateFormFile("file", filename); err != nil { + return nil, &Response{Error: NewAppError("UploadImportTeam", "model.client.upload_post_attachment.file.app_error", nil, err.Error(), http.StatusBadRequest)} + } else if _, err = io.Copy(part, bytes.NewBuffer(data)); err != nil { + return nil, &Response{Error: NewAppError("UploadImportTeam", "model.client.upload_post_attachment.file.app_error", nil, err.Error(), http.StatusBadRequest)} + } + + if part, err := writer.CreateFormField("filesize"); err != nil { + return nil, &Response{Error: NewAppError("UploadImportTeam", "model.client.upload_post_attachment.file_size.app_error", nil, err.Error(), http.StatusBadRequest)} + } else if _, err = io.Copy(part, strings.NewReader(strconv.Itoa(filesize))); err != nil { + return nil, &Response{Error: NewAppError("UploadImportTeam", "model.client.upload_post_attachment.file_size.app_error", nil, err.Error(), http.StatusBadRequest)} + } + + if part, err := writer.CreateFormField("importFrom"); err != nil { + return nil, &Response{Error: NewAppError("UploadImportTeam", "model.client.upload_post_attachment.import_from.app_error", nil, err.Error(), http.StatusBadRequest)} + } else if _, err = io.Copy(part, strings.NewReader(importFrom)); err != nil { + return nil, &Response{Error: NewAppError("UploadImportTeam", "model.client.upload_post_attachment.import_from.app_error", nil, err.Error(), http.StatusBadRequest)} + } + + if err := writer.Close(); err != nil { + return nil, &Response{Error: NewAppError("UploadImportTeam", "model.client.upload_post_attachment.writer.app_error", nil, err.Error(), http.StatusBadRequest)} + } + + return c.DoUploadImportTeam(c.GetTeamImportRoute(teamId), body.Bytes(), writer.FormDataContentType()) +} + // Channel Section // CreateChannel creates a channel based on the provided channel struct. diff --git a/tests/Fake_Team_Import.zip b/tests/Fake_Team_Import.zip new file mode 100644 index 0000000000000000000000000000000000000000..429f4374f00914cd52dfbcad14639b576f59b770 GIT binary patch literal 3478 zcmZ`+dpwi-AD_)-<}PHEkXuf&xf7AH7)oz1vOt>AR>%X7JkzDaX86&PN&MCZ=b_y zhExCr`5;paz5-vk)G7u$iqI?{a|NEOEuKY#4Q&osV#$(Ip}=*|dkE)_nh0ZX(O zCN=w<+MS@-?_2ap3605_)kwk1mDKcyspK40kDYL(1Y0(?ZQG@itkcp|4z{r@PgRet z+o$-~?rX43)@P?960tfN-3SPy+v8fIc>GH5YmqVCSDnx@QzSkj4W5*s;0_`Pxg{>Wy6mv%2c%6XX+$!D+G^Dl=Qkvq6UE8-mqD(zDysEr1v}x&bjCr2?G~|UUj+HcVZLB{=)2JHSR!Vlg860$_-onF73+?OrEzM-%B(b0N>Mr!Qt_r zT7Yjim7!S-^MNn&8hcE!Sgr$pWBeO;h+i20Eq8`LxQk@D$^Lr-esZUr#zSLs_kHgy z=4id+{fcCVqLAFcvlow)>sNYPVJ-MAd$-fYEvluJ{QW&Ko)|0+>s#mt03whcl62RK zuT`pcrehDYs*GhlKXp;OXQM6zSsf1ZA%uN*{10u|t-So&)}PuisoA(v_Lr5v(FbaB z2(8@nO=P8x_~08b(SJtViSso|u|#swOgese&+W_CGY{U#gcy$LxCtTdPA8YHzN@-v z22^@kQXo236E#=!Cc4f9&Rn7Vi4BL4(0=}5K@IImPs+}$QV6v?JT4HP?1#l;s^&-L z!JAsTFY_?loD;QEr9Fw4BDksqugWy4A4rt9$ZC$dBkNRCzxTToV*&Wh8=P*}2)a@S+7p8TTuORWs zSqchHhXX<+-5%(>dgcF>S=c`4B%8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