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 1/5] 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 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 2/5] 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 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 3/5] 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 4/5] 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 5/5] 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) {