Implement POST /teams endpoint (#5220)

Этот коммит содержится в:
Joram Wilander
2017-01-31 07:59:36 -05:00
коммит произвёл GitHub
родитель 450c0b3ccb
Коммит 7431050b42
8 изменённых файлов: 148 добавлений и 19 удалений

Просмотреть файл

@@ -138,6 +138,7 @@ func InitApi(full bool) {
BaseRoutes.Webrtc = BaseRoutes.ApiRoot.PathPrefix("/webrtc").Subrouter()
InitUser()
InitTeam()
// REMOVE CONDITION WHEN APIv3 REMOVED
if full {

42
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()))
}

75
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)
}