Add tests for JoinRoom, LeaveRoom and Download

This commit is contained in:
Tulir Asokan 2018-05-10 20:31:11 +03:00
parent 1e6b599649
commit 706f4c4404
2 changed files with 58 additions and 10 deletions

View File

@ -439,10 +439,6 @@ func (c *Container) SendTyping(roomID string, typing bool) {
// JoinRoom makes the current user try to join the given room. // JoinRoom makes the current user try to join the given room.
func (c *Container) JoinRoom(roomID, server string) (*rooms.Room, error) { 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) resp, err := c.client.JoinRoom(roomID, server, nil)
if err != nil { if err != nil {
return nil, err 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. // LeaveRoom makes the current user leave the given room.
func (c *Container) LeaveRoom(roomID string) error { func (c *Container) LeaveRoom(roomID string) error {
if len(roomID) == 0 {
return fmt.Errorf("invalid room ID")
}
_, err := c.client.LeaveRoom(roomID) _, err := c.client.LeaveRoom(roomID)
if err != nil { if err != nil {
return err return err
@ -500,7 +492,8 @@ func (c *Container) Download(mxcURL string) (data []byte, hs, id string, err err
id = parts[2] id = parts[2]
cacheFile := c.GetCachePath(hs, id) 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) data, err = ioutil.ReadFile(cacheFile)
if err == nil { if err == nil {
return return

View File

@ -26,9 +26,11 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"encoding/json" "encoding/json"
"os"
) )
func TestContainer_InitClient_Empty(t *testing.T) { 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 := config.NewConfig("/tmp/gomuks-mxtest-0", "/tmp/gomuks-mxtest-0")
cfg.HS = "https://matrix.org" cfg.HS = "https://matrix.org"
c := Container{config: cfg} c := Container{config: cfg}
@ -36,6 +38,7 @@ func TestContainer_InitClient_Empty(t *testing.T) {
} }
func TestContainer_GetCachePath(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") cfg := config.NewConfig("/tmp/gomuks-mxtest-1", "/tmp/gomuks-mxtest-1")
c := Container{config: cfg} c := Container{config: cfg}
assert.Equal(t, "/tmp/gomuks-mxtest-1/media/maunium.net/foobar", c.GetCachePath("maunium.net", "foobar")) 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) { func TestContainer_SendTyping(t *testing.T) {
var calls []gomatrix.ReqTyping var calls []gomatrix.ReqTyping
c := Container{client: mockClient(func(req *http.Request) (*http.Response, error) { 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) 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) 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 { func mockClient(fn func(*http.Request) (*http.Response, error)) *gomatrix.Client {
client, _ := gomatrix.NewClient("https://example.com", "@user:example.com", "foobar") client, _ := gomatrix.NewClient("https://example.com", "@user:example.com", "foobar")
client.Client = &http.Client{Transport: MockRoundTripper{RT: fn}} client.Client = &http.Client{Transport: MockRoundTripper{RT: fn}}