summaryrefslogtreecommitdiffstats
path: root/plugin/rpcplugin/api_test.go
blob: 080f2825f964efb1ad02ac205773f8e5e69c7d2a (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package rpcplugin

import (
	"encoding/json"
	"io"
	"net/http"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/mock"

	"github.com/mattermost/mattermost-server/model"
	"github.com/mattermost/mattermost-server/plugin"
	"github.com/mattermost/mattermost-server/plugin/plugintest"
)

func testAPIRPC(api plugin.API, f func(plugin.API)) {
	r1, w1 := io.Pipe()
	r2, w2 := io.Pipe()

	c1 := NewMuxer(NewReadWriteCloser(r1, w2), false)
	defer c1.Close()

	c2 := NewMuxer(NewReadWriteCloser(r2, w1), true)
	defer c2.Close()

	id, server := c1.Serve()
	go ServeAPI(api, server, c1)

	remote := ConnectAPI(c2.Connect(id), c2)
	defer remote.Close()

	f(remote)
}

func TestAPI(t *testing.T) {
	var api plugintest.API
	defer api.AssertExpectations(t)

	type Config struct {
		Foo string
		Bar struct {
			Baz string
		}
	}

	api.On("LoadPluginConfiguration", mock.MatchedBy(func(x interface{}) bool { return true })).Run(func(args mock.Arguments) {
		dest := args.Get(0).(interface{})
		json.Unmarshal([]byte(`{"Foo": "foo", "Bar": {"Baz": "baz"}}`), dest)
	}).Return(nil)

	testChannel := &model.Channel{
		Id: "thechannelid",
	}

	testTeam := &model.Team{
		Id: "theteamid",
	}
	teamNotFoundError := model.NewAppError("SqlTeamStore.GetByName", "store.sql_team.get_by_name.app_error", nil, "name=notateam", http.StatusNotFound)

	testUser := &model.User{
		Id: "theuserid",
	}

	testPost := &model.Post{
		Message: "hello",
	}

	testAPIRPC(&api, func(remote plugin.API) {
		var config Config
		assert.NoError(t, remote.LoadPluginConfiguration(&config))
		assert.Equal(t, "foo", config.Foo)
		assert.Equal(t, "baz", config.Bar.Baz)

		api.On("CreateChannel", mock.AnythingOfType("*model.Channel")).Return(func(c *model.Channel) (*model.Channel, *model.AppError) {
			c.Id = "thechannelid"
			return c, nil
		}).Once()
		channel, err := remote.CreateChannel(testChannel)
		assert.Equal(t, "thechannelid", channel.Id)
		assert.Nil(t, err)

		api.On("DeleteChannel", "thechannelid").Return(nil).Once()
		assert.Nil(t, remote.DeleteChannel("thechannelid"))

		api.On("GetChannel", "thechannelid").Return(testChannel, nil).Once()
		channel, err = remote.GetChannel("thechannelid")
		assert.Equal(t, testChannel, channel)
		assert.Nil(t, err)

		api.On("GetChannelByName", "foo", "theteamid").Return(testChannel, nil).Once()
		channel, err = remote.GetChannelByName("foo", "theteamid")
		assert.Equal(t, testChannel, channel)
		assert.Nil(t, err)

		api.On("GetDirectChannel", "user1", "user2").Return(testChannel, nil).Once()
		channel, err = remote.GetDirectChannel("user1", "user2")
		assert.Equal(t, testChannel, channel)
		assert.Nil(t, err)

		api.On("GetGroupChannel", []string{"user1", "user2", "user3"}).Return(testChannel, nil).Once()
		channel, err = remote.GetGroupChannel([]string{"user1", "user2", "user3"})
		assert.Equal(t, testChannel, channel)
		assert.Nil(t, err)

		api.On("UpdateChannel", mock.AnythingOfType("*model.Channel")).Return(func(c *model.Channel) (*model.Channel, *model.AppError) {
			return c, nil
		}).Once()
		channel, err = remote.UpdateChannel(testChannel)
		assert.Equal(t, testChannel, channel)
		assert.Nil(t, err)

		api.On("CreateUser", mock.AnythingOfType("*model.User")).Return(func(u *model.User) (*model.User, *model.AppError) {
			u.Id = "theuserid"
			return u, nil
		}).Once()
		user, err := remote.CreateUser(testUser)
		assert.Equal(t, "theuserid", user.Id)
		assert.Nil(t, err)

		api.On("DeleteUser", "theuserid").Return(nil).Once()
		assert.Nil(t, remote.DeleteUser("theuserid"))

		api.On("GetUser", "theuserid").Return(testUser, nil).Once()
		user, err = remote.GetUser("theuserid")
		assert.Equal(t, testUser, user)
		assert.Nil(t, err)

		api.On("GetUserByEmail", "foo@foo").Return(testUser, nil).Once()
		user, err = remote.GetUserByEmail("foo@foo")
		assert.Equal(t, testUser, user)
		assert.Nil(t, err)

		api.On("GetUserByUsername", "foo").Return(testUser, nil).Once()
		user, err = remote.GetUserByUsername("foo")
		assert.Equal(t, testUser, user)
		assert.Nil(t, err)

		api.On("UpdateUser", mock.AnythingOfType("*model.User")).Return(func(u *model.User) (*model.User, *model.AppError) {
			return u, nil
		}).Once()
		user, err = remote.UpdateUser(testUser)
		assert.Equal(t, testUser, user)
		assert.Nil(t, err)

		api.On("CreateTeam", mock.AnythingOfType("*model.Team")).Return(func(t *model.Team) (*model.Team, *model.AppError) {
			t.Id = "theteamid"
			return t, nil
		}).Once()
		team, err := remote.CreateTeam(testTeam)
		assert.Equal(t, "theteamid", team.Id)
		assert.Nil(t, err)

		api.On("DeleteTeam", "theteamid").Return(nil).Once()
		assert.Nil(t, remote.DeleteTeam("theteamid"))

		api.On("GetTeam", "theteamid").Return(testTeam, nil).Once()
		team, err = remote.GetTeam("theteamid")
		assert.Equal(t, testTeam, team)
		assert.Nil(t, err)

		api.On("GetTeamByName", "foo").Return(testTeam, nil).Once()
		team, err = remote.GetTeamByName("foo")
		assert.Equal(t, testTeam, team)
		assert.Nil(t, err)

		api.On("GetTeamByName", "notateam").Return(nil, teamNotFoundError).Once()
		team, err = remote.GetTeamByName("notateam")
		assert.Nil(t, team)
		assert.Equal(t, teamNotFoundError, err)

		api.On("UpdateTeam", mock.AnythingOfType("*model.Team")).Return(func(t *model.Team) (*model.Team, *model.AppError) {
			return t, nil
		}).Once()
		team, err = remote.UpdateTeam(testTeam)
		assert.Equal(t, testTeam, team)
		assert.Nil(t, err)

		api.On("CreatePost", mock.AnythingOfType("*model.Post")).Return(func(p *model.Post) (*model.Post, *model.AppError) {
			p.Id = "thepostid"
			return p, nil
		}).Once()
		post, err := remote.CreatePost(testPost)
		assert.NotEmpty(t, post.Id)
		assert.Equal(t, testPost.Message, post.Message)
		assert.Nil(t, err)

		api.On("DeletePost", "thepostid").Return(nil).Once()
		assert.Nil(t, remote.DeletePost("thepostid"))

		api.On("GetPost", "thepostid").Return(testPost, nil).Once()
		post, err = remote.GetPost("thepostid")
		assert.Equal(t, testPost, post)
		assert.Nil(t, err)

		api.On("UpdatePost", mock.AnythingOfType("*model.Post")).Return(func(p *model.Post) (*model.Post, *model.AppError) {
			return p, nil
		}).Once()
		post, err = remote.UpdatePost(testPost)
		assert.Equal(t, testPost, post)
		assert.Nil(t, err)
	})
}