mirror of
https://github.com/mattermost/mattermost.git
synced 2025-02-25 18:55:24 -06:00
Convert app/webhooks_test.go t.Fatal calls into assert/require calls (#12039)
* Convert app/webhooks_test.go t.Fatal calls into assert/require calls * simplified by repetition * Addressing PR review comments
This commit is contained in:
@@ -120,8 +120,6 @@ func TestCreateIncomingWebhookForChannel(t *testing.T) {
|
||||
},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableIncomingWebhooks = tc.EnableIncomingHooks })
|
||||
th.App.UpdateConfig(func(cfg *model.Config) {
|
||||
*cfg.ServiceSettings.EnablePostUsernameOverride = tc.EnablePostUsernameOverride
|
||||
@@ -129,22 +127,22 @@ func TestCreateIncomingWebhookForChannel(t *testing.T) {
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnablePostIconOverride = tc.EnablePostIconOverride })
|
||||
|
||||
createdHook, err := th.App.CreateIncomingWebhookForChannel(th.BasicUser.Id, th.BasicChannel, &tc.IncomingWebhook)
|
||||
if tc.ExpectedError && err == nil {
|
||||
t.Fatal("should have failed")
|
||||
} else if !tc.ExpectedError && err != nil {
|
||||
t.Fatalf("should not have failed: %v", err.Error())
|
||||
if tc.ExpectedError {
|
||||
require.NotNil(t, err, "should have failed")
|
||||
} else {
|
||||
require.Nil(t, err, "should not have failed")
|
||||
}
|
||||
if createdHook != nil {
|
||||
defer th.App.DeleteIncomingWebhook(createdHook.Id)
|
||||
}
|
||||
if tc.ExpectedIncomingWebhook == nil {
|
||||
assert.Nil(createdHook, "expected nil webhook")
|
||||
} else if assert.NotNil(createdHook, "expected non-nil webhook") {
|
||||
assert.Equal(tc.ExpectedIncomingWebhook.DisplayName, createdHook.DisplayName)
|
||||
assert.Equal(tc.ExpectedIncomingWebhook.Description, createdHook.Description)
|
||||
assert.Equal(tc.ExpectedIncomingWebhook.ChannelId, createdHook.ChannelId)
|
||||
assert.Equal(tc.ExpectedIncomingWebhook.Username, createdHook.Username)
|
||||
assert.Equal(tc.ExpectedIncomingWebhook.IconURL, createdHook.IconURL)
|
||||
assert.Nil(t, createdHook, "expected nil webhook")
|
||||
} else if assert.NotNil(t, createdHook, "expected non-nil webhook") {
|
||||
assert.Equal(t, tc.ExpectedIncomingWebhook.DisplayName, createdHook.DisplayName)
|
||||
assert.Equal(t, tc.ExpectedIncomingWebhook.Description, createdHook.Description)
|
||||
assert.Equal(t, tc.ExpectedIncomingWebhook.ChannelId, createdHook.ChannelId)
|
||||
assert.Equal(t, tc.ExpectedIncomingWebhook.Username, createdHook.Username)
|
||||
assert.Equal(t, tc.ExpectedIncomingWebhook.IconURL, createdHook.IconURL)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -251,16 +249,12 @@ func TestUpdateIncomingWebhook(t *testing.T) {
|
||||
},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableIncomingWebhooks = true })
|
||||
|
||||
hook, err := th.App.CreateIncomingWebhookForChannel(th.BasicUser.Id, th.BasicChannel, &model.IncomingWebhook{
|
||||
ChannelId: th.BasicChannel.Id,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
require.Nil(t, err)
|
||||
defer th.App.DeleteIncomingWebhook(hook.Id)
|
||||
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableIncomingWebhooks = tc.EnableIncomingHooks })
|
||||
@@ -270,19 +264,19 @@ func TestUpdateIncomingWebhook(t *testing.T) {
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnablePostIconOverride = tc.EnablePostIconOverride })
|
||||
|
||||
updatedHook, err := th.App.UpdateIncomingWebhook(hook, &tc.IncomingWebhook)
|
||||
if tc.ExpectedError && err == nil {
|
||||
t.Fatal("should have failed")
|
||||
} else if !tc.ExpectedError && err != nil {
|
||||
t.Fatalf("should not have failed: %v", err.Error())
|
||||
if tc.ExpectedError {
|
||||
require.NotNil(t, err, "should have failed")
|
||||
} else {
|
||||
require.Nil(t, err, "should not have failed")
|
||||
}
|
||||
if tc.ExpectedIncomingWebhook == nil {
|
||||
assert.Nil(updatedHook, "expected nil webhook")
|
||||
} else if assert.NotNil(updatedHook, "expected non-nil webhook") {
|
||||
assert.Equal(tc.ExpectedIncomingWebhook.DisplayName, updatedHook.DisplayName)
|
||||
assert.Equal(tc.ExpectedIncomingWebhook.Description, updatedHook.Description)
|
||||
assert.Equal(tc.ExpectedIncomingWebhook.ChannelId, updatedHook.ChannelId)
|
||||
assert.Equal(tc.ExpectedIncomingWebhook.Username, updatedHook.Username)
|
||||
assert.Equal(tc.ExpectedIncomingWebhook.IconURL, updatedHook.IconURL)
|
||||
assert.Nil(t, updatedHook, "expected nil webhook")
|
||||
} else if assert.NotNil(t, updatedHook, "expected non-nil webhook") {
|
||||
assert.Equal(t, tc.ExpectedIncomingWebhook.DisplayName, updatedHook.DisplayName)
|
||||
assert.Equal(t, tc.ExpectedIncomingWebhook.Description, updatedHook.Description)
|
||||
assert.Equal(t, tc.ExpectedIncomingWebhook.ChannelId, updatedHook.ChannelId)
|
||||
assert.Equal(t, tc.ExpectedIncomingWebhook.Username, updatedHook.Username)
|
||||
assert.Equal(t, tc.ExpectedIncomingWebhook.IconURL, updatedHook.IconURL)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -295,9 +289,7 @@ func TestCreateWebhookPost(t *testing.T) {
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableIncomingWebhooks = true })
|
||||
|
||||
hook, err := th.App.CreateIncomingWebhookForChannel(th.BasicUser.Id, th.BasicChannel, &model.IncomingWebhook{ChannelId: th.BasicChannel.Id})
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
require.Nil(t, err)
|
||||
defer th.App.DeleteIncomingWebhook(hook.Id)
|
||||
|
||||
post, err := th.App.CreateWebhookPost(hook.UserId, th.BasicChannel, "foo", "user", "http://iconurl", "", model.StringInterface{
|
||||
@@ -308,21 +300,14 @@ func TestCreateWebhookPost(t *testing.T) {
|
||||
},
|
||||
"webhook_display_name": hook.DisplayName,
|
||||
}, model.POST_SLACK_ATTACHMENT, "")
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
require.Nil(t, err)
|
||||
|
||||
for _, k := range []string{"from_webhook", "attachments", "webhook_display_name"} {
|
||||
if _, ok := post.Props[k]; !ok {
|
||||
t.Log("missing one props: " + k)
|
||||
t.Fatal(k)
|
||||
}
|
||||
}
|
||||
assert.Contains(t, post.Props, "from_webhook", "missing from_webhook prop")
|
||||
assert.Contains(t, post.Props, "attachments", "missing attachments prop")
|
||||
assert.Contains(t, post.Props, "webhook_display_name", "missing webhook_display_name prop")
|
||||
|
||||
_, err = th.App.CreateWebhookPost(hook.UserId, th.BasicChannel, "foo", "user", "http://iconurl", "", nil, model.POST_SYSTEM_GENERIC, "")
|
||||
if err == nil {
|
||||
t.Fatal("should have failed - bad post type")
|
||||
}
|
||||
require.NotNil(t, err, "Should have failed - bad post type")
|
||||
|
||||
expectedText := "`<>|<>|`"
|
||||
post, err = th.App.CreateWebhookPost(hook.UserId, th.BasicChannel, expectedText, "user", "http://iconurl", "", model.StringInterface{
|
||||
@@ -333,9 +318,7 @@ func TestCreateWebhookPost(t *testing.T) {
|
||||
},
|
||||
"webhook_display_name": hook.DisplayName,
|
||||
}, model.POST_SLACK_ATTACHMENT, "")
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, expectedText, post.Message)
|
||||
|
||||
expectedText = "< | \n|\n>"
|
||||
@@ -347,9 +330,7 @@ func TestCreateWebhookPost(t *testing.T) {
|
||||
},
|
||||
"webhook_display_name": hook.DisplayName,
|
||||
}, model.POST_SLACK_ATTACHMENT, "")
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, expectedText, post.Message)
|
||||
|
||||
expectedText = `commit bc95839e4a430ace453e8b209a3723c000c1729a
|
||||
@@ -377,9 +358,7 @@ Date: Thu Mar 1 19:46:48 2018 +0300
|
||||
},
|
||||
"webhook_display_name": hook.DisplayName,
|
||||
}, model.POST_SLACK_ATTACHMENT, "")
|
||||
if err != nil {
|
||||
t.Fatal(err.Error())
|
||||
}
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, expectedText, post.Message)
|
||||
}
|
||||
|
||||
@@ -495,10 +474,7 @@ func TestCreateOutGoingWebhookWithUsernameAndIconURL(t *testing.T) {
|
||||
th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableOutgoingWebhooks = true })
|
||||
|
||||
createdHook, err := th.App.CreateOutgoingWebhook(&outgoingWebhook)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("should not have failed: %v", err.Error())
|
||||
}
|
||||
require.Nil(t, err)
|
||||
|
||||
assert.NotNil(t, createdHook, "should not be null")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user