коммит произвёл
GitHub
родитель
236d46ecc6
Коммит
f2c955b3d3
@@ -18,6 +18,11 @@ import (
|
|||||||
|
|
||||||
// CreateBot creates the given bot and corresponding user.
|
// CreateBot creates the given bot and corresponding user.
|
||||||
func (a *App) CreateBot(c *request.Context, bot *model.Bot) (*model.Bot, *model.AppError) {
|
func (a *App) CreateBot(c *request.Context, bot *model.Bot) (*model.Bot, *model.AppError) {
|
||||||
|
vErr := bot.IsValidCreate()
|
||||||
|
if vErr != nil {
|
||||||
|
return nil, vErr
|
||||||
|
}
|
||||||
|
|
||||||
user, nErr := a.Srv().Store.User().Save(model.UserFromBot(bot))
|
user, nErr := a.Srv().Store.User().Save(model.UserFromBot(bot))
|
||||||
if nErr != nil {
|
if nErr != nil {
|
||||||
var appErr *model.AppError
|
var appErr *model.AppError
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ func TestCreateBot(t *testing.T) {
|
|||||||
OwnerId: th.BasicUser.Id,
|
OwnerId: th.BasicUser.Id,
|
||||||
})
|
})
|
||||||
require.NotNil(t, err)
|
require.NotNil(t, err)
|
||||||
require.Equal(t, "model.user.is_valid.username.app_error", err.Id)
|
require.Equal(t, "model.bot.is_valid.username.app_error", err.Id)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("relative to bot", func(t *testing.T) {
|
t.Run("relative to bot", func(t *testing.T) {
|
||||||
@@ -55,6 +55,18 @@ func TestCreateBot(t *testing.T) {
|
|||||||
require.Nil(t, bot)
|
require.Nil(t, bot)
|
||||||
require.Equal(t, "model.user.is_valid.email.app_error", err.Id)
|
require.Equal(t, "model.user.is_valid.email.app_error", err.Id)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("username missing", func(t *testing.T) {
|
||||||
|
th := Setup(t).InitBasic()
|
||||||
|
defer th.TearDown()
|
||||||
|
bot, err := th.App.CreateBot(th.Context, &model.Bot{
|
||||||
|
Description: "a bot",
|
||||||
|
OwnerId: th.BasicUser.Id,
|
||||||
|
})
|
||||||
|
require.NotNil(t, err)
|
||||||
|
require.Nil(t, bot)
|
||||||
|
require.Equal(t, "model.bot.is_valid.username.app_error", err.Id)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("create bot", func(t *testing.T) {
|
t.Run("create bot", func(t *testing.T) {
|
||||||
|
|||||||
20
model/bot.go
20
model/bot.go
@@ -63,12 +63,8 @@ func (b *Bot) Clone() *Bot {
|
|||||||
return ©
|
return ©
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsValid validates the bot and returns an error if it isn't configured correctly.
|
// IsValidCreate validates bot for Create call. This skips validations of fields that are auto-filled on Create
|
||||||
func (b *Bot) IsValid() *AppError {
|
func (b *Bot) IsValidCreate() *AppError {
|
||||||
if !IsValidId(b.UserId) {
|
|
||||||
return NewAppError("Bot.IsValid", "model.bot.is_valid.user_id.app_error", b.Trace(), "", http.StatusBadRequest)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !IsValidUsername(b.Username) {
|
if !IsValidUsername(b.Username) {
|
||||||
return NewAppError("Bot.IsValid", "model.bot.is_valid.username.app_error", b.Trace(), "", http.StatusBadRequest)
|
return NewAppError("Bot.IsValid", "model.bot.is_valid.username.app_error", b.Trace(), "", http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
@@ -85,6 +81,15 @@ func (b *Bot) IsValid() *AppError {
|
|||||||
return NewAppError("Bot.IsValid", "model.bot.is_valid.creator_id.app_error", b.Trace(), "", http.StatusBadRequest)
|
return NewAppError("Bot.IsValid", "model.bot.is_valid.creator_id.app_error", b.Trace(), "", http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsValid validates the bot and returns an error if it isn't configured correctly.
|
||||||
|
func (b *Bot) IsValid() *AppError {
|
||||||
|
if !IsValidId(b.UserId) {
|
||||||
|
return NewAppError("Bot.IsValid", "model.bot.is_valid.user_id.app_error", b.Trace(), "", http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
|
||||||
if b.CreateAt == 0 {
|
if b.CreateAt == 0 {
|
||||||
return NewAppError("Bot.IsValid", "model.bot.is_valid.create_at.app_error", b.Trace(), "", http.StatusBadRequest)
|
return NewAppError("Bot.IsValid", "model.bot.is_valid.create_at.app_error", b.Trace(), "", http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
@@ -92,8 +97,7 @@ func (b *Bot) IsValid() *AppError {
|
|||||||
if b.UpdateAt == 0 {
|
if b.UpdateAt == 0 {
|
||||||
return NewAppError("Bot.IsValid", "model.bot.is_valid.update_at.app_error", b.Trace(), "", http.StatusBadRequest)
|
return NewAppError("Bot.IsValid", "model.bot.is_valid.update_at.app_error", b.Trace(), "", http.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
return b.IsValidCreate()
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// PreSave should be run before saving a new bot to the database.
|
// PreSave should be run before saving a new bot to the database.
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user