From 706f4c44048e59bf914877f6f8bf17ad9915a65c Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 10 May 2018 20:31:11 +0300 Subject: Add tests for JoinRoom, LeaveRoom and Download --- matrix/matrix.go | 11 ++-------- matrix/matrix_test.go | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 10 deletions(-) diff --git a/matrix/matrix.go b/matrix/matrix.go index de65b60..cdd4ed5 100644 --- a/matrix/matrix.go +++ b/matrix/matrix.go @@ -439,10 +439,6 @@ func (c *Container) SendTyping(roomID string, typing bool) { // JoinRoom makes the current user try to join the given room. func (c *Container) JoinRoom(roomID, server string) (*rooms.Room, error) { - if len(roomID) == 0 { - return nil, fmt.Errorf("invalid room ID") - } - resp, err := c.client.JoinRoom(roomID, server, nil) if err != nil { return nil, err @@ -456,10 +452,6 @@ func (c *Container) JoinRoom(roomID, server string) (*rooms.Room, error) { // LeaveRoom makes the current user leave the given room. func (c *Container) LeaveRoom(roomID string) error { - if len(roomID) == 0 { - return fmt.Errorf("invalid room ID") - } - _, err := c.client.LeaveRoom(roomID) if err != nil { return err @@ -500,7 +492,8 @@ func (c *Container) Download(mxcURL string) (data []byte, hs, id string, err err id = parts[2] cacheFile := c.GetCachePath(hs, id) - if _, err = os.Stat(cacheFile); err != nil { + var info os.FileInfo + if info, err = os.Stat(cacheFile); err == nil && !info.IsDir() { data, err = ioutil.ReadFile(cacheFile) if err == nil { return diff --git a/matrix/matrix_test.go b/matrix/matrix_test.go index 8dc71e3..68ddfc9 100644 --- a/matrix/matrix_test.go +++ b/matrix/matrix_test.go @@ -26,9 +26,11 @@ import ( "fmt" "io/ioutil" "encoding/json" + "os" ) 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} @@ -36,6 +38,7 @@ func TestContainer_InitClient_Empty(t *testing.T) { } 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")) @@ -79,7 +82,7 @@ func TestContainer_SendMarkdownMessage_WithMarkdown(t *testing.T) { 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") { + 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) } @@ -111,6 +114,58 @@ func TestContainer_SendTyping(t *testing.T) { 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") + cfg.LoadSession("@user:example.com") + 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.Load() + cfg.LoadSession("@user:example.com") + 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 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}} -- cgit v1.2.3