-
Notifications
You must be signed in to change notification settings - Fork 12
/
profile_test.go
283 lines (268 loc) · 9.81 KB
/
profile_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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright 2023 go-bluesky authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package bluesky
import (
"context"
"errors"
"io"
"testing"
)
// Tests that the library can be used to fetch a user's profile from a Bluesky
// server.
func TestFetchProfileWithTrimmedHandle(t *testing.T) {
testFetchProfile(t, testHandleTester)
}
func TestFetchProfileWithShorthandHandle(t *testing.T) {
testFetchProfile(t, "@"+testHandleTester)
}
func TestFetchProfileWithCanonicalHandle(t *testing.T) {
testFetchProfile(t, "at://"+testHandleTester)
}
func TestFetchProfileWithTrimmedDID(t *testing.T) {
testFetchProfile(t, testDIDTester)
}
func TestFetchProfileWithCanonicalDID(t *testing.T) {
testFetchProfile(t, "at://"+testDIDTester)
}
func testFetchProfile(t *testing.T, id string) {
var (
client = makeTestClientWithLogin(t)
ctx = context.Background()
)
// Retrieve and validate the base fields of the profile
profile, err := client.FetchProfile(ctx, id)
if err != nil {
t.Fatalf("failed to fetch user profile: %v", err)
}
if profile.Handle != testHandleTester {
t.Errorf("handle mismatch: have %v, want %v", profile.Handle, testHandleTester)
}
if profile.DID != testDIDTester {
t.Errorf("did mismatch: have %v, want %v", profile.DID, testDIDTester)
}
if profile.Name != "go-bluesky tester" {
t.Errorf("name mismatch: have %v, want %v", profile.Name, "go-bluesky tester")
}
if profile.Bio != "I'm a test account used to run the https://github.com/karalabe/go-bluesky test suite. If I do anything weird, please contact @karalabe.bsky.social to fix me." {
t.Errorf("bio mismatch: have %v, want %v", profile.Bio, "I'm a test account used to run the https://github.com/karalabe/go-bluesky test suite. If I do anything weird, please contact @karalabe.bsky.social to fix me.")
}
if profile.AvatarURL != "" {
t.Errorf("avatar URL mismatch: have %v, want %v", profile.AvatarURL, "")
}
if profile.BannerURL != "" {
t.Errorf("banner URL mismatch: have %v, want %v", profile.BannerURL, "")
}
if profile.FollowerCount < 32 { // 06.05.2023 follower count
t.Errorf("followers count mismatch: have %v, want at least %v", profile.FollowerCount, 32)
}
if profile.FolloweeCount != 1 { // only follow this lib's author
t.Errorf("follows count mismatch: have %v, want %v", profile.FolloweeCount, 1)
}
if profile.FolloweeCount != 1 { // only follow this lib's author
t.Errorf("follows count mismatch: have %v, want %v", profile.FolloweeCount, 1)
}
if profile.PostCount != 2 { // outside of live tests, there are only 2 stable posts on the user
t.Errorf("posts count mismatch: have %v, want %v", profile.PostCount, 2)
}
// Avatar and banner resolution should succeed and be noops for the test user
if err := profile.ResolveAvatar(ctx); err != nil {
t.Errorf("avatar resolution failed for no avatar: %v", err)
}
if err := profile.ResolveBanner(ctx); err != nil {
t.Errorf("banner resolution failed for no banner: %v", err)
}
}
// Tests that the library can retrieve profile avatars and banners advertised.
func TestResolveProfileImages(t *testing.T) {
var (
client = makeTestClientWithLogin(t)
ctx = context.Background()
)
// Retrieve the library author's profile, hopefully the images are still set
profile, err := client.FetchProfile(ctx, testDIDPeter)
if err != nil {
t.Fatalf("failed to fetch author profile: %v", err)
}
if profile.AvatarURL == "" {
t.Errorf("avatar URL mismatch: have empty, want non-empty")
}
if profile.BannerURL == "" {
t.Errorf("banner URL mismatch: have empty, want non-empty")
}
// Avatar and banner resolution should succeed and actually download something
if err := profile.ResolveAvatar(ctx); err != nil {
t.Errorf("avatar resolution failed: %v", err)
}
if profile.Avatar == nil {
t.Errorf("resolved avatar still nil")
}
if err := profile.ResolveBanner(ctx); err != nil {
t.Errorf("banner resolution failed: %v", err)
}
if profile.Banner == nil {
t.Errorf("resolved banner still nil")
}
// Avatar and banner resolution should however fail if the user's desired
// download limit is smaller than the images
if err := profile.ResolveAvatarWithLimit(ctx, 100); !errors.Is(err, io.ErrUnexpectedEOF) {
t.Errorf("avatar resolution error mismatch: have %v, want %v", err, io.ErrUnexpectedEOF)
}
if err := profile.ResolveBannerWithLimit(ctx, 100); !errors.Is(err, io.ErrUnexpectedEOF) {
t.Errorf("banner resolution error mismatch: have %v, want %v", err, io.ErrUnexpectedEOF)
}
}
// Tests that the library can crawl the follower list and retrieve all of them.
func TestResolveProfileFollowers(t *testing.T) {
var (
client = makeTestClientWithLogin(t)
ctx = context.Background()
)
// Retrieve the library author's profile, hopefully there are many followers :P
profile, err := client.FetchProfile(ctx, testDIDPeter)
if err != nil {
t.Fatalf("failed to fetch author profile: %v", err)
}
// Resolve all the followers directly into the profile struct
if err := profile.ResolveFollowers(ctx); err != nil {
t.Fatalf("failed to fetch author followers: %v", err)
}
if profile.Followers == nil {
t.Errorf("embedded follower list nil")
}
if len(profile.Followers) != int(profile.FollowerCount) {
t.Errorf("follower count mismatch: have %v, want %v", len(profile.Followers), profile.FollowerCount)
}
}
// Tests that a cancelled context will stop resolving followers.
func TestResolveProfileFollowersWithCancellation(t *testing.T) {
var (
client = makeTestClientWithLogin(t)
ctx = context.Background()
)
// Retrieve the library author's profile, hopefully there are many followers :P
profile, err := client.FetchProfile(ctx, testDIDPeter)
if err != nil {
t.Fatalf("failed to fetch author profile: %v", err)
}
// Resolve the followers indirectly via channels, cancelling after the first
// read, ensuring that the full list does not get crawled
cctx, cancel := context.WithCancel(ctx)
followerc, errc := profile.StreamFollowers(cctx)
<-followerc
retrieved := 1
cancel()
for range followerc {
retrieved++
}
if retrieved >= int(profile.FollowerCount) {
t.Errorf("interrupted resolver retrieved all followers: have %d, want < %d", retrieved, profile.FollowerCount)
}
if err := <-errc; !errors.Is(err, context.Canceled) {
t.Errorf("interrupt error mismatch: have %v, want %v", err, context.Canceled)
}
}
// Tests that avatars of followers can be resolved.
func TestResolveProfileFollowerAvatar(t *testing.T) {
var (
client = makeTestClientWithLogin(t)
ctx = context.Background()
)
// Retrieve the library author's profile and list of followers
profile, err := client.FetchProfile(ctx, testDIDPeter)
if err != nil {
t.Fatalf("failed to fetch author profile: %v", err)
}
if err := profile.ResolveFollowers(ctx); err != nil {
t.Fatalf("failed to fetch author followers: %v", err)
}
// Find Jeromy and hope he has a profile picture set, resolve it
for _, follower := range profile.Followers {
if follower.DID == testDIDJeromy {
if err := follower.ResolveAvatar(ctx); err != nil {
t.Errorf("failed to resolve follower avatar: %v", err)
}
if follower.Avatar == nil {
t.Errorf("resolved avatar still nil")
}
break
}
}
}
// Tests that the library can crawl the followee list and retrieve all of them.
func TestResolveProfileFollowees(t *testing.T) {
var (
client = makeTestClientWithLogin(t)
ctx = context.Background()
)
// Retrieve the library author's profile, hopefully there are many followees :P
profile, err := client.FetchProfile(ctx, testDIDPeter)
if err != nil {
t.Fatalf("failed to fetch author profile: %v", err)
}
// Resolve all the followees directly into the profile struct
if err := profile.ResolveFollowees(ctx); err != nil {
t.Fatalf("failed to fetch author followees: %v", err)
}
if profile.Followees == nil {
t.Errorf("embedded followee list nil")
}
if len(profile.Followees) != int(profile.FolloweeCount) {
t.Errorf("followee count mismatch: have %v, want %v", len(profile.Followees), profile.FolloweeCount)
}
}
// Tests that a cancelled context will stop resolving followees.
func TestResolveProfileFolloweesWithCancellation(t *testing.T) {
var (
client = makeTestClientWithLogin(t)
ctx = context.Background()
)
// Retrieve Jeromy's profile, he's collecting followees like there's no tomorrow
profile, err := client.FetchProfile(ctx, testDIDJeromy)
if err != nil {
t.Fatalf("failed to fetch author profile: %v", err)
}
// Resolve the followees indirectly via channels, cancelling after the first
// read, ensuring that the full list does not get crawled
cctx, cancel := context.WithCancel(ctx)
followeec, errc := profile.StreamFollowees(cctx)
<-followeec
retrieved := 1
cancel()
for range followeec {
retrieved++
}
if retrieved >= int(profile.FolloweeCount) {
t.Errorf("interrupted resolver retrieved all followees: have %d, want < %d", retrieved, profile.FolloweeCount)
}
if err := <-errc; !errors.Is(err, context.Canceled) {
t.Errorf("interrupt error mismatch: have %v, want %v", err, context.Canceled)
}
}
// Tests that avatars of followees can be resolved.
func TestResolveProfileFolloweeAvatar(t *testing.T) {
var (
client = makeTestClientWithLogin(t)
ctx = context.Background()
)
// Retrieve the library author's profile and list of followees
profile, err := client.FetchProfile(ctx, testDIDPeter)
if err != nil {
t.Fatalf("failed to fetch author profile: %v", err)
}
if err := profile.ResolveFollowees(ctx); err != nil {
t.Fatalf("failed to fetch author followers: %v", err)
}
// Find Jeromy and hope he has a profile picture set, resolve it
for _, followee := range profile.Followees {
if followee.DID == testDIDJeromy {
if err := followee.ResolveAvatar(ctx); err != nil {
t.Errorf("failed to resolve followee avatar: %v", err)
}
if followee.Avatar == nil {
t.Errorf("resolved avatar still nil")
}
break
}
}
}