forked from go-ap/activitypub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intransitive_activity_test.go
391 lines (347 loc) · 9.42 KB
/
intransitive_activity_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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
package activitypub
import (
"testing"
"time"
)
func TestIntransitiveActivityNew(t *testing.T) {
testValue := ID("test")
var testType ActivityVocabularyType = "Arrive"
a := IntransitiveActivityNew(testValue, testType)
if a.ID != testValue {
t.Errorf("IntransitiveActivity Id '%v' different than expected '%v'", a.ID, testValue)
}
if a.Type != testType {
t.Errorf("IntransitiveActivity Type '%v' different than expected '%v'", a.Type, testType)
}
g := IntransitiveActivityNew(testValue, "")
if g.ID != testValue {
t.Errorf("IntransitiveActivity Id '%v' different than expected '%v'", g.ID, testValue)
}
if g.Type != IntransitiveActivityType {
t.Errorf("IntransitiveActivity Type '%v' different than expected '%v'", g.Type, IntransitiveActivityType)
}
}
func TestIntransitiveActivityRecipients(t *testing.T) {
bob := PersonNew("bob")
alice := PersonNew("alice")
foo := OrganizationNew("foo")
bar := GroupNew("bar")
a := IntransitiveActivityNew("test", "t")
a.To.Append(bob)
a.To.Append(alice)
a.To.Append(foo)
a.To.Append(bar)
if len(a.To) != 4 {
t.Errorf("%T.To should have exactly 4(four) elements, not %d", a, len(a.To))
}
a.To.Append(bar)
a.To.Append(alice)
a.To.Append(foo)
a.To.Append(bob)
if len(a.To) != 4 {
t.Errorf("%T.To should have exactly 4(eight) elements, not %d", a, len(a.To))
}
a.Recipients()
if len(a.To) != 4 {
t.Errorf("%T.To should have exactly 4(four) elements, not %d", a, len(a.To))
}
b := ActivityNew("t", "test", nil)
b.To.Append(bar)
b.To.Append(alice)
b.To.Append(foo)
b.To.Append(bob)
b.Bto.Append(bar)
b.Bto.Append(alice)
b.Bto.Append(foo)
b.Bto.Append(bob)
b.CC.Append(bar)
b.CC.Append(alice)
b.CC.Append(foo)
b.CC.Append(bob)
b.BCC.Append(bar)
b.BCC.Append(alice)
b.BCC.Append(foo)
b.BCC.Append(bob)
b.Recipients()
if len(b.To) != 4 {
t.Errorf("%T.To should have exactly 4(four) elements, not %d", b, len(b.To))
}
if len(b.Bto) != 0 {
t.Errorf("%T.Bto should have exactly 0(zero) elements, not %d", b, len(b.Bto))
}
if len(b.CC) != 0 {
t.Errorf("%T.CC should have exactly 0(zero) elements, not %d", b, len(b.CC))
}
if len(b.BCC) != 0 {
t.Errorf("%T.BCC should have exactly 0(zero) elements, not %d", b, len(b.BCC))
}
var err error
recIds := make([]ID, 0)
err = checkDedup(b.To, &recIds)
if err != nil {
t.Error(err)
}
err = checkDedup(b.Bto, &recIds)
if err != nil {
t.Error(err)
}
err = checkDedup(b.CC, &recIds)
if err != nil {
t.Error(err)
}
err = checkDedup(b.BCC, &recIds)
if err != nil {
t.Error(err)
}
}
func TestIntransitiveActivity_GetLink(t *testing.T) {
i := IntransitiveActivityNew("test", QuestionType)
if i.GetID() != "test" {
t.Errorf("%T should return an empty %T object. Received %#v", i, i, i)
}
}
func TestIntransitiveActivity_GetObject(t *testing.T) {
i := IntransitiveActivityNew("test", QuestionType)
if i.GetID() != "test" || i.GetType() != QuestionType {
t.Errorf("%T should not return an empty %T object. Received %#v", i, i, i)
}
}
func TestIntransitiveActivity_IsLink(t *testing.T) {
i := IntransitiveActivityNew("test", QuestionType)
if i.IsLink() {
t.Errorf("%T should not respond true to IsLink", i)
}
}
func TestIntransitiveActivity_IsObject(t *testing.T) {
i := IntransitiveActivityNew("test", ActivityType)
if !i.IsObject() {
t.Errorf("%T should respond true to IsObject", i)
}
}
func TestIntransitiveActivity_Recipients(t *testing.T) {
to := PersonNew("bob")
o := ObjectNew(ArticleType)
cc := PersonNew("alice")
o.ID = "something"
c := IntransitiveActivityNew("act", IntransitiveActivityType)
c.To.Append(to)
c.CC.Append(cc)
c.BCC.Append(cc)
c.Recipients()
var err error
recIds := make([]ID, 0)
err = checkDedup(c.To, &recIds)
if err != nil {
t.Error(err)
}
err = checkDedup(c.Bto, &recIds)
if err != nil {
t.Error(err)
}
err = checkDedup(c.CC, &recIds)
if err != nil {
t.Error(err)
}
err = checkDedup(c.BCC, &recIds)
if err != nil {
t.Error(err)
}
}
func TestIntransitiveActivity_GetID(t *testing.T) {
a := IntransitiveActivityNew("test", IntransitiveActivityType)
if a.GetID() != "test" {
t.Errorf("%T should return an empty %T object. Received %#v", a, a.GetID(), a.GetID())
}
}
func TestIntransitiveActivity_GetType(t *testing.T) {
{
a := IntransitiveActivityNew("test", IntransitiveActivityType)
if a.GetType() != IntransitiveActivityType {
t.Errorf("GetType should return %q for %T, received %q", IntransitiveActivityType, a, a.GetType())
}
}
{
a := IntransitiveActivityNew("test", ArriveType)
if a.GetType() != ArriveType {
t.Errorf("GetType should return %q for %T, received %q", ArriveType, a, a.GetType())
}
}
{
a := IntransitiveActivityNew("test", QuestionType)
if a.GetType() != QuestionType {
t.Errorf("GetType should return %q for %T, received %q", QuestionType, a, a.GetType())
}
}
}
func TestToIntransitiveActivity(t *testing.T) {
var it Item
act := IntransitiveActivityNew("test", TravelType)
it = act
a, err := ToIntransitiveActivity(it)
if err != nil {
t.Error(err)
}
if a != act {
t.Errorf("Invalid activity returned by ToActivity #%v", a)
}
ob := ObjectNew(ArticleType)
it = ob
o, err := ToIntransitiveActivity(it)
if err == nil {
t.Errorf("Error returned when calling ToActivity with object should not be nil")
}
if o != nil {
t.Errorf("Invalid return by ToActivity #%v, should have been nil", o)
}
}
func TestIntransitiveActivity_Clean(t *testing.T) {
t.Skipf("TODO")
}
func TestIntransitiveActivity_IsCollection(t *testing.T) {
t.Skipf("TODO")
}
func TestIntransitiveActivity_UnmarshalJSON(t *testing.T) {
t.Skipf("TODO")
}
func TestArriveNew(t *testing.T) {
testValue := ID("test")
a := ArriveNew(testValue)
if a.ID != testValue {
t.Errorf("Activity Id '%v' different than expected '%v'", a.ID, testValue)
}
if a.Type != ArriveType {
t.Errorf("Activity Type '%v' different than expected '%v'", a.Type, ArriveType)
}
}
func TestTravelNew(t *testing.T) {
testValue := ID("test")
a := TravelNew(testValue)
if a.ID != testValue {
t.Errorf("Activity Id '%v' different than expected '%v'", a.ID, testValue)
}
if a.Type != TravelType {
t.Errorf("Activity Type '%v' different than expected '%v'", a.Type, TravelType)
}
}
func TestIntransitiveActivity_Equals(t *testing.T) {
type fields struct {
ID ID
Type ActivityVocabularyType
Name NaturalLanguageValues
Attachment Item
AttributedTo Item
Audience ItemCollection
Content NaturalLanguageValues
Context Item
MediaType MimeType
EndTime time.Time
Generator Item
Icon Item
Image Item
InReplyTo Item
Location Item
Preview Item
Published time.Time
Replies Item
StartTime time.Time
Summary NaturalLanguageValues
Tag ItemCollection
Updated time.Time
URL Item
To ItemCollection
Bto ItemCollection
CC ItemCollection
BCC ItemCollection
Duration time.Duration
Likes Item
Shares Item
Source Source
Actor Item
Target Item
Result Item
Origin Item
Instrument Item
}
tests := []struct {
name string
fields fields
arg Item
want bool
}{
{
name: "equal-empty-intransitive-activity",
fields: fields{},
arg: IntransitiveActivity{},
want: true,
},
{
name: "equal-intransitive-activity-just-id",
fields: fields{ID: "test"},
arg: IntransitiveActivity{ID: "test"},
want: true,
},
{
name: "equal-intransitive-activity-id",
fields: fields{ID: "test", URL: IRI("example.com")},
arg: IntransitiveActivity{ID: "test"},
want: true,
},
{
name: "equal-false-with-id-and-url",
fields: fields{ID: "test"},
arg: IntransitiveActivity{ID: "test", URL: IRI("example.com")},
want: false,
},
{
name: "not a valid intransitive-activity",
fields: fields{ID: "http://example.com"},
arg: Link{ID: "http://example.com"},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := IntransitiveActivity{
ID: tt.fields.ID,
Type: tt.fields.Type,
Name: tt.fields.Name,
Attachment: tt.fields.Attachment,
AttributedTo: tt.fields.AttributedTo,
Audience: tt.fields.Audience,
Content: tt.fields.Content,
Context: tt.fields.Context,
MediaType: tt.fields.MediaType,
EndTime: tt.fields.EndTime,
Generator: tt.fields.Generator,
Icon: tt.fields.Icon,
Image: tt.fields.Image,
InReplyTo: tt.fields.InReplyTo,
Location: tt.fields.Location,
Preview: tt.fields.Preview,
Published: tt.fields.Published,
Replies: tt.fields.Replies,
StartTime: tt.fields.StartTime,
Summary: tt.fields.Summary,
Tag: tt.fields.Tag,
Updated: tt.fields.Updated,
URL: tt.fields.URL,
To: tt.fields.To,
Bto: tt.fields.Bto,
CC: tt.fields.CC,
BCC: tt.fields.BCC,
Duration: tt.fields.Duration,
Likes: tt.fields.Likes,
Shares: tt.fields.Shares,
Source: tt.fields.Source,
Actor: tt.fields.Actor,
Target: tt.fields.Target,
Result: tt.fields.Result,
Origin: tt.fields.Origin,
Instrument: tt.fields.Instrument,
}
if got := a.Equals(tt.arg); got != tt.want {
t.Errorf("Equals() = %v, want %v", got, tt.want)
}
})
}
}