aboutsummaryrefslogtreecommitdiff
path: root/matrix/matrix_test.go
blob: f66c83696c272805506c5cb0db7fae8902744361 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2019 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

package matrix

import (
	"encoding/json"
	"fmt"
	"github.com/stretchr/testify/assert"
	"io/ioutil"
	"maunium.net/go/gomuks/config"
	"maunium.net/go/mautrix"
	"net/http"
	"os"
	"strings"
	"testing"
)

func TestContainer_InitClient_Empty(t *testing.T) {
	defer os.RemoveAll("/tmp/gomuks-mxtest-0")
	cfg := config.NewConfig("/tmp/gomuks-mxtest-0", "/tmp/gomuks-mxtest-0")
	cfg.HS = "https://matrix.org"
	c := Container{config: cfg}
	assert.Nil(t, c.InitClient())
}

func TestContainer_GetCachePath(t *testing.T) {
	defer os.RemoveAll("/tmp/gomuks-mxtest-1")
	cfg := config.NewConfig("/tmp/gomuks-mxtest-1", "/tmp/gomuks-mxtest-1")
	c := Container{config: cfg}
	assert.Equal(t, "/tmp/gomuks-mxtest-1/media/maunium.net/foobar", c.GetCachePath("maunium.net", "foobar"))
}

func TestContainer_SendMarkdownMessage_NoMarkdown(t *testing.T) {
	c := Container{client: mockClient(func(req *http.Request) (*http.Response, error) {
		if req.Method != http.MethodPut || !strings.HasPrefix(req.URL.Path, "/_matrix/client/r0/rooms/!foo:example.com/send/m.room.message/") {
			return nil, fmt.Errorf("unexpected query: %s %s", req.Method, req.URL.Path)
		}

		body := parseBody(req)
		assert.Equal(t, "m.text", body["msgtype"])
		assert.Equal(t, "test message", body["body"])
		return mockResponse(http.StatusOK, `{"event_id": "!foobar1:example.com"}`), nil
	})}

	evtID, err := c.SendMarkdownMessage("!foo:example.com", "m.text", "test message")
	assert.Nil(t, err)
	assert.Equal(t, "!foobar1:example.com", evtID)
}

func TestContainer_SendMarkdownMessage_WithMarkdown(t *testing.T) {
	c := Container{client: mockClient(func(req *http.Request) (*http.Response, error) {
		if req.Method != http.MethodPut || !strings.HasPrefix(req.URL.Path, "/_matrix/client/r0/rooms/!foo:example.com/send/m.room.message/") {
			return nil, fmt.Errorf("unexpected query: %s %s", req.Method, req.URL.Path)
		}

		body := parseBody(req)
		assert.Equal(t, "m.text", body["msgtype"])
		assert.Equal(t, "**formatted** test _message_", body["body"])
		assert.Equal(t, "<strong>formatted</strong> <u>test</u> <em>message</em>", body["formatted_body"])
		return mockResponse(http.StatusOK, `{"event_id": "!foobar2:example.com"}`), nil
	})}

	evtID, err := c.SendMarkdownMessage("!foo:example.com", "m.text", "**formatted** <u>test</u> _message_")
	assert.Nil(t, err)
	assert.Equal(t, "!foobar2:example.com", evtID)
}

func TestContainer_SendTyping(t *testing.T) {
	var calls []mautrix.ReqTyping
	c := Container{client: mockClient(func(req *http.Request) (*http.Response, error) {
		if req.Method != http.MethodPut || req.URL.Path != "/_matrix/client/r0/rooms/!foo:example.com/typing/@user:example.com" {
			return nil, fmt.Errorf("unexpected query: %s %s", req.Method, req.URL.Path)
		}

		rawBody, err := ioutil.ReadAll(req.Body)
		if err != nil {
			return nil, err
		}

		call := mautrix.ReqTyping{}
		err = json.Unmarshal(rawBody, &call)
		if err != nil {
			return nil, err
		}
		calls = append(calls, call)

		return mockResponse(http.StatusOK, `{}`), nil
	})}

	c.SendTyping("!foo:example.com", true)
	c.SendTyping("!foo:example.com", true)
	c.SendTyping("!foo:example.com", true)
	c.SendTyping("!foo:example.com", false)
	c.SendTyping("!foo:example.com", true)
	c.SendTyping("!foo:example.com", false)
	assert.Len(t, calls, 4)
	assert.True(t, calls[0].Typing)
	assert.False(t, calls[1].Typing)
	assert.True(t, calls[2].Typing)
	assert.False(t, calls[3].Typing)
}

