github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+incompatible/api/command_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package api
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/mattermost/mattermost-server/model"
    13  )
    14  
    15  func TestListCommands(t *testing.T) {
    16  	th := Setup().InitBasic()
    17  	defer th.TearDown()
    18  
    19  	Client := th.BasicClient
    20  
    21  	if results, err := Client.ListCommands(); err != nil {
    22  		t.Fatal(err)
    23  	} else {
    24  		commands := results.Data.([]*model.Command)
    25  		foundEcho := false
    26  
    27  		for _, command := range commands {
    28  			if command.Trigger == "echo" {
    29  				foundEcho = true
    30  			}
    31  		}
    32  
    33  		if !foundEcho {
    34  			t.Fatal("Couldn't find echo command")
    35  		}
    36  	}
    37  }
    38  
    39  func TestCreateCommand(t *testing.T) {
    40  	th := Setup().InitBasic().InitSystemAdmin()
    41  	defer th.TearDown()
    42  
    43  	Client := th.BasicClient
    44  	user := th.SystemAdminUser
    45  	team := th.SystemAdminTeam
    46  
    47  	enableCommands := *th.App.Config().ServiceSettings.EnableCommands
    48  	defer func() {
    49  		th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableCommands = &enableCommands })
    50  	}()
    51  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCommands = true })
    52  
    53  	cmd1 := &model.Command{
    54  		CreatorId: user.Id,
    55  		TeamId:    team.Id,
    56  		URL:       "http://nowhere.com",
    57  		Method:    model.COMMAND_METHOD_POST,
    58  		Trigger:   "trigger"}
    59  
    60  	if _, err := Client.CreateCommand(cmd1); err == nil {
    61  		t.Fatal("should have failed because not admin")
    62  	}
    63  
    64  	Client = th.SystemAdminClient
    65  
    66  	cmd2 := &model.Command{
    67  		CreatorId: user.Id,
    68  		TeamId:    team.Id,
    69  		URL:       "http://nowhere.com",
    70  		Method:    model.COMMAND_METHOD_POST,
    71  		Trigger:   "trigger"}
    72  
    73  	var rcmd *model.Command
    74  	if result, err := Client.CreateCommand(cmd2); err != nil {
    75  		t.Fatal(err)
    76  	} else {
    77  		rcmd = result.Data.(*model.Command)
    78  	}
    79  
    80  	if rcmd.CreatorId != user.Id {
    81  		t.Fatal("user ids didn't match")
    82  	}
    83  
    84  	if rcmd.TeamId != team.Id {
    85  		t.Fatal("team ids didn't match")
    86  	}
    87  
    88  	cmd3 := &model.Command{
    89  		CreatorId: "123",
    90  		TeamId:    "456",
    91  		URL:       "http://nowhere.com",
    92  		Method:    model.COMMAND_METHOD_POST,
    93  		Trigger:   "trigger"}
    94  	if _, err := Client.CreateCommand(cmd3); err == nil {
    95  		t.Fatal("trigger cannot be duplicated")
    96  	}
    97  
    98  	cmd4 := cmd3
    99  	if _, err := Client.CreateCommand(cmd4); err == nil {
   100  		t.Fatal("command cannot be duplicated")
   101  	}
   102  }
   103  
   104  func TestListTeamCommands(t *testing.T) {
   105  	th := Setup().InitSystemAdmin()
   106  	defer th.TearDown()
   107  
   108  	Client := th.SystemAdminClient
   109  
   110  	enableCommands := *th.App.Config().ServiceSettings.EnableCommands
   111  	defer func() {
   112  		th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableCommands = &enableCommands })
   113  	}()
   114  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCommands = true })
   115  
   116  	cmd1 := &model.Command{URL: "http://nowhere.com", Method: model.COMMAND_METHOD_POST, Trigger: "trigger"}
   117  	cmd1 = Client.Must(Client.CreateCommand(cmd1)).Data.(*model.Command)
   118  
   119  	if result, err := Client.ListTeamCommands(); err != nil {
   120  		t.Fatal(err)
   121  	} else {
   122  		cmds := result.Data.([]*model.Command)
   123  
   124  		if len(cmds) != 1 {
   125  			t.Fatal("incorrect number of cmd")
   126  		}
   127  	}
   128  }
   129  
   130  func TestUpdateCommand(t *testing.T) {
   131  	th := Setup().InitSystemAdmin()
   132  	defer th.TearDown()
   133  
   134  	Client := th.SystemAdminClient
   135  	user := th.SystemAdminUser
   136  	team := th.SystemAdminTeam
   137  
   138  	enableCommands := *th.App.Config().ServiceSettings.EnableCommands
   139  	defer func() {
   140  		th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableCommands = &enableCommands })
   141  	}()
   142  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCommands = true })
   143  
   144  	cmd1 := &model.Command{
   145  		CreatorId: user.Id,
   146  		TeamId:    team.Id,
   147  		URL:       "http://nowhere.com",
   148  		Method:    model.COMMAND_METHOD_POST,
   149  		Trigger:   "trigger"}
   150  
   151  	cmd1 = Client.Must(Client.CreateCommand(cmd1)).Data.(*model.Command)
   152  
   153  	cmd2 := &model.Command{
   154  		CreatorId: user.Id,
   155  		TeamId:    team.Id,
   156  		URL:       "http://nowhere.com",
   157  		Method:    model.COMMAND_METHOD_POST,
   158  		Trigger:   "trigger2",
   159  		Token:     cmd1.Token,
   160  		Id:        cmd1.Id}
   161  
   162  	if result, err := Client.UpdateCommand(cmd2); err != nil {
   163  		t.Fatal(err)
   164  	} else {
   165  		if result.Data.(*model.Command).Trigger == cmd1.Trigger {
   166  			t.Fatal("update didn't work properly")
   167  		}
   168  	}
   169  }
   170  
   171  func TestRegenToken(t *testing.T) {
   172  	th := Setup().InitSystemAdmin()
   173  	defer th.TearDown()
   174  
   175  	Client := th.SystemAdminClient
   176  
   177  	enableCommands := *th.App.Config().ServiceSettings.EnableCommands
   178  	defer func() {
   179  		th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableCommands = &enableCommands })
   180  	}()
   181  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCommands = true })
   182  
   183  	cmd := &model.Command{URL: "http://nowhere.com", Method: model.COMMAND_METHOD_POST, Trigger: "trigger"}
   184  	cmd = Client.Must(Client.CreateCommand(cmd)).Data.(*model.Command)
   185  
   186  	data := make(map[string]string)
   187  	data["id"] = cmd.Id
   188  
   189  	if result, err := Client.RegenCommandToken(data); err != nil {
   190  		t.Fatal(err)
   191  	} else {
   192  		if result.Data.(*model.Command).Token == cmd.Token {
   193  			t.Fatal("regen didn't work properly")
   194  		}
   195  	}
   196  }
   197  
   198  func TestDeleteCommand(t *testing.T) {
   199  	th := Setup().InitBasic().InitSystemAdmin()
   200  	defer th.TearDown()
   201  
   202  	Client := th.SystemAdminClient
   203  
   204  	enableCommands := *th.App.Config().ServiceSettings.EnableCommands
   205  	defer func() {
   206  		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCommands = enableCommands })
   207  	}()
   208  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCommands = true })
   209  
   210  	cmd := &model.Command{URL: "http://nowhere.com", Method: model.COMMAND_METHOD_POST, Trigger: "trigger"}
   211  	cmd = Client.Must(Client.CreateCommand(cmd)).Data.(*model.Command)
   212  
   213  	data := make(map[string]string)
   214  	data["id"] = cmd.Id
   215  
   216  	if _, err := Client.DeleteCommand(data); err != nil {
   217  		t.Fatal(err)
   218  	}
   219  
   220  	cmds := Client.Must(Client.ListTeamCommands()).Data.([]*model.Command)
   221  	if len(cmds) != 0 {
   222  		t.Fatal("delete didn't work properly")
   223  	}
   224  
   225  	cmd2 := &model.Command{URL: "http://nowhere.com", Method: model.COMMAND_METHOD_POST, Trigger: "trigger2"}
   226  	cmd2 = Client.Must(Client.CreateCommand(cmd2)).Data.(*model.Command)
   227  
   228  	data2 := make(map[string]string)
   229  	data2["id"] = cmd2.Id
   230  	if _, err := th.BasicClient.DeleteCommand(data2); err == nil {
   231  		t.Fatal("Should have errored. Your not allowed to delete other's commands")
   232  	}
   233  
   234  	cmds2 := Client.Must(Client.ListTeamCommands()).Data.([]*model.Command)
   235  	if len(cmds2) != 1 {
   236  		t.Fatal("Client was able to delete command without permission.")
   237  	}
   238  }
   239  
   240  func TestTestCommand(t *testing.T) {
   241  	th := Setup().InitSystemAdmin()
   242  	defer th.TearDown()
   243  
   244  	Client := th.SystemAdminClient
   245  	channel1 := th.SystemAdminChannel
   246  
   247  	enableCommands := *th.App.Config().ServiceSettings.EnableCommands
   248  	allowedInternalConnections := *th.App.Config().ServiceSettings.AllowedUntrustedInternalConnections
   249  	defer func() {
   250  		th.App.UpdateConfig(func(cfg *model.Config) { cfg.ServiceSettings.EnableCommands = &enableCommands })
   251  		th.App.UpdateConfig(func(cfg *model.Config) {
   252  			cfg.ServiceSettings.AllowedUntrustedInternalConnections = &allowedInternalConnections
   253  		})
   254  	}()
   255  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCommands = true })
   256  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.AllowedUntrustedInternalConnections = "localhost" })
   257  
   258  	cmd1 := &model.Command{
   259  		URL:     fmt.Sprintf("http://localhost:%v", th.App.Srv.ListenAddr.Port) + model.API_URL_SUFFIX_V3 + "/teams/command_test",
   260  		Method:  model.COMMAND_METHOD_POST,
   261  		Trigger: "testcommand",
   262  	}
   263  
   264  	cmd1 = Client.Must(Client.CreateCommand(cmd1)).Data.(*model.Command)
   265  
   266  	r1 := Client.Must(Client.Command(channel1.Id, "/testcommand")).Data.(*model.CommandResponse)
   267  	if r1 == nil {
   268  		t.Fatal("Test command failed to execute")
   269  	}
   270  
   271  	time.Sleep(100 * time.Millisecond)
   272  
   273  	p1 := Client.Must(Client.GetPosts(channel1.Id, 0, 10, "")).Data.(*model.PostList)
   274  	// including system 'joined' message
   275  	if len(p1.Order) != 2 {
   276  		t.Fatal("Test command response failed to send")
   277  	}
   278  
   279  	cmdPosted := false
   280  	for _, post := range p1.Posts {
   281  		if strings.Contains(post.Message, "test command response") {
   282  			cmdPosted = true
   283  			break
   284  		}
   285  	}
   286  
   287  	if !cmdPosted {
   288  		t.Fatal("Test command response failed to post")
   289  	}
   290  
   291  	cmd2 := &model.Command{
   292  		URL:     fmt.Sprintf("http://localhost:%v", th.App.Srv.ListenAddr.Port) + model.API_URL_SUFFIX_V3 + "/teams/command_test",
   293  		Method:  model.COMMAND_METHOD_GET,
   294  		Trigger: "test2",
   295  	}
   296  
   297  	cmd2 = Client.Must(Client.CreateCommand(cmd2)).Data.(*model.Command)
   298  
   299  	r2 := Client.Must(Client.Command(channel1.Id, "/test2")).Data.(*model.CommandResponse)
   300  	if r2 == nil {
   301  		t.Fatal("Test2 command failed to execute")
   302  	}
   303  
   304  	time.Sleep(100 * time.Millisecond)
   305  
   306  	p2 := Client.Must(Client.GetPosts(channel1.Id, 0, 10, "")).Data.(*model.PostList)
   307  	if len(p2.Order) != 3 {
   308  		t.Fatal("Test command failed to send")
   309  	}
   310  }