From 13dd483b47f430ff1ba1d761eef93378e94a94bd Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Sat, 15 Apr 2017 00:26:13 +0900 Subject: [PATCH] Add ID to Relationship, add follow unfollow test --- accounts.go | 11 +++---- accounts_test.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 accounts_test.go diff --git a/accounts.go b/accounts.go index a751642..0a78ea2 100644 --- a/accounts.go +++ b/accounts.go @@ -67,11 +67,12 @@ func (c *Client) GetAccountFollowing(id int64) ([]*Account, error) { // Relationship hold information for relation-ship to the account. type Relationship struct { - Following bool `json:"following"` - FollowedBy bool `json:"followed_by"` - Blocking bool `json:"blocking"` - Muting bool `json:"muting"` - Requested bool `json:"requested"` + ID int64 `json:"id"` + Following bool `json:"following"` + FollowedBy bool `json:"followed_by"` + Blocking bool `json:"blocking"` + Muting bool `json:"muting"` + Requested bool `json:"requested"` } // AccountFollow follow the account. diff --git a/accounts_test.go b/accounts_test.go new file mode 100644 index 0000000..4ee715b --- /dev/null +++ b/accounts_test.go @@ -0,0 +1,74 @@ +package mastodon + +import ( + "fmt" + "net/http" + "net/http/httptest" + "testing" +) + +func TestAccountFollow(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/v1/accounts/1234567/follow" { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } + fmt.Fprintln(w, `{"id":1234567,"following":true}`) + return + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + rel, err := client.AccountFollow(123) + if err == nil { + t.Fatalf("should be fail: %v", err) + } + rel, err = client.AccountFollow(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.Following { + t.Fatalf("want %t but %t", true, rel.Following) + } +} + +func TestAccountUnfollow(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/v1/accounts/1234567/unfollow" { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } + fmt.Fprintln(w, `{"id":1234567,"following":false}`) + return + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + rel, err := client.AccountUnfollow(123) + if err == nil { + t.Fatalf("should be fail: %v", err) + } + rel, err = client.AccountUnfollow(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.Following { + t.Fatalf("want %t but %t", false, rel.Following) + } +}