[MM-14719] convert users to bots through cmd (#10672)
* [MM-14719] convert users to bots through cmd * address review comments, add / fix unit tests * change command from "modify" to "convert"; review comments * code review comments, clean up
Этот коммит содержится в:
коммит произвёл
Christopher Speller
родитель
dce6cb601f
Коммит
fc15eda37f
@@ -176,3 +176,12 @@ func (a *App) disableUserBots(userId string) *model.AppError {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ConvertUserToBot converts a user to bot
|
||||
func (a *App) ConvertUserToBot(user *model.User) (*model.Bot, *model.AppError) {
|
||||
result := <-a.Srv.Store.Bot().Save(model.BotFromUser(user))
|
||||
if result.Err != nil {
|
||||
return nil, result.Err
|
||||
}
|
||||
return result.Data.(*model.Bot), nil
|
||||
}
|
||||
|
||||
@@ -544,6 +544,48 @@ func TestDisableUserBots(t *testing.T) {
|
||||
require.Nil(t, err)
|
||||
}
|
||||
|
||||
func TestConvertUserToBot(t *testing.T) {
|
||||
t.Run("invalid user", func(t *testing.T) {
|
||||
t.Run("invalid user id", func(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
_, err := th.App.ConvertUserToBot(&model.User{
|
||||
Username: "username",
|
||||
Id: "",
|
||||
})
|
||||
require.NotNil(t, err)
|
||||
require.Equal(t, "model.bot.is_valid.user_id.app_error", err.Id)
|
||||
})
|
||||
|
||||
t.Run("invalid username", func(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
_, err := th.App.ConvertUserToBot(&model.User{
|
||||
Username: "invalid username",
|
||||
Id: th.BasicUser.Id,
|
||||
})
|
||||
require.NotNil(t, err)
|
||||
require.Equal(t, "model.bot.is_valid.username.app_error", err.Id)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("valid user", func(t *testing.T) {
|
||||
th := Setup(t).InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
bot, err := th.App.ConvertUserToBot(&model.User{
|
||||
Username: "username",
|
||||
Id: th.BasicUser.Id,
|
||||
})
|
||||
require.Nil(t, err)
|
||||
defer th.App.PermanentDeleteBot(bot.UserId)
|
||||
assert.Equal(t, "username", bot.Username)
|
||||
assert.Equal(t, th.BasicUser.Id, bot.OwnerId)
|
||||
})
|
||||
}
|
||||
|
||||
func sToP(s string) *string {
|
||||
return &s
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user