go-mastodon/status_test.go

871 lines
25 KiB
Go
Raw Permalink Normal View History

2017-04-14 20:40:06 +02:00
package mastodon
import (
"context"
2017-04-14 20:40:06 +02:00
"fmt"
"io/ioutil"
2017-04-14 20:40:06 +02:00
"net/http"
"net/http/httptest"
2019-03-11 03:55:15 +01:00
"os"
2019-05-12 16:32:17 +02:00
"testing"
2017-04-14 20:40:06 +02:00
)
func TestGetFavourites(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2017-04-20 12:14:24 +02:00
fmt.Fprintln(w, `[{"content": "foo"}, {"content": "bar"}]`)
2017-04-14 20:40:06 +02:00
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
2017-05-06 16:03:19 +02:00
favs, err := client.GetFavourites(context.Background(), nil)
2017-04-14 20:40:06 +02:00
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if len(favs) != 2 {
t.Fatalf("result should be two: %d", len(favs))
}
if favs[0].Content != "foo" {
t.Fatalf("want %q but %q", "foo", favs[0].Content)
}
if favs[1].Content != "bar" {
t.Fatalf("want %q but %q", "bar", favs[1].Content)
}
}
2017-04-16 06:11:56 +02:00
2021-06-29 17:02:28 +02:00
func TestGetBookmarks(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `[{"content": "foo"}, {"content": "bar"}]`)
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
books, err := client.GetBookmarks(context.Background(), nil)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if len(books) != 2 {
t.Fatalf("result should be two: %d", len(books))
}
if books[0].Content != "foo" {
t.Fatalf("want %q but %q", "foo", books[0].Content)
}
if books[1].Content != "bar" {
t.Fatalf("want %q but %q", "bar", books[1].Content)
}
}
2017-04-16 10:11:47 +02:00
func TestGetStatus(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/statuses/1234567" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
2017-10-26 08:00:17 +02:00
fmt.Fprintln(w, `{"content": "zzz", "emojis":[{"shortcode":"💩", "url":"http://example.com", "static_url": "http://example.com/static"}]}`)
2017-04-16 10:11:47 +02:00
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
2017-10-25 08:22:17 +02:00
_, err := client.GetStatus(context.Background(), "123")
2017-04-16 10:11:47 +02:00
if err == nil {
t.Fatalf("should be fail: %v", err)
}
2017-10-25 08:22:17 +02:00
status, err := client.GetStatus(context.Background(), "1234567")
2017-04-16 10:11:47 +02:00
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if status.Content != "zzz" {
t.Fatalf("want %q but %q", "zzz", status.Content)
}
2017-10-26 08:00:17 +02:00
if len(status.Emojis) != 1 {
t.Fatal("should have emojis")
}
if status.Emojis[0].ShortCode != "💩" {
2018-11-24 06:51:54 +01:00
t.Fatalf("want %q but %q", "💩", status.Emojis[0].ShortCode)
2017-10-26 08:00:17 +02:00
}
if status.Emojis[0].URL != "http://example.com" {
2017-10-26 08:18:07 +02:00
t.Fatalf("want %q but %q", "https://example.com", status.Emojis[0].URL)
2017-10-26 08:00:17 +02:00
}
if status.Emojis[0].StaticURL != "http://example.com/static" {
2017-10-26 08:18:07 +02:00
t.Fatalf("want %q but %q", "https://example.com/static", status.Emojis[0].StaticURL)
2017-10-26 08:00:17 +02:00
}
2017-04-16 10:11:47 +02:00
}
func TestGetStatusCard(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/statuses/1234567/card" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
2017-04-20 12:14:24 +02:00
fmt.Fprintln(w, `{"title": "zzz"}`)
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
2017-10-25 08:22:17 +02:00
_, err := client.GetStatusCard(context.Background(), "123")
if err == nil {
t.Fatalf("should be fail: %v", err)
}
2017-10-25 08:22:17 +02:00
card, err := client.GetStatusCard(context.Background(), "1234567")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if card.Title != "zzz" {
t.Fatalf("want %q but %q", "zzz", card.Title)
}
}
func TestGetStatusContext(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/statuses/1234567/context" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
2017-04-20 12:14:24 +02:00
fmt.Fprintln(w, `{"ancestors": [{"content": "zzz"},{"content": "bbb"}]}`)
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
2017-10-25 08:22:17 +02:00
_, err := client.GetStatusContext(context.Background(), "123")
if err == nil {
t.Fatalf("should be fail: %v", err)
}
2017-10-25 08:22:17 +02:00
context, err := client.GetStatusContext(context.Background(), "1234567")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if len(context.Ancestors) != 2 {
t.Fatalf("Ancestors should have 2 entries but %q", len(context.Ancestors))
}
if context.Ancestors[0].Content != "zzz" {
t.Fatalf("want %q but %q", "zzz", context.Ancestors[0].Content)
}
if context.Ancestors[1].Content != "bbb" {
t.Fatalf("want %q but %q", "bbb", context.Ancestors[1].Content)
}
if len(context.Descendants) > 0 {
t.Fatalf("Descendants should not be included")
}
}
func TestGetStatusSource(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/statuses/1234567/source" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Fprintln(w, `{"id":"1234567","text":"Foo","spoiler_text":"Bar"}%`)
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.GetStatusSource(context.Background(), "123")
if err == nil {
t.Fatalf("should be fail: %v", err)
}
source, err := client.GetStatusSource(context.Background(), "1234567")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if source.ID != ID("1234567") {
t.Fatalf("want %q but %q", "1234567", source.ID)
}
if source.Text != "Foo" {
t.Fatalf("want %q but %q", "Foo", source.Text)
}
if source.SpoilerText != "Bar" {
t.Fatalf("want %q but %q", "Bar", source.SpoilerText)
}
}
func TestGetStatusHistory(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/statuses/1234567/history" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Fprintln(w, `[{"content": "foo", "emojis":[{"shortcode":"💩", "url":"http://example.com", "static_url": "http://example.com/static"}]}, {"content": "bar", "emojis":[{"shortcode":"💩", "url":"http://example.com", "static_url": "http://example.com/static"}]}]`)
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.GetStatusHistory(context.Background(), "123")
if err == nil {
t.Fatalf("should be fail: %v", err)
}
statuses, err := client.GetStatusHistory(context.Background(), "1234567")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if len(statuses) != 2 {
t.Fatalf("want len %q but got %q", "2", len(statuses))
}
if statuses[0].Content != "foo" {
t.Fatalf("want %q but %q", "bar", statuses[0].Content)
}
if statuses[1].Content != "bar" {
t.Fatalf("want %q but %q", "bar", statuses[1].Content)
}
if len(statuses[0].Emojis) != 1 {
t.Fatal("should have emojis")
}
if statuses[0].Emojis[0].ShortCode != "💩" {
t.Fatalf("want %q but %q", "💩", statuses[0].Emojis[0].ShortCode)
}
if statuses[0].Emojis[0].URL != "http://example.com" {
t.Fatalf("want %q but %q", "https://example.com", statuses[0].Emojis[0].URL)
}
if statuses[0].Emojis[0].StaticURL != "http://example.com/static" {
t.Fatalf("want %q but %q", "https://example.com/static", statuses[0].Emojis[0].StaticURL)
}
}
2017-04-16 06:11:56 +02:00
func TestGetRebloggedBy(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/statuses/1234567/reblogged_by" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
2017-04-20 12:14:24 +02:00
fmt.Fprintln(w, `[{"username": "foo"}, {"username": "bar"}]`)
2017-04-16 06:11:56 +02:00
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
2017-10-25 08:22:17 +02:00
_, err := client.GetRebloggedBy(context.Background(), "123", nil)
2017-04-16 06:11:56 +02:00
if err == nil {
t.Fatalf("should be fail: %v", err)
}
2017-10-25 08:22:17 +02:00
rbs, err := client.GetRebloggedBy(context.Background(), "1234567", nil)
2017-04-16 06:11:56 +02:00
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if len(rbs) != 2 {
t.Fatalf("result should be two: %d", len(rbs))
}
if rbs[0].Username != "foo" {
t.Fatalf("want %q but %q", "foo", rbs[0].Username)
}
if rbs[1].Username != "bar" {
2017-04-17 06:03:31 +02:00
t.Fatalf("want %q but %q", "bar", rbs[1].Username)
2017-04-16 06:11:56 +02:00
}
}
2017-04-16 06:45:59 +02:00
func TestGetFavouritedBy(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/statuses/1234567/favourited_by" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
2017-04-20 12:14:24 +02:00
fmt.Fprintln(w, `[{"username": "foo"}, {"username": "bar"}]`)
2017-04-16 06:45:59 +02:00
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
2017-10-25 08:22:17 +02:00
_, err := client.GetFavouritedBy(context.Background(), "123", nil)
2017-04-16 06:45:59 +02:00
if err == nil {
t.Fatalf("should be fail: %v", err)
}
2017-10-25 08:22:17 +02:00
fbs, err := client.GetFavouritedBy(context.Background(), "1234567", nil)
2017-04-16 06:45:59 +02:00
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if len(fbs) != 2 {
t.Fatalf("result should be two: %d", len(fbs))
}
if fbs[0].Username != "foo" {
t.Fatalf("want %q but %q", "foo", fbs[0].Username)
}
if fbs[1].Username != "bar" {
2017-04-17 06:03:31 +02:00
t.Fatalf("want %q but %q", "bar", fbs[1].Username)
2017-04-16 06:45:59 +02:00
}
}
2017-04-16 07:53:15 +02:00
func TestReblog(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/statuses/1234567/reblog" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
2017-04-20 12:14:24 +02:00
fmt.Fprintln(w, `{"content": "zzz"}`)
2017-04-16 07:53:15 +02:00
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
2017-10-25 08:22:17 +02:00
_, err := client.Reblog(context.Background(), "123")
2017-04-16 07:53:15 +02:00
if err == nil {
t.Fatalf("should be fail: %v", err)
}
2017-10-25 08:22:17 +02:00
status, err := client.Reblog(context.Background(), "1234567")
2017-04-16 07:53:15 +02:00
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if status.Content != "zzz" {
t.Fatalf("want %q but %q", "zzz", status.Content)
}
}
func TestUnreblog(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/statuses/1234567/unreblog" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
2017-04-20 12:14:24 +02:00
fmt.Fprintln(w, `{"content": "zzz"}`)
2017-04-16 07:53:15 +02:00
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
2017-10-25 08:22:17 +02:00
_, err := client.Unreblog(context.Background(), "123")
2017-04-16 07:53:15 +02:00
if err == nil {
t.Fatalf("should be fail: %v", err)
}
2017-10-25 08:22:17 +02:00
status, err := client.Unreblog(context.Background(), "1234567")
2017-04-16 07:53:15 +02:00
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if status.Content != "zzz" {
t.Fatalf("want %q but %q", "zzz", status.Content)
}
}
2017-04-16 08:32:48 +02:00
func TestFavourite(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/statuses/1234567/favourite" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
2017-04-20 12:14:24 +02:00
fmt.Fprintln(w, `{"content": "zzz"}`)
2017-04-16 08:32:48 +02:00
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
2017-10-25 08:22:17 +02:00
_, err := client.Favourite(context.Background(), "123")
2017-04-16 08:32:48 +02:00
if err == nil {
t.Fatalf("should be fail: %v", err)
}
2017-10-25 08:22:17 +02:00
status, err := client.Favourite(context.Background(), "1234567")
2017-04-16 08:32:48 +02:00
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if status.Content != "zzz" {
t.Fatalf("want %q but %q", "zzz", status.Content)
}
}
func TestUnfavourite(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/statuses/1234567/unfavourite" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
2017-04-20 12:14:24 +02:00
fmt.Fprintln(w, `{"content": "zzz"}`)
2017-04-16 08:32:48 +02:00
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
2017-10-25 08:22:17 +02:00
_, err := client.Unfavourite(context.Background(), "123")
2017-04-16 08:32:48 +02:00
if err == nil {
t.Fatalf("should be fail: %v", err)
}
2017-10-25 08:22:17 +02:00
status, err := client.Unfavourite(context.Background(), "1234567")
2017-04-16 08:32:48 +02:00
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if status.Content != "zzz" {
t.Fatalf("want %q but %q", "zzz", status.Content)
}
}
2017-04-17 06:54:36 +02:00
2021-06-29 17:02:28 +02:00
func TestBookmark(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/statuses/1234567/bookmark" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Fprintln(w, `{"content": "zzz"}`)
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.Bookmark(context.Background(), "123")
if err == nil {
t.Fatalf("should be fail: %v", err)
}
status, err := client.Bookmark(context.Background(), "1234567")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if status.Content != "zzz" {
t.Fatalf("want %q but %q", "zzz", status.Content)
}
}
func TestUnbookmark(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/statuses/1234567/unbookmark" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Fprintln(w, `{"content": "zzz"}`)
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.Unbookmark(context.Background(), "123")
if err == nil {
t.Fatalf("should be fail: %v", err)
}
status, err := client.Unbookmark(context.Background(), "1234567")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if status.Content != "zzz" {
t.Fatalf("want %q but %q", "zzz", status.Content)
}
}
2017-04-25 20:16:32 +02:00
func TestGetTimelinePublic(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("local") == "" {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, `[{"content": "foo"}, {"content": "bar"}]`)
}))
defer ts.Close()
client := NewClient(&Config{Server: ts.URL})
2017-05-06 16:03:19 +02:00
_, err := client.GetTimelinePublic(context.Background(), false, nil)
2017-04-25 20:16:32 +02:00
if err == nil {
t.Fatalf("should be fail: %v", err)
}
2017-05-06 16:03:19 +02:00
tl, err := client.GetTimelinePublic(context.Background(), true, nil)
2017-04-25 20:16:32 +02:00
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if len(tl) != 2 {
t.Fatalf("result should be two: %d", len(tl))
}
if tl[0].Content != "foo" {
t.Fatalf("want %q but %q", "foo", tl[0].Content)
}
if tl[1].Content != "bar" {
t.Fatalf("want %q but %q", "bar", tl[1].Content)
}
}
2019-06-21 18:44:24 +02:00
func TestGetTimelineDirect(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `[{"id": "4", "unread":false, "last_status" : {"content": "zzz"}}, {"id": "3", "unread":true, "last_status" : {"content": "bar"}}]`)
2019-06-21 18:44:24 +02:00
}))
defer ts.Close()
client := NewClient(&Config{Server: ts.URL})
tl, err := client.GetTimelineDirect(context.Background(), nil)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if len(tl) != 2 {
t.Fatalf("result should be two: %d", len(tl))
}
if tl[0].Content != "zzz" {
2019-06-21 18:44:24 +02:00
t.Fatalf("want %q but %q", "foo", tl[0].Content)
}
if tl[1].Content != "bar" {
2019-06-21 18:44:24 +02:00
t.Fatalf("want %q but %q", "bar", tl[1].Content)
}
}
func TestGetTimelineHashtag(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/timelines/tag/zzz" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
2017-04-20 12:14:24 +02:00
fmt.Fprintln(w, `[{"content": "zzz"},{"content": "yyy"}]`)
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
2017-05-06 16:03:19 +02:00
_, err := client.GetTimelineHashtag(context.Background(), "notfound", false, nil)
if err == nil {
t.Fatalf("should be fail: %v", err)
}
2017-05-06 16:03:19 +02:00
tags, err := client.GetTimelineHashtag(context.Background(), "zzz", true, 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)
}
}
2019-05-12 16:32:17 +02:00
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"}]`)
}))
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)
}
}
2017-04-28 17:43:53 +02:00
func TestGetTimelineMedia(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("local") == "" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Fprintln(w, `[{"content": "zzz"},{"content": "yyy"}]`)
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
2017-05-06 16:03:19 +02:00
_, err := client.GetTimelineMedia(context.Background(), false, nil)
2017-04-28 17:43:53 +02:00
if err == nil {
t.Fatalf("should be fail: %v", err)
}
2017-05-06 16:03:19 +02:00
tags, err := client.GetTimelineMedia(context.Background(), true, nil)
2017-04-28 17:43:53 +02:00
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 TestDeleteStatus(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/statuses/1234567" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
if r.Method != "DELETE" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusMethodNotAllowed)
return
}
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
2017-10-25 08:22:17 +02:00
err := client.DeleteStatus(context.Background(), "123")
if err == nil {
t.Fatalf("should be fail: %v", err)
}
2017-10-25 08:22:17 +02:00
err = client.DeleteStatus(context.Background(), "1234567")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
}
func TestSearch(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v2/search" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
if r.RequestURI != "/api/v2/search?q=q&resolve=false" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusBadRequest)
return
}
fmt.Fprintln(w, `
2017-04-20 12:14:24 +02:00
{"accounts":[{"username": "zzz"},{"username": "yyy"}],
"statuses":[{"content": "aaa"}],
"hashtags":[{"name": "tag"},{"name": "tag2"},{"name": "tag3"}]
}`)
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
ret, err := client.Search(context.Background(), "q", false)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if len(ret.Accounts) != 2 {
t.Fatalf("Accounts have %q entries, but %q", "2", len(ret.Accounts))
}
if ret.Accounts[0].Username != "zzz" {
t.Fatalf("Accounts Username should %q , but %q", "zzz", ret.Accounts[0].Username)
}
if len(ret.Statuses) != 1 {
t.Fatalf("Statuses have %q entries, but %q", "1", len(ret.Statuses))
}
if ret.Statuses[0].Content != "aaa" {
t.Fatalf("Statuses Content should %q , but %q", "aaa", ret.Statuses[0].Content)
}
if len(ret.Hashtags) != 3 {
t.Fatalf("Hashtags have %q entries, but %q", "3", len(ret.Hashtags))
}
if ret.Hashtags[2].Name != "tag3" {
t.Fatalf("Hashtags[2] should %q , but %q", "tag3", ret.Hashtags[2])
}
}
2017-04-17 06:54:36 +02:00
func TestUploadMedia(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/media" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
if r.Method != "POST" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
2017-04-20 12:14:24 +02:00
fmt.Fprintln(w, `{"id": 123}`)
2017-04-17 06:54:36 +02:00
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
attachment, err := client.UploadMedia(context.Background(), "testdata/logo.png")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if attachment.ID != "123" {
t.Fatalf("want %q but %q", "123", attachment.ID)
2017-04-17 06:54:36 +02:00
}
2019-03-11 03:55:15 +01:00
file, err := os.Open("testdata/logo.png")
if err != nil {
t.Fatalf("could not open file: %v", err)
}
2019-03-11 03:55:15 +01:00
defer file.Close()
writerAttachment, err := client.UploadMediaFromReader(context.Background(), file)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if writerAttachment.ID != "123" {
t.Fatalf("want %q but %q", "123", attachment.ID)
}
bytes, err := ioutil.ReadFile("testdata/logo.png")
2022-11-15 03:54:56 +01:00
if err != nil {
t.Fatalf("could not open file: %v", err)
}
byteAttachment, err := client.UploadMediaFromBytes(context.Background(), bytes)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if byteAttachment.ID != "123" {
t.Fatalf("want %q but got %q", "123", attachment.ID)
}
2017-04-17 06:54:36 +02:00
}
func TestGetConversations(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/conversations" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
}
fmt.Fprintln(w, `[{"id": "4", "unread":false, "last_status" : {"content": "zzz"}}, {"id": "3", "unread":true, "last_status" : {"content": "bar"}}]`)
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
convs, err := client.GetConversations(context.Background(), nil)
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if len(convs) != 2 {
t.Fatalf("result should be 2: %d", len(convs))
}
if convs[0].ID != "4" {
t.Fatalf("want %q but %q", "4", convs[0].ID)
}
if convs[0].LastStatus.Content != "zzz" {
t.Fatalf("want %q but %q", "zzz", convs[0].LastStatus.Content)
}
if convs[1].Unread != true {
t.Fatalf("unread should be true: %t", convs[1].Unread)
}
}
func TestDeleteConversation(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/conversations/12345678" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
if r.Method != "DELETE" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusMethodNotAllowed)
return
}
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "hoge",
})
2020-02-25 12:38:06 +01:00
err := client.DeleteConversation(context.Background(), "12345678")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
}
func TestMarkConversationsAsRead(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/conversations/111111/read" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
if r.Method != "POST" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
err := client.MarkConversationAsRead(context.Background(), "111111")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
}