From 0e007e344bf10993529711f14c4168365c3504c3 Mon Sep 17 00:00:00 2001 From: Saturnino Abril Date: Thu, 27 Apr 2017 22:41:52 +0900 Subject: APIv4 POST /commands/execute (#6205) --- api4/command_test.go | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) (limited to 'api4/command_test.go') diff --git a/api4/command_test.go b/api4/command_test.go index 0aaca3c0f..4bbf20084 100644 --- a/api4/command_test.go +++ b/api4/command_test.go @@ -379,5 +379,91 @@ func TestRegenToken(t *testing.T) { if token != "" { t.Fatal("should not return the token") } +} + +func TestExecuteCommand(t *testing.T) { + th := Setup().InitBasic().InitSystemAdmin() + defer TearDown() + Client := th.Client + channel := th.BasicChannel + + enableCommands := *utils.Cfg.ServiceSettings.EnableCommands + defer func() { + utils.Cfg.ServiceSettings.EnableCommands = &enableCommands + }() + *utils.Cfg.ServiceSettings.EnableCommands = true + postCmd := &model.Command{ + CreatorId: th.BasicUser.Id, + TeamId: th.BasicTeam.Id, + URL: "http://localhost" + utils.Cfg.ServiceSettings.ListenAddress + model.API_URL_SUFFIX_V4 + "/teams/command_test", + Method: model.COMMAND_METHOD_POST, + Trigger: "postcommand", + } + + if _, err := app.CreateCommand(postCmd); err != nil { + t.Fatal("failed to create post command") + } + + commandResponse, resp := Client.ExecuteCommand(channel.Id, "/postcommand") + CheckNoError(t, resp) + + if commandResponse == nil { + t.Fatal("command response should have returned") + } + + posts, err := app.GetPostsPage(channel.Id, 0, 10) + if err != nil || posts == nil || len(posts.Order) != 2 { + t.Fatal("Test command failed to send") + } + + getCmd := &model.Command{ + CreatorId: th.BasicUser.Id, + TeamId: th.BasicTeam.Id, + URL: "http://localhost" + utils.Cfg.ServiceSettings.ListenAddress + model.API_URL_SUFFIX_V4 + "/teams/command_test", + Method: model.COMMAND_METHOD_GET, + Trigger: "getcommand", + } + + if _, err := app.CreateCommand(getCmd); err != nil { + t.Fatal("failed to create get command") + } + + commandResponse, resp = Client.ExecuteCommand(channel.Id, "/getcommand") + CheckNoError(t, resp) + + if commandResponse == nil { + t.Fatal("command response should have returned") + } + + posts, err = app.GetPostsPage(channel.Id, 0, 10) + if err != nil || posts == nil || len(posts.Order) != 3 { + t.Fatal("Test command failed to send") + } + + _, resp = Client.ExecuteCommand(channel.Id, "") + CheckBadRequestStatus(t, resp) + + _, resp = Client.ExecuteCommand(channel.Id, "/") + CheckBadRequestStatus(t, resp) + + _, resp = Client.ExecuteCommand(channel.Id, "getcommand") + CheckBadRequestStatus(t, resp) + + _, resp = Client.ExecuteCommand(channel.Id, "/junk") + CheckNotFoundStatus(t, resp) + + otherUser := th.CreateUser() + Client.Login(otherUser.Email, otherUser.Password) + + _, resp = Client.ExecuteCommand(channel.Id, "/getcommand") + CheckForbiddenStatus(t, resp) + + Client.Logout() + + _, resp = Client.ExecuteCommand(channel.Id, "/getcommand") + CheckUnauthorizedStatus(t, resp) + + _, resp = th.SystemAdminClient.ExecuteCommand(channel.Id, "/getcommand") + CheckNoError(t, resp) } -- cgit v1.2.3-1-g7c22