forked from mattn/go-mastodon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
report_test.go
92 lines (87 loc) · 2.29 KB
/
report_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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",
})
rp, err := client.Report(context.Background(), "121", nil, "")
if err == nil {
t.Fatalf("should be fail: %v", err)
}
rp, err = client.Report(context.Background(), "122", nil, "")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if rp.ID != 1234 {
t.Fatalf("want %q but %q", "1234", rp.ID)
}
if rp.ActionTaken {
t.Fatalf("want %v but %v", true, rp.ActionTaken)
}
rp, err = client.Report(context.Background(), "123", []ID{"567"}, "")
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if rp.ID != 1234 {
t.Fatalf("want %q but %q", "1234", rp.ID)
}
if !rp.ActionTaken {
t.Fatalf("want %v but %v", false, rp.ActionTaken)
}
}