diff options
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/platform/cli_test.go | 287 | ||||
-rw-r--r-- | cmd/platform/init.go | 2 | ||||
-rw-r--r-- | cmd/platform/jobserver.go | 6 | ||||
-rw-r--r-- | cmd/platform/server.go | 19 | ||||
-rw-r--r-- | cmd/platform/test.go | 8 |
5 files changed, 301 insertions, 21 deletions
diff --git a/cmd/platform/cli_test.go b/cmd/platform/cli_test.go new file mode 100644 index 000000000..7d6717660 --- /dev/null +++ b/cmd/platform/cli_test.go @@ -0,0 +1,287 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package main + +import ( + "io/ioutil" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/mattermost/mattermost-server/api" + "github.com/mattermost/mattermost-server/model" +) + +var testExePath string + +func checkCommand(t *testing.T, args ...string) string { + output, err := exec.Command(testExePath, args...).CombinedOutput() + require.NoError(t, err, string(output)) + return string(output) +} + +func TestCliVersion(t *testing.T) { + checkCommand(t, "version") +} + +func TestCliCreateTeam(t *testing.T) { + th := api.Setup().InitSystemAdmin() + defer th.TearDown() + + id := model.NewId() + name := "name" + id + displayName := "Name " + id + + checkCommand(t, "team", "create", "--name", name, "--display_name", displayName) + + found := th.SystemAdminClient.Must(th.SystemAdminClient.FindTeamByName(name)).Data.(bool) + + if !found { + t.Fatal("Failed to create Team") + } +} + +func TestCliCreateUserWithTeam(t *testing.T) { + th := api.Setup().InitSystemAdmin() + defer th.TearDown() + + id := model.NewId() + email := "success+" + id + "@simulator.amazonses.com" + username := "name" + id + + checkCommand(t, "user", "create", "--email", email, "--password", "mypassword1", "--username", username) + + checkCommand(t, "team", "add", th.SystemAdminTeam.Id, email) + + profiles := th.SystemAdminClient.Must(th.SystemAdminClient.GetProfilesInTeam(th.SystemAdminTeam.Id, 0, 1000, "")).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) { + th := api.Setup() + defer th.TearDown() + + id := model.NewId() + email := "success+" + id + "@simulator.amazonses.com" + username := "name" + id + + checkCommand(t, "user", "create", "--email", email, "--password", "mypassword1", "--username", username) + + if result := <-th.App.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) { + th := api.Setup().InitBasic() + defer th.TearDown() + + checkCommand(t, "roles", "system_admin", th.BasicUser.Email) + + if result := <-th.App.Srv.Store.User().GetByEmail(th.BasicUser.Email); result.Err != nil { + t.Fatal() + } else { + user := result.Data.(*model.User) + if user.Roles != "system_admin system_user" { + t.Fatal() + } + } +} + +func TestCliJoinChannel(t *testing.T) { + th := api.Setup().InitBasic() + defer th.TearDown() + + channel := th.CreateChannel(th.BasicClient, th.BasicTeam) + + checkCommand(t, "channel", "add", th.BasicTeam.Name+":"+channel.Name, th.BasicUser2.Email) + + // Joining twice should succeed + checkCommand(t, "channel", "add", th.BasicTeam.Name+":"+channel.Name, th.BasicUser2.Email) + + // should fail because channel does not exist + require.Error(t, exec.Command(testExePath, "channel", "add", th.BasicTeam.Name+":"+channel.Name+"asdf", th.BasicUser2.Email).Run()) +} + +func TestCliRemoveChannel(t *testing.T) { + th := api.Setup().InitBasic() + defer th.TearDown() + + channel := th.CreateChannel(th.BasicClient, th.BasicTeam) + + checkCommand(t, "channel", "add", th.BasicTeam.Name+":"+channel.Name, th.BasicUser2.Email) + + // should fail because channel does not exist + require.Error(t, exec.Command(testExePath, "channel", "remove", th.BasicTeam.Name+":doesnotexist", th.BasicUser2.Email).Run()) + + checkCommand(t, "channel", "remove", th.BasicTeam.Name+":"+channel.Name, th.BasicUser2.Email) + + // Leaving twice should succeed + checkCommand(t, "channel", "remove", th.BasicTeam.Name+":"+channel.Name, th.BasicUser2.Email) +} + +func TestCliListChannels(t *testing.T) { + th := api.Setup().InitBasic() + defer th.TearDown() + + channel := th.CreateChannel(th.BasicClient, th.BasicTeam) + th.BasicClient.Must(th.BasicClient.DeleteChannel(channel.Id)) + + output := checkCommand(t, "channel", "list", th.BasicTeam.Name) + + if !strings.Contains(string(output), "town-square") { + t.Fatal("should have channels") + } + + if !strings.Contains(string(output), channel.Name+" (archived)") { + t.Fatal("should have archived channel") + } +} + +func TestCliRestoreChannel(t *testing.T) { + th := api.Setup().InitBasic() + defer th.TearDown() + + channel := th.CreateChannel(th.BasicClient, th.BasicTeam) + th.BasicClient.Must(th.BasicClient.DeleteChannel(channel.Id)) + + checkCommand(t, "channel", "restore", th.BasicTeam.Name+":"+channel.Name) + + // restoring twice should succeed + checkCommand(t, "channel", "restore", th.BasicTeam.Name+":"+channel.Name) +} + +func TestCliJoinTeam(t *testing.T) { + th := api.Setup().InitSystemAdmin().InitBasic() + defer th.TearDown() + + checkCommand(t, "team", "add", th.SystemAdminTeam.Name, th.BasicUser.Email) + + profiles := th.SystemAdminClient.Must(th.SystemAdminClient.GetProfilesInTeam(th.SystemAdminTeam.Id, 0, 1000, "")).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 TestCliLeaveTeam(t *testing.T) { + th := api.Setup().InitBasic() + defer th.TearDown() + + checkCommand(t, "team", "remove", th.BasicTeam.Name, th.BasicUser.Email) + + profiles := th.BasicClient.Must(th.BasicClient.GetProfilesInTeam(th.BasicTeam.Id, 0, 1000, "")).Data.(map[string]*model.User) + + found := false + + for _, user := range profiles { + if user.Email == th.BasicUser.Email { + found = true + } + + } + + if found { + t.Fatal("profile should not be on team") + } + + if result := <-th.App.Srv.Store.Team().GetTeamsByUserId(th.BasicUser.Id); result.Err != nil { + teamMembers := result.Data.([]*model.TeamMember) + if len(teamMembers) > 0 { + t.Fatal("Shouldn't be in team") + } + } +} + +func TestCliResetPassword(t *testing.T) { + th := api.Setup().InitBasic() + defer th.TearDown() + + checkCommand(t, "user", "password", th.BasicUser.Email, "password2") + + th.BasicClient.Logout() + th.BasicUser.Password = "password2" + th.LoginBasic() +} + +func TestCliCreateChannel(t *testing.T) { + th := api.Setup().InitBasic() + defer th.TearDown() + + id := model.NewId() + name := "name" + id + + checkCommand(t, "channel", "create", "--display_name", name, "--team", th.BasicTeam.Name, "--name", name) + + name = name + "-private" + checkCommand(t, "channel", "create", "--display_name", name, "--team", th.BasicTeam.Name, "--private", "--name", name) +} + +func TestCliMakeUserActiveAndInactive(t *testing.T) { + th := api.Setup().InitBasic() + defer th.TearDown() + + // first inactivate the user + checkCommand(t, "user", "deactivate", th.BasicUser.Email) + + // activate the inactive user + checkCommand(t, "user", "activate", th.BasicUser.Email) +} + +func TestMain(m *testing.M) { + dir, err := ioutil.TempDir("", "cli_test") + if err != nil { + panic(err) + } + defer os.RemoveAll(dir) + + testExePath = filepath.Join(dir, "cli") + files, err := filepath.Glob("./*.go") + if err != nil { + panic(err) + } + + cmd := exec.Command("go", append([]string{"build", "-o", testExePath}, files...)...) + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + if err := cmd.Run(); err != nil { + panic(err) + } + + status := 0 + defer func() { + os.Exit(status) + }() + status = m.Run() +} diff --git a/cmd/platform/init.go b/cmd/platform/init.go index 2f41b2ad6..145c754d1 100644 --- a/cmd/platform/init.go +++ b/cmd/platform/init.go @@ -30,8 +30,6 @@ func initDBCommandContext(configFileLocation string) (*app.App, error) { utils.ConfigureCmdLineLog() a := app.New() - a.NewServer() - a.InitStores() if model.BuildEnterpriseReady == "true" { a.LoadLicense() } diff --git a/cmd/platform/jobserver.go b/cmd/platform/jobserver.go index 4f82a21ee..c38bfa4f0 100644 --- a/cmd/platform/jobserver.go +++ b/cmd/platform/jobserver.go @@ -8,8 +8,6 @@ import ( "syscall" l4g "github.com/alecthomas/log4go" - "github.com/mattermost/mattermost-server/store" - "github.com/mattermost/mattermost-server/store/sqlstore" "github.com/spf13/cobra" ) @@ -35,9 +33,7 @@ func jobserverCmdF(cmd *cobra.Command, args []string) { panic(err.Error()) } defer l4g.Close() - - a.Jobs.Store = store.NewLayeredStore(sqlstore.NewSqlSupplier(a.Metrics), a.Metrics, a.Cluster) - defer a.Jobs.Store.Close() + defer a.Shutdown() a.Jobs.LoadLicense() diff --git a/cmd/platform/server.go b/cmd/platform/server.go index 7ee7097e7..591a27457 100644 --- a/cmd/platform/server.go +++ b/cmd/platform/server.go @@ -67,21 +67,27 @@ func runServer(configFileLocation string) { a := app.New() defer a.Shutdown() - a.NewServer() - a.InitStores() - a.Srv.Router = api.NewRouter() - a.Srv.WebSocketRouter = a.NewWebSocketRouter() - if model.BuildEnterpriseReady == "true" { a.LoadLicense() } + a.InitBuiltInPlugins() + if webappDir, ok := utils.FindDir(model.CLIENT_DIR); ok { a.InitPlugins("plugins", webappDir+"/plugins") + + utils.AddConfigListener(func(prevCfg *model.Config, cfg *model.Config) { + if !*prevCfg.PluginSettings.Enable && *cfg.PluginSettings.Enable { + a.InitPlugins("plugins", webappDir+"/plugins") + } else if *prevCfg.PluginSettings.Enable && !*cfg.PluginSettings.Enable { + a.ShutDownPlugins() + } + }) } else { l4g.Error("Unable to find webapp directory, could not initialize plugins") } + a.StartServer() api4.Init(a, a.Srv.Router, false) api3 := api.Init(a, a.Srv.Router) wsapi.Init(a, a.Srv.WebSocketRouter) @@ -105,8 +111,6 @@ func runServer(configFileLocation string) { resetStatuses(a) - a.StartServer() - // If we allow testing then listen for manual testing URL hits if utils.Cfg.ServiceSettings.EnableTesting { manualtesting.Init(api3) @@ -139,7 +143,6 @@ func runServer(configFileLocation string) { } } - a.Jobs.Store = a.Srv.Store if *utils.Cfg.JobSettings.RunJobs { a.Jobs.StartWorkers() } diff --git a/cmd/platform/test.go b/cmd/platform/test.go index 7e8b9cf0f..08438e034 100644 --- a/cmd/platform/test.go +++ b/cmd/platform/test.go @@ -52,13 +52,11 @@ func webClientTestsCmdF(cmd *cobra.Command, args []string) error { defer a.Shutdown() utils.InitTranslations(utils.Cfg.LocalizationSettings) - a.Srv.Router = api.NewRouter() - a.Srv.WebSocketRouter = a.NewWebSocketRouter() + a.StartServer() api4.Init(a, a.Srv.Router, false) api.Init(a, a.Srv.Router) wsapi.Init(a, a.Srv.WebSocketRouter) setupClientTests() - a.StartServer() runWebClientTests() return nil @@ -72,13 +70,11 @@ func serverForWebClientTestsCmdF(cmd *cobra.Command, args []string) error { defer a.Shutdown() utils.InitTranslations(utils.Cfg.LocalizationSettings) - a.Srv.Router = api.NewRouter() - a.Srv.WebSocketRouter = a.NewWebSocketRouter() + a.StartServer() api4.Init(a, a.Srv.Router, false) api.Init(a, a.Srv.Router) wsapi.Init(a, a.Srv.WebSocketRouter) setupClientTests() - a.StartServer() c := make(chan os.Signal) signal.Notify(c, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) |