github.com/mad-app/mattermost-server@v5.11.1+incompatible/api4/emoji_test.go (about)

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