diff options
Diffstat (limited to 'api4')
-rw-r--r-- | api4/team.go | 79 | ||||
-rw-r--r-- | api4/team_test.go | 76 |
2 files changed, 155 insertions, 0 deletions
diff --git a/api4/team.go b/api4/team.go index 4bca56994..85a083ee1 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 a38acf2e6..6127919c1 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) + }) +} |