| 1 |
package activitypub |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
func TestPathTyper_Type(t *testing.T) { |
| 9 |
t.Skipf("TODO") |
| 10 |
} |
| 11 |
|
| 12 |
func TestValidActivityCollection(t *testing.T) { |
| 13 |
t.Skipf("TODO") |
| 14 |
} |
| 15 |
|
| 16 |
func TestValidCollection(t *testing.T) { |
| 17 |
t.Skipf("TODO") |
| 18 |
} |
| 19 |
|
| 20 |
func TestValidObjectCollection(t *testing.T) { |
| 21 |
t.Skipf("TODO") |
| 22 |
} |
| 23 |
|
| 24 |
func TestValidCollectionIRI(t *testing.T) { |
| 25 |
t.Skipf("TODO") |
| 26 |
} |
| 27 |
|
| 28 |
func TestSplit(t *testing.T) { |
| 29 |
t.Skipf("TODO") |
| 30 |
} |
| 31 |
|
| 32 |
func TestCollectionTypes_Of(t *testing.T) { |
| 33 |
type args struct { |
| 34 |
o Item |
| 35 |
t CollectionPath |
| 36 |
} |
| 37 |
tests := []struct { |
| 38 |
name string |
| 39 |
args args |
| 40 |
want Item |
| 41 |
}{ |
| 42 |
{ |
| 43 |
name: "nil from nil object", |
| 44 |
args: args{ |
| 45 |
o: nil, |
| 46 |
t: "likes", |
| 47 |
}, |
| 48 |
want: nil, |
| 49 |
}, |
| 50 |
{ |
| 51 |
name: "nil from invalid CollectionPath type", |
| 52 |
args: args{ |
| 53 |
o: Object{ |
| 54 |
Likes: IRI("test"), |
| 55 |
}, |
| 56 |
t: "like", |
| 57 |
}, |
| 58 |
want: nil, |
| 59 |
}, |
| 60 |
{ |
| 61 |
name: "nil from nil CollectionPath type", |
| 62 |
args: args{ |
| 63 |
o: Object{ |
| 64 |
Likes: nil, |
| 65 |
}, |
| 66 |
t: "likes", |
| 67 |
}, |
| 68 |
want: nil, |
| 69 |
}, |
| 70 |
{ |
| 71 |
name: "get likes iri", |
| 72 |
args: args{ |
| 73 |
o: Object{ |
| 74 |
Likes: IRI("test"), |
| 75 |
}, |
| 76 |
t: "likes", |
| 77 |
}, |
| 78 |
want: IRI("test"), |
| 79 |
}, |
| 80 |
} |
| 81 |
|
| 82 |
for _, test := range tests { |
| 83 |
t.Run(test.name, func(t *testing.T) { |
| 84 |
if ob := test.args.t.Of(test.args.o); ob != test.want { |
| 85 |
t.Errorf("Object received %#v is different, expected #%v", ob, test.want) |
| 86 |
} |
| 87 |
}) |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
func TestCollectionType_IRI(t *testing.T) { |
| 92 |
type args struct { |
| 93 |
o Item |
| 94 |
t CollectionPath |
| 95 |
} |
| 96 |
tests := []struct { |
| 97 |
name string |
| 98 |
args args |
| 99 |
want IRI |
| 100 |
}{ |
| 101 |
{ |
| 102 |
name: "just path from nil object", |
| 103 |
args: args{ |
| 104 |
o: nil, |
| 105 |
t: "likes", |
| 106 |
}, |
| 107 |
want: IRI("/likes"), |
| 108 |
}, |
| 109 |
{ |
| 110 |
name: "emptyIRI from invalid CollectionPath type", |
| 111 |
args: args{ |
| 112 |
o: Object{ |
| 113 |
Likes: IRI("test"), |
| 114 |
}, |
| 115 |
t: "like", |
| 116 |
}, |
| 117 |
want: "/like", |
| 118 |
}, |
| 119 |
{ |
| 120 |
name: "just path from object without ID", |
| 121 |
args: args{ |
| 122 |
o: Object{}, |
| 123 |
t: "likes", |
| 124 |
}, |
| 125 |
want: IRI("/likes"), |
| 126 |
}, |
| 127 |
{ |
| 128 |
name: "likes iri on object", |
| 129 |
args: args{ |
| 130 |
o: Object{ |
| 131 |
ID: "http://example.com", |
| 132 |
Likes: IRI("test"), |
| 133 |
}, |
| 134 |
t: "likes", |
| 135 |
}, |
| 136 |
want: IRI("test"), |
| 137 |
}, |
| 138 |
} |
| 139 |
|
| 140 |
for _, test := range tests { |
| 141 |
t.Run(test.name, func(t *testing.T) { |
| 142 |
if ob := test.args.t.IRI(test.args.o); ob != test.want { |
| 143 |
t.Errorf("IRI received %q is different, expected %q", ob, test.want) |
| 144 |
} |
| 145 |
}) |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
func TestCollectionType_OfActor(t *testing.T) { |
| 150 |
t.Skipf("TODO") |
| 151 |
} |
| 152 |
|
| 153 |
func TestCollectionTypes_Contains(t *testing.T) { |
| 154 |
t.Skipf("TODO") |
| 155 |
} |
| 156 |
|
| 157 |
func TestIRIf(t *testing.T) { |
| 158 |
type args struct { |
| 159 |
i IRI |
| 160 |
t CollectionPath |
| 161 |
} |
| 162 |
tests := []struct { |
| 163 |
name string |
| 164 |
args args |
| 165 |
want IRI |
| 166 |
}{ |
| 167 |
{ |
| 168 |
name: "nil iri", |
| 169 |
args: args{ |
| 170 |
i: Object{}.ID, |
| 171 |
t: "inbox", |
| 172 |
}, |
| 173 |
want: "/inbox", |
| 174 |
}, |
| 175 |
{ |
| 176 |
name: "empty iri", |
| 177 |
args: args{ |
| 178 |
i: "", |
| 179 |
t: "inbox", |
| 180 |
}, |
| 181 |
want: "/inbox", |
| 182 |
}, |
| 183 |
{ |
| 184 |
name: "plain concat", |
| 185 |
args: args{ |
| 186 |
i: "https://example.com", |
| 187 |
t: "inbox", |
| 188 |
}, |
| 189 |
want: "https://example.com/inbox", |
| 190 |
}, |
| 191 |
{ |
| 192 |
name: "strip root from iri", |
| 193 |
args: args{ |
| 194 |
i: "https://example.com/", |
| 195 |
t: "inbox", |
| 196 |
}, |
| 197 |
want: "https://example.com/inbox", |
| 198 |
}, |
| 199 |
{ |
| 200 |
name: "invalid iri", |
| 201 |
args: args{ |
| 202 |
i: "example.com", |
| 203 |
t: "test", |
| 204 |
}, |
| 205 |
want: "example.com/test", |
| 206 |
}, |
| 207 |
} |
| 208 |
for _, tt := range tests { |
| 209 |
t.Run(tt.name, func(t *testing.T) { |
| 210 |
if got := IRIf(tt.args.i, tt.args.t); got != tt.want { |
| 211 |
t.Errorf("IRIf() = %v, want %v", got, tt.want) |
| 212 |
} |
| 213 |
}) |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
func TestCollectionType_AddTo(t *testing.T) { |
| 218 |
type args struct { |
| 219 |
i Item |
| 220 |
} |
| 221 |
var i Item |
| 222 |
var o *Object |
| 223 |
tests := []struct { |
| 224 |
name string |
| 225 |
t CollectionPath |
| 226 |
args args |
| 227 |
want IRI |
| 228 |
want1 bool |
| 229 |
}{ |
| 230 |
{ |
| 231 |
name: "simple", |
| 232 |
t: "test", |
| 233 |
args: args{ |
| 234 |
i: &Object{ID: "http://example.com/addTo"}, |
| 235 |
}, |
| 236 |
want: "http://example.com/addTo/test", |
| 237 |
want1: false, |
| 238 |
}, |
| 239 |
{ |
| 240 |
name: "on-nil-item", |
| 241 |
t: "test", |
| 242 |
args: args{ |
| 243 |
i: i, |
| 244 |
}, |
| 245 |
want: NilIRI, |
| 246 |
want1: false, |
| 247 |
}, |
| 248 |
{ |
| 249 |
name: "on-nil", |
| 250 |
t: "test", |
| 251 |
args: args{ |
| 252 |
i: nil, |
| 253 |
}, |
| 254 |
want: NilIRI, |
| 255 |
want1: false, |
| 256 |
}, |
| 257 |
{ |
| 258 |
name: "on-nil-object", |
| 259 |
t: "test", |
| 260 |
args: args{ |
| 261 |
i: o, |
| 262 |
}, |
| 263 |
want: NilIRI, |
| 264 |
want1: false, |
| 265 |
}, |
| 266 |
{ |
| 267 |
name: "on-nil-item", |
| 268 |
t: "test", |
| 269 |
args: args{ |
| 270 |
i: i, |
| 271 |
}, |
| 272 |
want: NilIRI, |
| 273 |
want1: false, |
| 274 |
}, |
| 275 |
} |
| 276 |
for _, tt := range tests { |
| 277 |
t.Run(tt.name, func(t *testing.T) { |
| 278 |
got, got1 := tt.t.AddTo(tt.args.i) |
| 279 |
if got != tt.want { |
| 280 |
t.Errorf("AddTo() got = %v, want %v", got, tt.want) |
| 281 |
} |
| 282 |
if got1 != tt.want1 { |
| 283 |
t.Errorf("AddTo() got1 = %v, want %v", got1, tt.want1) |
| 284 |
} |
| 285 |
}) |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
func TestCollectionPaths_Split(t *testing.T) { |
| 290 |
tests := []struct { |
| 291 |
name string |
| 292 |
t CollectionPaths |
| 293 |
given IRI |
| 294 |
maybeActor IRI |
| 295 |
maybeCol CollectionPath |
| 296 |
}{ |
| 297 |
{ |
| 298 |
name: "empty", |
| 299 |
t: nil, |
| 300 |
given: "", |
| 301 |
maybeActor: "", |
| 302 |
maybeCol: "", |
| 303 |
}, |
| 304 |
{ |
| 305 |
name: "nil with example.com", |
| 306 |
t: nil, |
| 307 |
given: "example.com", |
| 308 |
maybeActor: "example.com", |
| 309 |
maybeCol: "", |
| 310 |
}, |
| 311 |
{ |
| 312 |
name: "nil with https://example.com", |
| 313 |
t: nil, |
| 314 |
given: "https://example.com/", |
| 315 |
maybeActor: "https://example.com", |
| 316 |
maybeCol: Unknown, |
| 317 |
}, |
| 318 |
{ |
| 319 |
name: "outbox with https://example.com/outbox", |
| 320 |
t: CollectionPaths{Outbox}, |
| 321 |
given: "https://example.com/outbox", |
| 322 |
maybeActor: "https://example.com", |
| 323 |
maybeCol: Outbox, |
| 324 |
}, |
| 325 |
{ |
| 326 |
name: "{outbox,inbox} with https://example.com/inbox", |
| 327 |
t: CollectionPaths{Outbox, Inbox}, |
| 328 |
given: "https://example.com/inbox", |
| 329 |
maybeActor: "https://example.com", |
| 330 |
maybeCol: Inbox, |
| 331 |
}, |
| 332 |
{ |
| 333 |
|
| 334 |
name: "outbox with https://example.com/inbox", |
| 335 |
t: CollectionPaths{Outbox}, |
| 336 |
given: "https://example.com/inbox", |
| 337 |
maybeActor: "https://example.com", |
| 338 |
maybeCol: Unknown, |
| 339 |
}, |
| 340 |
{ |
| 341 |
name: "invalid url", |
| 342 |
t: CollectionPaths{Inbox}, |
| 343 |
given: "127.0.0.1:666/inbox", |
| 344 |
maybeActor: "127.0.0.1:666", |
| 345 |
maybeCol: Inbox, |
| 346 |
}, |
| 347 |
{ |
| 348 |
name: "invalid url - collection doesn't match", |
| 349 |
t: CollectionPaths{Outbox}, |
| 350 |
given: "127.0.0.1:666/inbox", |
| 351 |
maybeActor: "127.0.0.1:666/inbox", |
| 352 |
maybeCol: Unknown, |
| 353 |
}, |
| 354 |
} |
| 355 |
for _, tt := range tests { |
| 356 |
t.Run(tt.name, func(t *testing.T) { |
| 357 |
ma, mc := tt.t.Split(tt.given) |
| 358 |
if ma != tt.maybeActor { |
| 359 |
t.Errorf("Split() got Actor = %q, want %q", ma, tt.maybeActor) |
| 360 |
} |
| 361 |
if mc != tt.maybeCol { |
| 362 |
t.Errorf("Split() got Colletion Path = %q, want %q", mc, tt.maybeCol) |
| 363 |
} |
| 364 |
}) |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
func TestCollectionPath_Of(t *testing.T) { |
| 369 |
tests := []struct { |
| 370 |
name string |
| 371 |
t CollectionPath |
| 372 |
arg Item |
| 373 |
want Item |
| 374 |
}{ |
| 375 |
{ |
| 376 |
name: "all-nil", |
| 377 |
t: "", |
| 378 |
}, |
| 379 |
{ |
| 380 |
name: "inbox-nil", |
| 381 |
t: Inbox, |
| 382 |
}, |
| 383 |
{ |
| 384 |
name: "outbox-nil", |
| 385 |
t: Outbox, |
| 386 |
}, |
| 387 |
{ |
| 388 |
name: "followers-nil", |
| 389 |
t: Followers, |
| 390 |
}, |
| 391 |
{ |
| 392 |
name: "following-nil", |
| 393 |
t: Following, |
| 394 |
}, |
| 395 |
{ |
| 396 |
name: "liked-nil", |
| 397 |
t: Liked, |
| 398 |
}, |
| 399 |
{ |
| 400 |
name: "likes-nil", |
| 401 |
t: Likes, |
| 402 |
}, |
| 403 |
{ |
| 404 |
name: "shares-nil", |
| 405 |
t: Shares, |
| 406 |
}, |
| 407 |
{ |
| 408 |
name: "replies-nil", |
| 409 |
t: Replies, |
| 410 |
}, |
| 411 |
{ |
| 412 |
name: "inbox-empty", |
| 413 |
t: Inbox, |
| 414 |
arg: &Actor{}, |
| 415 |
}, |
| 416 |
{ |
| 417 |
name: "outbox-empty", |
| 418 |
t: Outbox, |
| 419 |
arg: &Actor{}, |
| 420 |
}, |
| 421 |
{ |
| 422 |
name: "followers-empty", |
| 423 |
t: Followers, |
| 424 |
arg: &Actor{}, |
| 425 |
}, |
| 426 |
{ |
| 427 |
name: "following-empty", |
| 428 |
t: Following, |
| 429 |
arg: &Actor{}, |
| 430 |
}, |
| 431 |
{ |
| 432 |
name: "liked-empty", |
| 433 |
t: Liked, |
| 434 |
arg: &Actor{}, |
| 435 |
}, |
| 436 |
{ |
| 437 |
name: "likes-empty", |
| 438 |
t: Likes, |
| 439 |
arg: &Object{}, |
| 440 |
}, |
| 441 |
{ |
| 442 |
name: "shares-empty", |
| 443 |
t: Shares, |
| 444 |
arg: &Object{}, |
| 445 |
}, |
| 446 |
{ |
| 447 |
name: "replies-empty", |
| 448 |
t: Replies, |
| 449 |
arg: &Object{}, |
| 450 |
}, |
| 451 |
|
| 452 |
{ |
| 453 |
name: "inbox", |
| 454 |
t: Inbox, |
| 455 |
arg: &Actor{ |
| 456 |
Type: PersonType, |
| 457 |
Inbox: IRI("https://example.com/inbox"), |
| 458 |
}, |
| 459 |
want: IRI("https://example.com/inbox"), |
| 460 |
}, |
| 461 |
{ |
| 462 |
name: "outbox", |
| 463 |
t: Outbox, |
| 464 |
arg: &Actor{ |
| 465 |
Type: PersonType, |
| 466 |
Outbox: IRI("https://example.com/outbox"), |
| 467 |
}, |
| 468 |
want: IRI("https://example.com/outbox"), |
| 469 |
}, |
| 470 |
{ |
| 471 |
name: "followers", |
| 472 |
t: Followers, |
| 473 |
arg: &Actor{ |
| 474 |
Type: GroupType, |
| 475 |
Followers: IRI("https://example.com/c132-333"), |
| 476 |
}, |
| 477 |
want: IRI("https://example.com/c132-333"), |
| 478 |
}, |
| 479 |
{ |
| 480 |
name: "following", |
| 481 |
t: Following, |
| 482 |
arg: &Actor{ |
| 483 |
Type: GroupType, |
| 484 |
Following: IRI("https://example.com/c666-333"), |
| 485 |
}, |
| 486 |
want: IRI("https://example.com/c666-333"), |
| 487 |
}, |
| 488 |
{ |
| 489 |
name: "liked", |
| 490 |
t: Liked, |
| 491 |
arg: &Actor{ |
| 492 |
Type: ApplicationType, |
| 493 |
Liked: IRI("https://example.com/l666"), |
| 494 |
}, |
| 495 |
want: IRI("https://example.com/l666"), |
| 496 |
}, |
| 497 |
{ |
| 498 |
name: "likes", |
| 499 |
t: Likes, |
| 500 |
arg: &Object{ |
| 501 |
Type: NoteType, |
| 502 |
Likes: IRI("https://example.com/l166"), |
| 503 |
}, |
| 504 |
want: IRI("https://example.com/l166"), |
| 505 |
}, |
| 506 |
{ |
| 507 |
name: "shares", |
| 508 |
t: Shares, |
| 509 |
arg: &Object{ |
| 510 |
Type: PageType, |
| 511 |
Shares: IRI("https://example.com/s266"), |
| 512 |
}, |
| 513 |
want: IRI("https://example.com/s266"), |
| 514 |
}, |
| 515 |
{ |
| 516 |
name: "replies", |
| 517 |
t: Replies, |
| 518 |
arg: &Object{ |
| 519 |
Type: ArticleType, |
| 520 |
Replies: IRI("https://example.com/r466"), |
| 521 |
}, |
| 522 |
want: IRI("https://example.com/r466"), |
| 523 |
}, |
| 524 |
} |
| 525 |
for _, tt := range tests { |
| 526 |
t.Run(tt.name, func(t *testing.T) { |
| 527 |
if got := tt.t.Of(tt.arg); !reflect.DeepEqual(got, tt.want) { |
| 528 |
t.Errorf("Of() = %v, want %v", got, tt.want) |
| 529 |
} |
| 530 |
}) |
| 531 |
} |
| 532 |
} |