github.com/coincircle/mattermost-server@v4.8.1-0.20180321182714-9d701c704416+incompatible/api4/reaction_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  	"strings"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  
    12  	"github.com/mattermost/mattermost-server/model"
    13  )
    14  
    15  func TestSaveReaction(t *testing.T) {
    16  	th := Setup().InitBasic().InitSystemAdmin()
    17  	defer th.TearDown()
    18  	Client := th.Client
    19  	userId := th.BasicUser.Id
    20  	postId := th.BasicPost.Id
    21  
    22  	reaction := &model.Reaction{
    23  		UserId:    userId,
    24  		PostId:    postId,
    25  		EmojiName: "smile",
    26  	}
    27  
    28  	rr, resp := Client.SaveReaction(reaction)
    29  	CheckNoError(t, resp)
    30  
    31  	if rr.UserId != reaction.UserId {
    32  		t.Fatal("UserId did not match")
    33  	}
    34  
    35  	if rr.PostId != reaction.PostId {
    36  		t.Fatal("PostId did not match")
    37  	}
    38  
    39  	if rr.EmojiName != reaction.EmojiName {
    40  		t.Fatal("EmojiName did not match")
    41  	}
    42  
    43  	if rr.CreateAt == 0 {
    44  		t.Fatal("CreateAt should exist")
    45  	}
    46  
    47  	if reactions, err := th.App.GetReactionsForPost(postId); err != nil && len(reactions) != 1 {
    48  		t.Fatal("didn't save reaction correctly")
    49  	}
    50  
    51  	// saving a duplicate reaction
    52  	rr, resp = Client.SaveReaction(reaction)
    53  	CheckNoError(t, resp)
    54  
    55  	if reactions, err := th.App.GetReactionsForPost(postId); err != nil && len(reactions) != 1 {
    56  		t.Fatal("should have not save duplicated reaction")
    57  	}
    58  
    59  	reaction.EmojiName = "sad"
    60  
    61  	rr, resp = Client.SaveReaction(reaction)
    62  	CheckNoError(t, resp)
    63  
    64  	if rr.EmojiName != reaction.EmojiName {
    65  		t.Fatal("EmojiName did not match")
    66  	}
    67  
    68  	if reactions, err := th.App.GetReactionsForPost(postId); err != nil && len(reactions) != 2 {
    69  		t.Fatal("should have save multiple reactions")
    70  	}
    71  
    72  	// saving special case
    73  	reaction.EmojiName = "+1"
    74  
    75  	rr, resp = Client.SaveReaction(reaction)
    76  	CheckNoError(t, resp)
    77  
    78  	if rr.EmojiName != reaction.EmojiName {
    79  		t.Fatal("EmojiName did not match")
    80  	}
    81  
    82  	if reactions, err := th.App.GetReactionsForPost(postId); err != nil && len(reactions) != 3 {
    83  		t.Fatal("should have save multiple reactions")
    84  	}
    85  
    86  	reaction.PostId = GenerateTestId()
    87  
    88  	_, resp = Client.SaveReaction(reaction)
    89  	CheckForbiddenStatus(t, resp)
    90  
    91  	reaction.PostId = "junk"
    92  
    93  	_, resp = Client.SaveReaction(reaction)
    94  	CheckBadRequestStatus(t, resp)
    95  
    96  	reaction.PostId = postId
    97  	reaction.UserId = GenerateTestId()
    98  
    99  	_, resp = Client.SaveReaction(reaction)
   100  	CheckForbiddenStatus(t, resp)
   101  
   102  	reaction.UserId = "junk"
   103  
   104  	_, resp = Client.SaveReaction(reaction)
   105  	CheckBadRequestStatus(t, resp)
   106  
   107  	reaction.UserId = userId
   108  	reaction.EmojiName = ""
   109  
   110  	_, resp = Client.SaveReaction(reaction)
   111  	CheckBadRequestStatus(t, resp)
   112  
   113  	reaction.EmojiName = strings.Repeat("a", 65)
   114  
   115  	_, resp = Client.SaveReaction(reaction)
   116  	CheckBadRequestStatus(t, resp)
   117  
   118  	reaction.EmojiName = "smile"
   119  	otherUser := th.CreateUser()
   120  	Client.Logout()
   121  	Client.Login(otherUser.Email, otherUser.Password)
   122  
   123  	_, resp = Client.SaveReaction(reaction)
   124  	CheckForbiddenStatus(t, resp)
   125  
   126  	Client.Logout()
   127  	_, resp = Client.SaveReaction(reaction)
   128  	CheckUnauthorizedStatus(t, resp)
   129  
   130  	_, resp = th.SystemAdminClient.SaveReaction(reaction)
   131  	CheckForbiddenStatus(t, resp)
   132  }
   133  
   134  func TestGetReactions(t *testing.T) {
   135  	th := Setup().InitBasic().InitSystemAdmin()
   136  	defer th.TearDown()
   137  	Client := th.Client
   138  	userId := th.BasicUser.Id
   139  	user2Id := th.BasicUser2.Id
   140  	postId := th.BasicPost.Id
   141  
   142  	userReactions := []*model.Reaction{
   143  		{
   144  			UserId:    userId,
   145  			PostId:    postId,
   146  			EmojiName: "smile",
   147  		},
   148  		{
   149  			UserId:    userId,
   150  			PostId:    postId,
   151  			EmojiName: "happy",
   152  		},
   153  		{
   154  			UserId:    userId,
   155  			PostId:    postId,
   156  			EmojiName: "sad",
   157  		},
   158  		{
   159  			UserId:    user2Id,
   160  			PostId:    postId,
   161  			EmojiName: "smile",
   162  		},
   163  		{
   164  			UserId:    user2Id,
   165  			PostId:    postId,
   166  			EmojiName: "sad",
   167  		},
   168  	}
   169  
   170  	var reactions []*model.Reaction
   171  
   172  	for _, userReaction := range userReactions {
   173  		if result := <-th.App.Srv.Store.Reaction().Save(userReaction); result.Err != nil {
   174  			t.Fatal(result.Err)
   175  		} else {
   176  			reactions = append(reactions, result.Data.(*model.Reaction))
   177  		}
   178  	}
   179  
   180  	rr, resp := Client.GetReactions(postId)
   181  	CheckNoError(t, resp)
   182  
   183  	assert.Len(t, rr, 5)
   184  	for _, r := range reactions {
   185  		assert.Contains(t, reactions, r)
   186  	}
   187  
   188  	rr, resp = Client.GetReactions("junk")
   189  	CheckBadRequestStatus(t, resp)
   190  
   191  	assert.Empty(t, rr)
   192  
   193  	_, resp = Client.GetReactions(GenerateTestId())
   194  	CheckForbiddenStatus(t, resp)
   195  
   196  	Client.Logout()
   197  
   198  	_, resp = Client.GetReactions(postId)
   199  	CheckUnauthorizedStatus(t, resp)
   200  
   201  	_, resp = th.SystemAdminClient.GetReactions(postId)
   202  	CheckNoError(t, resp)
   203  }
   204  
   205  func TestDeleteReaction(t *testing.T) {
   206  	th := Setup().InitBasic().InitSystemAdmin()
   207  	defer th.TearDown()
   208  	Client := th.Client
   209  	userId := th.BasicUser.Id
   210  	user2Id := th.BasicUser2.Id
   211  	postId := th.BasicPost.Id
   212  
   213  	r1 := &model.Reaction{
   214  		UserId:    userId,
   215  		PostId:    postId,
   216  		EmojiName: "smile",
   217  	}
   218  
   219  	th.App.SaveReactionForPost(r1)
   220  	if reactions, err := th.App.GetReactionsForPost(postId); err != nil || len(reactions) != 1 {
   221  		t.Fatal("didn't save reaction correctly")
   222  	}
   223  
   224  	ok, resp := Client.DeleteReaction(r1)
   225  	CheckNoError(t, resp)
   226  
   227  	if !ok {
   228  		t.Fatal("should have returned true")
   229  	}
   230  
   231  	if reactions, err := th.App.GetReactionsForPost(postId); err != nil || len(reactions) != 0 {
   232  		t.Fatal("should have deleted reaction")
   233  	}
   234  
   235  	// deleting one reaction when a post has multiple reactions
   236  	r2 := &model.Reaction{
   237  		UserId:    userId,
   238  		PostId:    postId,
   239  		EmojiName: "smile-",
   240  	}
   241  
   242  	th.App.SaveReactionForPost(r1)
   243  	th.App.SaveReactionForPost(r2)
   244  	if reactions, err := th.App.GetReactionsForPost(postId); err != nil || len(reactions) != 2 {
   245  		t.Fatal("didn't save reactions correctly")
   246  	}
   247  
   248  	_, resp = Client.DeleteReaction(r2)
   249  	CheckNoError(t, resp)
   250  
   251  	if reactions, err := th.App.GetReactionsForPost(postId); err != nil || len(reactions) != 1 || *reactions[0] != *r1 {
   252  		t.Fatal("should have deleted 1 reaction only")
   253  	}
   254  
   255  	// deleting one reaction of name +1
   256  	r3 := &model.Reaction{
   257  		UserId:    userId,
   258  		PostId:    postId,
   259  		EmojiName: "+1",
   260  	}
   261  
   262  	th.App.SaveReactionForPost(r3)
   263  	if reactions, err := th.App.GetReactionsForPost(postId); err != nil || len(reactions) != 2 {
   264  		t.Fatal("didn't save reactions correctly")
   265  	}
   266  
   267  	_, resp = Client.DeleteReaction(r3)
   268  	CheckNoError(t, resp)
   269  
   270  	if reactions, err := th.App.GetReactionsForPost(postId); err != nil || len(reactions) != 1 || *reactions[0] != *r1 {
   271  		t.Fatal("should have deleted 1 reaction only")
   272  	}
   273  
   274  	// deleting a reaction made by another user
   275  	r4 := &model.Reaction{
   276  		UserId:    user2Id,
   277  		PostId:    postId,
   278  		EmojiName: "smile_",
   279  	}
   280  
   281  	th.LoginBasic2()
   282  	th.App.SaveReactionForPost(r4)
   283  	if reactions, err := th.App.GetReactionsForPost(postId); err != nil || len(reactions) != 2 {
   284  		t.Fatal("didn't save reaction correctly")
   285  	}
   286  
   287  	th.LoginBasic()
   288  
   289  	ok, resp = Client.DeleteReaction(r4)
   290  	CheckForbiddenStatus(t, resp)
   291  
   292  	if ok {
   293  		t.Fatal("should have returned false")
   294  	}
   295  
   296  	if reactions, err := th.App.GetReactionsForPost(postId); err != nil || len(reactions) != 2 {
   297  		t.Fatal("should have not deleted a reaction")
   298  	}
   299  
   300  	r1.PostId = GenerateTestId()
   301  	_, resp = Client.DeleteReaction(r1)
   302  	CheckForbiddenStatus(t, resp)
   303  
   304  	r1.PostId = "junk"
   305  
   306  	_, resp = Client.DeleteReaction(r1)
   307  	CheckBadRequestStatus(t, resp)
   308  
   309  	r1.PostId = postId
   310  	r1.UserId = GenerateTestId()
   311  
   312  	_, resp = Client.DeleteReaction(r1)
   313  	CheckForbiddenStatus(t, resp)
   314  
   315  	r1.UserId = "junk"
   316  
   317  	_, resp = Client.DeleteReaction(r1)
   318  	CheckBadRequestStatus(t, resp)
   319  
   320  	r1.UserId = userId
   321  	r1.EmojiName = ""
   322  
   323  	_, resp = Client.DeleteReaction(r1)
   324  	CheckNotFoundStatus(t, resp)
   325  
   326  	r1.EmojiName = strings.Repeat("a", 65)
   327  
   328  	_, resp = Client.DeleteReaction(r1)
   329  	CheckBadRequestStatus(t, resp)
   330  
   331  	Client.Logout()
   332  	r1.EmojiName = "smile"
   333  
   334  	_, resp = Client.DeleteReaction(r1)
   335  	CheckUnauthorizedStatus(t, resp)
   336  
   337  	_, resp = th.SystemAdminClient.DeleteReaction(r1)
   338  	CheckNoError(t, resp)
   339  
   340  	_, resp = th.SystemAdminClient.DeleteReaction(r4)
   341  	CheckNoError(t, resp)
   342  
   343  	if reactions, err := th.App.GetReactionsForPost(postId); err != nil || len(reactions) != 0 {
   344  		t.Fatal("should have deleted both reactions")
   345  	}
   346  }