Fix CLI to make -team_name optional or not required. I also added a -join_team cmd. (#2804)
* Fixing CLI and adding unit tests * Adding the upgrade_db_30 to the help text * Adding the upgrade_db_30 to the help text * Adding the upgrade_db_30 to the help text * Fixing CLI tests * Fixing typo
Этот коммит содержится в:
коммит произвёл
Christopher Speller
родитель
a5fc93fdc6
Коммит
d962e175f8
185
api/cli_test.go
Обычный файл
185
api/cli_test.go
Обычный файл
@@ -0,0 +1,185 @@
|
|||||||
|
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See License.txt for license information.
|
||||||
|
|
||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os/exec"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mattermost/platform/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
var disableCliTests bool = false
|
||||||
|
|
||||||
|
func TestCliVersion(t *testing.T) {
|
||||||
|
if disableCliTests {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command("bash", "-c", `go run ../mattermost.go -version`)
|
||||||
|
output, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
t.Log(string(output))
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCliCreateTeam(t *testing.T) {
|
||||||
|
if disableCliTests {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
th := Setup().InitSystemAdmin()
|
||||||
|
|
||||||
|
id := model.NewId()
|
||||||
|
email := "success+" + id + "@simulator.amazonses.com"
|
||||||
|
name := "name" + id
|
||||||
|
|
||||||
|
cmd := exec.Command("bash", "-c", `go run ../mattermost.go -create_team -team_name="`+name+`" -email="`+email+`"`)
|
||||||
|
output, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
t.Log(string(output))
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
found := th.SystemAdminClient.Must(th.SystemAdminClient.FindTeamByName(name)).Data.(bool)
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
t.Fatal("Failed to create Team")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCliCreateUserWithTeam(t *testing.T) {
|
||||||
|
if disableCliTests {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
th := Setup().InitSystemAdmin()
|
||||||
|
|
||||||
|
id := model.NewId()
|
||||||
|
email := "success+" + id + "@simulator.amazonses.com"
|
||||||
|
username := "name" + id
|
||||||
|
|
||||||
|
cmd := exec.Command("bash", "-c", `go run ../mattermost.go -create_user -team_name="`+th.SystemAdminTeam.Name+`" -email="`+email+`" -password="mypassword" -username="`+username+`"`)
|
||||||
|
output, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
t.Log(string(output))
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
profiles := th.SystemAdminClient.Must(th.SystemAdminClient.GetProfilesForTeam(th.SystemAdminTeam.Id, "")).Data.(map[string]*model.User)
|
||||||
|
|
||||||
|
found := false
|
||||||
|
|
||||||
|
for _, user := range profiles {
|
||||||
|
if user.Email == email {
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
t.Fatal("Failed to create User")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCliCreateUserWithoutTeam(t *testing.T) {
|
||||||
|
if disableCliTests {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
Setup()
|
||||||
|
id := model.NewId()
|
||||||
|
email := "success+" + id + "@simulator.amazonses.com"
|
||||||
|
username := "name" + id
|
||||||
|
|
||||||
|
cmd := exec.Command("bash", "-c", `go run ../mattermost.go -create_user -email="`+email+`" -password="mypassword" -username="`+username+`"`)
|
||||||
|
output, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
t.Log(string(output))
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if result := <-Srv.Store.User().GetByEmail(email); result.Err != nil {
|
||||||
|
t.Fatal()
|
||||||
|
} else {
|
||||||
|
user := result.Data.(*model.User)
|
||||||
|
if user.Email != email {
|
||||||
|
t.Fatal()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCliAssignRole(t *testing.T) {
|
||||||
|
if disableCliTests {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
th := Setup().InitBasic()
|
||||||
|
|
||||||
|
cmd := exec.Command("bash", "-c", `go run ../mattermost.go -assign_role -email="`+th.BasicUser.Email+`" -role="system_admin"`)
|
||||||
|
output, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
t.Log(string(output))
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if result := <-Srv.Store.User().GetByEmail(th.BasicUser.Email); result.Err != nil {
|
||||||
|
t.Fatal()
|
||||||
|
} else {
|
||||||
|
user := result.Data.(*model.User)
|
||||||
|
if user.Roles != "system_admin" {
|
||||||
|
t.Fatal()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCliJoinTeam(t *testing.T) {
|
||||||
|
if disableCliTests {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
th := Setup().InitSystemAdmin().InitBasic()
|
||||||
|
|
||||||
|
cmd := exec.Command("bash", "-c", `go run ../mattermost.go -join_team -team_name="`+th.SystemAdminTeam.Name+`" -email="`+th.BasicUser.Email+`"`)
|
||||||
|
output, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
t.Log(string(output))
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
profiles := th.SystemAdminClient.Must(th.SystemAdminClient.GetProfilesForTeam(th.SystemAdminTeam.Id, "")).Data.(map[string]*model.User)
|
||||||
|
|
||||||
|
found := false
|
||||||
|
|
||||||
|
for _, user := range profiles {
|
||||||
|
if user.Email == th.BasicUser.Email {
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
t.Fatal("Failed to create User")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCliResetPassword(t *testing.T) {
|
||||||
|
if disableCliTests {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
th := Setup().InitBasic()
|
||||||
|
|
||||||
|
cmd := exec.Command("bash", "-c", `go run ../mattermost.go -reset_password -email="`+th.BasicUser.Email+`" -password="password2"`)
|
||||||
|
output, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
t.Log(string(output))
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
th.BasicClient.Logout()
|
||||||
|
th.BasicUser.Password = "password2"
|
||||||
|
th.LoginBasic()
|
||||||
|
}
|
||||||
123
mattermost.go
123
mattermost.go
@@ -41,6 +41,7 @@ var flagCmdUpdateDb30 bool
|
|||||||
var flagCmdCreateTeam bool
|
var flagCmdCreateTeam bool
|
||||||
var flagCmdCreateUser bool
|
var flagCmdCreateUser bool
|
||||||
var flagCmdAssignRole bool
|
var flagCmdAssignRole bool
|
||||||
|
var flagCmdJoinTeam bool
|
||||||
var flagCmdVersion bool
|
var flagCmdVersion bool
|
||||||
var flagCmdResetPassword bool
|
var flagCmdResetPassword bool
|
||||||
var flagCmdResetMfa bool
|
var flagCmdResetMfa bool
|
||||||
@@ -244,6 +245,7 @@ func parseCmds() {
|
|||||||
flag.BoolVar(&flagCmdCreateTeam, "create_team", false, "")
|
flag.BoolVar(&flagCmdCreateTeam, "create_team", false, "")
|
||||||
flag.BoolVar(&flagCmdCreateUser, "create_user", false, "")
|
flag.BoolVar(&flagCmdCreateUser, "create_user", false, "")
|
||||||
flag.BoolVar(&flagCmdAssignRole, "assign_role", false, "")
|
flag.BoolVar(&flagCmdAssignRole, "assign_role", false, "")
|
||||||
|
flag.BoolVar(&flagCmdJoinTeam, "join_team", false, "")
|
||||||
flag.BoolVar(&flagCmdVersion, "version", false, "")
|
flag.BoolVar(&flagCmdVersion, "version", false, "")
|
||||||
flag.BoolVar(&flagCmdResetPassword, "reset_password", false, "")
|
flag.BoolVar(&flagCmdResetPassword, "reset_password", false, "")
|
||||||
flag.BoolVar(&flagCmdResetMfa, "reset_mfa", false, "")
|
flag.BoolVar(&flagCmdResetMfa, "reset_mfa", false, "")
|
||||||
@@ -258,6 +260,7 @@ func parseCmds() {
|
|||||||
flagRunCmds = (flagCmdCreateTeam ||
|
flagRunCmds = (flagCmdCreateTeam ||
|
||||||
flagCmdCreateUser ||
|
flagCmdCreateUser ||
|
||||||
flagCmdAssignRole ||
|
flagCmdAssignRole ||
|
||||||
|
flagCmdJoinTeam ||
|
||||||
flagCmdResetPassword ||
|
flagCmdResetPassword ||
|
||||||
flagCmdResetMfa ||
|
flagCmdResetMfa ||
|
||||||
flagCmdVersion ||
|
flagCmdVersion ||
|
||||||
@@ -273,6 +276,7 @@ func runCmds() {
|
|||||||
cmdCreateTeam()
|
cmdCreateTeam()
|
||||||
cmdCreateUser()
|
cmdCreateUser()
|
||||||
cmdAssignRole()
|
cmdAssignRole()
|
||||||
|
cmdJoinTeam()
|
||||||
cmdResetPassword()
|
cmdResetPassword()
|
||||||
cmdResetMfa()
|
cmdResetMfa()
|
||||||
cmdPermDeleteUser()
|
cmdPermDeleteUser()
|
||||||
@@ -562,12 +566,6 @@ func cmdCreateTeam() {
|
|||||||
|
|
||||||
func cmdCreateUser() {
|
func cmdCreateUser() {
|
||||||
if flagCmdCreateUser {
|
if flagCmdCreateUser {
|
||||||
if len(flagTeamName) == 0 {
|
|
||||||
fmt.Fprintln(os.Stderr, "flag needs an argument: -team_name")
|
|
||||||
flag.Usage()
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(flagEmail) == 0 {
|
if len(flagEmail) == 0 {
|
||||||
fmt.Fprintln(os.Stderr, "flag needs an argument: -email")
|
fmt.Fprintln(os.Stderr, "flag needs an argument: -email")
|
||||||
flag.Usage()
|
flag.Usage()
|
||||||
@@ -584,14 +582,21 @@ func cmdCreateUser() {
|
|||||||
user := &model.User{}
|
user := &model.User{}
|
||||||
user.Email = flagEmail
|
user.Email = flagEmail
|
||||||
user.Password = flagPassword
|
user.Password = flagPassword
|
||||||
splits := strings.Split(strings.Replace(flagEmail, "@", " ", -1), " ")
|
|
||||||
user.Username = splits[0]
|
|
||||||
|
|
||||||
if result := <-api.Srv.Store.Team().GetByName(flagTeamName); result.Err != nil {
|
if len(flagUsername) == 0 {
|
||||||
l4g.Error("%v", result.Err)
|
splits := strings.Split(strings.Replace(flagEmail, "@", " ", -1), " ")
|
||||||
flushLogAndExit(1)
|
user.Username = splits[0]
|
||||||
} else {
|
} else {
|
||||||
team = result.Data.(*model.Team)
|
user.Username = flagUsername
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(flagTeamName) > 0 {
|
||||||
|
if result := <-api.Srv.Store.Team().GetByName(flagTeamName); result.Err != nil {
|
||||||
|
l4g.Error("%v", result.Err)
|
||||||
|
flushLogAndExit(1)
|
||||||
|
} else {
|
||||||
|
team = result.Data.(*model.Team)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ruser, err := api.CreateUser(user)
|
ruser, err := api.CreateUser(user)
|
||||||
@@ -602,10 +607,12 @@ func cmdCreateUser() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = api.JoinUserToTeam(team, ruser)
|
if team != nil {
|
||||||
if err != nil {
|
err = api.JoinUserToTeam(team, ruser)
|
||||||
l4g.Error("%v", err)
|
if err != nil {
|
||||||
flushLogAndExit(1)
|
l4g.Error("%v", err)
|
||||||
|
flushLogAndExit(1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
@@ -626,12 +633,6 @@ func cmdVersion() {
|
|||||||
|
|
||||||
func cmdAssignRole() {
|
func cmdAssignRole() {
|
||||||
if flagCmdAssignRole {
|
if flagCmdAssignRole {
|
||||||
if len(flagTeamName) == 0 {
|
|
||||||
fmt.Fprintln(os.Stderr, "flag needs an argument: -team_name")
|
|
||||||
flag.Usage()
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(flagEmail) == 0 {
|
if len(flagEmail) == 0 {
|
||||||
fmt.Fprintln(os.Stderr, "flag needs an argument: -email")
|
fmt.Fprintln(os.Stderr, "flag needs an argument: -email")
|
||||||
flag.Usage()
|
flag.Usage()
|
||||||
@@ -662,10 +663,10 @@ func cmdAssignRole() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func cmdResetPassword() {
|
func cmdJoinTeam() {
|
||||||
if flagCmdResetPassword {
|
if flagCmdJoinTeam {
|
||||||
if len(flagTeamName) == 0 {
|
if len(flagTeamName) == 0 {
|
||||||
fmt.Fprintln(os.Stderr, "flag needs an argument: -team_name")
|
fmt.Fprintln(os.Stderr, "flag needs an argument: -email")
|
||||||
flag.Usage()
|
flag.Usage()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
@@ -676,6 +677,40 @@ func cmdResetPassword() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var team *model.Team
|
||||||
|
if result := <-api.Srv.Store.Team().GetByName(flagTeamName); result.Err != nil {
|
||||||
|
l4g.Error("%v", result.Err)
|
||||||
|
flushLogAndExit(1)
|
||||||
|
} else {
|
||||||
|
team = result.Data.(*model.Team)
|
||||||
|
}
|
||||||
|
|
||||||
|
var user *model.User
|
||||||
|
if result := <-api.Srv.Store.User().GetByEmail(flagEmail); result.Err != nil {
|
||||||
|
l4g.Error("%v", result.Err)
|
||||||
|
flushLogAndExit(1)
|
||||||
|
} else {
|
||||||
|
user = result.Data.(*model.User)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := api.JoinUserToTeam(team, user)
|
||||||
|
if err != nil {
|
||||||
|
l4g.Error("%v", err)
|
||||||
|
flushLogAndExit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func cmdResetPassword() {
|
||||||
|
if flagCmdResetPassword {
|
||||||
|
if len(flagEmail) == 0 {
|
||||||
|
fmt.Fprintln(os.Stderr, "flag needs an argument: -email")
|
||||||
|
flag.Usage()
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
if len(flagPassword) == 0 {
|
if len(flagPassword) == 0 {
|
||||||
fmt.Fprintln(os.Stderr, "flag needs an argument: -password")
|
fmt.Fprintln(os.Stderr, "flag needs an argument: -password")
|
||||||
flag.Usage()
|
flag.Usage()
|
||||||
@@ -949,12 +984,10 @@ FLAGS:
|
|||||||
|
|
||||||
-team_name="name" The team name used in other commands
|
-team_name="name" The team name used in other commands
|
||||||
|
|
||||||
-role="admin" The role used in other commands
|
-role="system_admin" The role used in other commands
|
||||||
valid values are
|
valid values are
|
||||||
"" - The empty role is basic user
|
"" - The empty role is basic user
|
||||||
permissions
|
permissions
|
||||||
"admin" - Represents a team admin and
|
|
||||||
is used to help administer one team.
|
|
||||||
"system_admin" - Represents a system
|
"system_admin" - Represents a system
|
||||||
admin who has access to all teams
|
admin who has access to all teams
|
||||||
and configuration settings.
|
and configuration settings.
|
||||||
@@ -964,22 +997,28 @@ COMMANDS:
|
|||||||
Example:
|
Example:
|
||||||
platform -create_team -team_name="name" -email="user@example.com"
|
platform -create_team -team_name="name" -email="user@example.com"
|
||||||
|
|
||||||
-create_user Creates a user. It requires the -team_name,
|
-create_user Creates a user. It requires the -email and -password flag
|
||||||
-email and -password flag to create a user.
|
and -team_name and -username are optional to create a user.
|
||||||
Example:
|
Example:
|
||||||
platform -create_user -team_name="name" -email="user@example.com" -password="mypassword"
|
platform -create_user -team_name="name" -email="user@example.com" -password="mypassword" -username="user"
|
||||||
|
|
||||||
-assign_role Assigns role to a user. It requires the -role,
|
-join_team Joins a user to the team. It required the -email and
|
||||||
-email and -team_name flag. You may need to log out
|
-team_name. You may need to logout of your current session
|
||||||
|
for the new team to be applied.
|
||||||
|
Example:
|
||||||
|
platform -join_team -email="user@example.com" -team_name="name"
|
||||||
|
|
||||||
|
-assign_role Assigns role to a user. It requires the -role and
|
||||||
|
-email flag. You may need to log out
|
||||||
of your current sessions for the new role to be
|
of your current sessions for the new role to be
|
||||||
applied.
|
applied.
|
||||||
Example:
|
Example:
|
||||||
platform -assign_role -team_name="name" -email="user@example.com" -role="admin"
|
platform -assign_role -email="user@example.com" -role="system_admin"
|
||||||
|
|
||||||
-reset_password Resets the password for a user. It requires the
|
-reset_password Resets the password for a user. It requires the
|
||||||
-team_name, -email and -password flag.
|
-email and -password flag.
|
||||||
Example:
|
Example:
|
||||||
platform -reset_password -team_name="name" -email="user@example.com" -password="newpassword"
|
platform -reset_password -email="user@example.com" -password="newpassword"
|
||||||
|
|
||||||
-reset_mfa Turns off multi-factor authentication for a user. It requires the
|
-reset_mfa Turns off multi-factor authentication for a user. It requires the
|
||||||
-email or -username flag.
|
-email or -username flag.
|
||||||
@@ -987,11 +1026,11 @@ COMMANDS:
|
|||||||
platform -reset_mfa -username="someuser"
|
platform -reset_mfa -username="someuser"
|
||||||
|
|
||||||
-reset_database Completely erases the database causing the loss of all data. This
|
-reset_database Completely erases the database causing the loss of all data. This
|
||||||
will reset Mattermost to it's initial state. (note this will not
|
will reset Mattermost to it's initial state. (note this will not
|
||||||
erase your configuration.)
|
erase your configuration.)
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
platform -reset_mfa -username="someuser"
|
platform -reset_database
|
||||||
|
|
||||||
-permanent_delete_user Permanently deletes a user and all related information
|
-permanent_delete_user Permanently deletes a user and all related information
|
||||||
including posts from the database. It requires the
|
including posts from the database. It requires the
|
||||||
@@ -1019,6 +1058,12 @@ COMMANDS:
|
|||||||
Example:
|
Example:
|
||||||
platform -upload_license -license="/path/to/license/example.mattermost-license"
|
platform -upload_license -license="/path/to/license/example.mattermost-license"
|
||||||
|
|
||||||
|
-upgrade_db_30 Upgrades the database from a version 2.x schema to version 3 see
|
||||||
|
http://www.mattermost.org/upgrading-to-mattermost-3-0/
|
||||||
|
|
||||||
|
Example:
|
||||||
|
platform -upgrade_db_30
|
||||||
|
|
||||||
-version Display the current of the Mattermost platform
|
-version Display the current of the Mattermost platform
|
||||||
|
|
||||||
-help Displays this help page`
|
-help Displays this help page`
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user