Implement POST /teams endpoint (#5220)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
450c0b3ccb
Коммит
7431050b42
@@ -45,7 +45,7 @@ func TestCreateTeam(t *testing.T) {
|
||||
|
||||
rteam.Data.(*model.Team).Id = ""
|
||||
if _, err := Client.CreateTeam(rteam.Data.(*model.Team)); err != nil {
|
||||
if err.Message != "A team with that domain already exists" {
|
||||
if err.Message != "A team with that name already exists" {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
Обычный файл
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
Обычный файл
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)
|
||||
|
||||
}
|
||||
@@ -5161,7 +5161,7 @@
|
||||
},
|
||||
{
|
||||
"id": "store.sql_team.save.domain_exists.app_error",
|
||||
"translation": "A team with that domain already exists"
|
||||
"translation": "A team with that name already exists"
|
||||
},
|
||||
{
|
||||
"id": "store.sql_team.save.existing.app_error",
|
||||
|
||||
@@ -213,7 +213,16 @@ func (c *Client4) UpdateUser(user *User) (*User, *Response) {
|
||||
}
|
||||
|
||||
// Team Section
|
||||
// to be filled in..
|
||||
|
||||
// CreateTeam creates a team in the system based on the provided team struct.
|
||||
func (c *Client4) CreateTeam(team *Team) (*Team, *Response) {
|
||||
if r, err := c.DoApiPost(c.GetTeamsRoute(), team.ToJson()); err != nil {
|
||||
return nil, &Response{StatusCode: r.StatusCode, Error: err}
|
||||
} else {
|
||||
defer closeBody(r)
|
||||
return TeamFromJson(r.Body), BuildResponse(r)
|
||||
}
|
||||
}
|
||||
|
||||
// Channel Section
|
||||
// to be filled in..
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
@@ -118,55 +119,55 @@ func (o *Team) Etag() string {
|
||||
func (o *Team) IsValid() *AppError {
|
||||
|
||||
if len(o.Id) != 26 {
|
||||
return NewLocAppError("Team.IsValid", "model.team.is_valid.id.app_error", nil, "")
|
||||
return NewAppError("Team.IsValid", "model.team.is_valid.id.app_error", nil, "", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if o.CreateAt == 0 {
|
||||
return NewLocAppError("Team.IsValid", "model.team.is_valid.create_at.app_error", nil, "id="+o.Id)
|
||||
return NewAppError("Team.IsValid", "model.team.is_valid.create_at.app_error", nil, "id="+o.Id, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if o.UpdateAt == 0 {
|
||||
return NewLocAppError("Team.IsValid", "model.team.is_valid.update_at.app_error", nil, "id="+o.Id)
|
||||
return NewAppError("Team.IsValid", "model.team.is_valid.update_at.app_error", nil, "id="+o.Id, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if len(o.Email) > TEAM_EMAIL_MAX_LENGTH {
|
||||
return NewLocAppError("Team.IsValid", "model.team.is_valid.email.app_error", nil, "id="+o.Id)
|
||||
return NewAppError("Team.IsValid", "model.team.is_valid.email.app_error", nil, "id="+o.Id, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if len(o.Email) > 0 && !IsValidEmail(o.Email) {
|
||||
return NewLocAppError("Team.IsValid", "model.team.is_valid.email.app_error", nil, "id="+o.Id)
|
||||
return NewAppError("Team.IsValid", "model.team.is_valid.email.app_error", nil, "id="+o.Id, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if utf8.RuneCountInString(o.DisplayName) == 0 || utf8.RuneCountInString(o.DisplayName) > TEAM_DISPLAY_NAME_MAX_RUNES {
|
||||
return NewLocAppError("Team.IsValid", "model.team.is_valid.name.app_error", nil, "id="+o.Id)
|
||||
return NewAppError("Team.IsValid", "model.team.is_valid.name.app_error", nil, "id="+o.Id, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if len(o.Name) > TEAM_NAME_MAX_LENGTH {
|
||||
return NewLocAppError("Team.IsValid", "model.team.is_valid.url.app_error", nil, "id="+o.Id)
|
||||
return NewAppError("Team.IsValid", "model.team.is_valid.url.app_error", nil, "id="+o.Id, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if len(o.Description) > TEAM_DESCRIPTION_MAX_LENGTH {
|
||||
return NewLocAppError("Team.IsValid", "model.team.is_valid.description.app_error", nil, "id="+o.Id)
|
||||
return NewAppError("Team.IsValid", "model.team.is_valid.description.app_error", nil, "id="+o.Id, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if IsReservedTeamName(o.Name) {
|
||||
return NewLocAppError("Team.IsValid", "model.team.is_valid.reserved.app_error", nil, "id="+o.Id)
|
||||
return NewAppError("Team.IsValid", "model.team.is_valid.reserved.app_error", nil, "id="+o.Id, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if !IsValidTeamName(o.Name) {
|
||||
return NewLocAppError("Team.IsValid", "model.team.is_valid.characters.app_error", nil, "id="+o.Id)
|
||||
return NewAppError("Team.IsValid", "model.team.is_valid.characters.app_error", nil, "id="+o.Id, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if !(o.Type == TEAM_OPEN || o.Type == TEAM_INVITE) {
|
||||
return NewLocAppError("Team.IsValid", "model.team.is_valid.type.app_error", nil, "id="+o.Id)
|
||||
return NewAppError("Team.IsValid", "model.team.is_valid.type.app_error", nil, "id="+o.Id, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if len(o.CompanyName) > TEAM_COMPANY_NAME_MAX_LENGTH {
|
||||
return NewLocAppError("Team.IsValid", "model.team.is_valid.company.app_error", nil, "id="+o.Id)
|
||||
return NewAppError("Team.IsValid", "model.team.is_valid.company.app_error", nil, "id="+o.Id, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
if len(o.AllowedDomains) > TEAM_ALLOWED_DOMAINS_MAX_LENGTH {
|
||||
return NewLocAppError("Team.IsValid", "model.team.is_valid.domains.app_error", nil, "id="+o.Id)
|
||||
return NewAppError("Team.IsValid", "model.team.is_valid.domains.app_error", nil, "id="+o.Id, http.StatusBadRequest)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -5,6 +5,7 @@ package store
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/mattermost/platform/model"
|
||||
@@ -62,8 +63,8 @@ func (s SqlTeamStore) Save(team *model.Team) StoreChannel {
|
||||
result := StoreResult{}
|
||||
|
||||
if len(team.Id) > 0 {
|
||||
result.Err = model.NewLocAppError("SqlTeamStore.Save",
|
||||
"store.sql_team.save.existing.app_error", nil, "id="+team.Id)
|
||||
result.Err = model.NewAppError("SqlTeamStore.Save",
|
||||
"store.sql_team.save.existing.app_error", nil, "id="+team.Id, http.StatusBadRequest)
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
return
|
||||
@@ -79,7 +80,7 @@ func (s SqlTeamStore) Save(team *model.Team) StoreChannel {
|
||||
|
||||
if err := s.GetMaster().Insert(team); err != nil {
|
||||
if IsUniqueConstraintError(err.Error(), []string{"Name", "teams_name_key"}) {
|
||||
result.Err = model.NewLocAppError("SqlTeamStore.Save", "store.sql_team.save.domain_exists.app_error", nil, "id="+team.Id+", "+err.Error())
|
||||
result.Err = model.NewAppError("SqlTeamStore.Save", "store.sql_team.save.domain_exists.app_error", nil, "id="+team.Id+", "+err.Error(), http.StatusBadRequest)
|
||||
} else {
|
||||
result.Err = model.NewLocAppError("SqlTeamStore.Save", "store.sql_team.save.app_error", nil, "id="+team.Id+", "+err.Error())
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user