summaryrefslogtreecommitdiffstats
path: root/api4/reaction_test.go
blob: 457b905daa07b46d90c7dc9f13214dc836b78d6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package api4

import (
	"testing"

	"reflect"

	"github.com/mattermost/platform/app"
	"github.com/mattermost/platform/model"
)

func TestGetReactions(t *testing.T) {
	th := Setup().InitBasic().InitSystemAdmin()
	defer TearDown()
	Client := th.Client
	userId := th.BasicUser.Id
	user2Id := th.BasicUser2.Id
	postId := th.BasicPost.Id

	userReactions := []*model.Reaction{
		{
			UserId:    userId,
			PostId:    postId,
			EmojiName: "smile",
		},
		{
			UserId:    userId,
			PostId:    postId,
			EmojiName: "happy",
		},
		{
			UserId:    userId,
			PostId:    postId,
			EmojiName: "sad",
		},
		{
			UserId:    user2Id,
			PostId:    postId,
			EmojiName: "smile",
		},
		{
			UserId:    user2Id,
			PostId:    postId,
			EmojiName: "sad",
		},
	}

	var reactions []*model.Reaction

	for _, userReaction := range userReactions {
		if result := <-app.Srv.Store.Reaction().Save(userReaction); result.Err != nil {
			t.Fatal(result.Err)
		} else {
			reactions = append(reactions, result.Data.(*model.Reaction))
		}
	}

	rr, resp := Client.GetReactions(postId)
	CheckNoError(t, resp)

	if len(rr) != 5 {
		t.Fatal("reactions should returned correct length")
	}

	if !reflect.DeepEqual(rr, reactions) {
		t.Fatal("reactions should have matched")
	}

	rr, resp = Client.GetReactions("junk")
	CheckBadRequestStatus(t, resp)

	if len(rr) != 0 {
		t.Fatal("reactions should return empty")
	}

	_, resp = Client.GetReactions(GenerateTestId())
	CheckForbiddenStatus(t, resp)

	Client.Logout()

	_, resp = Client.GetReactions(postId)
	CheckUnauthorizedStatus(t, resp)

	_, resp = th.SystemAdminClient.GetReactions(postId)
	CheckNoError(t, resp)
}