func TestContainer_JoinRoom(t *testing.T) {
	defer os.RemoveAll("/tmp/gomuks-mxtest-2")
	cfg := config.NewConfig("/tmp/gomuks-mxtest-2", "/tmp/gomuks-mxtest-2")
	c := Container{client: mockClient(func(req *http.Request) (*http.Response, error) {
		if req.Method == http.MethodPost && req.URL.Path == "/_matrix/client/r0/join/!foo:example.com" {
			return mockResponse(http.StatusOK, `{"room_id": "!foo:example.com"}`), nil
		} else if req.Method == http.MethodPost && req.URL.Path == "/_matrix/client/r0/rooms/!foo:example.com/leave" {
			return mockResponse(http.StatusOK, `{}`), nil
		}
		return nil, fmt.Errorf("unexpected query: %s %s", req.Method, req.URL.Path)
	}), config: cfg}

	room, err := c.JoinRoom("!foo:example.com", "")
	assert.Nil(t, err)
	assert.Equal(t, "!foo:example.com", room.ID)
	assert.False(t, room.HasLeft)

	err = c.LeaveRoom("!foo:example.com")
	assert.Nil(t, err)
	assert.True(t, room.HasLeft)
}

func TestContainer_Download(t *testing.T) {
	defer os.RemoveAll("/tmp/gomuks-mxtest-3")
	cfg := config.NewConfig("/tmp/gomuks-mxtest-3", "/tmp/gomuks-mxtest-3")
	cfg.LoadAll()
	callCounter := 0
	c := Container{client: mockClient(func(req *http.Request) (*http.Response, error) {
		if req.Method != http.MethodGet || req.URL.Path != "/_matrix/media/v1/download/example.com/foobar" {
			return nil, fmt.Errorf("unexpected query: %s %s", req.Method, req.URL.Path)
		}
		callCounter++
		return mockResponse(http.StatusOK, `example file`), nil
	}), config: cfg}

	// Check that download works
	data, hs, id, err := c.Download("mxc://example.com/foobar")
	assert.Equal(t, "example.com", hs)
	assert.Equal(t, "foobar", id)
	assert.Equal(t, 1, callCounter)
	assert.Equal(t, []byte("example file"), data)
	assert.Nil(t, err)

	// Check that cache works
	data, _, _, err = c.Download("mxc://example.com/foobar")
	assert.Nil(t, err)
	assert.Equal(t, []byte("example file"), data)
	assert.Equal(t, 1, callCounter)
}

func TestContainer_Download_InvalidURL(t *testing.T) {
	c := Container{}
	data, hs, id, err := c.Download("mxc://invalid mxc")
	assert.NotNil(t, err)
	assert.Empty(t, id)
	assert.Empty(t, hs)
	assert.Empty(t, data)
}

func TestContainer_GetHistory(t *testing.T) {
	c := Container{client: mockClient(func(req *http.Request) (*http.Response, error) {
		if req.Method != http.MethodGet || req.URL.Path != "/_matrix/client/r0/rooms/!foo:maunium.net/messages" {
			return nil, fmt.Errorf("unexpected query: %s %s", req.Method, req.URL.Path)
		}
		return mockResponse(http.StatusOK, `{"start": "123", "end": "456", "chunk": [{"event_id": "it works"}]}`), nil
	})}

	history, prevBatch, err := c.GetHistory("!foo:maunium.net", "123", 5)
	assert.Nil(t, err)
	assert.Equal(t, "it works", history[0].ID)
	assert.Equal(t, "456", prevBatch)
}

func mockClient(fn func(*http.Request) (*http.Response, error)) *mautrix.Client {
	client, _ := mautrix.NewClient("https://example.com", "@user:example.com", "foobar")
	client.Client = &http.Client{Transport: MockRoundTripper{RT: fn}}
	return client
}

func parseBody(req *http.Request) map[string]interface{} {
	rawBody, err := ioutil.ReadAll(req.Body)
	if err != nil {
		panic(err)
	}

	data := make(map[string]interface{})

	err = json.Unmarshal(rawBody, &data)
	if err != nil {
		panic(err)
	}

	return data
}

func mockResponse(status int, body string) *http.Response {
	return &http.Response{
		StatusCode: status,
		Body:       ioutil.NopCloser(strings.NewReader(body)),
	}
}

type MockRoundTripper struct {
	RT func(*http.Request) (*http.Response, error)
}

func (t MockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
	return t.RT(req)
}