From df0bb8aaa0508c61ff65e9e4ad44ff2fe45295f2 Mon Sep 17 00:00:00 2001 From: Luke Kingland Date: Thu, 31 Oct 2019 17:44:27 +0900 Subject: [PATCH] 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 --- api4/emoji_test.go | 128 +++++++++++++++------------------------------ 1 file changed, 41 insertions(+), 87 deletions(-) diff --git a/api4/emoji_test.go b/api4/emoji_test.go index 0769cb7561..68b05bafb8 100644 --- a/api4/emoji_test.go +++ b/api4/emoji_test.go @@ -9,12 +9,12 @@ import ( _ "image/gif" "testing" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/mattermost/mattermost-server/app" "github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/utils" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" ) 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 newEmoji, resp := Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif") CheckNoError(t, resp) - if newEmoji.Name != emoji.Name { - t.Fatal("create with wrong name") - } + require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name") // try to create an emoji with a duplicate name 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") CheckNoError(t, resp) - if newEmoji.Name != emoji.Name { - t.Fatal("create with wrong name") - } + require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name") // try to create a valid jpeg 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") CheckNoError(t, resp) - if newEmoji.Name != emoji.Name { - t.Fatal("create with wrong name") - } + require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name") // try to create a valid png 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") CheckNoError(t, resp) - if newEmoji.Name != emoji.Name { - t.Fatal("create with wrong name") - } + require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name") // try to create an emoji that's too wide emoji = &model.Emoji{ @@ -103,9 +95,7 @@ func TestCreateEmoji(t *testing.T) { newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 1000, 10), "image.gif") CheckNoError(t, resp) - if newEmoji.Name != emoji.Name { - t.Fatal("create with wrong name") - } + require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name") // try to create an emoji that's too wide 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") - if resp.Error == nil { - t.Fatal("should fail - emoji is too wide") - } + require.Error(t, resp.Error, "should fail - emoji is too wide") // try to create an emoji that's too tall 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") - if resp.Error == nil { - t.Fatal("should fail - emoji is too tall") - } + require.Error(t, resp.Error, "should fail - emoji is too tall") // try to create an emoji that's too large emoji = &model.Emoji{ @@ -136,9 +122,7 @@ func TestCreateEmoji(t *testing.T) { } _, resp = Client.CreateEmoji(emoji, utils.CreateTestAnimatedGif(t, 100, 100, 10000), "image.gif") - if resp.Error == nil { - t.Fatal("should fail - emoji is too big") - } + require.Error(t, resp.Error, "should fail - emoji is too big") // try to create an emoji with data that isn't an image emoji = &model.Emoji{ @@ -224,9 +208,7 @@ func TestGetEmojiList(t *testing.T) { break } } - if !found { - t.Fatalf("failed to get emoji with id %v, %v", emoji.Id, len(listEmoji)) - } + require.Truef(t, found, "failed to get emoji with id %v, %v", emoji.Id, len(listEmoji)) } _, resp = Client.DeleteEmoji(emojis[0].Id) @@ -245,16 +227,12 @@ func TestGetEmojiList(t *testing.T) { listEmoji, resp = Client.GetEmojiList(0, 1) CheckNoError(t, resp) - if len(listEmoji) != 1 { - t.Fatal("should only return 1") - } + require.Len(t, listEmoji, 1, "should only return 1") listEmoji, resp = Client.GetSortedEmojiList(0, 100, model.EMOJI_SORT_BY_NAME) CheckNoError(t, resp) - if len(listEmoji) == 0 { - t.Fatal("should return more than 0") - } + require.Greater(t, len(listEmoji), 0, "should return more than 0") } func TestDeleteEmoji(t *testing.T) { @@ -283,14 +261,11 @@ func TestDeleteEmoji(t *testing.T) { ok, resp := Client.DeleteEmoji(newEmoji.Id) CheckNoError(t, resp) - if !ok { - t.Fatal("should return true") - } else { - _, err := Client.GetEmoji(newEmoji.Id) - if err == nil { - t.Fatal("should not return the emoji it was deleted") - } - } + require.True(t, ok, "delete did not return OK") + + _, resp = Client.GetEmoji(newEmoji.Id) + require.NotNil(t, resp, "nil response") + require.Error(t, resp.Error, "expected error fetching deleted emoji") //Admin can delete other users emoji 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) CheckNoError(t, resp) - if !ok { - t.Fatal("should return true") - } else { - _, err := th.SystemAdminClient.GetEmoji(newEmoji.Id) - if err == nil { - t.Fatal("should not return the emoji it was deleted") - } - } + require.True(t, ok, "delete did not return OK") + + _, resp = th.SystemAdminClient.GetEmoji(newEmoji.Id) + require.NotNil(t, resp, "nil response") + require.Error(t, resp.Error, "expected error fetching deleted emoji") // Try to delete just deleted emoji _, resp = Client.DeleteEmoji(newEmoji.Id) @@ -445,9 +417,7 @@ func TestGetEmoji(t *testing.T) { emoji, resp = Client.GetEmoji(newEmoji.Id) CheckNoError(t, resp) - if emoji.Id != newEmoji.Id { - t.Fatal("wrong emoji was returned") - } + require.Equal(t, newEmoji.Id, emoji.Id, "wrong emoji was returned") _, resp = Client.GetEmoji(model.NewId()) CheckNotFoundStatus(t, resp) @@ -506,15 +476,11 @@ func TestGetEmojiImage(t *testing.T) { emojiImage, resp := Client.GetEmojiImage(emoji1.Id) CheckNoError(t, resp) - if len(emojiImage) <= 0 { - t.Fatal("should return the image") - } + require.Greater(t, len(emojiImage), 0, "should return the image") + _, imageType, err := image.DecodeConfig(bytes.NewReader(emojiImage)) - if err != nil { - t.Fatalf("unable to identify received image: %v", err.Error()) - } else if imageType != "gif" { - t.Fatal("should've received gif data") - } + require.NoError(t, err) + require.Equal(t, imageType, "gif", "expected gif") emoji2 := &model.Emoji{ CreatorId: th.BasicUser.Id, @@ -526,15 +492,11 @@ func TestGetEmojiImage(t *testing.T) { emojiImage, resp = Client.GetEmojiImage(emoji2.Id) CheckNoError(t, resp) - if len(emojiImage) <= 0 { - t.Fatal("should return the image") - } + require.Greater(t, len(emojiImage), 0, "no image returned") + _, imageType, err = image.DecodeConfig(bytes.NewReader(emojiImage)) - if err != nil { - t.Fatalf("unable to identify received image: %v", err.Error()) - } else if imageType != "gif" { - t.Fatal("should've received gif data") - } + require.NoError(t, err, "unable to indentify received image") + require.Equal(t, imageType, "gif", "expected gif") emoji3 := &model.Emoji{ CreatorId: th.BasicUser.Id, @@ -545,15 +507,11 @@ func TestGetEmojiImage(t *testing.T) { emojiImage, resp = Client.GetEmojiImage(emoji3.Id) CheckNoError(t, resp) - if len(emojiImage) <= 0 { - t.Fatal("should return the image") - } + require.Greater(t, len(emojiImage), 0, "no image returned") + _, imageType, err = image.DecodeConfig(bytes.NewReader(emojiImage)) - if err != nil { - t.Fatalf("unable to identify received image: %v", err.Error()) - } else if imageType != "jpeg" { - t.Fatal("should've received gif data") - } + require.NoError(t, err, "unable to indentify received image") + require.Equal(t, imageType, "jpeg", "expected jpeg") emoji4 := &model.Emoji{ CreatorId: th.BasicUser.Id, @@ -564,15 +522,11 @@ func TestGetEmojiImage(t *testing.T) { emojiImage, resp = Client.GetEmojiImage(emoji4.Id) CheckNoError(t, resp) - if len(emojiImage) <= 0 { - t.Fatal("should return the image") - } + require.Greater(t, len(emojiImage), 0, "no image returned") + _, imageType, err = image.DecodeConfig(bytes.NewReader(emojiImage)) - if err != nil { - t.Fatalf("unable to identify received image: %v", err.Error()) - } else if imageType != "png" { - t.Fatal("should've received gif data") - } + require.NoError(t, err, "unable to idenitify received image") + require.Equal(t, imageType, "png", "expected png") _, resp = Client.DeleteEmoji(emoji4.Id) CheckNoError(t, resp)