aboutsummaryrefslogtreecommitdiff
path: root/matrix/matrix_test.go
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2020-04-16 19:27:35 +0300
committerTulir Asokan <tulir@maunium.net>2020-04-19 15:01:16 +0300
commit815190be147e575f12211c468f8121e5c60e6337 (patch)
tree8abd9b3d3952dbf767ca369ddad400db8a6f2d45 /matrix/matrix_test.go
parentff20c2c44f86b40f9214f1dc3d339584e48374f1 (diff)
Update stuff and move pushrules to mautrix-go
Diffstat (limited to 'matrix/matrix_test.go')
-rw-r--r--matrix/matrix_test.go232
1 files changed, 0 insertions, 232 deletions
diff --git a/matrix/matrix_test.go b/matrix/matrix_test.go
deleted file mode 100644
index 05362b9..0000000
--- a/matrix/matrix_test.go
+++ /dev/null
@@ -1,232 +0,0 @@
-// 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"
- "io/ioutil"
- "net/http"
- "os"
- "strings"
- "testing"
-
- "github.com/stretchr/testify/assert"
-
- "maunium.net/go/gomuks/config"
- "maunium.net/go/mautrix"
-)
-
-func TestContainer_InitClient_Empty(t *testing.T) {
- defer os.RemoveAll("/tmp/gomuks-mxtest-0")
- os.MkdirAll("/tmp/gomuks-mxtest-0", 0700)
- 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"))
-}
-
-/* FIXME probably not applicable anymore
-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
- })}
-
- event := c.PrepareMarkdownMessage("!foo:example.com", "m.text", "test message")
- evtID, err := c.SendEvent(event)
- 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, "<p><strong>formatted</strong> <u>test</u> <em>message</em></p>\n", body["formatted_body"])
- return mockResponse(http.StatusOK, `{"event_id": "!foobar2:example.com"}`), nil
- }), config: &config.Config{UserID: "@user:example.com"}}
-
- event := c.PrepareMarkdownMessage("!foo:example.com", "m.text", "**formatted** <u>test</u> _message_")
- evtID, err := c.SendEvent(event)
- 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)
-}
-
-/* FIXME
-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)
-}