go-mastodon/report_test.go

93 lines
2.3 KiB
Go
Raw Normal View History

2017-04-19 11:09:48 +02:00
package mastodon
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
)
func TestGetReports(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/reports" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Fprintln(w, `[{"id": 122, "action_taken": false}, {"id": 123, "action_taken": true}]`)
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
rs, err := client.GetReports(context.Background())
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if len(rs) != 2 {
t.Fatalf("result should be two: %d", len(rs))
}
if rs[0].ID != 122 {
t.Fatalf("want %v but %v", 122, rs[0].ID)
}
if rs[1].ID != 123 {
t.Fatalf("want %v but %v", 123, rs[1].ID)
}
}
func TestReport(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/api/v1/reports" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
if r.FormValue("account_id") != "122" && r.FormValue("account_id") != "123" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
if r.FormValue("account_id") == "122" {
fmt.Fprintln(w, `{"id": 1234, "action_taken": false}`)
} else {
fmt.Fprintln(w, `{"id": 1234, "action_taken": true}`)
}
return
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
2017-10-25 08:22:17 +02:00
rp, err := client.Report(context.Background(), "121", nil, "")
2017-04-19 11:09:48 +02:00
if err == nil {
t.Fatalf("should be fail: %v", err)
}
2017-10-25 08:22:17 +02:00
rp, err = client.Report(context.Background(), "122", nil, "")
2017-04-19 11:09:48 +02:00
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if rp.ID != 1234 {
2017-10-25 08:22:17 +02:00
t.Fatalf("want %q but %q", "1234", rp.ID)
2017-04-19 11:09:48 +02:00
}
if rp.ActionTaken {
t.Fatalf("want %v but %v", true, rp.ActionTaken)
}
2017-10-25 08:22:17 +02:00
rp, err = client.Report(context.Background(), "123", []ID{"567"}, "")
2017-04-19 11:09:48 +02:00
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if rp.ID != 1234 {
2017-10-25 08:22:17 +02:00
t.Fatalf("want %q but %q", "1234", rp.ID)
2017-04-19 11:09:48 +02:00
}
if !rp.ActionTaken {
t.Fatalf("want %v but %v", false, rp.ActionTaken)
}
}