From 461a0b3b7c14cd59cb53eb66f419c965ab3bdd24 Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Sat, 15 Apr 2017 13:45:22 -0400 Subject: 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' --- cmd/platform/channelargs_test.go.disabled | 106 +++++++++++++++ cmd/platform/cmdtestlib.go | 45 +++++++ cmd/platform/mattermost.go | 19 +-- cmd/platform/teamargs_test.go.disabled | 58 ++++++++ cmd/platform/user.go | 32 +++-- cmd/platform/user_test.go.disabled | 212 ++++++++++++++++++++++++++++++ cmd/platform/userargs_test.go.disabled | 58 ++++++++ 7 files changed, 511 insertions(+), 19 deletions(-) create mode 100644 cmd/platform/channelargs_test.go.disabled create mode 100644 cmd/platform/cmdtestlib.go create mode 100644 cmd/platform/teamargs_test.go.disabled create mode 100644 cmd/platform/user_test.go.disabled create mode 100644 cmd/platform/userargs_test.go.disabled (limited to 'cmd') 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/mattermost.go b/cmd/platform/mattermost.go index 9985a6887..56e91dd8d 100644 --- a/cmd/platform/mattermost.go +++ b/cmd/platform/mattermost.go @@ -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{ 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/user.go b/cmd/platform/user.go index f306d17b3..dc0aa0f71 100644 --- a/cmd/platform/user.go +++ b/cmd/platform/user.go @@ -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 { 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_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]) + } +} -- cgit v1.2.3-1-g7c22