go-mastodon/status_test.go

290 lines
7.4 KiB
Go
Raw Normal View History

2017-04-14 20:40:06 +02:00
package mastodon
import (
"context"
2017-04-14 20:40:06 +02:00
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestGetFavourites(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, `[{"Content": "foo"}, {"Content": "bar"}]`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
favs, err := client.GetFavourites(context.Background())
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
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
}
fmt.Fprintln(w, `{"Content": "zzz"}`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.GetStatus(context.Background(), 123)
2017-04-16 10:11:47 +02:00
if err == nil {
t.Fatalf("should be fail: %v", err)
}
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-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
}
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.GetRebloggedBy(context.Background(), 123)
2017-04-16 06:11:56 +02:00
if err == nil {
t.Fatalf("should be fail: %v", err)
}
rbs, err := client.GetRebloggedBy(context.Background(), 1234567)
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" {
t.Fatalf("want %q but %q", "bar", rbs[0].Username)
}
}
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
}
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.GetFavouritedBy(context.Background(), 123)
2017-04-16 06:45:59 +02:00
if err == nil {
t.Fatalf("should be fail: %v", err)
}
fbs, err := client.GetFavouritedBy(context.Background(), 1234567)
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" {
t.Fatalf("want %q but %q", "bar", fbs[0].Username)
}
}
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
}
fmt.Fprintln(w, `{"Content": "zzz"}`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.Reblog(context.Background(), 123)
2017-04-16 07:53:15 +02:00
if err == nil {
t.Fatalf("should be fail: %v", err)
}
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
}
fmt.Fprintln(w, `{"Content": "zzz"}`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.Unreblog(context.Background(), 123)
2017-04-16 07:53:15 +02:00
if err == nil {
t.Fatalf("should be fail: %v", err)
}
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
}
fmt.Fprintln(w, `{"Content": "zzz"}`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.Favourite(context.Background(), 123)
2017-04-16 08:32:48 +02:00
if err == nil {
t.Fatalf("should be fail: %v", err)
}
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
}
fmt.Fprintln(w, `{"Content": "zzz"}`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.Unfavourite(context.Background(), 123)
2017-04-16 08:32:48 +02:00
if err == nil {
t.Fatalf("should be fail: %v", err)
}
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
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
}
fmt.Fprintln(w, `{"ID": 123}`)
return
}))
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)
}
}