From e725c814500729bbc5c998e827de744997ed2236 Mon Sep 17 00:00:00 2001
From: Christian Muehlhaeuser <muesli@gmail.com>
Date: Sun, 12 May 2019 16:32:17 +0200
Subject: [PATCH] Added tests for list API calls

---
 lists_test.go     | 289 ++++++++++++++++++++++++++++++++++++++++++++++
 status_test.go    |  38 +++++-
 streaming_test.go |  40 +++++++
 3 files changed, 366 insertions(+), 1 deletion(-)
 create mode 100644 lists_test.go

diff --git a/lists_test.go b/lists_test.go
new file mode 100644
index 0000000..ec98cc7
--- /dev/null
+++ b/lists_test.go
@@ -0,0 +1,289 @@
+package mastodon
+
+import (
+	"context"
+	"fmt"
+	"net/http"
+	"net/http/httptest"
+	"testing"
+)
+
+func TestGetLists(t *testing.T) {
+	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		if r.URL.Path != "/api/v1/lists" {
+			http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
+			return
+		}
+		fmt.Fprintln(w, `[{"id": "1", "title": "foo"}, {"id": "2", "title": "bar"}]`)
+		return
+	}))
+	defer ts.Close()
+
+	client := NewClient(&Config{
+		Server:       ts.URL,
+		ClientID:     "foo",
+		ClientSecret: "bar",
+		AccessToken:  "zoo",
+	})
+	lists, err := client.GetLists(context.Background())
+	if err != nil {
+		t.Fatalf("should not be fail: %v", err)
+	}
+	if len(lists) != 2 {
+		t.Fatalf("result should be two: %d", len(lists))
+	}
+	if lists[0].Title != "foo" {
+		t.Fatalf("want %q but %q", "foo", lists[0].Title)
+	}
+	if lists[1].Title != "bar" {
+		t.Fatalf("want %q but %q", "bar", lists[1].Title)
+	}
+}
+
+func TestGetAccountLists(t *testing.T) {
+	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		if r.URL.Path != "/api/v1/accounts/1/lists" {
+			http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
+			return
+		}
+		fmt.Fprintln(w, `[{"id": "1", "title": "foo"}, {"id": "2", "title": "bar"}]`)
+		return
+	}))
+	defer ts.Close()
+
+	client := NewClient(&Config{
+		Server:       ts.URL,
+		ClientID:     "foo",
+		ClientSecret: "bar",
+		AccessToken:  "zoo",
+	})
+	lists, err := client.GetAccountLists(context.Background(), "2")
+	if err == nil {
+		t.Fatalf("should be fail: %v", err)
+	}
+	lists, err = client.GetAccountLists(context.Background(), "1")
+	if err != nil {
+		t.Fatalf("should not be fail: %v", err)
+	}
+	if len(lists) != 2 {
+		t.Fatalf("result should be two: %d", len(lists))
+	}
+	if lists[0].Title != "foo" {
+		t.Fatalf("want %q but %q", "foo", lists[0].Title)
+	}
+	if lists[1].Title != "bar" {
+		t.Fatalf("want %q but %q", "bar", lists[1].Title)
+	}
+}
+
+func TestGetListAccounts(t *testing.T) {
+	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		if r.URL.Path != "/api/v1/lists/1/accounts" {
+			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",
+	})
+	accounts, err := client.GetListAccounts(context.Background(), "2")
+	if err == nil {
+		t.Fatalf("should be fail: %v", err)
+	}
+	accounts, err = client.GetListAccounts(context.Background(), "1")
+	if err != nil {
+		t.Fatalf("should not be fail: %v", err)
+	}
+	if len(accounts) != 2 {
+		t.Fatalf("result should be two: %d", len(accounts))
+	}
+	if accounts[0].Username != "foo" {
+		t.Fatalf("want %q but %q", "foo", accounts[0].Username)
+	}
+	if accounts[1].Username != "bar" {
+		t.Fatalf("want %q but %q", "bar", accounts[1].Username)
+	}
+}
+
+func TestGetList(t *testing.T) {
+	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		if r.URL.Path != "/api/v1/lists/1" {
+			http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
+			return
+		}
+		fmt.Fprintln(w, `{"id": "1", "title": "foo"}`)
+		return
+	}))
+	defer ts.Close()
+
+	client := NewClient(&Config{
+		Server:       ts.URL,
+		ClientID:     "foo",
+		ClientSecret: "bar",
+		AccessToken:  "zoo",
+	})
+	list, err := client.GetList(context.Background(), "2")
+	if err == nil {
+		t.Fatalf("should be fail: %v", err)
+	}
+	list, err = client.GetList(context.Background(), "1")
+	if err != nil {
+		t.Fatalf("should not be fail: %v", err)
+	}
+	if list.Title != "foo" {
+		t.Fatalf("want %q but %q", "foo", list.Title)
+	}
+}
+
+func TestCreateList(t *testing.T) {
+	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		if r.PostFormValue("title") != "foo" {
+			http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
+			return
+		}
+		fmt.Fprintln(w, `{"id": "1", "title": "foo"}`)
+		return
+	}))
+	defer ts.Close()
+
+	client := NewClient(&Config{
+		Server:       ts.URL,
+		ClientID:     "foo",
+		ClientSecret: "bar",
+		AccessToken:  "zoo",
+	})
+	list, err := client.CreateList(context.Background(), "")
+	if err == nil {
+		t.Fatalf("should be fail: %v", err)
+	}
+	list, err = client.CreateList(context.Background(), "foo")
+	if err != nil {
+		t.Fatalf("should not be fail: %v", err)
+	}
+	if list.Title != "foo" {
+		t.Fatalf("want %q but %q", "foo", list.Title)
+	}
+}
+
+func TestRenameList(t *testing.T) {
+	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		if r.URL.Path != "/api/v1/lists/1" {
+			http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
+			return
+		}
+		if r.PostFormValue("title") != "bar" {
+			http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
+			return
+		}
+		fmt.Fprintln(w, `{"id": "1", "title": "bar"}`)
+		return
+	}))
+	defer ts.Close()
+
+	client := NewClient(&Config{
+		Server:       ts.URL,
+		ClientID:     "foo",
+		ClientSecret: "bar",
+		AccessToken:  "zoo",
+	})
+	list, err := client.RenameList(context.Background(), "2", "bar")
+	if err == nil {
+		t.Fatalf("should be fail: %v", err)
+	}
+	list, err = client.RenameList(context.Background(), "1", "bar")
+	if err != nil {
+		t.Fatalf("should not be fail: %v", err)
+	}
+	if list.Title != "bar" {
+		t.Fatalf("want %q but %q", "bar", list.Title)
+	}
+}
+
+func TestDeleteList(t *testing.T) {
+	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		if r.URL.Path != "/api/v1/lists/1" {
+			http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
+			return
+		}
+		if r.Method != "DELETE" {
+			http.Error(w, http.StatusText(http.StatusNotFound), http.StatusMethodNotAllowed)
+			return
+		}
+		return
+	}))
+	defer ts.Close()
+
+	client := NewClient(&Config{
+		Server:       ts.URL,
+		ClientID:     "foo",
+		ClientSecret: "bar",
+		AccessToken:  "zoo",
+	})
+	err := client.DeleteList(context.Background(), "2")
+	if err == nil {
+		t.Fatalf("should be fail: %v", err)
+	}
+	err = client.DeleteList(context.Background(), "1")
+	if err != nil {
+		t.Fatalf("should not be fail: %v", err)
+	}
+}
+
+func TestAddToList(t *testing.T) {
+	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		if r.URL.Path != "/api/v1/lists/1/accounts" {
+			http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
+			return
+		}
+		if r.PostFormValue("account_ids") != "1" {
+			http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
+			return
+		}
+		return
+	}))
+	defer ts.Close()
+
+	client := NewClient(&Config{
+		Server:       ts.URL,
+		ClientID:     "foo",
+		ClientSecret: "bar",
+		AccessToken:  "zoo",
+	})
+	err := client.AddToList(context.Background(), "1", "1")
+	if err != nil {
+		t.Fatalf("should not be fail: %v", err)
+	}
+}
+
+func TestRemoveFromList(t *testing.T) {
+	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		if r.URL.Path != "/api/v1/lists/1/accounts" {
+			http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
+			return
+		}
+		if r.Method != "DELETE" {
+			http.Error(w, http.StatusText(http.StatusNotFound), http.StatusMethodNotAllowed)
+			return
+		}
+		return
+	}))
+	defer ts.Close()
+
+	client := NewClient(&Config{
+		Server:       ts.URL,
+		ClientID:     "foo",
+		ClientSecret: "bar",
+		AccessToken:  "zoo",
+	})
+	err := client.RemoveFromList(context.Background(), "1", "1")
+	if err != nil {
+		t.Fatalf("should not be fail: %v", err)
+	}
+}
diff --git a/status_test.go b/status_test.go
index 1da1bf1..9fca529 100644
--- a/status_test.go
+++ b/status_test.go
@@ -5,8 +5,8 @@ import (
 	"fmt"
 	"net/http"
 	"net/http/httptest"
-	"testing"
 	"os"
+	"testing"
 )
 
 func TestGetFavourites(t *testing.T) {
@@ -406,6 +406,42 @@ func TestGetTimelineHashtag(t *testing.T) {
 	}
 }
 
