aboutsummaryrefslogtreecommitdiff
path: root/matrix/matrix_test.go
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2018-05-10 20:03:19 +0300
committerTulir Asokan <tulir@maunium.net>2018-05-10 20:03:19 +0300
commit248db71c76988188f4021621837a8d024efa1d73 (patch)
tree966efc6120b63af02d4f49b72bca740e487cf622 /matrix/matrix_test.go
parent151141f288e51e427168cee1340a7163bf58309a (diff)
Add tests for sending stuff to Matrix
Diffstat (limited to 'matrix/matrix_test.go')
-rw-r--r--matrix/matrix_test.go119
1 files changed, 113 insertions, 6 deletions
diff --git a/matrix/matrix_test.go b/matrix/matrix_test.go
index 35debe1..8dc71e3 100644
--- a/matrix/matrix_test.go
+++ b/matrix/matrix_test.go
@@ -20,6 +20,12 @@ import (
"testing"
"maunium.net/go/gomuks/config"
"github.com/stretchr/testify/assert"
+ "net/http"
+ "maunium.net/go/gomatrix"
+ "strings"
+ "fmt"
+ "io/ioutil"
+ "encoding/json"
)
func TestContainer_InitClient_Empty(t *testing.T) {
@@ -29,14 +35,115 @@ func TestContainer_InitClient_Empty(t *testing.T) {
assert.Nil(t, c.InitClient())
}
-func TestContainer_renderMarkdown(t *testing.T) {
- text := "**foo** _bar_"
- c := Container{}
- assert.Equal(t, "<strong>foo</strong> <em>bar</em>", c.renderMarkdown(text))
-}
-
func TestContainer_GetCachePath(t *testing.T) {
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** <u>test</u> _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 []gomatrix.ReqTyping
+ 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/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 := gomatrix.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 mockClient(fn func(*http.Request) (*http.Response, error)) *gomatrix.Client {
+ client, _ := gomatrix.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)
+}