forked from mattn/go-mastodon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance_test.go
188 lines (177 loc) · 5.66 KB
/
instance_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package mastodon
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestGetInstance(t *testing.T) {
canErr := true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if canErr {
canErr = false
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, `{"title": "mastodon", "uri": "http://mstdn.example.com", "description": "test mastodon", "email": "mstdn@mstdn.example.com", "contact_account": {"username": "mattn"}}`)
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.GetInstance(context.Background())
if err == nil {
t.Fatalf("should be fail: %v", err)
}
ins, err := client.GetInstance(context.Background())
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if ins.Title != "mastodon" {
t.Fatalf("want %q but %q", "mastodon", ins.Title)
}
if ins.URI != "http://mstdn.example.com" {
t.Fatalf("want %q but %q", "http://mstdn.example.com", ins.URI)
}
if ins.Description != "test mastodon" {
t.Fatalf("want %q but %q", "test mastodon", ins.Description)
}
if ins.EMail != "mstdn@mstdn.example.com" {
t.Fatalf("want %q but %q", "mstdn@mstdn.example.com", ins.EMail)
}
if ins.ContactAccount.Username != "mattn" {
t.Fatalf("want %q but %q", "mattn", ins.ContactAccount.Username)
}
}
func TestGetInstanceMore(t *testing.T) {
canErr := true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if canErr {
canErr = false
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, `{"title": "mastodon", "uri": "http://mstdn.example.com", "description": "test mastodon", "email": "mstdn@mstdn.example.com", "version": "0.0.1", "urls":{"foo":"http://stream1.example.com", "bar": "http://stream2.example.com"}, "thumbnail": "http://mstdn.example.com/logo.png", "configuration":{"accounts": {"max_featured_tags": 10}, "statuses": {"max_characters": 500}}, "stats":{"user_count":1, "status_count":2, "domain_count":3}}}`)
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
_, err := client.GetInstance(context.Background())
if err == nil {
t.Fatalf("should be fail: %v", err)
}
ins, err := client.GetInstance(context.Background())
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if ins.Title != "mastodon" {
t.Fatalf("want %q but %q", "mastodon", ins.Title)
}
if ins.URI != "http://mstdn.example.com" {
t.Fatalf("want %q but %q", "mastodon", ins.URI)
}
if ins.Description != "test mastodon" {
t.Fatalf("want %q but %q", "test mastodon", ins.Description)
}
if ins.EMail != "mstdn@mstdn.example.com" {
t.Fatalf("want %q but %q", "mstdn@mstdn.example.com", ins.EMail)
}
if ins.Version != "0.0.1" {
t.Fatalf("want %q but %q", "0.0.1", ins.Version)
}
if ins.URLs["foo"] != "http://stream1.example.com" {
t.Fatalf("want %q but %q", "http://stream1.example.com", ins.Version)
}
if ins.URLs["bar"] != "http://stream2.example.com" {
t.Fatalf("want %q but %q", "http://stream2.example.com", ins.Version)
}
if ins.Thumbnail != "http://mstdn.example.com/logo.png" {
t.Fatalf("want %q but %q", "http://mstdn.example.com/logo.png", ins.Thumbnail)
}
if ins.Stats == nil {
t.Fatal("stats should not be nil")
}
if ins.Stats.UserCount != 1 {
t.Fatalf("want %v but %v", 1, ins.Stats.UserCount)
}
if ins.Stats.StatusCount != 2 {
t.Fatalf("want %v but %v", 2, ins.Stats.StatusCount)
}
if ins.Stats.DomainCount != 3 {
t.Fatalf("want %v but %v", 3, ins.Stats.DomainCount)
}
cfg := ins.GetConfig()
if cfg.Accounts == nil {
t.Error("expected accounts to be non nil")
}
if cfg.Statuses == nil {
t.Error("expected statuses to be non nil")
}
}
func TestGetInstanceActivity(t *testing.T) {
canErr := true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if canErr {
canErr = false
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, `[{"week":"1516579200","statuses":"1","logins":"1","registrations":"0"}]`)
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
})
_, err := client.GetInstanceActivity(context.Background())
if err == nil {
t.Fatalf("should be fail: %v", err)
}
activity, err := client.GetInstanceActivity(context.Background())
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if activity[0].Week != Unixtime(time.Unix(1516579200, 0)) {
t.Fatalf("want %v but %v", Unixtime(time.Unix(1516579200, 0)), activity[0].Week)
}
if activity[0].Logins != 1 {
t.Fatalf("want %q but %q", 1, activity[0].Logins)
}
}
func TestGetInstancePeers(t *testing.T) {
canErr := true
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if canErr {
canErr = false
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, `["mastodon.social","mstdn.jp"]`)
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
})
_, err := client.GetInstancePeers(context.Background())
if err == nil {
t.Fatalf("should be fail: %v", err)
}
peers, err := client.GetInstancePeers(context.Background())
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if peers[0] != "mastodon.social" {
t.Fatalf("want %q but %q", "mastodon.social", peers[0])
}
if peers[1] != "mstdn.jp" {
t.Fatalf("want %q but %q", "mstdn.jp", peers[1])
}
}