+func TestGetTimelineList(t *testing.T) {
+	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		if r.URL.Path != "/api/v1/timelines/list/1" {
+			http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
+			return
+		}
+		fmt.Fprintln(w, `[{"content": "zzz"},{"content": "yyy"}]`)
+		return
+	}))
+	defer ts.Close()
+
+	client := NewClient(&Config{
+		Server:       ts.URL,
+		ClientID:     "foo",
+		ClientSecret: "bar",
+		AccessToken:  "zoo",
+	})
+	_, err := client.GetTimelineList(context.Background(), "notfound", nil)
+	if err == nil {
+		t.Fatalf("should be fail: %v", err)
+	}
+	tags, err := client.GetTimelineList(context.Background(), "1", nil)
+	if err != nil {
+		t.Fatalf("should not be fail: %v", err)
+	}
+	if len(tags) != 2 {
+		t.Fatalf("should have %q entries but %q", "2", len(tags))
+	}
+	if tags[0].Content != "zzz" {
+		t.Fatalf("want %q but %q", "zzz", tags[0].Content)
+	}
+	if tags[1].Content != "yyy" {
+		t.Fatalf("want %q but %q", "zzz", tags[1].Content)
+	}
+}
+
 func TestGetTimelineMedia(t *testing.T) {
 	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 		if r.URL.Query().Get("local") == "" {
diff --git a/streaming_test.go b/streaming_test.go
index 686ae40..bc0e867 100644
--- a/streaming_test.go
+++ b/streaming_test.go
@@ -293,3 +293,43 @@ data: {"content": "foo"}
 		t.Fatalf("want %q but %q", "foo", events[0].(*UpdateEvent).Status.Content)
 	}
 }
+
+func TestStreamingList(t *testing.T) {
+	var isEnd bool
+	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		if isEnd {
+			return
+		} else if r.URL.Path != "/api/v1/streaming/list" {
+			http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
+			return
+		}
+		f, _ := w.(http.Flusher)
+		fmt.Fprintln(w, `
+event: update
+data: {"content": "foo"}
+		`)
+		f.Flush()
+		isEnd = true
+	}))
+	defer ts.Close()
+
+	client := NewClient(&Config{Server: ts.URL})
+	ctx, cancel := context.WithCancel(context.Background())
+	time.AfterFunc(time.Second, cancel)
+	q, err := client.StreamingList(ctx, "1")
+	if err != nil {
+		t.Fatalf("should not be fail: %v", err)
+	}
+	events := []Event{}
+	for e := range q {
+		if _, ok := e.(*ErrorEvent); !ok {
+			events = append(events, e)
+		}
+	}
+	if len(events) != 1 {
+		t.Fatalf("result should be one: %d", len(events))
+	}
+	if events[0].(*UpdateEvent).Status.Content != "foo" {
+		t.Fatalf("want %q but %q", "foo", events[0].(*UpdateEvent).Status.Content)
+	}
+}