PLT-6113 Added initial unit tests for cmd package (#6086)

* Fixed app.CreateUser not using the provided locale

* Added initial unit tests for cmd package

* Disabled unit tests while we move to 'go build'
This commit is contained in:
Harrison Healey
2017-04-15 13:45:22 -04:00
committed by Christopher Speller
parent 24667e3e54
commit 461a0b3b7c
10 changed files with 525 additions and 20 deletions

View File

@@ -75,6 +75,14 @@ func (me *TestHelper) InitBasic() *TestHelper {
return me
}
func (me *TestHelper) MakeUsername() string {
return "un_" + model.NewId()
}
func (me *TestHelper) MakeEmail() string {
return "success_" + model.NewId() + "@simulator.amazonses.com"
}
func (me *TestHelper) CreateTeam() *model.Team {
id := model.NewId()
team := &model.Team{

View File

@@ -186,7 +186,9 @@ func CreateUser(user *model.User) (*model.User, *model.AppError) {
}
}
user.Locale = *utils.Cfg.LocalizationSettings.DefaultClientLocale
if _, ok := utils.GetSupportedLocales()[user.Locale]; !ok {
user.Locale = *utils.Cfg.LocalizationSettings.DefaultClientLocale
}
if ruser, err := createUser(user); err != nil {
return nil, err

View File

@@ -0,0 +1,106 @@
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package main
import (
"testing"
"github.com/mattermost/platform/app"
"github.com/mattermost/platform/model"
)
func TestParseChannelArg(t *testing.T) {
if team, channel := parseChannelArg("channel"); team != "" {
t.Fatal("got incorrect team", team)
} else if channel != "channel" {
t.Fatal("got incorrect channel", channel)
}
if team, channel := parseChannelArg("team:channel"); team != "team" {
t.Fatal("got incorrect team", team)
} else if channel != "channel" {
t.Fatal("got incorrect channel", channel)
}
}
func TestGetChannelFromChannelArg(t *testing.T) {
th := app.Setup().InitBasic()
team := th.BasicTeam
channel := th.BasicChannel
if found := getChannelFromChannelArg(""); found != nil {
t.Fatal("shoudn't have gotten a channel", found)
}
if found := getChannelFromChannelArg(channel.Id); found == nil || found.Id != channel.Id {
t.Fatal("got incorrect channel", found)
}
if found := getChannelFromChannelArg(model.NewId()); found != nil {
t.Fatal("shouldn't have gotten a channel that doesn't exist", found)
}
if found := getChannelFromChannelArg(channel.Name); found != nil {
t.Fatal("shouldn't have gotten a channel by name without team", found)
}
if found := getChannelFromChannelArg(team.Id + ":" + channel.Name); found == nil || found.Id != channel.Id {
t.Fatal("got incorrect channel", found)
}
if found := getChannelFromChannelArg(team.Name + ":" + channel.Name); found == nil || found.Id != channel.Id {
t.Fatal("got incorrect channel", found)
}
if found := getChannelFromChannelArg(team.Name + ":" + channel.Id); found == nil || found.Id != channel.Id {
t.Fatal("got incorrect channel", found)
}
if found := getChannelFromChannelArg("notateam" + ":" + channel.Name); found != nil {
t.Fatal("shouldn't have gotten a channel by name on incorrect team", found)
}
if found := getChannelFromChannelArg(team.Name + ":" + "notachannel"); found != nil {
t.Fatal("shouldn't have gotten a channel that doesn't exist", found)
}
}
func TestGetChannelsFromChannelArg(t *testing.T) {
th := app.Setup().InitBasic()
team := th.BasicTeam
channel := th.BasicChannel
channel2 := th.CreateChannel(team)
if found := getChannelsFromChannelArgs([]string{}); len(found) != 0 {
t.Fatal("shoudn't have gotten any channels", found)
}
if found := getChannelsFromChannelArgs([]string{channel.Id}); len(found) == 1 && found[0].Id != channel.Id {
t.Fatal("got incorrect channel", found)
}
if found := getChannelsFromChannelArgs([]string{team.Name + ":" + channel2.Name}); len(found) == 1 && found[0].Id != channel2.Id {
t.Fatal("got incorrect channel", found)
}
if found := getChannelsFromChannelArgs([]string{team.Name + ":" + channel.Name, team.Name + ":" + channel2.Name}); len(found) != 2 {
t.Fatal("got incorrect number of channels", found)
} else if !(found[0].Id == channel.Id && found[1].Id == channel2.Id) && !(found[1].Id == channel.Id && found[0].Id == channel2.Id) {
t.Fatal("got incorrect channels", found[0], found[1])
}
if found := getChannelsFromChannelArgs([]string{channel.Id, channel2.Id}); len(found) != 2 {
t.Fatal("got incorrect number of channels", found)
} else if !(found[0].Id == channel.Id && found[1].Id == channel2.Id) && !(found[1].Id == channel.Id && found[0].Id == channel2.Id) {
t.Fatal("got incorrect channels", found[0], found[1])
}
if found := getChannelsFromChannelArgs([]string{channel.Id, team.Name + ":" + channel2.Name}); len(found) != 2 {
t.Fatal("got incorrect number of channels", found)
} else if !(found[0].Id == channel.Id && found[1].Id == channel2.Id) && !(found[1].Id == channel.Id && found[0].Id == channel2.Id) {
t.Fatal("got incorrect channels", found[0], found[1])
}
}

View File

@@ -0,0 +1,45 @@
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package main
import (
"bytes"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
func runCommand(argString string) error {
err, _ := runCommandWithOutput(argString)
return err
}
func runCommandWithOutput(argString string) (error, string) {
// Set arguments on the root command
rootCmd.SetArgs(strings.Split(argString, " "))
defer rootCmd.SetArgs([]string{})
output := new(bytes.Buffer)
rootCmd.SetOutput(output)
defer rootCmd.SetOutput(nil)
// Executing the root command will call the necessary subcommand
cmd, err := rootCmd.ExecuteC()
// And clear the arguments on the subcommand that was actually called since they'd otherwise
// be used on the next call to this command
clearArgs(cmd)
return err, output.String()
}
func clearArgs(command *cobra.Command) {
command.Flags().VisitAll(clearFlag)
}
func clearFlag(flag *pflag.Flag) {
flag.Value.Set(flag.DefValue)
}

View File

@@ -23,21 +23,24 @@ import (
//ENTERPRISE_IMPORTS
func main() {
var rootCmd = &cobra.Command{
Use: "platform",
Short: "Open source, self-hosted Slack-alternative",
Long: `Mattermost offers workplace messaging across web, PC and phones with archiving, search and integration with your existing systems. Documentation available at https://docs.mattermost.com`,
RunE: runServerCmd,
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func init() {
rootCmd.PersistentFlags().StringP("config", "c", "config.json", "Configuration file to use.")
resetCmd.Flags().Bool("confirm", false, "Confirm you really want to delete everything and a DB backup has been performed.")
rootCmd.AddCommand(serverCmd, versionCmd, userCmd, teamCmd, licenseCmd, importCmd, resetCmd, channelCmd, rolesCmd, testCmd, ldapCmd)
}
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
var rootCmd = &cobra.Command{
Use: "platform",
Short: "Open source, self-hosted Slack-alternative",
Long: `Mattermost offers workplace messaging across web, PC and phones with archiving, search and integration with your existing systems. Documentation available at https://docs.mattermost.com`,
RunE: runServerCmd,
}
var resetCmd = &cobra.Command{

View File

@@ -0,0 +1,58 @@
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package main
import (
"testing"
"github.com/mattermost/platform/app"
"github.com/mattermost/platform/model"
)
func TestGetTeamFromTeamArg(t *testing.T) {
th := app.Setup().InitBasic()
team := th.BasicTeam
if found := getTeamFromTeamArg(""); found != nil {
t.Fatal("shoudn't have gotten a team", found)
}
if found := getTeamFromTeamArg(model.NewId()); found != nil {
t.Fatal("shoudn't have gotten a team", found)
}
if found := getTeamFromTeamArg(team.Id); found == nil || found.Id != team.Id {
t.Fatal("got incorrect team", found)
}
if found := getTeamFromTeamArg(team.Name); found == nil || found.Id != team.Id {
t.Fatal("got incorrect team", found)
}
}
func TestGetTeamsFromTeamArg(t *testing.T) {
th := app.Setup().InitBasic()
team := th.BasicTeam
team2 := th.CreateTeam()
if found := getTeamsFromTeamArgs([]string{}); len(found) != 0 {
t.Fatal("shoudn't have gotten any teams", found)
}
if found := getTeamsFromTeamArgs([]string{team.Id}); len(found) == 1 && found[0].Id != team.Id {
t.Fatal("got incorrect team", found)
}
if found := getTeamsFromTeamArgs([]string{team2.Name}); len(found) == 1 && found[0].Id != team2.Id {
t.Fatal("got incorrect team", found)
}
if found := getTeamsFromTeamArgs([]string{team.Name, team2.Id}); len(found) != 2 {
t.Fatal("got incorrect number of teams", found)
} else if !(found[0].Id == team.Id && found[1].Id == team2.Id) && !(found[1].Id == team.Id && found[0].Id == team2.Id) {
t.Fatal("got incorrect teams", found[0], found[1])
}
}

View File

@@ -170,22 +170,26 @@ func userActivateCmdF(cmd *cobra.Command, args []string) error {
func changeUsersActiveStatus(userArgs []string, active bool) {
users := getUsersFromUserArgs(userArgs)
for i, user := range users {
changeUserActiveStatus(user, userArgs[i], active)
err := changeUserActiveStatus(user, userArgs[i], active)
if err != nil {
CommandPrintErrorln(err.Error())
}
}
}
func changeUserActiveStatus(user *model.User, userArg string, activate bool) {
func changeUserActiveStatus(user *model.User, userArg string, activate bool) error {
if user == nil {
CommandPrintErrorln("Can't find user '" + userArg + "'")
return
return fmt.Errorf("Can't find user '%v'", userArg)
}
if user.IsLDAPUser() {
CommandPrintErrorln(utils.T("api.user.update_active.no_deactivate_ldap.app_error"))
return
return errors.New(utils.T("api.user.update_active.no_deactivate_ldap.app_error"))
}
if _, err := app.UpdateActive(user, activate); err != nil {
CommandPrintErrorln("Unable to change activation status of user: " + userArg)
return fmt.Errorf("Unable to change activation status of user: %v", userArg)
}
return nil
}
func userDeactivateCmdF(cmd *cobra.Command, args []string) error {
@@ -258,20 +262,26 @@ func userInviteCmdF(cmd *cobra.Command, args []string) error {
teams := getTeamsFromTeamArgs(args[1:])
for i, team := range teams {
inviteUser(email, team, args[i+1])
err := inviteUser(email, team, args[i+1])
if err != nil {
CommandPrintErrorln(err.Error())
}
}
return nil
}
func inviteUser(email string, team *model.Team, teamArg string) {
func inviteUser(email string, team *model.Team, teamArg string) error {
invites := []string{email}
if team == nil {
CommandPrintErrorln("Can't find team '" + teamArg + "'")
return
return fmt.Errorf("Can't find team '%v'", teamArg)
}
app.SendInviteEmails(team, "Administrator", invites, *utils.Cfg.ServiceSettings.SiteURL)
CommandPrettyPrintln("Invites may or may not have been sent.")
return nil
}
func resetUserPasswordCmdF(cmd *cobra.Command, args []string) error {

View File

@@ -0,0 +1,212 @@
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package main
import (
"testing"
"github.com/mattermost/platform/app"
"github.com/mattermost/platform/model"
)
func TestChangeUserActiveStatus(t *testing.T) {
th := app.Setup().InitBasic()
user := th.BasicUser
if err := changeUserActiveStatus(nil, "user", false); err == nil {
t.Fatal("should've returned error when user doesn't exist")
}
if err := changeUserActiveStatus(user, user.Username, false); err != nil {
t.Fatal(err)
} else if user, _ = app.GetUser(user.Id); user.DeleteAt == 0 {
t.Fatal("should've deactivated user")
}
if err := changeUserActiveStatus(user, user.Username, true); err != nil {
t.Fatal(err)
} else if user, _ := app.GetUser(user.Id); user.DeleteAt != 0 {
t.Fatal("should've activated user")
}
}
func TestChangeUsersActiveStatus(t *testing.T) {
th := app.Setup().InitBasic()
user := th.BasicUser
user2 := th.CreateUser()
changeUsersActiveStatus([]string{user.Username, user2.Id}, false)
if user, _ = app.GetUser(user.Id); user.DeleteAt == 0 {
t.Fatal("should've deactivated user")
} else if user2, _ = app.GetUser(user2.Id); user2.DeleteAt == 0 {
t.Fatal("should've deactivated user")
}
changeUsersActiveStatus([]string{user.Username, user2.Id}, true)
if user, _ = app.GetUser(user.Id); user.DeleteAt != 0 {
t.Fatal("should've activated user")
} else if user2, _ = app.GetUser(user2.Id); user2.DeleteAt != 0 {
t.Fatal("should've activated user")
}
}
func TestUserActivateDeactivateCmdF(t *testing.T) {
th := app.Setup().InitBasic()
user := th.BasicUser
user2 := th.CreateUser()
userDeactivateCmdF(userDeactivateCmd, []string{user.Username, user2.Id})
if user, _ = app.GetUser(user.Id); user.DeleteAt == 0 {
t.Fatal("should've deactivated user")
} else if user2, _ = app.GetUser(user2.Id); user2.DeleteAt == 0 {
t.Fatal("should've deactivated user")
}
userActivateCmdF(userActivateCmd, []string{user.Username, user2.Id})
if user, _ = app.GetUser(user.Id); user.DeleteAt != 0 {
t.Fatal("should've activated user")
} else if user2, _ = app.GetUser(user2.Id); user2.DeleteAt != 0 {
t.Fatal("should've activated user")
}
}
func TestUserActivateDeactivateCmd(t *testing.T) {
th := app.Setup().InitBasic()
user := th.BasicUser
user2 := th.CreateUser()
if err := runCommand("user deactivate " + user.Username + " " + user2.Id); err != nil {
t.Fatal(err)
} else if user, _ = app.GetUser(user.Id); user.DeleteAt == 0 {
t.Fatal("should've deactivated user")
} else if user2, _ = app.GetUser(user2.Id); user2.DeleteAt == 0 {
t.Fatal("should've deactivated user")
}
if err := runCommand("user activate " + user.Id + " " + user2.Username); err != nil {
t.Fatal(err)
} else if user, _ = app.GetUser(user.Id); user.DeleteAt != 0 {
t.Fatal("should've activated user")
} else if user2, _ = app.GetUser(user2.Id); user2.DeleteAt != 0 {
t.Fatal("should've activated user")
}
}
func TestUserCreateCmd(t *testing.T) {
th := app.Setup().InitBasic()
if err := runCommand("user create"); err == nil {
t.Fatal("should've failed without any arguments")
}
username := th.MakeUsername()
email := th.MakeEmail()
if err := runCommand("user create --username " + username + " --email " + email + " --password " + model.NewId()); err != nil {
t.Fatal(err)
} else if user, err := app.GetUserByUsername(username); err != nil {
t.Fatal(err.Message)
} else if user.Username != username {
t.Fatal("should've set correct username")
} else if user.Email != email {
t.Fatal("should've set correct email")
}
username = th.MakeUsername()
nickname := model.NewId()
firstName := model.NewId()
lastName := model.NewId()
locale := "fr"
if err := runCommand("user create --username " + username + " --email " + th.MakeEmail() + " --password " + model.NewId() +
" --nickname " + nickname + " --firstname " + firstName + " --lastname " + lastName + " --locale " + locale); err != nil {
t.Fatal(err)
} else if user, err := app.GetUserByUsername(username); err != nil {
t.Fatal(err)
} else if user.Nickname != nickname {
t.Fatal("should've set correct nickname")
} else if user.FirstName != firstName {
t.Fatal("should've set correct first name")
} else if user.LastName != lastName {
t.Fatal("should've set correct last name")
} else if user.Locale != locale {
t.Fatal("should've set correct locale", user.Locale)
} else if user.Roles != "system_user" {
t.Fatal("should've set correct roles for user")
}
username = th.MakeUsername()
if err := runCommand("user create --username " + username + " --email " + th.MakeEmail() + " --password " + model.NewId() + " --system_admin"); err != nil {
t.Fatal(err)
} else if user, err := app.GetUserByUsername(username); err != nil {
t.Fatal(err)
} else if user.Roles != "system_user system_admin" {
t.Fatal("should've set correct roles for system admin")
}
if err := runCommand("user create --email " + th.MakeEmail() + " --password " + model.NewId()); err == nil {
t.Fatal("should've failed without username")
}
if err := runCommand("user create --username " + th.MakeUsername() + " --email " + th.MakeEmail()); err == nil {
t.Fatal("should've failed without password")
}
if err := runCommand("user create --username " + th.MakeUsername() + " --password " + model.NewId()); err == nil {
t.Fatal("should've failed without email")
}
}
func TestInviteUser(t *testing.T) {
th := app.Setup().InitBasic()
team := th.CreateTeam()
if err := inviteUser(th.MakeEmail(), nil, "faketeam"); err == nil {
t.Fatal("should've failed with nonexistent team")
}
if err := inviteUser(th.MakeEmail(), team, team.Name); err != nil {
t.Fatal(err)
}
// Nothing else to test here since this just fires off an email
}
func TestUserInviteCmd(t *testing.T) {
th := app.Setup().InitBasic()
team := th.BasicTeam
team2 := th.CreateTeam()
if err := runCommand("user invite"); err == nil {
t.Fatal("should've failed without any arguments")
}
if err := runCommand("user invite " + th.MakeEmail()); err == nil {
t.Fatal("should've failed with 1 argument")
}
if err := runCommand("user invite " + th.MakeEmail() + " " + team.Id); err != nil {
t.Fatal(err)
}
if err := runCommand("user invite " + th.MakeEmail() + " " + team.Name); err != nil {
t.Fatal(err)
}
if err := runCommand("user invite " + th.MakeEmail() + " " + team.Id + " " + team2.Name); err != nil {
t.Fatal(err)
}
if err := runCommand("user invite " + th.MakeEmail() + " " + team.Id + " " + team2.Name + " " + "faketeam"); err != nil {
t.Fatal(err)
}
}

View File

@@ -0,0 +1,58 @@
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
package main
import (
"testing"
"github.com/mattermost/platform/app"
"github.com/mattermost/platform/model"
)
func TestGetUserFromUserArg(t *testing.T) {
th := app.Setup().InitBasic()
user := th.BasicUser
if found := getUserFromUserArg(""); found != nil {
t.Fatal("shoudn't have gotten a user", found)
}
if found := getUserFromUserArg(model.NewId()); found != nil {
t.Fatal("shoudn't have gotten a user", found)
}
if found := getUserFromUserArg(user.Id); found == nil || found.Id != user.Id {
t.Fatal("got incorrect user", found)
}
if found := getUserFromUserArg(user.Username); found == nil || found.Id != user.Id {
t.Fatal("got incorrect user", found)
}
}
func TestGetUsersFromUserArg(t *testing.T) {
th := app.Setup().InitBasic()
user := th.BasicUser
user2 := th.CreateUser()
if found := getUsersFromUserArgs([]string{}); len(found) != 0 {
t.Fatal("shoudn't have gotten any users", found)
}
if found := getUsersFromUserArgs([]string{user.Id}); len(found) == 1 && found[0].Id != user.Id {
t.Fatal("got incorrect user", found)
}
if found := getUsersFromUserArgs([]string{user2.Username}); len(found) == 1 && found[0].Id != user2.Id {
t.Fatal("got incorrect user", found)
}
if found := getUsersFromUserArgs([]string{user.Username, user2.Id}); len(found) != 2 {
t.Fatal("got incorrect number of users", found)
} else if !(found[0].Id == user.Id && found[1].Id == user2.Id) && !(found[1].Id == user.Id && found[0].Id == user2.Id) {
t.Fatal("got incorrect users", found[0], found[1])
}
}

View File

@@ -66,6 +66,8 @@ func FindDir(dir string) string {
fileName, _ = filepath.Abs("./" + dir + "/")
} else if _, err := os.Stat("../" + dir + "/"); err == nil {
fileName, _ = filepath.Abs("../" + dir + "/")
} else if _, err := os.Stat("../../" + dir + "/"); err == nil {
fileName, _ = filepath.Abs("../../" + dir + "/")
}
return fileName + "/"
@@ -257,6 +259,7 @@ func LoadConfig(fileName string) {
viper.SetConfigType("json")
viper.AddConfigPath("./config")
viper.AddConfigPath("../config")
viper.AddConfigPath("../../config")
viper.AddConfigPath(".")
configReadErr := viper.ReadInConfig()