From d6ee0499f53190bbf8a686a512902417772f31ac Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Mon, 17 Apr 2017 22:39:57 +0900 Subject: [PATCH 01/12] Fix tty.Open() position --- cmd/mstdn/main.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/cmd/mstdn/main.go b/cmd/mstdn/main.go index 2bd18fd..cb11270 100644 --- a/cmd/mstdn/main.go +++ b/cmd/mstdn/main.go @@ -66,12 +66,6 @@ var ( ) func prompt() (string, string, error) { - t, err := tty.Open() - if err != nil { - return "", "", err - } - defer t.Close() - fmt.Print("E-Mail: ") email, err := readUsername() if err != nil { @@ -81,6 +75,11 @@ func prompt() (string, string, error) { fmt.Print("Password: ") var password string if readPassword == nil { + t, err := tty.Open() + if err != nil { + return "", "", err + } + defer t.Close() password, err = t.ReadPassword() } else { password, err = readPassword() From 39239ae270f7764e0e8b360b5437e2d29248eda7 Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Mon, 17 Apr 2017 23:21:52 +0900 Subject: [PATCH 02/12] Fix accounts test --- accounts_test.go | 107 ++++++++++++++++++++++++++++++++++++++++++++++- mastodon_test.go | 30 ------------- 2 files changed, 106 insertions(+), 31 deletions(-) diff --git a/accounts_test.go b/accounts_test.go index 1b9851c..9f99a8d 100644 --- a/accounts_test.go +++ b/accounts_test.go @@ -8,8 +8,12 @@ import ( "testing" ) -func TestAccountUpdate(t *testing.T) { +func TestGetAccount(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/v1/accounts/1234567" { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } fmt.Fprintln(w, `{"Username": "zzz"}`) return })) @@ -21,6 +25,74 @@ func TestAccountUpdate(t *testing.T) { ClientSecret: "bar", AccessToken: "zoo", }) + a, err := client.GetAccount(context.Background(), 1) + if err == nil { + t.Fatalf("should not be fail: %v", err) + } + a, err = client.GetAccount(context.Background(), 1234567) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if a.Username != "zzz" { + t.Fatalf("want %q but %q", "zzz", a.Username) + } +} + +func TestGetAccountCurrentUser(t *testing.T) { + canErr := true + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if canErr { + canErr = false + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } + fmt.Fprintln(w, `{"Username": "zzz"}`) + return + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + _, err := client.GetAccountCurrentUser(context.Background()) + if err == nil { + t.Fatalf("should be fail: %v", err) + } + a, err := client.GetAccountCurrentUser(context.Background()) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if a.Username != "zzz" { + t.Fatalf("want %q but %q", "zzz", a.Username) + } +} + +func TestAccountUpdate(t *testing.T) { + canErr := true + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if canErr { + canErr = false + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } + fmt.Fprintln(w, `{"Username": "zzz"}`) + return + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + _, err := client.AccountUpdate(context.Background(), &Profile{}) + if err == nil { + t.Fatalf("should be fail: %v", err) + } a, err := client.AccountUpdate(context.Background(), &Profile{ DisplayName: String("display_name"), Note: String("note"), @@ -35,6 +107,39 @@ func TestAccountUpdate(t *testing.T) { } } +func TestGetAccountStatuses(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/v1/accounts/1234567/statuses" { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } + fmt.Fprintln(w, `[{"Content": "foo"}, {"Content": "bar"}]`) + return + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + _, err := client.GetAccountStatuses(context.Background(), 123) + if err == nil { + t.Fatalf("should be fail: %v", err) + } + ss, err := client.GetAccountStatuses(context.Background(), 1234567) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if ss[0].Content != "foo" { + t.Fatalf("want %q but %q", "foo", ss[0].Content) + } + if ss[1].Content != "bar" { + t.Fatalf("want %q but %q", "bar", ss[1].Content) + } +} + func TestGetBlocks(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, `[{"Username": "foo"}, {"Username": "bar"}]`) diff --git a/mastodon_test.go b/mastodon_test.go index 4e9cee4..abde791 100644 --- a/mastodon_test.go +++ b/mastodon_test.go @@ -126,36 +126,6 @@ func TestForTheCoverages(t *testing.T) { _ = (&ErrorEvent{io.EOF}).Error() } -func TestGetAccount(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != "/api/v1/accounts/1234567" { - http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) - return - } - fmt.Fprintln(w, `{"Username": "zzz"}`) - return - })) - defer ts.Close() - - client := NewClient(&Config{ - Server: ts.URL, - ClientID: "foo", - ClientSecret: "bar", - AccessToken: "zoo", - }) - a, err := client.GetAccount(context.Background(), 1) - if err == nil { - t.Fatalf("should not be fail: %v", err) - } - a, err = client.GetAccount(context.Background(), 1234567) - if err != nil { - t.Fatalf("should not be fail: %v", err) - } - if a.Username != "zzz" { - t.Fatalf("want %q but %q", "zzz", a.Username) - } -} - func TestGetAccountFollowing(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/api/v1/accounts/1234567/following" { From e7f2469bc0501638db2fefee2d44fd48850c0bcf Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Mon, 17 Apr 2017 23:29:44 +0900 Subject: [PATCH 03/12] add stream -json --- cmd/mstdn/cmd_stream.go | 26 ++++++++++++++++---------- cmd/mstdn/main.go | 10 ++++++++-- streaming.go | 4 +++- 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/cmd/mstdn/cmd_stream.go b/cmd/mstdn/cmd_stream.go index 50aba7d..b7122f3 100644 --- a/cmd/mstdn/cmd_stream.go +++ b/cmd/mstdn/cmd_stream.go @@ -2,6 +2,7 @@ package main import ( "context" + "encoding/json" "fmt" "os" "os/signal" @@ -12,6 +13,7 @@ import ( ) func cmdStream(c *cli.Context) error { + asJSON := c.Bool("json") client := c.App.Metadata["client"].(*mastodon.Client) ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -27,16 +29,20 @@ func cmdStream(c *cli.Context) error { close(q) }() for e := range q { - switch t := e.(type) { - case *mastodon.UpdateEvent: - color.Set(color.FgHiRed) - fmt.Fprintln(c.App.Writer, t.Status.Account.Username) - color.Set(color.Reset) - fmt.Fprintln(c.App.Writer, textContent(t.Status.Content)) - case *mastodon.ErrorEvent: - color.Set(color.FgYellow) - fmt.Fprintln(c.App.Writer, t.Error()) - color.Set(color.Reset) + if asJSON { + json.NewEncoder(c.App.Writer).Encode(e) + } else { + switch t := e.(type) { + case *mastodon.UpdateEvent: + color.Set(color.FgHiRed) + fmt.Fprintln(c.App.Writer, t.Status.Account.Username) + color.Set(color.Reset) + fmt.Fprintln(c.App.Writer, textContent(t.Status.Content)) + case *mastodon.ErrorEvent: + color.Set(color.FgYellow) + fmt.Fprintln(c.App.Writer, t.Error()) + color.Set(color.Reset) + } } } return nil diff --git a/cmd/mstdn/main.go b/cmd/mstdn/main.go index cb11270..0e2cb4e 100644 --- a/cmd/mstdn/main.go +++ b/cmd/mstdn/main.go @@ -178,8 +178,14 @@ func makeApp() *cli.App { Action: cmdToot, }, { - Name: "stream", - Usage: "stream statuses", + Name: "stream", + Usage: "stream statuses", + Flags: []cli.Flag{ + cli.BoolFlag{ + Name: "json", + Usage: "output JSON", + }, + }, Action: cmdStream, }, { diff --git a/streaming.go b/streaming.go index ded8aad..41ffe8d 100644 --- a/streaming.go +++ b/streaming.go @@ -14,7 +14,9 @@ import ( ) // UpdateEvent is struct for passing status event to app. -type UpdateEvent struct{ Status *Status } +type UpdateEvent struct { + Status *Status `json:"status"` +} func (e *UpdateEvent) event() {} From 0579b3163363dbc474bae7bbafe6fb74296e2be8 Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Mon, 17 Apr 2017 23:36:21 +0900 Subject: [PATCH 04/12] check response --- streaming.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/streaming.go b/streaming.go index 41ffe8d..6df8148 100644 --- a/streaming.go +++ b/streaming.go @@ -94,7 +94,7 @@ func (c *Client) streaming(ctx context.Context, p string, tag string) (chan Even if err == nil { req.Header.Set("Authorization", "Bearer "+c.config.AccessToken) resp, err = c.Do(req) - if resp.StatusCode != 200 { + if resp != nil && resp.StatusCode != 200 { err = fmt.Errorf("bad request: %v", resp.Status) } } From a752a1bd6b08987a0b0b846cdb4bbd58bce11e8e Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Mon, 17 Apr 2017 23:39:47 +0900 Subject: [PATCH 05/12] Add accounts test --- accounts_test.go | 84 +++++++++++++++++++++++++++++++++++++++++++++++- mastodon_test.go | 36 --------------------- 2 files changed, 83 insertions(+), 37 deletions(-) diff --git a/accounts_test.go b/accounts_test.go index 9f99a8d..0461bae 100644 --- a/accounts_test.go +++ b/accounts_test.go @@ -140,8 +140,12 @@ func TestGetAccountStatuses(t *testing.T) { } } -func TestGetBlocks(t *testing.T) { +func TestGetAccountFollowers(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/v1/accounts/1234567/followers" { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } fmt.Fprintln(w, `[{"Username": "foo"}, {"Username": "bar"}]`) return })) @@ -153,6 +157,84 @@ func TestGetBlocks(t *testing.T) { ClientSecret: "bar", AccessToken: "zoo", }) + _, err := client.GetAccountFollowers(context.Background(), 123) + if err == nil { + t.Fatalf("should be fail: %v", err) + } + fl, err := client.GetAccountFollowers(context.Background(), 1234567) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if len(fl) != 2 { + t.Fatalf("result should be two: %d", len(fl)) + } + if fl[0].Username != "foo" { + t.Fatalf("want %q but %q", "foo", fl[0].Username) + } + if fl[1].Username != "bar" { + t.Fatalf("want %q but %q", "bar", fl[1].Username) + } +} + +func TestGetAccountFollowing(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/v1/accounts/1234567/following" { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } + fmt.Fprintln(w, `[{"Username": "foo"}, {"Username": "bar"}]`) + return + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + fl, err := client.GetAccountFollowing(context.Background(), 123) + if err == nil { + t.Fatalf("should be fail: %v", err) + } + fl, err = client.GetAccountFollowing(context.Background(), 1234567) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if len(fl) != 2 { + t.Fatalf("result should be two: %d", len(fl)) + } + if fl[0].Username != "foo" { + t.Fatalf("want %q but %q", "foo", fl[0].Username) + } + if fl[1].Username != "bar" { + t.Fatalf("want %q but %q", "bar", fl[1].Username) + } +} + +func TestGetBlocks(t *testing.T) { + canErr := true + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if canErr { + canErr = false + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } + fmt.Fprintln(w, `[{"Username": "foo"}, {"Username": "bar"}]`) + return + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + _, err := client.GetBlocks(context.Background()) + if err == nil { + t.Fatalf("should be fail: %v", err) + } bl, err := client.GetBlocks(context.Background()) if err != nil { t.Fatalf("should not be fail: %v", err) diff --git a/mastodon_test.go b/mastodon_test.go index abde791..8005b75 100644 --- a/mastodon_test.go +++ b/mastodon_test.go @@ -125,39 +125,3 @@ func TestForTheCoverages(t *testing.T) { (*ErrorEvent)(nil).event() _ = (&ErrorEvent{io.EOF}).Error() } - -func TestGetAccountFollowing(t *testing.T) { - ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != "/api/v1/accounts/1234567/following" { - http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) - return - } - fmt.Fprintln(w, `[{"Username": "foo"}, {"Username": "bar"}]`) - return - })) - defer ts.Close() - - client := NewClient(&Config{ - Server: ts.URL, - ClientID: "foo", - ClientSecret: "bar", - AccessToken: "zoo", - }) - fl, err := client.GetAccountFollowing(context.Background(), 123) - if err == nil { - t.Fatalf("should not be fail: %v", err) - } - fl, err = client.GetAccountFollowing(context.Background(), 1234567) - if err != nil { - t.Fatalf("should not be fail: %v", err) - } - if len(fl) != 2 { - t.Fatalf("result should be two: %d", len(fl)) - } - if fl[0].Username != "foo" { - t.Fatalf("want %q but %q", "foo", fl[0].Username) - } - if fl[1].Username != "bar" { - t.Fatalf("want %q but %q", "bar", fl[1].Username) - } -} From eb0803d442d49224fde531b61ee4e73bd20df5af Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Mon, 17 Apr 2017 23:43:11 +0900 Subject: [PATCH 06/12] add stream -simplejson --- cmd/mstdn/cmd_stream.go | 15 +++++++++++++++ cmd/mstdn/main.go | 4 ++++ 2 files changed, 19 insertions(+) diff --git a/cmd/mstdn/cmd_stream.go b/cmd/mstdn/cmd_stream.go index b7122f3..1ba9a6c 100644 --- a/cmd/mstdn/cmd_stream.go +++ b/cmd/mstdn/cmd_stream.go @@ -12,8 +12,15 @@ import ( "github.com/urfave/cli" ) +type SimpleJSON struct { + Username string `json:"username"` + Avatar string `json:"avatar"` + Content string `json:"content"` +} + func cmdStream(c *cli.Context) error { asJSON := c.Bool("json") + asSimpleJSON := c.Bool("simplejson") client := c.App.Metadata["client"].(*mastodon.Client) ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -31,6 +38,14 @@ func cmdStream(c *cli.Context) error { for e := range q { if asJSON { json.NewEncoder(c.App.Writer).Encode(e) + } else if asSimpleJSON { + if t, ok := e.(*mastodon.UpdateEvent); ok { + json.NewEncoder(c.App.Writer).Encode(&SimpleJSON{ + Username: t.Status.Account.Username, + Avatar: t.Status.Account.AvatarStatic, + Content: textContent(t.Status.Content), + }) + } } else { switch t := e.(type) { case *mastodon.UpdateEvent: diff --git a/cmd/mstdn/main.go b/cmd/mstdn/main.go index 0e2cb4e..e24586a 100644 --- a/cmd/mstdn/main.go +++ b/cmd/mstdn/main.go @@ -185,6 +185,10 @@ func makeApp() *cli.App { Name: "json", Usage: "output JSON", }, + cli.BoolFlag{ + Name: "simplejson", + Usage: "output simple JSON", + }, }, Action: cmdStream, }, From c56694e78e31fb340a9f15175705de26e9b056f2 Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Mon, 17 Apr 2017 23:52:51 +0900 Subject: [PATCH 07/12] Add TestAccountBlock and TestAccountUnblock --- accounts_test.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/accounts_test.go b/accounts_test.go index 0461bae..2bb9cf2 100644 --- a/accounts_test.go +++ b/accounts_test.go @@ -316,6 +316,72 @@ func TestAccountUnfollow(t *testing.T) { } } +func TestAccountBlock(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/v1/accounts/1234567/block" { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } + fmt.Fprintln(w, `{"id":1234567,"blocking":true}`) + return + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + _, err := client.AccountBlock(context.Background(), 123) + if err == nil { + t.Fatalf("should be fail: %v", err) + } + rel, err := client.AccountBlock(context.Background(), 1234567) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if rel.ID != 1234567 { + t.Fatalf("want %d but %d", 1234567, rel.ID) + } + if !rel.Blocking { + t.Fatalf("want %t but %t", true, rel.Blocking) + } +} + +func TestAccountUnblock(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/v1/accounts/1234567/unblock" { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } + fmt.Fprintln(w, `{"id":1234567,"blocking":false}`) + return + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + _, err := client.AccountUnblock(context.Background(), 123) + if err == nil { + t.Fatalf("should be fail: %v", err) + } + rel, err := client.AccountUnblock(context.Background(), 1234567) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if rel.ID != 1234567 { + t.Fatalf("want %d but %d", 1234567, rel.ID) + } + if rel.Blocking { + t.Fatalf("want %t but %t", false, rel.Blocking) + } +} + func TestGetFollowRequests(t *testing.T) { canErr := true ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { From e37705c104acad0b25744c062af33f3551540e3d Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Tue, 18 Apr 2017 00:00:05 +0900 Subject: [PATCH 08/12] Add TestAccountMute and TestAccountUnmute --- accounts_test.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/accounts_test.go b/accounts_test.go index 2bb9cf2..12fbd9d 100644 --- a/accounts_test.go +++ b/accounts_test.go @@ -382,6 +382,72 @@ func TestAccountUnblock(t *testing.T) { } } +func TestAccountMute(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/v1/accounts/1234567/mute" { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } + fmt.Fprintln(w, `{"id":1234567,"muting":true}`) + return + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + _, err := client.AccountMute(context.Background(), 123) + if err == nil { + t.Fatalf("should be fail: %v", err) + } + rel, err := client.AccountMute(context.Background(), 1234567) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if rel.ID != 1234567 { + t.Fatalf("want %d but %d", 1234567, rel.ID) + } + if !rel.Muting { + t.Fatalf("want %t but %t", true, rel.Muting) + } +} + +func TestAccountUnmute(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/v1/accounts/1234567/unmute" { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } + fmt.Fprintln(w, `{"id":1234567,"muting":false}`) + return + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + _, err := client.AccountUnmute(context.Background(), 123) + if err == nil { + t.Fatalf("should be fail: %v", err) + } + rel, err := client.AccountUnmute(context.Background(), 1234567) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if rel.ID != 1234567 { + t.Fatalf("want %d but %d", 1234567, rel.ID) + } + if rel.Muting { + t.Fatalf("want %t but %t", false, rel.Muting) + } +} + func TestGetFollowRequests(t *testing.T) { canErr := true ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { From 6020167f20256c796661effcd74ba5dce0184eec Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Tue, 18 Apr 2017 00:28:59 +0900 Subject: [PATCH 09/12] Add TestGetAccountRelationship --- accounts_test.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/accounts_test.go b/accounts_test.go index 12fbd9d..73fea5f 100644 --- a/accounts_test.go +++ b/accounts_test.go @@ -448,6 +448,37 @@ func TestAccountUnmute(t *testing.T) { } } +func TestGetAccountRelationship(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Println(r.URL.String()) + if r.URL.Query().Get("id") != "1234567" { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } + fmt.Fprintln(w, `[{"id":1234567}]`) + return + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + _, err := client.GetAccountRelationship(context.Background(), 123) + if err == nil { + t.Fatalf("should be fail: %v", err) + } + rels, err := client.GetAccountRelationship(context.Background(), 1234567) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if rels[0].ID != 1234567 { + t.Fatalf("want %d but %d", 1234567, rels[0].ID) + } +} + func TestGetFollowRequests(t *testing.T) { canErr := true ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { From a7e0c19d35cf405ccd29e7ffeade3d4d4f7b30f1 Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Tue, 18 Apr 2017 01:59:52 +0900 Subject: [PATCH 10/12] Fix get query to request url --- mastodon.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mastodon.go b/mastodon.go index 399eac4..a14c7c8 100644 --- a/mastodon.go +++ b/mastodon.go @@ -39,7 +39,13 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in var req *http.Request ct := "application/x-www-form-urlencoded" if values, ok := params.(url.Values); ok { - req, err = http.NewRequest(method, u.String(), strings.NewReader(values.Encode())) + var body io.Reader + if method == http.MethodGet { + u.RawQuery = values.Encode() + } else { + body = strings.NewReader(values.Encode()) + } + req, err = http.NewRequest(method, u.String(), body) if err != nil { return err } From 6f244235d46c1782be8360084ffef7d9e3e25127 Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Tue, 18 Apr 2017 02:45:40 +0900 Subject: [PATCH 11/12] Fix to be able to specify multiple id for GetAccountRelationships --- accounts.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/accounts.go b/accounts.go index 9e0b17d..a6d1e75 100644 --- a/accounts.go +++ b/accounts.go @@ -194,12 +194,14 @@ func (c *Client) AccountUnmute(ctx context.Context, id int64) (*Relationship, er } // GetAccountRelationship return relationship for the account. -func (c *Client) GetAccountRelationship(ctx context.Context, id int64) ([]*Relationship, error) { +func (c *Client) GetAccountRelationships(ctx context.Context, ids []int64) ([]*Relationship, error) { params := url.Values{} - params.Set("id", fmt.Sprint(id)) + for _, id := range ids { + params.Add("id[]", fmt.Sprint(id)) + } var relationships []*Relationship - err := c.doAPI(ctx, http.MethodGet, "/api/v1/accounts/relationship", params, &relationships) + err := c.doAPI(ctx, http.MethodGet, "/api/v1/accounts/relationships", params, &relationships) if err != nil { return nil, err } From 5d106e8942d233fd0cc0fe90e28e57c755b66190 Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Tue, 18 Apr 2017 02:46:10 +0900 Subject: [PATCH 12/12] Add accounts test --- accounts_test.go | 84 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 76 insertions(+), 8 deletions(-) diff --git a/accounts_test.go b/accounts_test.go index 73fea5f..5605331 100644 --- a/accounts_test.go +++ b/accounts_test.go @@ -450,12 +450,44 @@ func TestAccountUnmute(t *testing.T) { func TestGetAccountRelationship(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - fmt.Println(r.URL.String()) - if r.URL.Query().Get("id") != "1234567" { - http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + ids := r.URL.Query()["id[]"] + if ids[0] == "1234567" && ids[1] == "8901234" { + fmt.Fprintln(w, `[{"id":1234567},{"id":8901234}]`) return } - fmt.Fprintln(w, `[{"id":1234567}]`) + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + _, err := client.GetAccountRelationships(context.Background(), []int64{123, 456}) + if err == nil { + t.Fatalf("should be fail: %v", err) + } + rels, err := client.GetAccountRelationships(context.Background(), []int64{1234567, 8901234}) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if rels[0].ID != 1234567 { + t.Fatalf("want %d but %d", 1234567, rels[0].ID) + } + if rels[1].ID != 8901234 { + t.Fatalf("want %d but %d", 8901234, rels[1].ID) + } +} + +func TestAccountsSearch(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Query()["q"][0] != "foo" { + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } + fmt.Fprintln(w, `[{"Username": "foobar"}, {"Username": "barfoo"}]`) return })) defer ts.Close() @@ -466,16 +498,52 @@ func TestGetAccountRelationship(t *testing.T) { ClientSecret: "bar", AccessToken: "zoo", }) - _, err := client.GetAccountRelationship(context.Background(), 123) + _, err := client.AccountsSearch(context.Background(), "zzz", 2) if err == nil { t.Fatalf("should be fail: %v", err) } - rels, err := client.GetAccountRelationship(context.Background(), 1234567) + res, err := client.AccountsSearch(context.Background(), "foo", 2) if err != nil { t.Fatalf("should not be fail: %v", err) } - if rels[0].ID != 1234567 { - t.Fatalf("want %d but %d", 1234567, rels[0].ID) + if len(res) != 2 { + t.Fatalf("result should be two: %d", len(res)) + } + if res[0].Username != "foobar" { + t.Fatalf("want %q but %q", "foobar", res[0].Username) + } + if res[1].Username != "barfoo" { + t.Fatalf("want %q but %q", "barfoo", res[1].Username) + } +} + +func TestFollowRemoteUser(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.PostFormValue("uri") != "foo@success.social" { + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } + fmt.Fprintln(w, `{"Username": "zzz"}`) + return + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + _, err := client.FollowRemoteUser(context.Background(), "foo@fail.social") + if err == nil { + t.Fatalf("should be fail: %v", err) + } + ru, err := client.FollowRemoteUser(context.Background(), "foo@success.social") + if err != nil { + t.Fatalf("should not be fail: %v", err) + } + if ru.Username != "zzz" { + t.Fatalf("want %q but %q", "zzz", ru.Username) } }