-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcollection_management_test.go
More file actions
376 lines (364 loc) · 11.5 KB
/
collection_management_test.go
File metadata and controls
376 lines (364 loc) · 11.5 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package processing
import (
"sync"
"testing"
"git.sr.ht/~mariusor/lw"
vocab "github.com/go-ap/activitypub"
c "github.com/go-ap/client"
"github.com/go-ap/errors"
)
func emptyCol(id vocab.IRI) *vocab.OrderedCollection {
return &vocab.OrderedCollection{
ID: id,
Type: vocab.OrderedCollectionType,
OrderedItems: make(vocab.ItemCollection, 0),
}
}
func mockStorage(t *testing.T, l lw.Logger) Store {
store := mockStore{Map: &sync.Map{}}
_, err := store.Save(defaultActor)
if err != nil {
t.Errorf("Failed to save default actor %v", err)
}
for _, col := range vocab.ActivityPubCollections {
_, err = store.Create(emptyCol(col.IRI(defaultActor)))
}
return store
}
func mockClient(t *testing.T, l lw.Logger) c.Basic {
return c.New(c.WithLogger(l), c.SkipTLSValidation(true))
}
func mockProcessor(t *testing.T, base vocab.IRI) *P {
l := lw.Dev(lw.SetOutput(t.Output()))
return &P{
baseIRI: vocab.IRIs{base},
async: false,
c: mockClient(t, l),
s: mockStorage(t, l),
l: l,
localIRICheckFn: defaultLocalIRICheck,
createIDFn: defaultIDGenerator(base),
actorKeyGenFn: defaultKeyGenerator(),
}
}
func TestP_AddActivity(t *testing.T) {
tests := []struct {
name string
base vocab.IRI
add *vocab.Activity
want *vocab.Activity
wantErr error
}{
{
name: "empty",
base: "https://example.local",
wantErr: InvalidActivity("nil Add activity"),
},
{
name: "add jdoe to his own followers",
base: "https://jdoe.example.local",
add: &vocab.Activity{
Target: vocab.IRI("https://jdoe.example.com/followers"),
Object: &vocab.Actor{ID: "https://jdoe.example.com"},
},
want: &vocab.Activity{
Target: vocab.IRI("https://jdoe.example.com/followers"),
Object: &vocab.Actor{ID: "https://jdoe.example.com"},
},
},
{
name: "add jdoe to his own followers and following",
base: "https://jdoe.example.local",
add: &vocab.Activity{
Target: vocab.IRIs{"https://jdoe.example.com/followers", "https://jdoe.example.com/following"},
Object: &vocab.Actor{ID: "https://jdoe.example.com"},
},
want: &vocab.Activity{
Target: vocab.IRIs{"https://jdoe.example.com/followers", "https://jdoe.example.com/following"},
Object: &vocab.Actor{ID: "https://jdoe.example.com"},
},
},
{
name: "add random object to inbox",
base: "https://jdoe.example.local",
add: &vocab.Activity{
Target: vocab.IRI("https://jdoe.example.com/inbox"),
Object: &vocab.Object{ID: "https://example.com", Type: vocab.ProfileType, Content: vocab.NaturalLanguageValuesNew(vocab.DefaultLangRef("test"))},
},
want: &vocab.Activity{
Target: vocab.IRI("https://jdoe.example.com/inbox"),
Object: &vocab.Object{ID: "https://example.com", Type: vocab.ProfileType, Content: vocab.NaturalLanguageValuesNew(vocab.DefaultLangRef("test"))},
},
},
{
name: "add IRI to outbox",
base: "https://jdoe.example.local",
add: &vocab.Activity{
Target: vocab.IRI("https://jdoe.example.com/outbox"),
Object: vocab.IRI("https://example.com"),
},
want: &vocab.Activity{
Target: vocab.IRI("https://jdoe.example.com/outbox"),
Object: vocab.IRI("https://example.com"),
},
},
{
name: "add multiple IRIs to outbox",
base: "https://jdoe.example.local",
add: &vocab.Activity{
Target: vocab.IRI("https://jdoe.example.com/outbox"),
Object: vocab.IRIs{"https://example.com", "https://jdoe.example.com"},
},
want: &vocab.Activity{
Target: vocab.IRI("https://jdoe.example.com/outbox"),
Object: vocab.IRIs{"https://example.com", "https://jdoe.example.com"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := mockProcessor(t, tt.base)
got, err := p.AddActivity(tt.add)
if (err != nil) && !errors.Is(tt.wantErr, err) {
t.Errorf("AddActivity() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !vocab.ItemsEqual(got, tt.want) {
t.Errorf("AddActivity() got = %v, want %v", got, tt.want)
}
if vocab.IsNil(got) {
return
}
_ = vocab.OnItem(got.Target, func(tgt vocab.Item) error {
col, err := p.s.Load(tgt.GetLink())
if err != nil {
t.Errorf("AddActivity() unable to load target form storage: %v", err)
return nil
}
target, ok := col.(vocab.CollectionInterface)
if !ok {
t.Errorf("AddActivity() got Activity %T target %T is not %T", got, tgt, vocab.CollectionInterface(nil))
return nil
}
return vocab.OnItem(got.Object, func(item vocab.Item) error {
if !target.Contains(item) {
t.Errorf("AddActivity() got Activity %T object %s can't be found in %#v", got, item.GetLink(), tgt)
}
return nil
})
})
})
}
}
func TestP_RemoveActivity(t *testing.T) {
tests := []struct {
name string
base vocab.IRI
remove *vocab.Activity
items vocab.ItemCollection
want *vocab.Activity
wantErr error
}{
{
name: "empty",
base: "https://example.local",
wantErr: InvalidActivity("nil Remove activity"),
},
{
name: "remove jdoe from his own followers",
base: "https://jdoe.example.local",
items: vocab.ItemCollection{&vocab.Actor{ID: "https://jdoe.example.com"}},
remove: &vocab.Activity{
Origin: vocab.IRI("https://jdoe.example.com/followers"),
Object: vocab.IRI("https://jdoe.example.com"),
},
want: &vocab.Activity{
Origin: vocab.IRI("https://jdoe.example.com/followers"),
Object: vocab.IRI("https://jdoe.example.com"),
},
},
{
name: "remove random object from inbox",
base: "https://jdoe.example.local",
items: vocab.ItemCollection{&vocab.Object{ID: "https://example.com", Type: vocab.ProfileType, Content: vocab.NaturalLanguageValuesNew(vocab.DefaultLangRef("test"))}},
remove: &vocab.Activity{
Origin: vocab.IRI("https://jdoe.example.com/inbox"),
Object: vocab.IRI("https://example.com"),
},
want: &vocab.Activity{
Origin: vocab.IRI("https://jdoe.example.com/inbox"),
Object: vocab.IRI("https://example.com"),
},
},
{
name: "try to remove inexistent object from outbox",
base: "https://jdoe.example.local",
items: vocab.ItemCollection{},
remove: &vocab.Activity{
Origin: vocab.IRI("https://jdoe.example.com/inbox"),
Object: vocab.IRI("https://example.com"),
},
want: &vocab.Activity{
Origin: vocab.IRI("https://jdoe.example.com/inbox"),
Object: vocab.IRI("https://example.com"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := mockProcessor(t, tt.base)
// NOTE(marius): add items to collection
for _, it := range tt.items {
_ = p.s.AddTo(tt.remove.Origin.GetLink(), it)
}
got, err := p.RemoveActivity(tt.remove)
if (err != nil) && !errors.Is(tt.wantErr, err) {
t.Errorf("RemoveActivity() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !vocab.ItemsEqual(got, tt.want) {
t.Errorf("RemoveActivity() got = %v, want %v", got, tt.want)
}
if vocab.IsNil(got) {
return
}
_ = vocab.OnItem(got.Target, func(tgt vocab.Item) error {
col, err := p.s.Load(tgt.GetLink())
if err != nil {
t.Errorf("RemoveActivity() unable to load target form storage: %v", err)
return nil
}
target, ok := col.(vocab.CollectionInterface)
if !ok {
t.Errorf("RemoveActivity() got Activity %T target %T is not %T", got, tgt, vocab.CollectionInterface(nil))
return nil
}
return vocab.OnItem(got.Object, func(item vocab.Item) error {
if !target.Contains(item) {
t.Errorf("RemoveActivity() got Activity %T object %s can't be found in %#v", got, item.GetLink(), tgt)
}
return nil
})
})
})
}
}
func TestP_MoveActivity(t *testing.T) {
tests := []struct {
name string
base vocab.IRI
remove *vocab.Activity
items vocab.ItemCollection
want *vocab.Activity
wantErr error
}{
{
name: "empty",
base: "https://example.local",
wantErr: InvalidActivity("nil Move activity"),
},
{
name: "move jdoe from followers to following",
base: "https://jdoe.example.local",
items: vocab.ItemCollection{&vocab.Actor{ID: "https://jdoe.example.com"}},
remove: &vocab.Activity{
Origin: vocab.IRI("https://jdoe.example.com/followers"),
Target: vocab.IRI("https://jdoe.example.com/following"),
Object: vocab.IRI("https://jdoe.example.com"),
},
want: &vocab.Activity{
Origin: vocab.IRI("https://jdoe.example.com/followers"),
Target: vocab.IRI("https://jdoe.example.com/following"),
Object: vocab.IRI("https://jdoe.example.com"),
},
},
{
name: "move random object from inbox to outbox",
base: "https://jdoe.example.local",
items: vocab.ItemCollection{
&vocab.Object{ID: "https://example.com", Type: vocab.ProfileType, Content: vocab.NaturalLanguageValuesNew(vocab.DefaultLangRef("test"))},
},
remove: &vocab.Activity{
Origin: vocab.IRI("https://jdoe.example.com/inbox"),
Target: vocab.IRI("https://jdoe.example.com/outbox"),
Object: vocab.IRI("https://example.com"),
},
want: &vocab.Activity{
Origin: vocab.IRI("https://jdoe.example.com/inbox"),
Target: vocab.IRI("https://jdoe.example.com/outbox"),
Object: vocab.IRI("https://example.com"),
},
},
{
name: "try to move inexistent object from inbox to outbox",
base: "https://jdoe.example.local",
items: vocab.ItemCollection{},
remove: &vocab.Activity{
Origin: vocab.IRI("https://jdoe.example.com/inbox"),
Target: vocab.IRI("https://jdoe.example.com/outbox"),
Object: vocab.IRI("https://example.com"),
},
want: &vocab.Activity{
Origin: vocab.IRI("https://jdoe.example.com/inbox"),
Target: vocab.IRI("https://jdoe.example.com/outbox"),
Object: vocab.IRI("https://example.com"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := mockProcessor(t, tt.base)
// NOTE(marius): add items to origin collection
for _, it := range tt.items {
_ = p.s.AddTo(tt.remove.Origin.GetLink(), it)
}
got, err := p.MoveActivity(tt.remove)
if (err != nil) && !errors.Is(tt.wantErr, err) {
t.Errorf("MoveActivity() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !vocab.ItemsEqual(got, tt.want) {
t.Errorf("MoveActivity() got = %v, want %v", got, tt.want)
}
if vocab.IsNil(got) {
return
}
_ = vocab.OnItem(got.Origin, func(orig vocab.Item) error {
col, err := p.s.Load(orig.GetLink())
if err != nil {
t.Errorf("MoveActivity() unable to load origin form storage: %v", err)
return nil
}
origin, ok := col.(vocab.CollectionInterface)
if !ok {
t.Errorf("MoveActivity() got Activity %T origin %T is not %T", got, orig, vocab.CollectionInterface(nil))
return nil
}
return vocab.OnItem(got.Object, func(item vocab.Item) error {
if origin.Contains(item) {
t.Errorf("MoveActivity() got Activity %T object %s still exists origin %#v", got, item.GetLink(), orig)
}
return nil
})
})
_ = vocab.OnItem(got.Target, func(tgt vocab.Item) error {
col, err := p.s.Load(tgt.GetLink())
if err != nil {
t.Errorf("MoveActivity() unable to load target form storage: %v", err)
return nil
}
target, ok := col.(vocab.CollectionInterface)
if !ok {
t.Errorf("MoveActivity() got Activity %T target %T is not %T", got, tgt, vocab.CollectionInterface(nil))
return nil
}
return vocab.OnItem(got.Object, func(item vocab.Item) error {
if !target.Contains(item) {
t.Errorf("MoveActivity() got Activity %T object %s can't be found in target %#v", got, item.GetLink(), tgt)
}
return nil
})
})
})
}
}