github.com/iDevoid/mattermost-server@v5.11.1+incompatible/cmd/mattermost/commands/webhook_test.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package commands
     5  
     6  import (
     7  	"strconv"
     8  	"strings"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/mattermost/mattermost-server/api4"
    14  	"github.com/mattermost/mattermost-server/model"
    15  )
    16  
    17  func TestListWebhooks(t *testing.T) {
    18  	th := Setup().InitBasic()
    19  	defer th.TearDown()
    20  	adminClient := th.SystemAdminClient
    21  
    22  	config := th.Config()
    23  	*config.ServiceSettings.EnableCommands = true
    24  	*config.ServiceSettings.EnableIncomingWebhooks = true
    25  	*config.ServiceSettings.EnableOutgoingWebhooks = true
    26  	*config.ServiceSettings.EnablePostUsernameOverride = true
    27  	*config.ServiceSettings.EnablePostIconOverride = true
    28  	th.SetConfig(config)
    29  
    30  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableIncomingWebhooks = true })
    31  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOutgoingWebhooks = true })
    32  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnablePostUsernameOverride = true })
    33  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnablePostIconOverride = true })
    34  
    35  	defaultRolePermissions := th.SaveDefaultRolePermissions()
    36  	defer func() {
    37  		th.RestoreDefaultRolePermissions(defaultRolePermissions)
    38  	}()
    39  	th.AddPermissionToRole(model.PERMISSION_MANAGE_INCOMING_WEBHOOKS.Id, model.TEAM_ADMIN_ROLE_ID)
    40  	th.RemovePermissionFromRole(model.PERMISSION_MANAGE_INCOMING_WEBHOOKS.Id, model.TEAM_USER_ROLE_ID)
    41  	th.AddPermissionToRole(model.PERMISSION_MANAGE_OUTGOING_WEBHOOKS.Id, model.TEAM_ADMIN_ROLE_ID)
    42  	th.RemovePermissionFromRole(model.PERMISSION_MANAGE_OUTGOING_WEBHOOKS.Id, model.TEAM_USER_ROLE_ID)
    43  
    44  	dispName := "myhookinc"
    45  	hook := &model.IncomingWebhook{DisplayName: dispName, ChannelId: th.BasicChannel.Id, TeamId: th.BasicChannel.TeamId}
    46  	_, resp := adminClient.CreateIncomingWebhook(hook)
    47  	api4.CheckNoError(t, resp)
    48  
    49  	dispName2 := "myhookout"
    50  	outHook := &model.OutgoingWebhook{DisplayName: dispName2, ChannelId: th.BasicChannel.Id, TeamId: th.BasicChannel.TeamId, CallbackURLs: []string{"http://nowhere.com"}, Username: "some-user-name", IconURL: "http://some-icon-url/"}
    51  	_, resp = adminClient.CreateOutgoingWebhook(outHook)
    52  	api4.CheckNoError(t, resp)
    53  
    54  	output := th.CheckCommand(t, "webhook", "list", th.BasicTeam.Name)
    55  
    56  	if !strings.Contains(string(output), dispName) {
    57  		t.Fatal("should have incoming webhooks")
    58  	}
    59  
    60  	if !strings.Contains(string(output), dispName2) {
    61  		t.Fatal("should have outgoing webhooks")
    62  	}
    63  
    64  }
    65  
    66  func TestShowWebhook(t *testing.T) {
    67  	th := Setup().InitBasic()
    68  	defer th.TearDown()
    69  	adminClient := th.SystemAdminClient
    70  
    71  	config := th.Config()
    72  	*config.ServiceSettings.EnableCommands = true
    73  	*config.ServiceSettings.EnableIncomingWebhooks = true
    74  	*config.ServiceSettings.EnableOutgoingWebhooks = true
    75  	*config.ServiceSettings.EnablePostUsernameOverride = true
    76  	*config.ServiceSettings.EnablePostIconOverride = true
    77  	th.SetConfig(config)
    78  
    79  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableIncomingWebhooks = true })
    80  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOutgoingWebhooks = true })
    81  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnablePostUsernameOverride = true })
    82  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnablePostIconOverride = true })
    83  
    84  	defaultRolePermissions := th.SaveDefaultRolePermissions()
    85  	defer func() {
    86  		th.RestoreDefaultRolePermissions(defaultRolePermissions)
    87  	}()
    88  	th.AddPermissionToRole(model.PERMISSION_MANAGE_INCOMING_WEBHOOKS.Id, model.TEAM_ADMIN_ROLE_ID)
    89  	th.RemovePermissionFromRole(model.PERMISSION_MANAGE_INCOMING_WEBHOOKS.Id, model.TEAM_USER_ROLE_ID)
    90  	th.AddPermissionToRole(model.PERMISSION_MANAGE_OUTGOING_WEBHOOKS.Id, model.TEAM_ADMIN_ROLE_ID)
    91  	th.RemovePermissionFromRole(model.PERMISSION_MANAGE_OUTGOING_WEBHOOKS.Id, model.TEAM_USER_ROLE_ID)
    92  
    93  	dispName := "incominghook"
    94  	hook := &model.IncomingWebhook{
    95  		DisplayName: dispName,
    96  		ChannelId:   th.BasicChannel.Id,
    97  		TeamId:      th.BasicChannel.TeamId,
    98  	}
    99  	incomingWebhook, resp := adminClient.CreateIncomingWebhook(hook)
   100  	api4.CheckNoError(t, resp)
   101  
   102  	// should return an error when no webhookid is provided
   103  	require.Error(t, th.RunCommand(t, "webhook", "show"))
   104  
   105  	// invalid webhook should return error
   106  	require.Error(t, th.RunCommand(t, "webhook", "show", "invalid-webhook"))
   107  
   108  	// valid incoming webhook should return webhook data
   109  	output := th.CheckCommand(t, "webhook", "show", incomingWebhook.Id)
   110  	if !strings.Contains(string(output), "DisplayName: \""+dispName+"\"") {
   111  		t.Fatal("incoming: should have incominghook as displayname")
   112  	}
   113  	if !strings.Contains(string(output), "ChannelId: \""+hook.ChannelId+"\"") {
   114  		t.Fatal("incoming: should have a valid channelId")
   115  	}
   116  
   117  	dispName = "outgoinghook"
   118  	outgoingHook := &model.OutgoingWebhook{
   119  		DisplayName:  dispName,
   120  		ChannelId:    th.BasicChannel.Id,
   121  		TeamId:       th.BasicChannel.TeamId,
   122  		CallbackURLs: []string{"http://nowhere.com"},
   123  		Username:     "some-user-name",
   124  		IconURL:      "http://some-icon-url/",
   125  	}
   126  	outgoingWebhook, resp := adminClient.CreateOutgoingWebhook(outgoingHook)
   127  	api4.CheckNoError(t, resp)
   128  
   129  	// valid outgoing webhook should return webhook data
   130  	output = th.CheckCommand(t, "webhook", "show", outgoingWebhook.Id)
   131  	if !strings.Contains(string(output), "DisplayName: \""+dispName+"\"") {
   132  		t.Fatal("outgoing: should have outgoinghook as displayname")
   133  	}
   134  	if !strings.Contains(string(output), "ChannelId: \""+hook.ChannelId+"\"") {
   135  		t.Fatal("outgoing: should have a valid channelId")
   136  	}
   137  
   138  }
   139  
   140  func TestCreateIncomingWebhook(t *testing.T) {
   141  	th := Setup().InitBasic()
   142  	defer th.TearDown()
   143  
   144  	config := th.Config()
   145  	*config.ServiceSettings.EnableCommands = true
   146  	*config.ServiceSettings.EnableIncomingWebhooks = true
   147  	*config.ServiceSettings.EnableOutgoingWebhooks = true
   148  	*config.ServiceSettings.EnablePostUsernameOverride = true
   149  	*config.ServiceSettings.EnablePostIconOverride = true
   150  	th.SetConfig(config)
   151  
   152  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableIncomingWebhooks = true })
   153  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOutgoingWebhooks = true })
   154  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnablePostUsernameOverride = true })
   155  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnablePostIconOverride = true })
   156  
   157  	defaultRolePermissions := th.SaveDefaultRolePermissions()
   158  	defer func() {
   159  		th.RestoreDefaultRolePermissions(defaultRolePermissions)
   160  	}()
   161  	th.AddPermissionToRole(model.PERMISSION_MANAGE_INCOMING_WEBHOOKS.Id, model.TEAM_ADMIN_ROLE_ID)
   162  	th.RemovePermissionFromRole(model.PERMISSION_MANAGE_INCOMING_WEBHOOKS.Id, model.TEAM_USER_ROLE_ID)
   163  
   164  	// should fail because you need to specify valid channel
   165  	require.Error(t, th.RunCommand(t, "webhook", "create-incoming"))
   166  	require.Error(t, th.RunCommand(t, "webhook", "create-incoming", "--channel", th.BasicTeam.Name+":doesnotexist"))
   167  
   168  	// should fail because you need to specify valid user
   169  	require.Error(t, th.RunCommand(t, "webhook", "create-incoming", "--channel", th.BasicChannel.Id))
   170  	require.Error(t, th.RunCommand(t, "webhook", "create-incoming", "--channel", th.BasicChannel.Id, "--user", "doesnotexist"))
   171  
   172  	description := "myhookinc"
   173  	displayName := "myhookinc"
   174  	th.CheckCommand(t, "webhook", "create-incoming", "--channel", th.BasicChannel.Id, "--user", th.BasicUser.Email, "--description", description, "--display-name", displayName)
   175  
   176  	webhooks, err := th.App.GetIncomingWebhooksPage(0, 1000)
   177  	if err != nil {
   178  		t.Fatal("unable to retrieve incoming webhooks")
   179  	}
   180  
   181  	found := false
   182  	for _, webhook := range webhooks {
   183  		if webhook.Description == description && webhook.UserId == th.BasicUser.Id {
   184  			found = true
   185  		}
   186  	}
   187  	if !found {
   188  		t.Fatal("Failed to create incoming webhook")
   189  	}
   190  }
   191  
   192  func TestModifyIncomingWebhook(t *testing.T) {
   193  	th := Setup().InitBasic()
   194  	defer th.TearDown()
   195  
   196  	config := th.Config()
   197  	*config.ServiceSettings.EnableCommands = true
   198  	*config.ServiceSettings.EnableIncomingWebhooks = true
   199  	*config.ServiceSettings.EnableOutgoingWebhooks = true
   200  	*config.ServiceSettings.EnablePostUsernameOverride = true
   201  	*config.ServiceSettings.EnablePostIconOverride = true
   202  	th.SetConfig(config)
   203  
   204  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableIncomingWebhooks = true })
   205  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOutgoingWebhooks = true })
   206  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnablePostUsernameOverride = true })
   207  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnablePostIconOverride = true })
   208  
   209  	defaultRolePermissions := th.SaveDefaultRolePermissions()
   210  	defer func() {
   211  		th.RestoreDefaultRolePermissions(defaultRolePermissions)
   212  	}()
   213  	th.AddPermissionToRole(model.PERMISSION_MANAGE_INCOMING_WEBHOOKS.Id, model.TEAM_ADMIN_ROLE_ID)
   214  	th.RemovePermissionFromRole(model.PERMISSION_MANAGE_INCOMING_WEBHOOKS.Id, model.TEAM_USER_ROLE_ID)
   215  
   216  	description := "myhookincdesc"
   217  	displayName := "myhookincname"
   218  
   219  	incomingWebhook := &model.IncomingWebhook{
   220  		ChannelId:   th.BasicChannel.Id,
   221  		DisplayName: displayName,
   222  		Description: description,
   223  	}
   224  
   225  	oldHook, err := th.App.CreateIncomingWebhookForChannel(th.BasicUser.Id, th.BasicChannel, incomingWebhook)
   226  	if err != nil {
   227  		t.Fatal("unable to create incoming webhooks")
   228  	}
   229  	defer func() {
   230  		th.App.DeleteIncomingWebhook(oldHook.Id)
   231  	}()
   232  
   233  	// should fail because you need to specify valid incoming webhook
   234  	require.Error(t, th.RunCommand(t, "webhook", "modify-incoming", "doesnotexist"))
   235  	// should fail because you need to specify valid channel
   236  	require.Error(t, th.RunCommand(t, "webhook", "modify-incoming", oldHook.Id, "--channel", th.BasicTeam.Name+":doesnotexist"))
   237  
   238  	modifiedDescription := "myhookincdesc2"
   239  	modifiedDisplayName := "myhookincname2"
   240  	modifiedIconUrl := "myhookincicon2"
   241  	modifiedChannelLocked := true
   242  	modifiedChannelId := th.BasicChannel2.Id
   243  
   244  	th.CheckCommand(t, "webhook", "modify-incoming", oldHook.Id, "--channel", modifiedChannelId, "--description", modifiedDescription, "--display-name", modifiedDisplayName, "--icon", modifiedIconUrl, "--lock-to-channel", strconv.FormatBool(modifiedChannelLocked))
   245  
   246  	modifiedHook, err := th.App.GetIncomingWebhook(oldHook.Id)
   247  	if err != nil {
   248  		t.Fatal("unable to retrieve modified incoming webhook")
   249  	}
   250  	if modifiedHook.DisplayName != modifiedDisplayName || modifiedHook.Description != modifiedDescription || modifiedHook.IconURL != modifiedIconUrl || modifiedHook.ChannelLocked != modifiedChannelLocked || modifiedHook.ChannelId != modifiedChannelId {
   251  		t.Fatal("Failed to update incoming webhook")
   252  	}
   253  }
   254  
   255  func TestCreateOutgoingWebhook(t *testing.T) {
   256  	th := Setup().InitBasic()
   257  	defer th.TearDown()
   258  
   259  	config := th.Config()
   260  	*config.ServiceSettings.EnableCommands = true
   261  	*config.ServiceSettings.EnableIncomingWebhooks = true
   262  	*config.ServiceSettings.EnableOutgoingWebhooks = true
   263  	*config.ServiceSettings.EnablePostUsernameOverride = true
   264  	*config.ServiceSettings.EnablePostIconOverride = true
   265  	th.SetConfig(config)
   266  
   267  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableIncomingWebhooks = true })
   268  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOutgoingWebhooks = true })
   269  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnablePostUsernameOverride = true })
   270  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnablePostIconOverride = true })
   271  
   272  	defaultRolePermissions := th.SaveDefaultRolePermissions()
   273  	defer func() {
   274  		th.RestoreDefaultRolePermissions(defaultRolePermissions)
   275  	}()
   276  	th.AddPermissionToRole(model.PERMISSION_MANAGE_OUTGOING_WEBHOOKS.Id, model.TEAM_ADMIN_ROLE_ID)
   277  	th.RemovePermissionFromRole(model.PERMISSION_MANAGE_OUTGOING_WEBHOOKS.Id, model.TEAM_USER_ROLE_ID)
   278  
   279  	// team, user, display name, trigger words, callback urls are required
   280  	team := th.BasicTeam.Id
   281  	user := th.BasicUser.Id
   282  	displayName := "totally radical webhook"
   283  	triggerWord1 := "build"
   284  	triggerWord2 := "defenestrate"
   285  	callbackURL1 := "http://localhost:8000/my-webhook-handler"
   286  	callbackURL2 := "http://localhost:8000/my-webhook-handler2"
   287  
   288  	// should fail because team is not specified
   289  	require.Error(t, th.RunCommand(t, "webhook", "create-outgoing", "--display-name", displayName, "--trigger-word", triggerWord1, "--trigger-word", triggerWord2, "--url", callbackURL1, "--url", callbackURL2, "--user", user))
   290  
   291  	// should fail because user is not specified
   292  	require.Error(t, th.RunCommand(t, "webhook", "create-outgoing", "--team", team, "--display-name", displayName, "--trigger-word", triggerWord1, "--trigger-word", triggerWord2, "--url", callbackURL1, "--url", callbackURL2))
   293  
   294  	// should fail because display name is not specified
   295  	require.Error(t, th.RunCommand(t, "webhook", "create-outgoing", "--team", team, "--trigger-word", triggerWord1, "--trigger-word", triggerWord2, "--url", callbackURL1, "--url", callbackURL2, "--user", user))
   296  
   297  	// should fail because trigger words are not specified
   298  	require.Error(t, th.RunCommand(t, "webhook", "create-outgoing", "--team", team, "--display-name", displayName, "--url", callbackURL1, "--url", callbackURL2, "--user", user))
   299  
   300  	// should fail because callback URLs are not specified
   301  	require.Error(t, th.RunCommand(t, "webhook", "create-outgoing", "--team", team, "--display-name", displayName, "--trigger-word", triggerWord1, "--trigger-word", triggerWord2, "--user", user))
   302  
   303  	// should fail because outgoing webhooks cannot be made for private channels
   304  	require.Error(t, th.RunCommand(t, "webhook", "create-outgoing", "--team", team, "--channel", th.BasicPrivateChannel.Id, "--display-name", displayName, "--trigger-word", triggerWord1, "--trigger-word", triggerWord2, "--url", callbackURL1, "--url", callbackURL2, "--user", user))
   305  
   306  	th.CheckCommand(t, "webhook", "create-outgoing", "--team", team, "--channel", th.BasicChannel.Id, "--display-name", displayName, "--trigger-word", triggerWord1, "--trigger-word", triggerWord2, "--url", callbackURL1, "--url", callbackURL2, "--user", user)
   307  
   308  	webhooks, err := th.App.GetOutgoingWebhooksPage(0, 1000)
   309  	if err != nil {
   310  		t.Fatal("Unable to retreive outgoing webhooks")
   311  	}
   312  
   313  	found := false
   314  	for _, webhook := range webhooks {
   315  		if webhook.DisplayName == displayName && webhook.CreatorId == th.BasicUser.Id {
   316  			found = true
   317  		}
   318  	}
   319  	if !found {
   320  		t.Fatal("Failed to create incoming webhook")
   321  	}
   322  }
   323  
   324  func TestModifyOutgoingWebhook(t *testing.T) {
   325  	th := Setup().InitBasic()
   326  	defer th.TearDown()
   327  
   328  	config := th.Config()
   329  	*config.ServiceSettings.EnableOutgoingWebhooks = true
   330  	th.SetConfig(config)
   331  
   332  	defaultRolePermissions := th.SaveDefaultRolePermissions()
   333  	defer func() {
   334  		th.RestoreDefaultRolePermissions(defaultRolePermissions)
   335  	}()
   336  	th.AddPermissionToRole(model.PERMISSION_MANAGE_OUTGOING_WEBHOOKS.Id, model.TEAM_ADMIN_ROLE_ID)
   337  	th.RemovePermissionFromRole(model.PERMISSION_MANAGE_OUTGOING_WEBHOOKS.Id, model.TEAM_USER_ROLE_ID)
   338  
   339  	description := "myhookoutdesc"
   340  	displayName := "myhookoutname"
   341  	triggerWords := model.StringArray{"myhookoutword1"}
   342  	triggerWhen := 0
   343  	callbackURLs := model.StringArray{"http://myhookouturl1"}
   344  	iconURL := "myhookicon1"
   345  	contentType := "myhookcontent1"
   346  
   347  	outgoingWebhook := &model.OutgoingWebhook{
   348  		CreatorId:    th.BasicUser.Id,
   349  		Username:     th.BasicUser.Username,
   350  		TeamId:       th.BasicTeam.Id,
   351  		ChannelId:    th.BasicChannel.Id,
   352  		DisplayName:  displayName,
   353  		Description:  description,
   354  		TriggerWords: triggerWords,
   355  		TriggerWhen:  triggerWhen,
   356  		CallbackURLs: callbackURLs,
   357  		IconURL:      iconURL,
   358  		ContentType:  contentType,
   359  	}
   360  
   361  	oldHook, err := th.App.CreateOutgoingWebhook(outgoingWebhook)
   362  	if err != nil {
   363  		t.Fatal("unable to create outgoing webhooks: " + err.Error())
   364  	}
   365  	defer func() {
   366  		th.App.DeleteOutgoingWebhook(oldHook.Id)
   367  	}()
   368  
   369  	// should fail because you need to specify valid outgoing webhook
   370  	require.Error(t, th.RunCommand(t, "webhook", "modify-outgoing", "doesnotexist"))
   371  	// should fail because you need to specify valid channel
   372  	require.Error(t, th.RunCommand(t, "webhook", "modify-outgoing", oldHook.Id, "--channel", th.BasicTeam.Name+":doesnotexist"))
   373  	// should fail because you need to specify valid trigger when
   374  	require.Error(t, th.RunCommand(t, "webhook", "modify-outgoing", oldHook.Id, "--channel", th.BasicTeam.Name+th.BasicChannel.Id, "--trigger-when", "invalid"))
   375  	// should fail because you need to specify a valid callback URL
   376  	require.Error(t, th.RunCommand(t, "webhook", "modify-outgoing", oldHook.Id, "--channel", th.BasicTeam.Name+th.BasicChannel.Id, "--callback-url", "invalid"))
   377  
   378  	modifiedChannelID := th.BasicChannel2.Id
   379  	modifiedDisplayName := "myhookoutname2"
   380  	modifiedDescription := "myhookoutdesc2"
   381  	modifiedTriggerWords := model.StringArray{"myhookoutword2A", "myhookoutword2B"}
   382  	modifiedTriggerWhen := "start"
   383  	modifiedIconURL := "myhookouticon2"
   384  	modifiedContentType := "myhookcontent2"
   385  	modifiedCallbackURLs := model.StringArray{"http://myhookouturl2A", "http://myhookouturl2B"}
   386  
   387  	th.CheckCommand(t, "webhook", "modify-outgoing", oldHook.Id,
   388  		"--channel", modifiedChannelID,
   389  		"--display-name", modifiedDisplayName,
   390  		"--description", modifiedDescription,
   391  		"--trigger-word", modifiedTriggerWords[0],
   392  		"--trigger-word", modifiedTriggerWords[1],
   393  		"--trigger-when", modifiedTriggerWhen,
   394  		"--icon", modifiedIconURL,
   395  		"--content-type", modifiedContentType,
   396  		"--url", modifiedCallbackURLs[0],
   397  		"--url", modifiedCallbackURLs[1],
   398  	)
   399  
   400  	modifiedHook, err := th.App.GetOutgoingWebhook(oldHook.Id)
   401  	if err != nil {
   402  		t.Fatal("unable to retrieve modified outgoing webhook")
   403  	}
   404  
   405  	updateFailed := modifiedHook.ChannelId != modifiedChannelID ||
   406  		modifiedHook.DisplayName != modifiedDisplayName ||
   407  		modifiedHook.Description != modifiedDescription ||
   408  		len(modifiedHook.TriggerWords) != len(modifiedTriggerWords) ||
   409  		modifiedHook.TriggerWords[0] != modifiedTriggerWords[0] ||
   410  		modifiedHook.TriggerWords[1] != modifiedTriggerWords[1] ||
   411  		modifiedHook.TriggerWhen != 1 ||
   412  		modifiedHook.IconURL != modifiedIconURL ||
   413  		modifiedHook.ContentType != modifiedContentType ||
   414  		len(modifiedHook.CallbackURLs) != len(modifiedCallbackURLs) ||
   415  		modifiedHook.CallbackURLs[0] != modifiedCallbackURLs[0] ||
   416  		modifiedHook.CallbackURLs[1] != modifiedCallbackURLs[1]
   417  
   418  	if updateFailed {
   419  		t.Fatal("Failed to update outgoing webhook")
   420  	}
   421  }
   422  
   423  func TestDeleteWebhooks(t *testing.T) {
   424  	th := Setup().InitBasic()
   425  	defer th.TearDown()
   426  	adminClient := th.SystemAdminClient
   427  
   428  	config := th.Config()
   429  	*config.ServiceSettings.EnableCommands = true
   430  	*config.ServiceSettings.EnableIncomingWebhooks = true
   431  	*config.ServiceSettings.EnableOutgoingWebhooks = true
   432  	*config.ServiceSettings.EnablePostUsernameOverride = true
   433  	*config.ServiceSettings.EnablePostIconOverride = true
   434  	th.SetConfig(config)
   435  
   436  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableIncomingWebhooks = true })
   437  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOutgoingWebhooks = true })
   438  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnablePostUsernameOverride = true })
   439  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnablePostIconOverride = true })
   440  
   441  	defaultRolePermissions := th.SaveDefaultRolePermissions()
   442  	defer func() {
   443  		th.RestoreDefaultRolePermissions(defaultRolePermissions)
   444  	}()
   445  	th.AddPermissionToRole(model.PERMISSION_MANAGE_INCOMING_WEBHOOKS.Id, model.TEAM_ADMIN_ROLE_ID)
   446  	th.RemovePermissionFromRole(model.PERMISSION_MANAGE_INCOMING_WEBHOOKS.Id, model.TEAM_USER_ROLE_ID)
   447  	th.AddPermissionToRole(model.PERMISSION_MANAGE_OUTGOING_WEBHOOKS.Id, model.TEAM_ADMIN_ROLE_ID)
   448  	th.RemovePermissionFromRole(model.PERMISSION_MANAGE_OUTGOING_WEBHOOKS.Id, model.TEAM_USER_ROLE_ID)
   449  
   450  	dispName := "myhookinc"
   451  	inHookStruct := &model.IncomingWebhook{DisplayName: dispName, ChannelId: th.BasicChannel.Id, TeamId: th.BasicChannel.TeamId}
   452  	incomingHook, resp := adminClient.CreateIncomingWebhook(inHookStruct)
   453  	api4.CheckNoError(t, resp)
   454  
   455  	dispName2 := "myhookout"
   456  	outHookStruct := &model.OutgoingWebhook{DisplayName: dispName2, ChannelId: th.BasicChannel.Id, TeamId: th.BasicChannel.TeamId, CallbackURLs: []string{"http://nowhere.com"}, Username: "some-user-name", IconURL: "http://some-icon-url/"}
   457  	outgoingHook, resp := adminClient.CreateOutgoingWebhook(outHookStruct)
   458  	api4.CheckNoError(t, resp)
   459  
   460  	hooksBeforeDeletion := th.CheckCommand(t, "webhook", "list", th.BasicTeam.Name)
   461  
   462  	if !strings.Contains(string(hooksBeforeDeletion), dispName) {
   463  		t.Fatal("Should have incoming webhooks")
   464  	}
   465  
   466  	if !strings.Contains(string(hooksBeforeDeletion), dispName2) {
   467  		t.Fatal("Should have outgoing webhooks")
   468  	}
   469  
   470  	th.CheckCommand(t, "webhook", "delete", incomingHook.Id)
   471  	th.CheckCommand(t, "webhook", "delete", outgoingHook.Id)
   472  
   473  	hooksAfterDeletion := th.CheckCommand(t, "webhook", "list", th.BasicTeam.Name)
   474  
   475  	if strings.Contains(string(hooksAfterDeletion), dispName) {
   476  		t.Fatal("Should not have incoming webhooks")
   477  	}
   478  
   479  	if strings.Contains(string(hooksAfterDeletion), dispName2) {
   480  		t.Fatal("Should not have outgoing webhooks")
   481  	}
   482  }