Debian Codegraph @

actor_test.go

raw symbols SCIP
1 package activitypub
2
3 import (
4 "fmt"
5 "reflect"
6 "testing"
7 "time"
8
9 "github.com/google/go-cmp/cmp"
10 )
11
12 func TestActorNew(t *testing.T) {
13 testValue := ID("test")
14 testType := ApplicationType
15
16 o := ActorNew(testValue, testType)
17
18 if o.ID != testValue {
19 t.Errorf("APObject Id '%v' different than expected '%v'", o.ID, testValue)
20 }
21 if !testType.Match(o.GetType()) {
22 t.Errorf("APObject Type '%v' different than expected '%v'", o.GetType(), testType)
23 }
24
25 n := ActorNew(testValue, "")
26 if n.ID != testValue {
27 t.Errorf("APObject Id '%v' different than expected '%v'", n.ID, testValue)
28 }
29 if !cmp.Equal(n.GetType(), ActorType) {
30 t.Errorf("APObject Type '%v' different than expected '%v'", n.GetType(), ActorType)
31 }
32 }
33
34 func TestPersonNew(t *testing.T) {
35 testValue := ID("test")
36
37 o := PersonNew(testValue)
38 if o.ID != testValue {
39 t.Errorf("APObject Id '%v' different than expected '%v'", o.ID, testValue)
40 }
41 if !cmp.Equal(o.GetType(), PersonType) {
42 t.Errorf("APObject Type '%v' different than expected '%v'", o.Type, PersonType)
43 }
44 }
45
46 func TestApplicationNew(t *testing.T) {
47 testValue := ID("test")
48
49 o := ApplicationNew(testValue)
50 if o.ID != testValue {
51 t.Errorf("APObject Id '%v' different than expected '%v'", o.ID, testValue)
52 }
53 if !cmp.Equal(o.GetType(), ApplicationType) {
54 t.Errorf("APObject Type '%v' different than expected '%v'", o.Type, ApplicationType)
55 }
56 }
57
58 func TestGroupNew(t *testing.T) {
59 testValue := ID("test")
60
61 o := GroupNew(testValue)
62 if o.ID != testValue {
63 t.Errorf("APObject Id '%v' different than expected '%v'", o.ID, testValue)
64 }
65 if !cmp.Equal(o.GetType(), GroupType) {
66 t.Errorf("APObject Type '%v' different than expected '%v'", o.Type, GroupType)
67 }
68 }
69
70 func TestOrganizationNew(t *testing.T) {
71 testValue := ID("test")
72
73 o := OrganizationNew(testValue)
74 if o.ID != testValue {
75 t.Errorf("APObject Id '%v' different than expected '%v'", o.ID, testValue)
76 }
77 if !cmp.Equal(o.GetType(), OrganizationType) {
78 t.Errorf("APObject Type '%v' different than expected '%v'", o.Type, OrganizationType)
79 }
80 }
81
82 func TestServiceNew(t *testing.T) {
83 testValue := ID("test")
84
85 o := ServiceNew(testValue)
86 if o.ID != testValue {
87 t.Errorf("APObject Id '%v' different than expected '%v'", o.ID, testValue)
88 }
89 if !cmp.Equal(o.GetType(), ServiceType) {
90 t.Errorf("APObject Type '%v' different than expected '%v'", o.Type, ServiceType)
91 }
92 }
93
94 func TestActor_IsLink(t *testing.T) {
95 m := ActorNew("test", ActorType)
96 if m.IsLink() {
97 t.Errorf("%#v should not be a valid Link", m.Type)
98 }
99 }
100
101 func TestActor_IsObject(t *testing.T) {
102 m := ActorNew("test", ActorType)
103 if !m.IsObject() {
104 t.Errorf("%#v should be a valid object", m.Type)
105 }
106 }
107
108 func TestActor_Object(t *testing.T) {
109 m := ActorNew("test", ActorType)
110 if reflect.DeepEqual(ID(""), m.GetID()) {
111 t.Errorf("%#v should not be an empty activity pub object", m.GetID())
112 }
113 }
114
115 func TestActor_Type(t *testing.T) {
116 m := ActorNew("test", ActorType)
117 if !cmp.Equal(m.GetType(), ActorType) {
118 t.Errorf("%#v should be an empty Link object", m.GetType())
119 }
120 }
121
122 func TestPerson_IsLink(t *testing.T) {
123 m := PersonNew("test")
124 if m.IsLink() {
125 t.Errorf("%T should not be a valid Link", m)
126 }
127 }
128
129 func TestPerson_IsObject(t *testing.T) {
130 m := PersonNew("test")
131 if !m.IsObject() {
132 t.Errorf("%T should be a valid object", m)
133 }
134 }
135
136 func TestActor_UnmarshalJSON(t *testing.T) {
137 t.Skipf("TODO")
138 }
139
140 func TestActor_GetActor(t *testing.T) {
141 t.Skipf("TODO")
142 }
143
144 func TestActor_GetID(t *testing.T) {
145 t.Skipf("TODO")
146 }
147
148 func TestActor_GetLink(t *testing.T) {
149 t.Skipf("TODO")
150 }
151
152 func TestActor_GetType(t *testing.T) {
153 t.Skipf("TODO")
154 }
155
156 func TestApplication_GetActor(t *testing.T) {
157 t.Skipf("TODO")
158 }
159
160 func TestApplication_GetID(t *testing.T) {
161 t.Skipf("TODO")
162 }
163
164 func TestApplication_GetLink(t *testing.T) {
165 t.Skipf("TODO")
166 }
167
168 func TestApplication_GetType(t *testing.T) {
169 t.Skipf("TODO")
170 }
171
172 func TestApplication_IsLink(t *testing.T) {
173 t.Skipf("TODO")
174 }
175
176 func TestApplication_IsObject(t *testing.T) {
177 t.Skipf("TODO")
178 }
179
180 func TestGroup_GetActor(t *testing.T) {
181 t.Skipf("TODO")
182 }
183
184 func TestGroup_GetID(t *testing.T) {
185 t.Skipf("TODO")
186 }
187
188 func TestGroup_GetLink(t *testing.T) {
189 t.Skipf("TODO")
190 }
191
192 func TestGroup_GetType(t *testing.T) {
193 t.Skipf("TODO")
194 }
195
196 func TestGroup_IsLink(t *testing.T) {
197 t.Skipf("TODO")
198 }
199
200 func TestGroup_IsObject(t *testing.T) {
201 t.Skipf("TODO")
202 }
203
204 func TestOrganization_GetActor(t *testing.T) {
205 t.Skipf("TODO")
206 }
207
208 func TestOrganization_GetID(t *testing.T) {
209 t.Skipf("TODO")
210 }
211
212 func TestOrganization_GetLink(t *testing.T) {
213 t.Skipf("TODO")
214 }
215
216 func TestOrganization_GetType(t *testing.T) {
217 t.Skipf("TODO")
218 }
219
220 func TestOrganization_IsLink(t *testing.T) {
221 t.Skipf("TODO")
222 }
223
224 func TestOrganization_IsObject(t *testing.T) {
225 t.Skipf("TODO")
226 }
227
228 func TestPerson_GetActor(t *testing.T) {
229 t.Skipf("TODO")
230 }
231
232 func TestPerson_GetID(t *testing.T) {
233 t.Skipf("TODO")
234 }
235
236 func TestPerson_GetLink(t *testing.T) {
237 t.Skipf("TODO")
238 }
239
240 func TestPerson_GetType(t *testing.T) {
241 t.Skipf("TODO")
242 }
243
244 func validateEmptyPerson(p Person, t *testing.T) {
245 if p.ID != "" {
246 t.Errorf("Unmarshaled object %T should have empty ID, received %q", p, p.ID)
247 }
248 if HasTypes(p) {
249 t.Errorf("Unmarshaled object "+
250 "%T should have empty Type, received %s", p, p.GetType())
251 }
252 if p.AttributedTo != nil {
253 t.Errorf("Unmarshaled object %T should have empty AttributedTo, received %q", p, p.AttributedTo)
254 }
255 if len(p.Name) != 0 {
256 t.Errorf("Unmarshaled object %T should have empty Name, received %q", p, p.Name)
257 }
258 if len(p.Summary) != 0 {
259 t.Errorf("Unmarshaled object %T should have empty Summary, received %q", p, p.Summary)
260 }
261 if len(p.Content) != 0 {
262 t.Errorf("Unmarshaled object %T should have empty Content, received %q", p, p.Content)
263 }
264 if p.URL != nil {
265 t.Errorf("Unmarshaled object %T should have empty URL, received %v", p, p.URL)
266 }
267 if !p.Published.IsZero() {
268 t.Errorf("Unmarshaled object %T should have empty Published, received %q", p, p.Published)
269 }
270 if !p.StartTime.IsZero() {
271 t.Errorf("Unmarshaled object %T should have empty StartTime, received %q", p, p.StartTime)
272 }
273 if !p.Updated.IsZero() {
274 t.Errorf("Unmarshaled object %T should have empty Updated, received %q", p, p.Updated)
275 }
276 }
277
278 func TestPerson_UnmarshalJSON(t *testing.T) {
279 p := Person{}
280
281 dataEmpty := []byte("{}")
282 _ = p.UnmarshalJSON(dataEmpty)
283 validateEmptyPerson(p, t)
284 }
285
286 func TestApplication_UnmarshalJSON(t *testing.T) {
287 a := Application{}
288
289 dataEmpty := []byte("{}")
290 a.UnmarshalJSON(dataEmpty)
291 validateEmptyPerson(Person(a), t)
292 }
293
294 func TestGroup_UnmarshalJSON(t *testing.T) {
295 g := Group{}
296
297 dataEmpty := []byte("{}")
298 g.UnmarshalJSON(dataEmpty)
299 validateEmptyPerson(Person(g), t)
300 }
301
302 func TestOrganization_UnmarshalJSON(t *testing.T) {
303 o := Organization{}
304
305 dataEmpty := []byte("{}")
306 o.UnmarshalJSON(dataEmpty)
307 validateEmptyPerson(Person(o), t)
308 }
309
310 func TestService_UnmarshalJSON(t *testing.T) {
311 s := Service{}
312
313 dataEmpty := []byte("{}")
314 s.UnmarshalJSON(dataEmpty)
315 validateEmptyPerson(Person(s), t)
316 }
317
318 func TestService_GetActor(t *testing.T) {
319 t.Skipf("TODO")
320 }
321
322 func TestService_GetID(t *testing.T) {
323 t.Skipf("TODO")
324 }
325
326 func TestService_GetLink(t *testing.T) {
327 t.Skipf("TODO")
328 }
329
330 func TestService_GetType(t *testing.T) {
331 t.Skipf("TODO")
332 }
333
334 func TestService_IsLink(t *testing.T) {
335 t.Skipf("TODO")
336 }
337
338 func TestService_IsObject(t *testing.T) {
339 t.Skipf("TODO")
340 }
341
342 func TestToPerson(t *testing.T) {
343 t.Skipf("TODO")
344 }
345
346 func TestEndpoints_UnmarshalJSON(t *testing.T) {
347 t.Skipf("TODO")
348 }
349
350 func TestActor_Clean(t *testing.T) {
351 t.Skipf("TODO")
352 }
353
354 func TestToActor(t *testing.T) {
355 tests := []struct {
356 name string
357 it LinkOrIRI
358 want *Actor
359 wantErr error
360 }{
361 {
362 name: "empty",
363 },
364 {
365 name: "Valid Actor",
366 it: Actor{ID: "test", Type: UpdateType},
367 want: &Actor{ID: "test", Type: UpdateType},
368 },
369 {
370 name: "Valid *Actor",
371 it: ActorNew("test", CreateType),
372 want: ActorNew("test", CreateType),
373 },
374 {
375 name: "IRI",
376 it: IRI("https://example.com"),
377 wantErr: ErrorInvalidType[Actor](IRI("")),
378 },
379 {
380 name: "IRIs",
381 it: IRIs{IRI("https://example.com")},
382 wantErr: ErrorInvalidType[Actor](IRIs{}),
383 },
384 {
385 name: "ItemCollection",
386 it: ItemCollection{},
387 wantErr: ErrorInvalidType[Actor](ItemCollection{}),
388 },
389 {
390 name: "Object",
391 it: &Object{ID: "test", Type: ArticleType},
392 wantErr: ErrorInvalidType[Actor](&Object{}),
393 },
394 {
395 name: "Activity",
396 it: &Activity{ID: "test", Type: CreateType},
397 wantErr: ErrorInvalidType[Actor](&Activity{}),
398 },
399 {
400 name: "IntransitiveActivity",
401 it: &IntransitiveActivity{ID: "test", Type: ArriveType},
402 wantErr: ErrorInvalidType[Actor](&IntransitiveActivity{}),
403 },
404 }
405 for _, tt := range tests {
406 t.Run(tt.name, func(t *testing.T) {
407 got, err := ToActor(tt.it)
408 if !cmp.Equal(err, tt.wantErr, EquateWeakErrors) {
409 t.Errorf("ToActor() error = %v, wantErr %v", err, tt.wantErr)
410 return
411 }
412 if !cmp.Equal(got, tt.want) {
413 t.Errorf("ToActor(): %s got = %s", tt.name, cmp.Diff(tt.want, got))
414 }
415 })
416 }
417 }
418
419 func TestActor_IsCollection(t *testing.T) {
420 t.Skipf("TODO")
421 }
422
423 func TestActor_Recipients(t *testing.T) {
424 t.Skipf("TODO")
425 }
426
427 func TestPublicKey_UnmarshalJSON(t *testing.T) {
428 t.Skipf("TODO")
429 }
430
431 func TestActor_MarshalJSON(t *testing.T) {
432 t.Skipf("TODO")
433 }
434
435 func TestEndpoints_MarshalJSON(t *testing.T) {
436 t.Skipf("TODO")
437 }
438
439 func TestPublicKey_MarshalJSON(t *testing.T) {
440 t.Skipf("TODO")
441 }
442
443 func assertPersonWithTesting(fn canErrorFunc, expected Item) WithActorFn {
444 return func(p *Person) error {
445 if !assertDeepEquals(fn, p, expected) {
446 return fmt.Errorf("not equal")
447 }
448 return nil
449 }
450 }
451
452 func TestOnActor(t *testing.T) {
453 testPerson := Actor{
454 ID: "https://example.com",
455 }
456 type args struct {
457 it Item
458 fn func(canErrorFunc, Item) WithActorFn
459 }
460 tests := []struct {
461 name string
462 args args
463 expected Item
464 wantErr bool
465 }{
466 {
467 name: "single",
468 args: args{testPerson, assertPersonWithTesting},
469 expected: &testPerson,
470 wantErr: false,
471 },
472 {
473 name: "single fails",
474 args: args{Person{ID: "https://not-equals"}, assertPersonWithTesting},
475 expected: &testPerson,
476 wantErr: true,
477 },
478 {
479 name: "collectionOfPersons",
480 args: args{ItemCollection{testPerson, testPerson}, assertPersonWithTesting},
481 expected: &testPerson,
482 wantErr: false,
483 },
484 {
485 name: "collectionOfPersons fails",
486 args: args{ItemCollection{testPerson, Person{ID: "https://not-equals"}}, assertPersonWithTesting},
487 expected: &testPerson,
488 wantErr: true,
489 },
490 }
491 for _, tt := range tests {
492 var logFn canErrorFunc
493 if tt.wantErr {
494 logFn = t.Logf
495 } else {
496 logFn = t.Errorf
497 }
498 t.Run(tt.name, func(t *testing.T) {
499 if err := OnActor(tt.args.it, tt.args.fn(logFn, tt.expected)); (err != nil) != tt.wantErr {
500 t.Errorf("OnPerson() error = %v, wantErr %v", err, tt.wantErr)
501 }
502 })
503 }
504 }
505
506 func TestActor_Equals(t *testing.T) {
507 type fields struct {
508 ID ID
509 Type Typer
510 Name NaturalLanguageValues
511 Attachment Item
512 AttributedTo Item
513 Audience ItemCollection
514 Content NaturalLanguageValues
515 Context Item
516 MediaType MimeType
517 EndTime time.Time
518 Generator Item
519 Icon Item
520 Image Item
521 InReplyTo Item
522 Location Item
523 Preview Item
524 Published time.Time
525 Replies Item
526 StartTime time.Time
527 Summary NaturalLanguageValues
528 Tag ItemCollection
529 Updated time.Time
530 URL Item
531 To ItemCollection
532 Bto ItemCollection
533 CC ItemCollection
534 BCC ItemCollection
535 Duration time.Duration
536 Likes Item
537 Shares Item
538 Source Source
539 Inbox Item
540 Outbox Item
541 Following Item
542 Followers Item
543 Liked Item
544 PreferredUsername NaturalLanguageValues
545 Endpoints *Endpoints
546 Streams ItemCollection
547 PublicKey PublicKey
548 }
549 tests := []struct {
550 name string
551 fields fields
552 arg Item
553 want bool
554 }{
555 {
556 name: "equal-empty-actor",
557 fields: fields{},
558 arg: Actor{},
559 want: true,
560 },
561 {
562 name: "equal-actor-just-id",
563 fields: fields{ID: "test"},
564 arg: Actor{ID: "test"},
565 want: true,
566 },
567 {
568 name: "equal-actor-id",
569 fields: fields{ID: "test", URL: IRI("example.com")},
570 arg: Actor{ID: "test"},
571 want: true,
572 },
573 {
574 name: "equal-false-with-id-and-url",
575 fields: fields{ID: "test"},
576 arg: Actor{ID: "test", URL: IRI("example.com")},
577 want: false,
578 },
579 {
580 name: "not a valid actor",
581 fields: fields{ID: "http://example.com"},
582 arg: Activity{ID: "http://example.com"},
583 want: false,
584 },
585 }
586 for _, tt := range tests {
587 t.Run(tt.name, func(t *testing.T) {
588 a := Actor{
589 ID: tt.fields.ID,
590 Type: tt.fields.Type,
591 Name: tt.fields.Name,
592 Attachment: tt.fields.Attachment,
593 AttributedTo: tt.fields.AttributedTo,
594 Audience: tt.fields.Audience,
595 Content: tt.fields.Content,
596 Context: tt.fields.Context,
597 MediaType: tt.fields.MediaType,
598 EndTime: tt.fields.EndTime,
599 Generator: tt.fields.Generator,
600 Icon: tt.fields.Icon,
601 Image: tt.fields.Image,
602 InReplyTo: tt.fields.InReplyTo,
603 Location: tt.fields.Location,
604 Preview: tt.fields.Preview,
605 Published: tt.fields.Published,
606 Replies: tt.fields.Replies,
607 StartTime: tt.fields.StartTime,
608 Summary: tt.fields.Summary,
609 Tag: tt.fields.Tag,
610 Updated: tt.fields.Updated,
611 URL: tt.fields.URL,
612 To: tt.fields.To,
613 Bto: tt.fields.Bto,
614 CC: tt.fields.CC,
615 BCC: tt.fields.BCC,
616 Duration: tt.fields.Duration,
617 Likes: tt.fields.Likes,
618 Shares: tt.fields.Shares,
619 Source: tt.fields.Source,
620 Inbox: tt.fields.Inbox,
621 Outbox: tt.fields.Outbox,
622 Following: tt.fields.Following,
623 Followers: tt.fields.Followers,
624 Liked: tt.fields.Liked,
625 PreferredUsername: tt.fields.PreferredUsername,
626 Endpoints: tt.fields.Endpoints,
627 Streams: tt.fields.Streams,
628 PublicKey: tt.fields.PublicKey,
629 }
630 if got := a.Equals(tt.arg); got != tt.want {
631 t.Errorf("Equal() = %v, want %v", got, tt.want)
632 }
633 })
634 }
635 }