From 104663d1e6216b34c49f845a1111ac00dd167e3b Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Mon, 17 Apr 2017 00:03:05 +0900 Subject: [PATCH 1/4] Fix doAPI error handling --- mastodon.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/mastodon.go b/mastodon.go index e3fbb88..b3d59de 100644 --- a/mastodon.go +++ b/mastodon.go @@ -44,12 +44,11 @@ func (c *Client) doAPI(method string, uri string, params url.Values, res interfa return err } defer resp.Body.Close() - if res == nil { - return nil - } - if method == http.MethodGet && resp.StatusCode != http.StatusOK { + if resp.StatusCode != http.StatusOK { return fmt.Errorf("bad request: %v", resp.Status) + } else if res == nil { + return nil } return json.NewDecoder(resp.Body).Decode(&res) From f5194b9ebb24366078b46301b53c00d0a73ef20e Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Mon, 17 Apr 2017 00:04:25 +0900 Subject: [PATCH 2/4] Add FollowRequestAuthorize and FollowRequestReject --- accounts.go | 10 ++++++++++ accounts_test.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/accounts.go b/accounts.go index 0dd7fa3..c7967a0 100644 --- a/accounts.go +++ b/accounts.go @@ -241,3 +241,13 @@ func (c *Client) GetFollowRequests() ([]*Account, error) { } return accounts, nil } + +// FollowRequestAuthorize is authorize the follow request of user with id. +func (c *Client) FollowRequestAuthorize(id int64) error { + return c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/follow_requests/%d/authorize", id), nil, nil) +} + +// FollowRequestReject is rejects the follow request of user with id. +func (c *Client) FollowRequestReject(id int64) error { + return c.doAPI(http.MethodPost, fmt.Sprintf("/api/v1/follow_requests/%d/reject", id), nil, nil) +} diff --git a/accounts_test.go b/accounts_test.go index e7bc167..3bc55b2 100644 --- a/accounts_test.go +++ b/accounts_test.go @@ -155,3 +155,51 @@ func TestGetFollowRequests(t *testing.T) { t.Fatalf("want %q but %q", "bar", fReqs[0].Username) } } + +func TestFollowRequestAuthorize(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/v1/follow_requests/1234567/authorize" { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + } + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + err := client.FollowRequestAuthorize(123) + if err == nil { + t.Fatalf("should be fail: %v", err) + } + err = client.FollowRequestAuthorize(1234567) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } +} + +func TestFollowRequestReject(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/api/v1/follow_requests/1234567/reject" { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + } + })) + defer ts.Close() + + client := NewClient(&Config{ + Server: ts.URL, + ClientID: "foo", + ClientSecret: "bar", + AccessToken: "zoo", + }) + err := client.FollowRequestReject(123) + if err == nil { + t.Fatalf("should be fail: %v", err) + } + err = client.FollowRequestReject(1234567) + if err != nil { + t.Fatalf("should not be fail: %v", err) + } +} From c39d5765d43ddee6454163c417a6d4851ae84d5f Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Mon, 17 Apr 2017 00:07:31 +0900 Subject: [PATCH 3/4] Fix cover error to TestGetFollowRequests --- accounts_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/accounts_test.go b/accounts_test.go index 3bc55b2..b891225 100644 --- a/accounts_test.go +++ b/accounts_test.go @@ -129,7 +129,13 @@ func TestAccountUnfollow(t *testing.T) { } func TestGetFollowRequests(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 })) @@ -141,6 +147,10 @@ func TestGetFollowRequests(t *testing.T) { ClientSecret: "bar", AccessToken: "zoo", }) + _, err := client.GetFollowRequests() + if err == nil { + t.Fatalf("should be fail: %v", err) + } fReqs, err := client.GetFollowRequests() if err != nil { t.Fatalf("should not be fail: %v", err) From 679457b6ddad4f41bcf4a4f31bc89fbe6d9ae52b Mon Sep 17 00:00:00 2001 From: 178inaba <178inaba@users.noreply.github.com> Date: Mon, 17 Apr 2017 00:12:11 +0900 Subject: [PATCH 4/4] Fix README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 78aee62..d2e618a 100644 --- a/README.md +++ b/README.md @@ -42,8 +42,8 @@ if err != nil { * [x] GET /api/v1/blocks * [x] GET /api/v1/favourites * [x] GET /api/v1/follow_requests -* [ ] POST /api/v1/follow_requests/authorize -* [ ] POST /api/v1/follow_requests/reject +* [ ] POST /api/v1/follow_requests/:id/authorize +* [ ] POST /api/v1/follow_requests/:id/reject * [x] POST /api/v1/follows * [x] GET /api/v1/instance * [ ] POST /api/v1/media