Migrated tests from "model/incoming_webhook_test.go" to use testify (#12602)
* Replaced test assertions with testify * Changes based on suggestions https://github.com/mattermost/mattermost-server/pull/12602#pullrequestreview-296859312 * Unused variable replaced with blank identifier
Этот коммит содержится в:
коммит произвёл
Miguel de la Cruz
родитель
293d4c762e
Коммит
f97ed668ba
@@ -6,6 +6,8 @@ package model
|
|||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestIncomingWebhookJson(t *testing.T) {
|
func TestIncomingWebhookJson(t *testing.T) {
|
||||||
@@ -13,102 +15,64 @@ func TestIncomingWebhookJson(t *testing.T) {
|
|||||||
json := o.ToJson()
|
json := o.ToJson()
|
||||||
ro := IncomingWebhookFromJson(strings.NewReader(json))
|
ro := IncomingWebhookFromJson(strings.NewReader(json))
|
||||||
|
|
||||||
if o.Id != ro.Id {
|
require.Equal(t, o.Id, ro.Id)
|
||||||
t.Fatal("Ids do not match")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIncomingWebhookIsValid(t *testing.T) {
|
func TestIncomingWebhookIsValid(t *testing.T) {
|
||||||
o := IncomingWebhook{}
|
o := IncomingWebhook{}
|
||||||
|
|
||||||
if err := o.IsValid(); err == nil {
|
require.Error(t, o.IsValid())
|
||||||
t.Fatal("should be invalid")
|
|
||||||
}
|
|
||||||
|
|
||||||
o.Id = NewId()
|
o.Id = NewId()
|
||||||
if err := o.IsValid(); err == nil {
|
require.Error(t, o.IsValid())
|
||||||
t.Fatal("should be invalid")
|
|
||||||
}
|
|
||||||
|
|
||||||
o.CreateAt = GetMillis()
|
o.CreateAt = GetMillis()
|
||||||
if err := o.IsValid(); err == nil {
|
require.Error(t, o.IsValid())
|
||||||
t.Fatal("should be invalid")
|
|
||||||
}
|
|
||||||
|
|
||||||
o.UpdateAt = GetMillis()
|
o.UpdateAt = GetMillis()
|
||||||
if err := o.IsValid(); err == nil {
|
require.Error(t, o.IsValid())
|
||||||
t.Fatal("should be invalid")
|
|
||||||
}
|
|
||||||
|
|
||||||
o.UserId = "123"
|
o.UserId = "123"
|
||||||
if err := o.IsValid(); err == nil {
|
require.Error(t, o.IsValid())
|
||||||
t.Fatal("should be invalid")
|
|
||||||
}
|
|
||||||
|
|
||||||
o.UserId = NewId()
|
o.UserId = NewId()
|
||||||
if err := o.IsValid(); err == nil {
|
require.Error(t, o.IsValid())
|
||||||
t.Fatal("should be invalid")
|
|
||||||
}
|
|
||||||
|
|
||||||
o.ChannelId = "123"
|
o.ChannelId = "123"
|
||||||
if err := o.IsValid(); err == nil {
|
require.Error(t, o.IsValid())
|
||||||
t.Fatal("should be invalid")
|
|
||||||
}
|
|
||||||
|
|
||||||
o.ChannelId = NewId()
|
o.ChannelId = NewId()
|
||||||
if err := o.IsValid(); err == nil {
|
require.Error(t, o.IsValid())
|
||||||
t.Fatal("should be invalid")
|
|
||||||
}
|
|
||||||
|
|
||||||
o.TeamId = "123"
|
o.TeamId = "123"
|
||||||
if err := o.IsValid(); err == nil {
|
require.Error(t, o.IsValid())
|
||||||
t.Fatal("should be invalid")
|
|
||||||
}
|
|
||||||
|
|
||||||
o.TeamId = NewId()
|
o.TeamId = NewId()
|
||||||
if err := o.IsValid(); err != nil {
|
require.Nil(t, o.IsValid())
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
o.DisplayName = strings.Repeat("1", 65)
|
o.DisplayName = strings.Repeat("1", 65)
|
||||||
if err := o.IsValid(); err == nil {
|
require.Error(t, o.IsValid())
|
||||||
t.Fatal("should be invalid")
|
|
||||||
}
|
|
||||||
|
|
||||||
o.DisplayName = strings.Repeat("1", 64)
|
o.DisplayName = strings.Repeat("1", 64)
|
||||||
if err := o.IsValid(); err != nil {
|
require.Nil(t, o.IsValid())
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
o.Description = strings.Repeat("1", 501)
|
o.Description = strings.Repeat("1", 501)
|
||||||
if err := o.IsValid(); err == nil {
|
require.Error(t, o.IsValid())
|
||||||
t.Fatal("should be invalid")
|
|
||||||
}
|
|
||||||
|
|
||||||
o.Description = strings.Repeat("1", 500)
|
o.Description = strings.Repeat("1", 500)
|
||||||
if err := o.IsValid(); err != nil {
|
require.Nil(t, o.IsValid())
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
o.Username = strings.Repeat("1", 65)
|
o.Username = strings.Repeat("1", 65)
|
||||||
if err := o.IsValid(); err == nil {
|
require.Error(t, o.IsValid())
|
||||||
t.Fatal("should be invalid")
|
|
||||||
}
|
|
||||||
|
|
||||||
o.Username = strings.Repeat("1", 64)
|
o.Username = strings.Repeat("1", 64)
|
||||||
if err := o.IsValid(); err != nil {
|
require.Nil(t, o.IsValid())
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
o.IconURL = strings.Repeat("1", 1025)
|
o.IconURL = strings.Repeat("1", 1025)
|
||||||
if err := o.IsValid(); err == nil {
|
require.Error(t, o.IsValid())
|
||||||
t.Fatal("should be invalid")
|
|
||||||
}
|
|
||||||
|
|
||||||
o.IconURL = strings.Repeat("1", 1024)
|
o.IconURL = strings.Repeat("1", 1024)
|
||||||
if err := o.IsValid(); err != nil {
|
require.Nil(t, o.IsValid())
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIncomingWebhookPreSave(t *testing.T) {
|
func TestIncomingWebhookPreSave(t *testing.T) {
|
||||||
@@ -138,7 +102,7 @@ func TestIncomingWebhookRequestFromJson(t *testing.T) {
|
|||||||
`,
|
`,
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, text := range texts {
|
for _, text := range texts {
|
||||||
// build a sample payload with the text
|
// build a sample payload with the text
|
||||||
payload := `{
|
payload := `{
|
||||||
"text": "` + text + `",
|
"text": "` + text + `",
|
||||||
@@ -179,30 +143,18 @@ func TestIncomingWebhookRequestFromJson(t *testing.T) {
|
|||||||
|
|
||||||
// After it has been decoded, the JSON string won't contain the escape char anymore
|
// After it has been decoded, the JSON string won't contain the escape char anymore
|
||||||
expected := strings.Replace(text, `\"`, `"`, -1)
|
expected := strings.Replace(text, `\"`, `"`, -1)
|
||||||
if iwr == nil {
|
require.NotNil(t, iwr)
|
||||||
t.Fatal("IncomingWebhookRequest should not be nil")
|
require.Equal(t, expected, iwr.Text)
|
||||||
}
|
|
||||||
if iwr.Text != expected {
|
|
||||||
t.Fatalf("Sample %d text should be: %s, got: %s", i, expected, iwr.Text)
|
|
||||||
}
|
|
||||||
|
|
||||||
attachment := iwr.Attachments[0]
|
attachment := iwr.Attachments[0]
|
||||||
if attachment.Text != expected {
|
require.Equal(t, expected, attachment.Text)
|
||||||
t.Fatalf("Sample %d attachment text should be: %s, got: %s", i, expected, attachment.Text)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIncomingWebhookNullArrayItems(t *testing.T) {
|
func TestIncomingWebhookNullArrayItems(t *testing.T) {
|
||||||
payload := `{"attachments":[{"fields":[{"title":"foo","value":"bar","short":true}, null]}, null]}`
|
payload := `{"attachments":[{"fields":[{"title":"foo","value":"bar","short":true}, null]}, null]}`
|
||||||
iwr, _ := IncomingWebhookRequestFromJson(strings.NewReader(payload))
|
iwr, _ := IncomingWebhookRequestFromJson(strings.NewReader(payload))
|
||||||
if iwr == nil {
|
require.NotNil(t, iwr)
|
||||||
t.Fatal("IncomingWebhookRequest should not be nil")
|
require.Len(t, iwr.Attachments, 1)
|
||||||
}
|
require.Len(t, iwr.Attachments[0].Fields, 1)
|
||||||
if len(iwr.Attachments) != 1 {
|
|
||||||
t.Fatalf("expected one attachment")
|
|
||||||
}
|
|
||||||
if len(iwr.Attachments[0].Fields) != 1 {
|
|
||||||
t.Fatalf("expected one field")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user