Added tests for list API calls

pull/94/head
Christian Muehlhaeuser 2019-05-12 16:32:17 +02:00 committed by mattn
parent 9427a55316
commit e725c81450
3 changed files with 366 additions and 1 deletions

289
lists_test.go 100644
View File

@ -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)
}
}

View File

@ -5,8 +5,8 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing"
"os" "os"
"testing"
) )
func TestGetFavourites(t *testing.T) { 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) { func TestGetTimelineMedia(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("local") == "" { if r.URL.Query().Get("local") == "" {

View File

@ -293,3 +293,43 @@ data: {"content": "foo"}
t.Fatalf("want %q but %q", "foo", events[0].(*UpdateEvent).Status.Content) 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)
}
}