emoji_test: update to use testify (#12932)

* emoji_test: update to use testify

* emoji_test: use testify builtin Len check, and add missing err check
Этот коммит содержится в:
Luke Kingland
2019-10-31 17:44:27 +09:00
коммит произвёл Shota Gvinepadze
родитель e4ed763e2a
Коммит df0bb8aaa0

Просмотреть файл

@@ -9,12 +9,12 @@ import (
_ "image/gif" _ "image/gif"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost-server/app" "github.com/mattermost/mattermost-server/app"
"github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/utils" "github.com/mattermost/mattermost-server/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestCreateEmoji(t *testing.T) { func TestCreateEmoji(t *testing.T) {
@@ -46,9 +46,7 @@ func TestCreateEmoji(t *testing.T) {
// try to create a valid gif emoji when they're enabled // try to create a valid gif emoji when they're enabled
newEmoji, resp := Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif") newEmoji, resp := Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
CheckNoError(t, resp) CheckNoError(t, resp)
if newEmoji.Name != emoji.Name { require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name")
t.Fatal("create with wrong name")
}
// try to create an emoji with a duplicate name // try to create an emoji with a duplicate name
emoji2 := &model.Emoji{ emoji2 := &model.Emoji{
@@ -67,9 +65,7 @@ func TestCreateEmoji(t *testing.T) {
newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestAnimatedGif(t, 10, 10, 10), "image.gif") newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestAnimatedGif(t, 10, 10, 10), "image.gif")
CheckNoError(t, resp) CheckNoError(t, resp)
if newEmoji.Name != emoji.Name { require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name")
t.Fatal("create with wrong name")
}
// try to create a valid jpeg emoji // try to create a valid jpeg emoji
emoji = &model.Emoji{ emoji = &model.Emoji{
@@ -79,9 +75,7 @@ func TestCreateEmoji(t *testing.T) {
newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestJpeg(t, 10, 10), "image.gif") newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestJpeg(t, 10, 10), "image.gif")
CheckNoError(t, resp) CheckNoError(t, resp)
if newEmoji.Name != emoji.Name { require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name")
t.Fatal("create with wrong name")
}
// try to create a valid png emoji // try to create a valid png emoji
emoji = &model.Emoji{ emoji = &model.Emoji{
@@ -91,9 +85,7 @@ func TestCreateEmoji(t *testing.T) {
newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestPng(t, 10, 10), "image.gif") newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestPng(t, 10, 10), "image.gif")
CheckNoError(t, resp) CheckNoError(t, resp)
if newEmoji.Name != emoji.Name { require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name")
t.Fatal("create with wrong name")
}
// try to create an emoji that's too wide // try to create an emoji that's too wide
emoji = &model.Emoji{ emoji = &model.Emoji{
@@ -103,9 +95,7 @@ func TestCreateEmoji(t *testing.T) {
newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 1000, 10), "image.gif") newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 1000, 10), "image.gif")
CheckNoError(t, resp) CheckNoError(t, resp)
if newEmoji.Name != emoji.Name { require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name")
t.Fatal("create with wrong name")
}
// try to create an emoji that's too wide // try to create an emoji that's too wide
emoji = &model.Emoji{ emoji = &model.Emoji{
@@ -114,9 +104,7 @@ func TestCreateEmoji(t *testing.T) {
} }
newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, app.MaxEmojiOriginalWidth+1), "image.gif") newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, app.MaxEmojiOriginalWidth+1), "image.gif")
if resp.Error == nil { require.Error(t, resp.Error, "should fail - emoji is too wide")
t.Fatal("should fail - emoji is too wide")
}
// try to create an emoji that's too tall // try to create an emoji that's too tall
emoji = &model.Emoji{ emoji = &model.Emoji{
@@ -125,9 +113,7 @@ func TestCreateEmoji(t *testing.T) {
} }
newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, app.MaxEmojiOriginalHeight+1, 10), "image.gif") newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, app.MaxEmojiOriginalHeight+1, 10), "image.gif")
if resp.Error == nil { require.Error(t, resp.Error, "should fail - emoji is too tall")
t.Fatal("should fail - emoji is too tall")
}
// try to create an emoji that's too large // try to create an emoji that's too large
emoji = &model.Emoji{ emoji = &model.Emoji{
@@ -136,9 +122,7 @@ func TestCreateEmoji(t *testing.T) {
} }
_, resp = Client.CreateEmoji(emoji, utils.CreateTestAnimatedGif(t, 100, 100, 10000), "image.gif") _, resp = Client.CreateEmoji(emoji, utils.CreateTestAnimatedGif(t, 100, 100, 10000), "image.gif")
if resp.Error == nil { require.Error(t, resp.Error, "should fail - emoji is too big")
t.Fatal("should fail - emoji is too big")
}
// try to create an emoji with data that isn't an image // try to create an emoji with data that isn't an image
emoji = &model.Emoji{ emoji = &model.Emoji{
@@ -224,9 +208,7 @@ func TestGetEmojiList(t *testing.T) {
break break
} }
} }
if !found { require.Truef(t, found, "failed to get emoji with id %v, %v", emoji.Id, len(listEmoji))
t.Fatalf("failed to get emoji with id %v, %v", emoji.Id, len(listEmoji))
}
} }
_, resp = Client.DeleteEmoji(emojis[0].Id) _, resp = Client.DeleteEmoji(emojis[0].Id)
@@ -245,16 +227,12 @@ func TestGetEmojiList(t *testing.T) {
listEmoji, resp = Client.GetEmojiList(0, 1) listEmoji, resp = Client.GetEmojiList(0, 1)
CheckNoError(t, resp) CheckNoError(t, resp)
if len(listEmoji) != 1 { require.Len(t, listEmoji, 1, "should only return 1")
t.Fatal("should only return 1")
}
listEmoji, resp = Client.GetSortedEmojiList(0, 100, model.EMOJI_SORT_BY_NAME) listEmoji, resp = Client.GetSortedEmojiList(0, 100, model.EMOJI_SORT_BY_NAME)
CheckNoError(t, resp) CheckNoError(t, resp)
if len(listEmoji) == 0 { require.Greater(t, len(listEmoji), 0, "should return more than 0")
t.Fatal("should return more than 0")
}
} }
func TestDeleteEmoji(t *testing.T) { func TestDeleteEmoji(t *testing.T) {
@@ -283,14 +261,11 @@ func TestDeleteEmoji(t *testing.T) {
ok, resp := Client.DeleteEmoji(newEmoji.Id) ok, resp := Client.DeleteEmoji(newEmoji.Id)
CheckNoError(t, resp) CheckNoError(t, resp)
if !ok { require.True(t, ok, "delete did not return OK")
t.Fatal("should return true")
} else { _, resp = Client.GetEmoji(newEmoji.Id)
_, err := Client.GetEmoji(newEmoji.Id) require.NotNil(t, resp, "nil response")
if err == nil { require.Error(t, resp.Error, "expected error fetching deleted emoji")
t.Fatal("should not return the emoji it was deleted")
}
}
//Admin can delete other users emoji //Admin can delete other users emoji
newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif") newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
@@ -298,14 +273,11 @@ func TestDeleteEmoji(t *testing.T) {
ok, resp = th.SystemAdminClient.DeleteEmoji(newEmoji.Id) ok, resp = th.SystemAdminClient.DeleteEmoji(newEmoji.Id)
CheckNoError(t, resp) CheckNoError(t, resp)
if !ok { require.True(t, ok, "delete did not return OK")
t.Fatal("should return true")
} else { _, resp = th.SystemAdminClient.GetEmoji(newEmoji.Id)
_, err := th.SystemAdminClient.GetEmoji(newEmoji.Id) require.NotNil(t, resp, "nil response")
if err == nil { require.Error(t, resp.Error, "expected error fetching deleted emoji")
t.Fatal("should not return the emoji it was deleted")
}
}
// Try to delete just deleted emoji // Try to delete just deleted emoji
_, resp = Client.DeleteEmoji(newEmoji.Id) _, resp = Client.DeleteEmoji(newEmoji.Id)
@@ -445,9 +417,7 @@ func TestGetEmoji(t *testing.T) {
emoji, resp = Client.GetEmoji(newEmoji.Id) emoji, resp = Client.GetEmoji(newEmoji.Id)
CheckNoError(t, resp) CheckNoError(t, resp)
if emoji.Id != newEmoji.Id { require.Equal(t, newEmoji.Id, emoji.Id, "wrong emoji was returned")
t.Fatal("wrong emoji was returned")
}
_, resp = Client.GetEmoji(model.NewId()) _, resp = Client.GetEmoji(model.NewId())
CheckNotFoundStatus(t, resp) CheckNotFoundStatus(t, resp)
@@ -506,15 +476,11 @@ func TestGetEmojiImage(t *testing.T) {
emojiImage, resp := Client.GetEmojiImage(emoji1.Id) emojiImage, resp := Client.GetEmojiImage(emoji1.Id)
CheckNoError(t, resp) CheckNoError(t, resp)
if len(emojiImage) <= 0 { require.Greater(t, len(emojiImage), 0, "should return the image")
t.Fatal("should return the image")
}
_, imageType, err := image.DecodeConfig(bytes.NewReader(emojiImage)) _, imageType, err := image.DecodeConfig(bytes.NewReader(emojiImage))
if err != nil { require.NoError(t, err)
t.Fatalf("unable to identify received image: %v", err.Error()) require.Equal(t, imageType, "gif", "expected gif")
} else if imageType != "gif" {
t.Fatal("should've received gif data")
}
emoji2 := &model.Emoji{ emoji2 := &model.Emoji{
CreatorId: th.BasicUser.Id, CreatorId: th.BasicUser.Id,
@@ -526,15 +492,11 @@ func TestGetEmojiImage(t *testing.T) {
emojiImage, resp = Client.GetEmojiImage(emoji2.Id) emojiImage, resp = Client.GetEmojiImage(emoji2.Id)
CheckNoError(t, resp) CheckNoError(t, resp)
if len(emojiImage) <= 0 { require.Greater(t, len(emojiImage), 0, "no image returned")
t.Fatal("should return the image")
}
_, imageType, err = image.DecodeConfig(bytes.NewReader(emojiImage)) _, imageType, err = image.DecodeConfig(bytes.NewReader(emojiImage))
if err != nil { require.NoError(t, err, "unable to indentify received image")
t.Fatalf("unable to identify received image: %v", err.Error()) require.Equal(t, imageType, "gif", "expected gif")
} else if imageType != "gif" {
t.Fatal("should've received gif data")
}
emoji3 := &model.Emoji{ emoji3 := &model.Emoji{
CreatorId: th.BasicUser.Id, CreatorId: th.BasicUser.Id,
@@ -545,15 +507,11 @@ func TestGetEmojiImage(t *testing.T) {
emojiImage, resp = Client.GetEmojiImage(emoji3.Id) emojiImage, resp = Client.GetEmojiImage(emoji3.Id)
CheckNoError(t, resp) CheckNoError(t, resp)
if len(emojiImage) <= 0 { require.Greater(t, len(emojiImage), 0, "no image returned")
t.Fatal("should return the image")
}
_, imageType, err = image.DecodeConfig(bytes.NewReader(emojiImage)) _, imageType, err = image.DecodeConfig(bytes.NewReader(emojiImage))
if err != nil { require.NoError(t, err, "unable to indentify received image")
t.Fatalf("unable to identify received image: %v", err.Error()) require.Equal(t, imageType, "jpeg", "expected jpeg")
} else if imageType != "jpeg" {
t.Fatal("should've received gif data")
}
emoji4 := &model.Emoji{ emoji4 := &model.Emoji{
CreatorId: th.BasicUser.Id, CreatorId: th.BasicUser.Id,
@@ -564,15 +522,11 @@ func TestGetEmojiImage(t *testing.T) {
emojiImage, resp = Client.GetEmojiImage(emoji4.Id) emojiImage, resp = Client.GetEmojiImage(emoji4.Id)
CheckNoError(t, resp) CheckNoError(t, resp)
if len(emojiImage) <= 0 { require.Greater(t, len(emojiImage), 0, "no image returned")
t.Fatal("should return the image")
}
_, imageType, err = image.DecodeConfig(bytes.NewReader(emojiImage)) _, imageType, err = image.DecodeConfig(bytes.NewReader(emojiImage))
if err != nil { require.NoError(t, err, "unable to idenitify received image")
t.Fatalf("unable to identify received image: %v", err.Error()) require.Equal(t, imageType, "png", "expected png")
} else if imageType != "png" {
t.Fatal("should've received gif data")
}
_, resp = Client.DeleteEmoji(emoji4.Id) _, resp = Client.DeleteEmoji(emoji4.Id)
CheckNoError(t, resp) CheckNoError(t, resp)