go-mastodon/mastodon_test.go

194 lines
4.6 KiB
Go
Raw Normal View History

2017-04-14 10:10:20 +02:00
package mastodon
import (
"context"
2017-04-14 10:10:20 +02:00
"fmt"
2017-04-14 10:30:27 +02:00
"io"
2017-04-14 10:10:20 +02:00
"net/http"
"net/http/httptest"
"testing"
)
func TestAuthenticate(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.FormValue("username") != "valid" || r.FormValue("password") != "user" {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
fmt.Fprintln(w, `{"AccessToken": "zoo"}`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
})
err := client.Authenticate(context.Background(), "invalid", "user")
2017-04-14 10:10:20 +02:00
if err == nil {
t.Fatalf("should be fail: %v", err)
}
client = NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
})
err = client.Authenticate(context.Background(), "valid", "user")
2017-04-14 10:10:20 +02:00
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
}
func TestPostStatus(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Bearer zoo" {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
fmt.Fprintln(w, `{"AccessToken": "zoo"}`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
})
_, err := client.PostStatus(context.Background(), &Toot{
2017-04-14 10:10:20 +02:00
Status: "foobar",
})
if err == nil {
t.Fatalf("should be fail: %v", err)
}
client = NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err = client.PostStatus(context.Background(), &Toot{
2017-04-14 10:10:20 +02:00
Status: "foobar",
})
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
}
2017-04-14 10:21:31 +02:00
2017-04-14 10:26:22 +02:00
func TestGetTimelineHome(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",
})
_, err := client.PostStatus(context.Background(), &Toot{
2017-04-14 10:26:22 +02:00
Status: "foobar",
})
if err == nil {
t.Fatalf("should be fail: %v", err)
}
client = NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
tl, err := client.GetTimelineHome(context.Background())
2017-04-14 10:26:22 +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" {
2017-04-14 11:53:14 +02:00
t.Fatalf("want %q but %q", "bar", tl[1].Content)
2017-04-14 10:26:22 +02:00
}
}
2017-04-14 10:21:31 +02:00
func TestForTheCoverages(t *testing.T) {
(*UpdateEvent)(nil).event()
(*NotificationEvent)(nil).event()
(*DeleteEvent)(nil).event()
(*ErrorEvent)(nil).event()
2017-04-14 12:20:23 +02:00
_ = (&ErrorEvent{io.EOF}).Error()
2017-04-14 10:21:31 +02:00
}
2017-04-14 10:37:59 +02:00
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",
2017-04-14 10:47:15 +02:00
AccessToken: "zoo",
2017-04-14 10:37:59 +02:00
})
a, err := client.GetAccount(context.Background(), 1)
2017-04-14 10:37:59 +02:00
if err == nil {
2017-04-14 10:47:15 +02:00
t.Fatalf("should not be fail: %v", err)
}
a, err = client.GetAccount(context.Background(), 1234567)
2017-04-14 10:47:15 +02:00
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if a.Username != "zzz" {
t.Fatalf("want %q but %q", "zzz", a.Username)
2017-04-14 10:37:59 +02:00
}
2017-04-14 10:47:15 +02:00
}
2017-04-14 10:37:59 +02:00
2017-04-14 10:47:15 +02:00
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" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Fprintln(w, `[{"Username": "foo"}, {"Username": "bar"}]`)
return
}))
defer ts.Close()
client := NewClient(&Config{
2017-04-14 10:37:59 +02:00
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
fl, err := client.GetAccountFollowing(context.Background(), 123)
2017-04-14 10:37:59 +02:00
if err == nil {
t.Fatalf("should not be fail: %v", err)
}
fl, err = client.GetAccountFollowing(context.Background(), 1234567)
2017-04-14 10:37:59 +02:00
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
2017-04-14 10:47:15 +02:00
if len(fl) != 2 {
t.Fatalf("result should be two: %d", len(fl))
}
if fl[0].Username != "foo" {
t.Fatalf("want %q but %q", "foo", fl[0].Username)
}
if fl[1].Username != "bar" {
2017-04-17 06:03:31 +02:00
t.Fatalf("want %q but %q", "bar", fl[1].Username)
2017-04-14 10:37:59 +02:00
}
}