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
This commit is contained in:
Corey Hulen
2016-04-27 05:26:45 -07:00
committed by Christopher Speller
parent a5fc93fdc6
commit d962e175f8
2 changed files with 269 additions and 39 deletions

185
api/cli_test.go Normal file
View File

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

View File

@@ -41,6 +41,7 @@ var flagCmdUpdateDb30 bool
var flagCmdCreateTeam bool
var flagCmdCreateUser bool
var flagCmdAssignRole bool
var flagCmdJoinTeam bool
var flagCmdVersion bool
var flagCmdResetPassword bool
var flagCmdResetMfa bool
@@ -244,6 +245,7 @@ func parseCmds() {
flag.BoolVar(&flagCmdCreateTeam, "create_team", false, "")
flag.BoolVar(&flagCmdCreateUser, "create_user", false, "")
flag.BoolVar(&flagCmdAssignRole, "assign_role", false, "")
flag.BoolVar(&flagCmdJoinTeam, "join_team", false, "")
flag.BoolVar(&flagCmdVersion, "version", false, "")
flag.BoolVar(&flagCmdResetPassword, "reset_password", false, "")
flag.BoolVar(&flagCmdResetMfa, "reset_mfa", false, "")
@@ -258,6 +260,7 @@ func parseCmds() {
flagRunCmds = (flagCmdCreateTeam ||
flagCmdCreateUser ||
flagCmdAssignRole ||
flagCmdJoinTeam ||
flagCmdResetPassword ||
flagCmdResetMfa ||
flagCmdVersion ||
@@ -273,6 +276,7 @@ func runCmds() {
cmdCreateTeam()
cmdCreateUser()
cmdAssignRole()
cmdJoinTeam()
cmdResetPassword()
cmdResetMfa()
cmdPermDeleteUser()
@@ -562,12 +566,6 @@ func cmdCreateTeam() {
func cmdCreateUser() {
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 {
fmt.Fprintln(os.Stderr, "flag needs an argument: -email")
flag.Usage()
@@ -584,14 +582,21 @@ func cmdCreateUser() {
user := &model.User{}
user.Email = flagEmail
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 {
l4g.Error("%v", result.Err)
flushLogAndExit(1)
if len(flagUsername) == 0 {
splits := strings.Split(strings.Replace(flagEmail, "@", " ", -1), " ")
user.Username = splits[0]
} 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)
@@ -602,10 +607,12 @@ func cmdCreateUser() {
}
}
err = api.JoinUserToTeam(team, ruser)
if err != nil {
l4g.Error("%v", err)
flushLogAndExit(1)
if team != nil {
err = api.JoinUserToTeam(team, ruser)
if err != nil {
l4g.Error("%v", err)
flushLogAndExit(1)
}
}
os.Exit(0)
@@ -626,12 +633,6 @@ func cmdVersion() {
func cmdAssignRole() {
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 {
fmt.Fprintln(os.Stderr, "flag needs an argument: -email")
flag.Usage()
@@ -662,10 +663,10 @@ func cmdAssignRole() {
}
}
func cmdResetPassword() {
if flagCmdResetPassword {
func cmdJoinTeam() {
if flagCmdJoinTeam {
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()
os.Exit(1)
}
@@ -676,6 +677,40 @@ func cmdResetPassword() {
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 {
fmt.Fprintln(os.Stderr, "flag needs an argument: -password")
flag.Usage()
@@ -949,12 +984,10 @@ FLAGS:
-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
"" - The empty role is basic user
permissions
"admin" - Represents a team admin and
is used to help administer one team.
"system_admin" - Represents a system
admin who has access to all teams
and configuration settings.
@@ -964,22 +997,28 @@ COMMANDS:
Example:
platform -create_team -team_name="name" -email="user@example.com"
-create_user Creates a user. It requires the -team_name,
-email and -password flag to create a user.
-create_user Creates a user. It requires the -email and -password flag
and -team_name and -username are optional to create a user.
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,
-email and -team_name flag. You may need to log out
-join_team Joins a user to the team. It required the -email and
-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
applied.
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
-team_name, -email and -password flag.
-email and -password flag.
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
-email or -username flag.
@@ -987,11 +1026,11 @@ COMMANDS:
platform -reset_mfa -username="someuser"
-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
erase your configuration.)
will reset Mattermost to it's initial state. (note this will not
erase your configuration.)
Example:
platform -reset_mfa -username="someuser"
platform -reset_database
-permanent_delete_user Permanently deletes a user and all related information
including posts from the database. It requires the
@@ -1019,6 +1058,12 @@ COMMANDS:
Example:
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
-help Displays this help page`