github.com/gigforks/mattermost-server@v4.9.1-0.20180619094218-800d97fa55d0+incompatible/api/reaction_test.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package api
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/mattermost/mattermost-server/model"
    10  )
    11  
    12  func TestSaveReaction(t *testing.T) {
    13  	th := Setup().InitBasic()
    14  	defer th.TearDown()
    15  
    16  	Client := th.BasicClient
    17  
    18  	user := th.BasicUser
    19  	user2 := th.BasicUser2
    20  
    21  	channel := th.BasicChannel
    22  	post := th.BasicPost
    23  
    24  	// saving a reaction
    25  	reaction := &model.Reaction{
    26  		UserId:    user.Id,
    27  		PostId:    post.Id,
    28  		EmojiName: "smile",
    29  	}
    30  	if returned, err := Client.SaveReaction(channel.Id, reaction); err != nil {
    31  		t.Fatal(err)
    32  	} else {
    33  		reaction = returned
    34  	}
    35  
    36  	if reactions := Client.MustGeneric(Client.ListReactions(channel.Id, post.Id)).([]*model.Reaction); len(reactions) != 1 || *reactions[0] != *reaction {
    37  		t.Fatal("didn't save reaction correctly")
    38  	}
    39  
    40  	// saving a duplicate reaction
    41  	if _, err := Client.SaveReaction(channel.Id, reaction); err != nil {
    42  		t.Fatal(err)
    43  	}
    44  
    45  	// saving a second reaction on a post
    46  	reaction2 := &model.Reaction{
    47  		UserId:    user.Id,
    48  		PostId:    post.Id,
    49  		EmojiName: "sad",
    50  	}
    51  	if returned, err := Client.SaveReaction(channel.Id, reaction2); err != nil {
    52  		t.Fatal(err)
    53  	} else {
    54  		reaction2 = returned
    55  	}
    56  
    57  	if reactions := Client.MustGeneric(Client.ListReactions(channel.Id, post.Id)).([]*model.Reaction); len(reactions) != 2 ||
    58  		(*reactions[0] != *reaction && *reactions[1] != *reaction) || (*reactions[0] != *reaction2 && *reactions[1] != *reaction2) {
    59  		t.Fatal("didn't save multiple reactions correctly")
    60  	}
    61  
    62  	// saving a reaction without a user id
    63  	reaction3 := &model.Reaction{
    64  		PostId:    post.Id,
    65  		EmojiName: "smile",
    66  	}
    67  	if _, err := Client.SaveReaction(channel.Id, reaction3); err == nil {
    68  		t.Fatal("should've failed to save reaction without user id")
    69  	}
    70  
    71  	// saving a reaction without a post id
    72  	reaction4 := &model.Reaction{
    73  		UserId:    user.Id,
    74  		EmojiName: "smile",
    75  	}
    76  	if _, err := Client.SaveReaction(channel.Id, reaction4); err == nil {
    77  		t.Fatal("should've failed to save reaction without post id")
    78  	}
    79  
    80  	// saving a reaction without a emoji name
    81  	reaction5 := &model.Reaction{
    82  		UserId: user.Id,
    83  		PostId: post.Id,
    84  	}
    85  	if _, err := Client.SaveReaction(channel.Id, reaction5); err == nil {
    86  		t.Fatal("should've failed to save reaction without emoji name")
    87  	}
    88  
    89  	// saving a reaction for another user
    90  	reaction6 := &model.Reaction{
    91  		UserId:    user2.Id,
    92  		PostId:    post.Id,
    93  		EmojiName: "smile",
    94  	}
    95  	if _, err := Client.SaveReaction(channel.Id, reaction6); err == nil {
    96  		t.Fatal("should've failed to save reaction for another user")
    97  	}
    98  
    99  	// saving a reaction to a channel we're not a member of
   100  	th.LoginBasic2()
   101  	channel2 := th.CreateChannel(th.BasicClient, th.BasicTeam)
   102  	post2 := th.CreatePost(th.BasicClient, channel2)
   103  	th.LoginBasic()
   104  
   105  	reaction7 := &model.Reaction{
   106  		UserId:    user.Id,
   107  		PostId:    post2.Id,
   108  		EmojiName: "smile",
   109  	}
   110  	if _, err := Client.SaveReaction(channel2.Id, reaction7); err == nil {
   111  		t.Fatal("should've failed to save reaction to a channel we're not a member of")
   112  	}
   113  
   114  	// saving a reaction to a direct channel
   115  	directChannel := Client.Must(Client.CreateDirectChannel(user2.Id)).Data.(*model.Channel)
   116  	directPost := th.CreatePost(th.BasicClient, directChannel)
   117  
   118  	reaction8 := &model.Reaction{
   119  		UserId:    user.Id,
   120  		PostId:    directPost.Id,
   121  		EmojiName: "smile",
   122  	}
   123  	if returned, err := Client.SaveReaction(directChannel.Id, reaction8); err != nil {
   124  		t.Fatal(err)
   125  	} else {
   126  		reaction8 = returned
   127  	}
   128  
   129  	if reactions := Client.MustGeneric(Client.ListReactions(directChannel.Id, directPost.Id)).([]*model.Reaction); len(reactions) != 1 || *reactions[0] != *reaction8 {
   130  		t.Fatal("didn't save reaction correctly")
   131  	}
   132  
   133  	// saving a reaction for a post in the wrong channel
   134  	reaction9 := &model.Reaction{
   135  		UserId:    user.Id,
   136  		PostId:    directPost.Id,
   137  		EmojiName: "sad",
   138  	}
   139  	if _, err := Client.SaveReaction(channel.Id, reaction9); err == nil {
   140  		t.Fatal("should've failed to save reaction to a post that isn't in the given channel")
   141  	}
   142  }
   143  
   144  func TestDeleteReaction(t *testing.T) {
   145  	th := Setup().InitBasic()
   146  	defer th.TearDown()
   147  
   148  	Client := th.BasicClient
   149  
   150  	user := th.BasicUser
   151  	user2 := th.BasicUser2
   152  
   153  	channel := th.BasicChannel
   154  	post := th.BasicPost
   155  
   156  	reaction1 := &model.Reaction{
   157  		UserId:    user.Id,
   158  		PostId:    post.Id,
   159  		EmojiName: "smile",
   160  	}
   161  
   162  	// deleting a reaction that does exist
   163  	Client.MustGeneric(Client.SaveReaction(channel.Id, reaction1))
   164  	if err := Client.DeleteReaction(channel.Id, reaction1); err != nil {
   165  		t.Fatal(err)
   166  	}
   167  
   168  	if reactions := Client.MustGeneric(Client.ListReactions(channel.Id, post.Id)).([]*model.Reaction); len(reactions) != 0 {
   169  		t.Fatal("should've deleted reaction")
   170  	}
   171  
   172  	// deleting one reaction when a post has multiple
   173  	reaction2 := &model.Reaction{
   174  		UserId:    user.Id,
   175  		PostId:    post.Id,
   176  		EmojiName: "sad",
   177  	}
   178  	reaction1 = Client.MustGeneric(Client.SaveReaction(channel.Id, reaction1)).(*model.Reaction)
   179  	reaction2 = Client.MustGeneric(Client.SaveReaction(channel.Id, reaction2)).(*model.Reaction)
   180  	if err := Client.DeleteReaction(channel.Id, reaction2); err != nil {
   181  		t.Fatal(err)
   182  	}
   183  
   184  	if reactions := Client.MustGeneric(Client.ListReactions(channel.Id, post.Id)).([]*model.Reaction); len(reactions) != 1 || *reactions[0] != *reaction1 {
   185  		t.Fatal("should've deleted only one reaction")
   186  	}
   187  
   188  	// deleting a reaction made by another user
   189  	reaction3 := &model.Reaction{
   190  		UserId:    user2.Id,
   191  		PostId:    post.Id,
   192  		EmojiName: "smile",
   193  	}
   194  
   195  	th.LoginBasic2()
   196  	Client.Must(Client.JoinChannel(channel.Id))
   197  	reaction3 = Client.MustGeneric(Client.SaveReaction(channel.Id, reaction3)).(*model.Reaction)
   198  
   199  	th.LoginBasic()
   200  	if err := Client.DeleteReaction(channel.Id, reaction3); err == nil {
   201  		t.Fatal("should've failed to delete another user's reaction")
   202  	}
   203  
   204  	// deleting a reaction for a post we can't see
   205  	channel2 := th.CreateChannel(th.BasicClient, th.BasicTeam)
   206  	post2 := th.CreatePost(th.BasicClient, channel2)
   207  
   208  	reaction4 := &model.Reaction{
   209  		UserId:    user.Id,
   210  		PostId:    post2.Id,
   211  		EmojiName: "smile",
   212  	}
   213  
   214  	reaction4 = Client.MustGeneric(Client.SaveReaction(channel2.Id, reaction4)).(*model.Reaction)
   215  	Client.Must(Client.LeaveChannel(channel2.Id))
   216  
   217  	if err := Client.DeleteReaction(channel2.Id, reaction4); err == nil {
   218  		t.Fatal("should've failed to delete a reaction from a channel we're not in")
   219  	}
   220  
   221  	// deleting a reaction for a post with the wrong channel
   222  	channel3 := th.CreateChannel(th.BasicClient, th.BasicTeam)
   223  
   224  	reaction5 := &model.Reaction{
   225  		UserId:    user.Id,
   226  		PostId:    post.Id,
   227  		EmojiName: "happy",
   228  	}
   229  	if _, err := Client.SaveReaction(channel3.Id, reaction5); err == nil {
   230  		t.Fatal("should've failed to save reaction to a post that isn't in the given channel")
   231  	}
   232  }
   233  
   234  func TestListReactions(t *testing.T) {
   235  	th := Setup().InitBasic()
   236  	defer th.TearDown()
   237  
   238  	Client := th.BasicClient
   239  
   240  	user := th.BasicUser
   241  	user2 := th.BasicUser2
   242  
   243  	channel := th.BasicChannel
   244  
   245  	post := th.BasicPost
   246  
   247  	userReactions := []*model.Reaction{
   248  		{
   249  			UserId:    user.Id,
   250  			PostId:    post.Id,
   251  			EmojiName: "smile",
   252  		},
   253  		{
   254  			UserId:    user.Id,
   255  			PostId:    post.Id,
   256  			EmojiName: "happy",
   257  		},
   258  		{
   259  			UserId:    user.Id,
   260  			PostId:    post.Id,
   261  			EmojiName: "sad",
   262  		},
   263  	}
   264  
   265  	for i, reaction := range userReactions {
   266  		userReactions[i] = Client.MustGeneric(Client.SaveReaction(channel.Id, reaction)).(*model.Reaction)
   267  	}
   268  
   269  	th.LoginBasic2()
   270  	Client.Must(Client.JoinChannel(channel.Id))
   271  
   272  	userReactions2 := []*model.Reaction{
   273  		{
   274  			UserId:    user2.Id,
   275  			PostId:    post.Id,
   276  			EmojiName: "smile",
   277  		},
   278  		{
   279  			UserId:    user2.Id,
   280  			PostId:    post.Id,
   281  			EmojiName: "sad",
   282  		},
   283  	}
   284  
   285  	for i, reaction := range userReactions2 {
   286  		userReactions2[i] = Client.MustGeneric(Client.SaveReaction(channel.Id, reaction)).(*model.Reaction)
   287  	}
   288  
   289  	if reactions, err := Client.ListReactions(channel.Id, post.Id); err != nil {
   290  		t.Fatal(err)
   291  	} else if len(reactions) != 5 {
   292  		t.Fatal("should've returned 5 reactions")
   293  	} else {
   294  		checkForReaction := func(expected *model.Reaction) {
   295  			found := false
   296  
   297  			for _, reaction := range reactions {
   298  				if *reaction == *expected {
   299  					found = true
   300  					break
   301  				}
   302  			}
   303  
   304  			if !found {
   305  				t.Fatalf("didn't return expected reaction %v", *expected)
   306  			}
   307  		}
   308  
   309  		for _, reaction := range userReactions {
   310  			checkForReaction(reaction)
   311  		}
   312  
   313  		for _, reaction := range userReactions2 {
   314  			checkForReaction(reaction)
   315  		}
   316  	}
   317  }