github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/api4/emoji_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package api4
     5  
     6  import (
     7  	"bytes"
     8  	"image"
     9  	_ "image/gif"
    10  	"os"
    11  	"path/filepath"
    12  	"testing"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/stretchr/testify/require"
    16  
    17  	"github.com/masterhung0112/hk_server/v5/app"
    18  	"github.com/masterhung0112/hk_server/v5/model"
    19  	"github.com/masterhung0112/hk_server/v5/utils"
    20  	"github.com/masterhung0112/hk_server/v5/utils/fileutils"
    21  )
    22  
    23  func TestCreateEmoji(t *testing.T) {
    24  	th := Setup(t).InitBasic()
    25  	defer th.TearDown()
    26  	Client := th.Client
    27  
    28  	EnableCustomEmoji := *th.App.Config().ServiceSettings.EnableCustomEmoji
    29  	defer func() {
    30  		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCustomEmoji = EnableCustomEmoji })
    31  	}()
    32  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCustomEmoji = false })
    33  
    34  	defaultRolePermissions := th.SaveDefaultRolePermissions()
    35  	defer func() {
    36  		th.RestoreDefaultRolePermissions(defaultRolePermissions)
    37  	}()
    38  
    39  	// constants to be used along with checkEmojiFile
    40  	emojiWidth := app.MaxEmojiWidth
    41  	emojiHeight := app.MaxEmojiHeight * 2
    42  	// check that emoji gets resized correctly, respecting proportions, and is of expected type
    43  	checkEmojiFile := func(id, expectedImageType string) {
    44  		path, _ := fileutils.FindDir("data")
    45  		file, fileErr := os.Open(filepath.Join(path, "/emoji/"+id+"/image"))
    46  		require.NoError(t, fileErr)
    47  		defer file.Close()
    48  		config, imageType, err := image.DecodeConfig(file)
    49  		require.NoError(t, err)
    50  		require.Equal(t, expectedImageType, imageType)
    51  		require.Equal(t, emojiWidth/2, config.Width)
    52  		require.Equal(t, emojiHeight/2, config.Height)
    53  	}
    54  
    55  	emoji := &model.Emoji{
    56  		CreatorId: th.BasicUser.Id,
    57  		Name:      model.NewId(),
    58  	}
    59  
    60  	// try to create an emoji when they're disabled
    61  	_, resp := Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
    62  	CheckNotImplementedStatus(t, resp)
    63  
    64  	// enable emoji creation for next cases
    65  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCustomEmoji = true })
    66  
    67  	// try to create a valid gif emoji when they're enabled
    68  	newEmoji, resp := Client.CreateEmoji(emoji, utils.CreateTestGif(t, emojiWidth, emojiHeight), "image.gif")
    69  	CheckNoError(t, resp)
    70  	require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name")
    71  	checkEmojiFile(newEmoji.Id, "gif")
    72  
    73  	// try to create an emoji with a duplicate name
    74  	emoji2 := &model.Emoji{
    75  		CreatorId: th.BasicUser.Id,
    76  		Name:      newEmoji.Name,
    77  	}
    78  	_, resp = Client.CreateEmoji(emoji2, utils.CreateTestGif(t, 10, 10), "image.gif")
    79  	CheckBadRequestStatus(t, resp)
    80  	CheckErrorMessage(t, resp, "api.emoji.create.duplicate.app_error")
    81  
    82  	// try to create a valid animated gif emoji
    83  	emoji = &model.Emoji{
    84  		CreatorId: th.BasicUser.Id,
    85  		Name:      model.NewId(),
    86  	}
    87  
    88  	newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestAnimatedGif(t, emojiWidth, emojiHeight, 10), "image.gif")
    89  	CheckNoError(t, resp)
    90  	require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name")
    91  	checkEmojiFile(newEmoji.Id, "gif")
    92  
    93  	// try to create a valid jpeg emoji
    94  	emoji = &model.Emoji{
    95  		CreatorId: th.BasicUser.Id,
    96  		Name:      model.NewId(),
    97  	}
    98  
    99  	newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestJpeg(t, emojiWidth, emojiHeight), "image.jpeg")
   100  	CheckNoError(t, resp)
   101  	require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name")
   102  	checkEmojiFile(newEmoji.Id, "png") // emoji must be converted from jpeg to png
   103  
   104  	// try to create a valid png emoji
   105  	emoji = &model.Emoji{
   106  		CreatorId: th.BasicUser.Id,
   107  		Name:      model.NewId(),
   108  	}
   109  
   110  	newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestPng(t, emojiWidth, emojiHeight), "image.png")
   111  	CheckNoError(t, resp)
   112  	require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name")
   113  	checkEmojiFile(newEmoji.Id, "png")
   114  
   115  	// try to create an emoji that's too wide
   116  	emoji = &model.Emoji{
   117  		CreatorId: th.BasicUser.Id,
   118  		Name:      model.NewId(),
   119  	}
   120  
   121  	newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 1000, 10), "image.gif")
   122  	CheckNoError(t, resp)
   123  	require.Equal(t, newEmoji.Name, emoji.Name, "create with wrong name")
   124  
   125  	// try to create an emoji that's too wide
   126  	emoji = &model.Emoji{
   127  		CreatorId: th.BasicUser.Id,
   128  		Name:      model.NewId(),
   129  	}
   130  
   131  	newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, app.MaxEmojiOriginalWidth+1), "image.gif")
   132  	require.NotNil(t, resp.Error, "should fail - emoji is too wide")
   133  
   134  	// try to create an emoji that's too tall
   135  	emoji = &model.Emoji{
   136  		CreatorId: th.BasicUser.Id,
   137  		Name:      model.NewId(),
   138  	}
   139  
   140  	newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, app.MaxEmojiOriginalHeight+1, 10), "image.gif")
   141  	require.NotNil(t, resp.Error, "should fail - emoji is too tall")
   142  
   143  	// try to create an emoji that's too large
   144  	emoji = &model.Emoji{
   145  		CreatorId: th.BasicUser.Id,
   146  		Name:      model.NewId(),
   147  	}
   148  
   149  	_, resp = Client.CreateEmoji(emoji, utils.CreateTestAnimatedGif(t, 100, 100, 10000), "image.gif")
   150  	require.NotNil(t, resp.Error, "should fail - emoji is too big")
   151  
   152  	// try to create an emoji with data that isn't an image
   153  	emoji = &model.Emoji{
   154  		CreatorId: th.BasicUser.Id,
   155  		Name:      model.NewId(),
   156  	}
   157  
   158  	_, resp = Client.CreateEmoji(emoji, make([]byte, 100), "image.gif")
   159  	CheckBadRequestStatus(t, resp)
   160  	CheckErrorMessage(t, resp, "api.emoji.upload.image.app_error")
   161  
   162  	// try to create an emoji as another user
   163  	emoji = &model.Emoji{
   164  		CreatorId: th.BasicUser2.Id,
   165  		Name:      model.NewId(),
   166  	}
   167  
   168  	_, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
   169  	CheckForbiddenStatus(t, resp)
   170  
   171  	// try to create an emoji without permissions
   172  	th.RemovePermissionFromRole(model.PERMISSION_CREATE_EMOJIS.Id, model.SYSTEM_USER_ROLE_ID)
   173  
   174  	emoji = &model.Emoji{
   175  		CreatorId: th.BasicUser.Id,
   176  		Name:      model.NewId(),
   177  	}
   178  
   179  	_, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
   180  	CheckForbiddenStatus(t, resp)
   181  
   182  	// create an emoji with permissions in one team
   183  	th.AddPermissionToRole(model.PERMISSION_CREATE_EMOJIS.Id, model.TEAM_USER_ROLE_ID)
   184  
   185  	emoji = &model.Emoji{
   186  		CreatorId: th.BasicUser.Id,
   187  		Name:      model.NewId(),
   188  	}
   189  
   190  	_, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
   191  	CheckNoError(t, resp)
   192  }
   193  
   194  func TestGetEmojiList(t *testing.T) {
   195  	th := Setup(t).InitBasic()
   196  	defer th.TearDown()
   197  	Client := th.Client
   198  
   199  	EnableCustomEmoji := *th.App.Config().ServiceSettings.EnableCustomEmoji
   200  	defer func() {
   201  		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCustomEmoji = EnableCustomEmoji })
   202  	}()
   203  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCustomEmoji = true })
   204  
   205  	emojis := []*model.Emoji{
   206  		{
   207  			CreatorId: th.BasicUser.Id,
   208  			Name:      model.NewId(),
   209  		},
   210  		{
   211  			CreatorId: th.BasicUser.Id,
   212  			Name:      model.NewId(),
   213  		},
   214  		{
   215  			CreatorId: th.BasicUser.Id,
   216  			Name:      model.NewId(),
   217  		},
   218  	}
   219  
   220  	for idx, emoji := range emojis {
   221  		newEmoji, resp := Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
   222  		CheckNoError(t, resp)
   223  		emojis[idx] = newEmoji
   224  	}
   225  
   226  	listEmoji, resp := Client.GetEmojiList(0, 100)
   227  	CheckNoError(t, resp)
   228  	for _, emoji := range emojis {
   229  		found := false
   230  		for _, savedEmoji := range listEmoji {
   231  			if emoji.Id == savedEmoji.Id {
   232  				found = true
   233  				break
   234  			}
   235  		}
   236  		require.Truef(t, found, "failed to get emoji with id %v, %v", emoji.Id, len(listEmoji))
   237  	}
   238  
   239  	_, resp = Client.DeleteEmoji(emojis[0].Id)
   240  	CheckNoError(t, resp)
   241  	listEmoji, resp = Client.GetEmojiList(0, 100)
   242  	CheckNoError(t, resp)
   243  	found := false
   244  	for _, savedEmoji := range listEmoji {
   245  		if savedEmoji.Id == emojis[0].Id {
   246  			found = true
   247  			break
   248  		}
   249  	}
   250  	require.Falsef(t, found, "should not get a deleted emoji %v", emojis[0].Id)
   251  
   252  	listEmoji, resp = Client.GetEmojiList(0, 1)
   253  	CheckNoError(t, resp)
   254  
   255  	require.Len(t, listEmoji, 1, "should only return 1")
   256  
   257  	listEmoji, resp = Client.GetSortedEmojiList(0, 100, model.EMOJI_SORT_BY_NAME)
   258  	CheckNoError(t, resp)
   259  
   260  	require.Greater(t, len(listEmoji), 0, "should return more than 0")
   261  }
   262  
   263  func TestDeleteEmoji(t *testing.T) {
   264  	th := Setup(t).InitBasic()
   265  	defer th.TearDown()
   266  	Client := th.Client
   267  
   268  	EnableCustomEmoji := *th.App.Config().ServiceSettings.EnableCustomEmoji
   269  	defer func() {
   270  		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCustomEmoji = EnableCustomEmoji })
   271  	}()
   272  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCustomEmoji = true })
   273  
   274  	defaultRolePermissions := th.SaveDefaultRolePermissions()
   275  	defer func() {
   276  		th.RestoreDefaultRolePermissions(defaultRolePermissions)
   277  	}()
   278  
   279  	emoji := &model.Emoji{
   280  		CreatorId: th.BasicUser.Id,
   281  		Name:      model.NewId(),
   282  	}
   283  
   284  	newEmoji, resp := Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
   285  	CheckNoError(t, resp)
   286  
   287  	ok, resp := Client.DeleteEmoji(newEmoji.Id)
   288  	CheckNoError(t, resp)
   289  	require.True(t, ok, "delete did not return OK")
   290  
   291  	_, resp = Client.GetEmoji(newEmoji.Id)
   292  	require.NotNil(t, resp, "nil response")
   293  	require.NotNil(t, resp.Error, "expected error fetching deleted emoji")
   294  
   295  	//Admin can delete other users emoji
   296  	newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
   297  	CheckNoError(t, resp)
   298  
   299  	ok, resp = th.SystemAdminClient.DeleteEmoji(newEmoji.Id)
   300  	CheckNoError(t, resp)
   301  	require.True(t, ok, "delete did not return OK")
   302  
   303  	_, resp = th.SystemAdminClient.GetEmoji(newEmoji.Id)
   304  	require.NotNil(t, resp, "nil response")
   305  	require.NotNil(t, resp.Error, "expected error fetching deleted emoji")
   306  
   307  	// Try to delete just deleted emoji
   308  	_, resp = Client.DeleteEmoji(newEmoji.Id)
   309  	CheckNotFoundStatus(t, resp)
   310  
   311  	//Try to delete non-existing emoji
   312  	_, resp = Client.DeleteEmoji(model.NewId())
   313  	CheckNotFoundStatus(t, resp)
   314  
   315  	//Try to delete without Id
   316  	_, resp = Client.DeleteEmoji("")
   317  	CheckNotFoundStatus(t, resp)
   318  
   319  	//Try to delete my custom emoji without permissions
   320  	newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
   321  	CheckNoError(t, resp)
   322  
   323  	th.RemovePermissionFromRole(model.PERMISSION_DELETE_EMOJIS.Id, model.SYSTEM_USER_ROLE_ID)
   324  	_, resp = Client.DeleteEmoji(newEmoji.Id)
   325  	CheckForbiddenStatus(t, resp)
   326  	th.AddPermissionToRole(model.PERMISSION_DELETE_EMOJIS.Id, model.SYSTEM_USER_ROLE_ID)
   327  
   328  	//Try to delete other user's custom emoji without DELETE_EMOJIS permissions
   329  	emoji = &model.Emoji{
   330  		CreatorId: th.BasicUser.Id,
   331  		Name:      model.NewId(),
   332  	}
   333  
   334  	newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
   335  	CheckNoError(t, resp)
   336  
   337  	th.RemovePermissionFromRole(model.PERMISSION_DELETE_EMOJIS.Id, model.SYSTEM_USER_ROLE_ID)
   338  	th.AddPermissionToRole(model.PERMISSION_DELETE_OTHERS_EMOJIS.Id, model.SYSTEM_USER_ROLE_ID)
   339  
   340  	Client.Logout()
   341  	th.LoginBasic2()
   342  
   343  	_, resp = Client.DeleteEmoji(newEmoji.Id)
   344  	CheckForbiddenStatus(t, resp)
   345  
   346  	th.RemovePermissionFromRole(model.PERMISSION_DELETE_OTHERS_EMOJIS.Id, model.SYSTEM_USER_ROLE_ID)
   347  	th.AddPermissionToRole(model.PERMISSION_DELETE_EMOJIS.Id, model.SYSTEM_USER_ROLE_ID)
   348  
   349  	Client.Logout()
   350  	th.LoginBasic()
   351  
   352  	//Try to delete other user's custom emoji without DELETE_OTHERS_EMOJIS permissions
   353  	emoji = &model.Emoji{
   354  		CreatorId: th.BasicUser.Id,
   355  		Name:      model.NewId(),
   356  	}
   357  
   358  	newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
   359  	CheckNoError(t, resp)
   360  
   361  	Client.Logout()
   362  	th.LoginBasic2()
   363  
   364  	_, resp = Client.DeleteEmoji(newEmoji.Id)
   365  	CheckForbiddenStatus(t, resp)
   366  
   367  	Client.Logout()
   368  	th.LoginBasic()
   369  
   370  	//Try to delete other user's custom emoji with permissions
   371  	emoji = &model.Emoji{
   372  		CreatorId: th.BasicUser.Id,
   373  		Name:      model.NewId(),
   374  	}
   375  
   376  	newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
   377  	CheckNoError(t, resp)
   378  
   379  	th.AddPermissionToRole(model.PERMISSION_DELETE_EMOJIS.Id, model.SYSTEM_USER_ROLE_ID)
   380  	th.AddPermissionToRole(model.PERMISSION_DELETE_OTHERS_EMOJIS.Id, model.SYSTEM_USER_ROLE_ID)
   381  
   382  	Client.Logout()
   383  	th.LoginBasic2()
   384  
   385  	_, resp = Client.DeleteEmoji(newEmoji.Id)
   386  	CheckNoError(t, resp)
   387  
   388  	Client.Logout()
   389  	th.LoginBasic()
   390  
   391  	//Try to delete my custom emoji with permissions at team level
   392  	newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
   393  	CheckNoError(t, resp)
   394  
   395  	th.RemovePermissionFromRole(model.PERMISSION_DELETE_EMOJIS.Id, model.SYSTEM_USER_ROLE_ID)
   396  	th.AddPermissionToRole(model.PERMISSION_DELETE_EMOJIS.Id, model.TEAM_USER_ROLE_ID)
   397  	_, resp = Client.DeleteEmoji(newEmoji.Id)
   398  	CheckNoError(t, resp)
   399  	th.AddPermissionToRole(model.PERMISSION_DELETE_EMOJIS.Id, model.SYSTEM_USER_ROLE_ID)
   400  	th.RemovePermissionFromRole(model.PERMISSION_DELETE_EMOJIS.Id, model.TEAM_USER_ROLE_ID)
   401  
   402  	//Try to delete other user's custom emoji with permissions at team level
   403  	emoji = &model.Emoji{
   404  		CreatorId: th.BasicUser.Id,
   405  		Name:      model.NewId(),
   406  	}
   407  
   408  	newEmoji, resp = Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
   409  	CheckNoError(t, resp)
   410  
   411  	th.RemovePermissionFromRole(model.PERMISSION_DELETE_EMOJIS.Id, model.SYSTEM_USER_ROLE_ID)
   412  	th.RemovePermissionFromRole(model.PERMISSION_DELETE_OTHERS_EMOJIS.Id, model.SYSTEM_USER_ROLE_ID)
   413  
   414  	th.AddPermissionToRole(model.PERMISSION_DELETE_EMOJIS.Id, model.TEAM_USER_ROLE_ID)
   415  	th.AddPermissionToRole(model.PERMISSION_DELETE_OTHERS_EMOJIS.Id, model.TEAM_USER_ROLE_ID)
   416  
   417  	Client.Logout()
   418  	th.LoginBasic2()
   419  
   420  	_, resp = Client.DeleteEmoji(newEmoji.Id)
   421  	CheckNoError(t, resp)
   422  }
   423  
   424  func TestGetEmoji(t *testing.T) {
   425  	th := Setup(t).InitBasic()
   426  	defer th.TearDown()
   427  	Client := th.Client
   428  
   429  	EnableCustomEmoji := *th.App.Config().ServiceSettings.EnableCustomEmoji
   430  	defer func() {
   431  		th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCustomEmoji = EnableCustomEmoji })
   432  	}()
   433  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCustomEmoji = true })
   434  
   435  	emoji := &model.Emoji{
   436  		CreatorId: th.BasicUser.Id,
   437  		Name:      model.NewId(),
   438  	}
   439  
   440  	newEmoji, resp := Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
   441  	CheckNoError(t, resp)
   442  
   443  	emoji, resp = Client.GetEmoji(newEmoji.Id)
   444  	CheckNoError(t, resp)
   445  	require.Equal(t, newEmoji.Id, emoji.Id, "wrong emoji was returned")
   446  
   447  	_, resp = Client.GetEmoji(model.NewId())
   448  	CheckNotFoundStatus(t, resp)
   449  }
   450  
   451  func TestGetEmojiByName(t *testing.T) {
   452  	th := Setup(t).InitBasic()
   453  	defer th.TearDown()
   454  	Client := th.Client
   455  
   456  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCustomEmoji = true })
   457  
   458  	emoji := &model.Emoji{
   459  		CreatorId: th.BasicUser.Id,
   460  		Name:      model.NewId(),
   461  	}
   462  
   463  	newEmoji, resp := Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
   464  	CheckNoError(t, resp)
   465  
   466  	emoji, resp = Client.GetEmojiByName(newEmoji.Name)
   467  	CheckNoError(t, resp)
   468  	assert.Equal(t, newEmoji.Name, emoji.Name)
   469  
   470  	_, resp = Client.GetEmojiByName(model.NewId())
   471  	CheckNotFoundStatus(t, resp)
   472  
   473  	Client.Logout()
   474  	_, resp = Client.GetEmojiByName(newEmoji.Name)
   475  	CheckUnauthorizedStatus(t, resp)
   476  }
   477  
   478  func TestGetEmojiImage(t *testing.T) {
   479  	th := Setup(t).InitBasic()
   480  	defer th.TearDown()
   481  	Client := th.Client
   482  
   483  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCustomEmoji = true })
   484  
   485  	emoji1 := &model.Emoji{
   486  		CreatorId: th.BasicUser.Id,
   487  		Name:      model.NewId(),
   488  	}
   489  
   490  	emoji1, resp := Client.CreateEmoji(emoji1, utils.CreateTestGif(t, 10, 10), "image.gif")
   491  	CheckNoError(t, resp)
   492  
   493  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCustomEmoji = false })
   494  
   495  	_, resp = Client.GetEmojiImage(emoji1.Id)
   496  	CheckNotImplementedStatus(t, resp)
   497  	CheckErrorMessage(t, resp, "api.emoji.disabled.app_error")
   498  
   499  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCustomEmoji = true })
   500  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.FileSettings.DriverName = "local" })
   501  
   502  	emojiImage, resp := Client.GetEmojiImage(emoji1.Id)
   503  	CheckNoError(t, resp)
   504  	require.Greater(t, len(emojiImage), 0, "should return the image")
   505  
   506  	_, imageType, err := image.DecodeConfig(bytes.NewReader(emojiImage))
   507  	require.NoError(t, err)
   508  	require.Equal(t, imageType, "gif", "expected gif")
   509  
   510  	emoji2 := &model.Emoji{
   511  		CreatorId: th.BasicUser.Id,
   512  		Name:      model.NewId(),
   513  	}
   514  
   515  	emoji2, resp = Client.CreateEmoji(emoji2, utils.CreateTestAnimatedGif(t, 10, 10, 10), "image.gif")
   516  	CheckNoError(t, resp)
   517  
   518  	emojiImage, resp = Client.GetEmojiImage(emoji2.Id)
   519  	CheckNoError(t, resp)
   520  	require.Greater(t, len(emojiImage), 0, "no image returned")
   521  
   522  	_, imageType, err = image.DecodeConfig(bytes.NewReader(emojiImage))
   523  	require.NoError(t, err, "unable to indentify received image")
   524  	require.Equal(t, imageType, "gif", "expected gif")
   525  
   526  	emoji3 := &model.Emoji{
   527  		CreatorId: th.BasicUser.Id,
   528  		Name:      model.NewId(),
   529  	}
   530  	emoji3, resp = Client.CreateEmoji(emoji3, utils.CreateTestJpeg(t, 10, 10), "image.jpg")
   531  	CheckNoError(t, resp)
   532  
   533  	emojiImage, resp = Client.GetEmojiImage(emoji3.Id)
   534  	CheckNoError(t, resp)
   535  	require.Greater(t, len(emojiImage), 0, "no image returned")
   536  
   537  	_, imageType, err = image.DecodeConfig(bytes.NewReader(emojiImage))
   538  	require.NoError(t, err, "unable to indentify received image")
   539  	require.Equal(t, imageType, "jpeg", "expected jpeg")
   540  
   541  	emoji4 := &model.Emoji{
   542  		CreatorId: th.BasicUser.Id,
   543  		Name:      model.NewId(),
   544  	}
   545  	emoji4, resp = Client.CreateEmoji(emoji4, utils.CreateTestPng(t, 10, 10), "image.png")
   546  	CheckNoError(t, resp)
   547  
   548  	emojiImage, resp = Client.GetEmojiImage(emoji4.Id)
   549  	CheckNoError(t, resp)
   550  	require.Greater(t, len(emojiImage), 0, "no image returned")
   551  
   552  	_, imageType, err = image.DecodeConfig(bytes.NewReader(emojiImage))
   553  	require.NoError(t, err, "unable to idenitify received image")
   554  	require.Equal(t, imageType, "png", "expected png")
   555  
   556  	_, resp = Client.DeleteEmoji(emoji4.Id)
   557  	CheckNoError(t, resp)
   558  
   559  	_, resp = Client.GetEmojiImage(emoji4.Id)
   560  	CheckNotFoundStatus(t, resp)
   561  
   562  	_, resp = Client.GetEmojiImage(model.NewId())
   563  	CheckNotFoundStatus(t, resp)
   564  
   565  	_, resp = Client.GetEmojiImage("")
   566  	CheckBadRequestStatus(t, resp)
   567  }
   568  
   569  func TestSearchEmoji(t *testing.T) {
   570  	th := Setup(t).InitBasic()
   571  	defer th.TearDown()
   572  	Client := th.Client
   573  
   574  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCustomEmoji = true })
   575  
   576  	searchTerm1 := model.NewId()
   577  	searchTerm2 := model.NewId()
   578  
   579  	emojis := []*model.Emoji{
   580  		{
   581  			CreatorId: th.BasicUser.Id,
   582  			Name:      searchTerm1,
   583  		},
   584  		{
   585  			CreatorId: th.BasicUser.Id,
   586  			Name:      "blargh_" + searchTerm2,
   587  		},
   588  	}
   589  
   590  	for idx, emoji := range emojis {
   591  		newEmoji, resp := Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
   592  		CheckNoError(t, resp)
   593  		emojis[idx] = newEmoji
   594  	}
   595  
   596  	search := &model.EmojiSearch{Term: searchTerm1}
   597  	remojis, resp := Client.SearchEmoji(search)
   598  	CheckNoError(t, resp)
   599  	CheckOKStatus(t, resp)
   600  
   601  	found := false
   602  	for _, e := range remojis {
   603  		if e.Name == emojis[0].Name {
   604  			found = true
   605  		}
   606  	}
   607  
   608  	assert.True(t, found)
   609  
   610  	search.Term = searchTerm2
   611  	search.PrefixOnly = true
   612  	remojis, resp = Client.SearchEmoji(search)
   613  	CheckNoError(t, resp)
   614  	CheckOKStatus(t, resp)
   615  
   616  	found = false
   617  	for _, e := range remojis {
   618  		if e.Name == emojis[1].Name {
   619  			found = true
   620  		}
   621  	}
   622  
   623  	assert.False(t, found)
   624  
   625  	search.PrefixOnly = false
   626  	remojis, resp = Client.SearchEmoji(search)
   627  	CheckNoError(t, resp)
   628  	CheckOKStatus(t, resp)
   629  
   630  	found = false
   631  	for _, e := range remojis {
   632  		if e.Name == emojis[1].Name {
   633  			found = true
   634  		}
   635  	}
   636  
   637  	assert.True(t, found)
   638  
   639  	search.Term = ""
   640  	_, resp = Client.SearchEmoji(search)
   641  	CheckBadRequestStatus(t, resp)
   642  
   643  	Client.Logout()
   644  	_, resp = Client.SearchEmoji(search)
   645  	CheckUnauthorizedStatus(t, resp)
   646  }
   647  
   648  func TestAutocompleteEmoji(t *testing.T) {
   649  	th := Setup(t).InitBasic()
   650  	defer th.TearDown()
   651  	Client := th.Client
   652  
   653  	th.App.UpdateConfig(func(cfg *model.Config) { *cfg.ServiceSettings.EnableCustomEmoji = true })
   654  
   655  	searchTerm1 := model.NewId()
   656  
   657  	emojis := []*model.Emoji{
   658  		{
   659  			CreatorId: th.BasicUser.Id,
   660  			Name:      searchTerm1,
   661  		},
   662  		{
   663  			CreatorId: th.BasicUser.Id,
   664  			Name:      "blargh_" + searchTerm1,
   665  		},
   666  	}
   667  
   668  	for idx, emoji := range emojis {
   669  		newEmoji, resp := Client.CreateEmoji(emoji, utils.CreateTestGif(t, 10, 10), "image.gif")
   670  		CheckNoError(t, resp)
   671  		emojis[idx] = newEmoji
   672  	}
   673  
   674  	remojis, resp := Client.AutocompleteEmoji(searchTerm1, "")
   675  	CheckNoError(t, resp)
   676  	CheckOKStatus(t, resp)
   677  
   678  	found1 := false
   679  	found2 := false
   680  	for _, e := range remojis {
   681  		if e.Name == emojis[0].Name {
   682  			found1 = true
   683  		}
   684  
   685  		if e.Name == emojis[1].Name {
   686  			found2 = true
   687  		}
   688  	}
   689  
   690  	assert.True(t, found1)
   691  	assert.False(t, found2)
   692  
   693  	_, resp = Client.AutocompleteEmoji("", "")
   694  	CheckBadRequestStatus(t, resp)
   695  
   696  	Client.Logout()
   697  	_, resp = Client.AutocompleteEmoji(searchTerm1, "")
   698  	CheckUnauthorizedStatus(t, resp)
   699  }