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] 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" {