From 9dce50281471f356a63544680907cbd7346e7dc2 Mon Sep 17 00:00:00 2001 From: Carlos Tadeu Panato Junior Date: Tue, 4 Apr 2017 03:32:09 +0200 Subject: Implement POST /teams/{team_id}/invite/email for apiV4 (#5944) --- api4/team.go | 33 +++++++++++++++++++++++++++++++++ api4/team_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ model/client4.go | 10 ++++++++++ 3 files changed, 91 insertions(+) diff --git a/api4/team.go b/api4/team.go index a420e77f4..8da308a1c 100644 --- a/api4/team.go +++ b/api4/team.go @@ -48,6 +48,7 @@ func InitTeam() { BaseRoutes.TeamMember.Handle("/roles", ApiSessionRequired(updateTeamMemberRoles)).Methods("PUT") BaseRoutes.Team.Handle("/import", ApiSessionRequired(importTeam)).Methods("POST") + BaseRoutes.Team.Handle("/invite/email", ApiSessionRequired(inviteUsersToTeam)).Methods("POST") } func createTeam(c *Context, w http.ResponseWriter, r *http.Request) { @@ -648,3 +649,35 @@ func importTeam(c *Context, w http.ResponseWriter, r *http.Request) { } io.Copy(w, bytes.NewReader(log.Bytes())) } + +func inviteUsersToTeam(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_INVITE_USER) { + c.SetPermissionError(model.PERMISSION_INVITE_USER) + return + } + + if !app.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_ADD_USER_TO_TEAM) { + c.SetPermissionError(model.PERMISSION_INVITE_USER) + return + } + + emailList := model.ArrayFromJson(r.Body) + + if len(emailList) == 0 { + c.SetInvalidParam("user_email") + return + } + + err := app.InviteNewUsersToTeam(emailList, c.Params.TeamId, c.Session.UserId, utils.GetSiteURL()) + if err != nil { + c.Err = err + return + } + + ReturnStatusOK(w) +} diff --git a/api4/team_test.go b/api4/team_test.go index f145515aa..b58a4dc72 100644 --- a/api4/team_test.go +++ b/api4/team_test.go @@ -1372,3 +1372,51 @@ func TestImportTeam(t *testing.T) { CheckForbiddenStatus(t, resp) }) } + +func TestInviteUsersToTeam(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + + user1 := GenerateTestEmail() + user2 := GenerateTestEmail() + + emailList := []string{user1, user2} + + //Delete all the messages before check the sample email + utils.DeleteMailBox(user1) + utils.DeleteMailBox(user2) + + okMsg, resp := th.SystemAdminClient.InviteUsersToTeam(th.BasicTeam.Id, emailList) + CheckNoError(t, resp) + if okMsg != true { + t.Fatal("should return true") + } + + expectedSubject := "[Mattermost] " + th.SystemAdminUser.GetDisplayName() + " invited you to join " + th.BasicTeam.DisplayName + " Team" + //Check if the email was send to the rigth email address + for _, email := range emailList { + var resultsMailbox utils.JSONMessageHeaderInbucket + err := utils.RetryInbucket(5, func() error { + var err error + resultsMailbox, err = utils.GetMailBox(email) + return err + }) + if err != nil { + t.Log(err) + t.Log("No email was received, maybe due load on the server. Disabling this verification") + } + if err == nil && len(resultsMailbox) > 0 { + if !strings.ContainsAny(resultsMailbox[0].To[0], email) { + t.Fatal("Wrong To recipient") + } else { + if resultsEmail, err := utils.GetMessageFromMailbox(email, resultsMailbox[0].ID); err == nil { + if resultsEmail.Subject != expectedSubject { + t.Log(resultsEmail.Subject) + t.Log(expectedSubject) + t.Fatal("Wrong Subject") + } + } + } + } + } +} diff --git a/model/client4.go b/model/client4.go index 6e7f95c99..91ecaa134 100644 --- a/model/client4.go +++ b/model/client4.go @@ -1057,6 +1057,16 @@ func (c *Client4) ImportTeam(data []byte, filesize int, importFrom, filename, te return c.DoUploadImportTeam(c.GetTeamImportRoute(teamId), body.Bytes(), writer.FormDataContentType()) } +// InviteUsersToTeam invite users by email to the team. +func (c *Client4) InviteUsersToTeam(teamId string, userEmails []string) (bool, *Response) { + if r, err := c.DoApiPost(c.GetTeamRoute(teamId)+"/invite/email", ArrayToJson(userEmails)); err != nil { + return false, &Response{StatusCode: r.StatusCode, Error: err} + } else { + defer closeBody(r) + return CheckStatusOK(r), BuildResponse(r) + } +} + // Channel Section // CreateChannel creates a channel based on the provided channel struct. -- cgit v1.2.3-1-g7c22