summaryrefslogtreecommitdiffstats
path: root/api4
diff options
context:
space:
mode:
Diffstat (limited to 'api4')
-rw-r--r--api4/api.go1
-rw-r--r--api4/team.go42
-rw-r--r--api4/team_test.go75
3 files changed, 118 insertions, 0 deletions
diff --git a/api4/api.go b/api4/api.go
index 9314bb616..ace5de30a 100644
--- a/api4/api.go
+++ b/api4/api.go
@@ -138,6 +138,7 @@ func InitApi(full bool) {
BaseRoutes.Webrtc = BaseRoutes.ApiRoot.PathPrefix("/webrtc").Subrouter()
InitUser()
+ InitTeam()
// REMOVE CONDITION WHEN APIv3 REMOVED
if full {
diff --git a/api4/team.go b/api4/team.go
new file mode 100644
index 000000000..6365ff6de
--- /dev/null
+++ b/api4/team.go
@@ -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()))
+}
diff --git a/api4/team_test.go b/api4/team_test.go
new file mode 100644
index 000000000..6e4f0c427
--- /dev/null
+++ b/api4/team_test.go
@@ -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)
+
+}