Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
735 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved. | ||
// See License.txt for license information. | ||
|
||
package api4 | ||
|
||
import ( | ||
"net/http" | ||
|
||
l4g "github.com/alecthomas/log4go" | ||
"github.com/mattermost/platform/app" | ||
"github.com/mattermost/platform/model" | ||
"github.com/mattermost/platform/utils" | ||
) | ||
|
||
func InitTeam() { | ||
l4g.Debug(utils.T("api.team.init.debug")) | ||
|
||
BaseRoutes.Teams.Handle("", ApiSessionRequired(createTeam)).Methods("POST") | ||
|
||
} | ||
|
||
func createTeam(c *Context, w http.ResponseWriter, r *http.Request) { | ||
team := model.TeamFromJson(r.Body) | ||
if team == nil { | ||
c.SetInvalidParam("team") | ||
return | ||
} | ||
|
||
if !app.SessionHasPermissionTo(c.Session, model.PERMISSION_CREATE_TEAM) { | ||
c.Err = model.NewAppError("createTeam", "api.team.is_team_creation_allowed.disabled.app_error", nil, "", http.StatusForbidden) | ||
return | ||
} | ||
|
||
rteam, err := app.CreateTeamWithUser(team, c.Session.UserId) | ||
if err != nil { | ||
c.Err = err | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusCreated) | ||
w.Write([]byte(rteam.ToJson())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved. | ||
// See License.txt for license information. | ||
|
||
package api4 | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/mattermost/platform/model" | ||
"github.com/mattermost/platform/utils" | ||
) | ||
|
||
func TestCreateTeam(t *testing.T) { | ||
th := Setup().InitBasic() | ||
Client := th.Client | ||
|
||
team := &model.Team{Name: GenerateTestUsername(), DisplayName: "Some Team", Type: model.TEAM_OPEN} | ||
rteam, resp := Client.CreateTeam(team) | ||
CheckNoError(t, resp) | ||
|
||
if rteam.Name != team.Name { | ||
t.Fatal("names did not match") | ||
} | ||
|
||
if rteam.DisplayName != team.DisplayName { | ||
t.Fatal("display names did not match") | ||
} | ||
|
||
if rteam.Type != team.Type { | ||
t.Fatal("types did not match") | ||
} | ||
|
||
_, resp = Client.CreateTeam(rteam) | ||
CheckBadRequestStatus(t, resp) | ||
|
||
rteam.Id = "" | ||
_, resp = Client.CreateTeam(rteam) | ||
CheckErrorMessage(t, resp, "A team with that name already exists") | ||
CheckBadRequestStatus(t, resp) | ||
|
||
rteam.Name = "" | ||
_, resp = Client.CreateTeam(rteam) | ||
CheckErrorMessage(t, resp, "Name must be 2 or more lowercase alphanumeric characters") | ||
CheckBadRequestStatus(t, resp) | ||
|
||
if r, err := Client.DoApiPost("/teams", "garbage"); err == nil { | ||
t.Fatal("should have errored") | ||
} else { | ||
if r.StatusCode != http.StatusBadRequest { | ||
t.Log("actual: " + strconv.Itoa(r.StatusCode)) | ||
t.Log("expected: " + strconv.Itoa(http.StatusBadRequest)) | ||
t.Fatal("wrong status code") | ||
} | ||
} | ||
|
||
Client.Logout() | ||
|
||
_, resp = Client.CreateTeam(rteam) | ||
CheckUnauthorizedStatus(t, resp) | ||
|
||
// Update permission | ||
enableTeamCreation := utils.Cfg.TeamSettings.EnableTeamCreation | ||
defer func() { | ||
utils.Cfg.TeamSettings.EnableTeamCreation = enableTeamCreation | ||
}() | ||
utils.Cfg.TeamSettings.EnableTeamCreation = false | ||
utils.SetDefaultRolesBasedOnConfig() | ||
|
||
th.LoginBasic() | ||
_, resp = Client.CreateTeam(team) | ||
CheckForbiddenStatus(t, resp) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.