diff options
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/platform/channel.go | 2 | ||||
-rw-r--r-- | cmd/platform/channelargs.go | 2 | ||||
-rw-r--r-- | cmd/platform/channelargs_test.go.disabled | 106 | ||||
-rw-r--r-- | cmd/platform/cmdtestlib.go | 45 | ||||
-rw-r--r-- | cmd/platform/config.go | 66 | ||||
-rw-r--r-- | cmd/platform/import.go | 2 | ||||
-rw-r--r-- | cmd/platform/init.go | 1 | ||||
-rw-r--r-- | cmd/platform/ldap.go | 2 | ||||
-rw-r--r-- | cmd/platform/license.go | 2 | ||||
-rw-r--r-- | cmd/platform/mattermost.go | 24 | ||||
-rw-r--r-- | cmd/platform/output.go | 2 | ||||
-rw-r--r-- | cmd/platform/roles.go | 2 | ||||
-rw-r--r-- | cmd/platform/server.go | 4 | ||||
-rw-r--r-- | cmd/platform/team.go | 2 | ||||
-rw-r--r-- | cmd/platform/teamargs.go | 2 | ||||
-rw-r--r-- | cmd/platform/teamargs_test.go.disabled | 58 | ||||
-rw-r--r-- | cmd/platform/test.go | 2 | ||||
-rw-r--r-- | cmd/platform/user.go | 83 | ||||
-rw-r--r-- | cmd/platform/user_test.go.disabled | 212 | ||||
-rw-r--r-- | cmd/platform/userargs.go | 2 | ||||
-rw-r--r-- | cmd/platform/userargs_test.go.disabled | 58 | ||||
-rw-r--r-- | cmd/platform/version.go | 2 |
22 files changed, 641 insertions, 40 deletions
diff --git a/cmd/platform/channel.go b/cmd/platform/channel.go index cd8df0873..7419a0492 100644 --- a/cmd/platform/channel.go +++ b/cmd/platform/channel.go @@ -1,4 +1,4 @@ -// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. package main diff --git a/cmd/platform/channelargs.go b/cmd/platform/channelargs.go index d64db10bd..02ee1b6ab 100644 --- a/cmd/platform/channelargs.go +++ b/cmd/platform/channelargs.go @@ -1,4 +1,4 @@ -// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. package main diff --git a/cmd/platform/channelargs_test.go.disabled b/cmd/platform/channelargs_test.go.disabled new file mode 100644 index 000000000..5447a061d --- /dev/null +++ b/cmd/platform/channelargs_test.go.disabled @@ -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]) + } +} diff --git a/cmd/platform/cmdtestlib.go b/cmd/platform/cmdtestlib.go new file mode 100644 index 000000000..16bfc2b21 --- /dev/null +++ b/cmd/platform/cmdtestlib.go @@ -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) +} diff --git a/cmd/platform/config.go b/cmd/platform/config.go new file mode 100644 index 000000000..d66129de4 --- /dev/null +++ b/cmd/platform/config.go @@ -0,0 +1,66 @@ +// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. +package main + +import ( + "encoding/json" + "errors" + "github.com/mattermost/platform/model" + "github.com/mattermost/platform/utils" + "github.com/spf13/cobra" + "os" +) + +var configCmd = &cobra.Command{ + Use: "config", + Short: "Configuration", +} + +var validateConfigCmd = &cobra.Command{ + Use: "validate", + Short: "Validate config file", + Run: configValidateCmdF, +} + +func init() { + configCmd.AddCommand( + validateConfigCmd, + ) +} + +func configValidateCmdF(cmd *cobra.Command, args []string) { + utils.TranslationsPreInit() + filePath, err := cmd.Flags().GetString("config") + if err != nil { + CommandPrintErrorln(err) + return + } + + filePath = utils.FindConfigFile(filePath) + + file, err := os.Open(filePath) + if err != nil { + CommandPrintErrorln(err) + return + } + + decoder := json.NewDecoder(file) + config := model.Config{} + err = decoder.Decode(&config) + if err != nil { + CommandPrintErrorln(err) + return + } + + if _, err := file.Stat(); err != nil { + CommandPrintErrorln(err) + return + } + + if err := config.IsValid(); err != nil { + CommandPrintErrorln(errors.New(utils.T(err.Id))) + return + } + + CommandPrettyPrintln("The document is valid") +} diff --git a/cmd/platform/import.go b/cmd/platform/import.go index d0b2de162..9cee26a52 100644 --- a/cmd/platform/import.go +++ b/cmd/platform/import.go @@ -1,4 +1,4 @@ -// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. package main diff --git a/cmd/platform/init.go b/cmd/platform/init.go index 0458e7a1c..7d01eb890 100644 --- a/cmd/platform/init.go +++ b/cmd/platform/init.go @@ -20,6 +20,7 @@ func doLoadConfig(filename string) (err string) { utils.LoadConfig(filename) utils.InitializeConfigWatch() utils.EnableConfigWatch() + return "" } diff --git a/cmd/platform/ldap.go b/cmd/platform/ldap.go index 8f3aa0c81..f578ec3e2 100644 --- a/cmd/platform/ldap.go +++ b/cmd/platform/ldap.go @@ -1,4 +1,4 @@ -// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. package main diff --git a/cmd/platform/license.go b/cmd/platform/license.go index dedc4d306..91dc3bfba 100644 --- a/cmd/platform/license.go +++ b/cmd/platform/license.go @@ -1,4 +1,4 @@ -// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. package main diff --git a/cmd/platform/mattermost.go b/cmd/platform/mattermost.go index fc2f1d4c2..a4e44e016 100644 --- a/cmd/platform/mattermost.go +++ b/cmd/platform/mattermost.go @@ -1,4 +1,4 @@ -// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. package main @@ -23,21 +23,25 @@ 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.") + rootCmd.PersistentFlags().Bool("disableconfigwatch", false, "When set config.json will not be loaded from disk when the file is changed.") 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) + rootCmd.AddCommand(serverCmd, versionCmd, userCmd, teamCmd, licenseCmd, importCmd, resetCmd, channelCmd, rolesCmd, testCmd, ldapCmd, configCmd) +} - 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{ diff --git a/cmd/platform/output.go b/cmd/platform/output.go index 5b7f91385..e4666910e 100644 --- a/cmd/platform/output.go +++ b/cmd/platform/output.go @@ -1,4 +1,4 @@ -// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. package main diff --git a/cmd/platform/roles.go b/cmd/platform/roles.go index a65eb2bd6..df4b49436 100644 --- a/cmd/platform/roles.go +++ b/cmd/platform/roles.go @@ -1,4 +1,4 @@ -// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. package main diff --git a/cmd/platform/server.go b/cmd/platform/server.go index 855cde19a..0d971afb6 100644 --- a/cmd/platform/server.go +++ b/cmd/platform/server.go @@ -1,4 +1,4 @@ -// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. package main @@ -36,6 +36,8 @@ func runServerCmd(cmd *cobra.Command, args []string) error { return err } + utils.CfgDisableConfigWatch, _ = cmd.Flags().GetBool("disableconfigwatch") + runServer(config) return nil } diff --git a/cmd/platform/team.go b/cmd/platform/team.go index eb2939617..71bcd543c 100644 --- a/cmd/platform/team.go +++ b/cmd/platform/team.go @@ -1,4 +1,4 @@ -// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. package main diff --git a/cmd/platform/teamargs.go b/cmd/platform/teamargs.go index 506cc88ef..31f7b489e 100644 --- a/cmd/platform/teamargs.go +++ b/cmd/platform/teamargs.go @@ -1,4 +1,4 @@ -// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. package main diff --git a/cmd/platform/teamargs_test.go.disabled b/cmd/platform/teamargs_test.go.disabled new file mode 100644 index 000000000..573ec8a99 --- /dev/null +++ b/cmd/platform/teamargs_test.go.disabled @@ -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]) + } +} diff --git a/cmd/platform/test.go b/cmd/platform/test.go index cd548568f..735261439 100644 --- a/cmd/platform/test.go +++ b/cmd/platform/test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. package main diff --git a/cmd/platform/user.go b/cmd/platform/user.go index 41f0b4f66..dc0aa0f71 100644 --- a/cmd/platform/user.go +++ b/cmd/platform/user.go @@ -1,4 +1,4 @@ -// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. package main @@ -40,7 +40,7 @@ var userCreateCmd = &cobra.Command{ Use: "create", Short: "Create a user", Long: "Create a user", - Example: ` user create --email user@example.com --username userexample --password Password1 + Example: ` user create --email user@example.com --username userexample --password Password1 user create --firstname Joe --system_admin --email joe@example.com --username joe --password Password1`, RunE: userCreateCmdF, } @@ -67,7 +67,7 @@ var resetUserPasswordCmd = &cobra.Command{ var resetUserMfaCmd = &cobra.Command{ Use: "resetmfa [users]", Short: "Turn off MFA", - Long: `Turn off multi-factor authentication for a user. + Long: `Turn off multi-factor authentication for a user. If MFA enforcement is enabled, the user will be forced to re-enable MFA as soon as they login.`, Example: " user resetmfa user@example.com", RunE: resetUserMfaCmdF, @@ -94,13 +94,13 @@ var migrateAuthCmd = &cobra.Command{ Short: "Mass migrate user accounts authentication type", Long: `Migrates accounts from one authentication provider to another. For example, you can upgrade your authentication provider from email to ldap. -from_auth: +from_auth: The authentication service to migrate users accounts from. - Supported options: email, gitlab, saml. + Supported options: email, gitlab, saml. to_auth: The authentication service to migrate users to. - Supported options: ldap. + Supported options: ldap. match_field: The field that is guaranteed to be the same in both authentication services. For example, if the users emails are consistent set to email. @@ -119,6 +119,14 @@ var verifyUserCmd = &cobra.Command{ RunE: verifyUserCmdF, } +var searchUserCmd = &cobra.Command{ + Use: "search [users]", + Short: "Search for users", + Long: "Search for users based on username, email, or user ID.", + Example: " user search user1@mail.com user2@mail.com", + RunE: searchUserCmdF, +} + func init() { userCreateCmd.Flags().String("username", "", "Username") userCreateCmd.Flags().String("email", "", "Email") @@ -144,6 +152,7 @@ func init() { deleteAllUsersCmd, migrateAuthCmd, verifyUserCmd, + searchUserCmd, ) } @@ -161,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 { @@ -249,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 { @@ -431,3 +450,33 @@ func verifyUserCmdF(cmd *cobra.Command, args []string) error { return nil } + +func searchUserCmdF(cmd *cobra.Command, args []string) error { + initDBCommandContextCobra(cmd) + if len(args) < 1 { + return errors.New("Enter at least one query.") + } + + users := getUsersFromUserArgs(args) + + for i, user := range users { + if i > 0 { + CommandPrettyPrintln("------------------------------") + } + if user == nil { + CommandPrintErrorln("Unable to find user '" + args[i] + "'") + continue + } + + CommandPrettyPrintln("id: " + user.Id) + CommandPrettyPrintln("username: " + user.Username) + CommandPrettyPrintln("nickname: " + user.Nickname) + CommandPrettyPrintln("position: " + user.Position) + CommandPrettyPrintln("first_name: " + user.FirstName) + CommandPrettyPrintln("last_name: " + user.LastName) + CommandPrettyPrintln("email: " + user.Email) + CommandPrettyPrintln("auth_service: " + user.AuthService) + } + + return nil +} diff --git a/cmd/platform/user_test.go.disabled b/cmd/platform/user_test.go.disabled new file mode 100644 index 000000000..4119574f3 --- /dev/null +++ b/cmd/platform/user_test.go.disabled @@ -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) + } +} diff --git a/cmd/platform/userargs.go b/cmd/platform/userargs.go index 31ae3c251..43b53edd7 100644 --- a/cmd/platform/userargs.go +++ b/cmd/platform/userargs.go @@ -1,4 +1,4 @@ -// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. package main diff --git a/cmd/platform/userargs_test.go.disabled b/cmd/platform/userargs_test.go.disabled new file mode 100644 index 000000000..2b6a50bb8 --- /dev/null +++ b/cmd/platform/userargs_test.go.disabled @@ -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]) + } +} diff --git a/cmd/platform/version.go b/cmd/platform/version.go index 8978aa841..13ebdc1cd 100644 --- a/cmd/platform/version.go +++ b/cmd/platform/version.go @@ -1,4 +1,4 @@ -// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. // See License.txt for license information. package main |