|
5 | 5 | use BookStack\Activity\ActivityType; |
6 | 6 | use BookStack\Facades\Theme; |
7 | 7 | use BookStack\Theming\ThemeEvents; |
| 8 | +use BookStack\Uploads\UserAvatars; |
8 | 9 | use BookStack\Users\Models\Role; |
9 | 10 | use BookStack\Users\Models\User; |
10 | 11 | use GuzzleHttp\Psr7\Response; |
@@ -41,6 +42,7 @@ protected function setUp(): void |
41 | 42 | 'oidc.discover' => false, |
42 | 43 | 'oidc.dump_user_details' => false, |
43 | 44 | 'oidc.additional_scopes' => '', |
| 45 | + 'odic.fetch_avatar' => false, |
44 | 46 | 'oidc.user_to_groups' => false, |
45 | 47 | 'oidc.groups_claim' => 'group', |
46 | 48 | 'oidc.remove_from_groups' => false, |
@@ -457,6 +459,105 @@ public function test_auth_uses_mulitple_display_name_claims_if_configured() |
457 | 459 | ]); |
458 | 460 | } |
459 | 461 |
|
| 462 | + public function test_user_avatar_fetched_from_picture_on_first_login_if_enabled() |
| 463 | + { |
| 464 | + config()->set(['oidc.fetch_avatar' => true]); |
| 465 | + |
| 466 | + $this->runLogin([ |
| 467 | + 'email' => 'avatar@example.com', |
| 468 | + 'picture' => 'https://example.com/my-avatar.jpg', |
| 469 | + ], [ |
| 470 | + new Response(200, ['Content-Type' => 'image/jpeg'], $this->files->jpegImageData()) |
| 471 | + ]); |
| 472 | + |
| 473 | + $user = User::query()->where('email', '=', 'avatar@example.com')->first(); |
| 474 | + $this->assertNotNull($user); |
| 475 | + |
| 476 | + $this->assertTrue($user->avatar()->exists()); |
| 477 | + } |
| 478 | + |
| 479 | + public function test_user_avatar_fetched_for_existing_user_when_no_avatar_already_assigned() |
| 480 | + { |
| 481 | + config()->set(['oidc.fetch_avatar' => true]); |
| 482 | + $editor = $this->users->editor(); |
| 483 | + $editor->external_auth_id = 'benny509'; |
| 484 | + $editor->save(); |
| 485 | + |
| 486 | + $this->assertFalse($editor->avatar()->exists()); |
| 487 | + |
| 488 | + $this->runLogin([ |
| 489 | + 'picture' => 'https://example.com/my-avatar.jpg', |
| 490 | + 'sub' => 'benny509', |
| 491 | + ], [ |
| 492 | + new Response(200, ['Content-Type' => 'image/jpeg'], $this->files->jpegImageData()) |
| 493 | + ]); |
| 494 | + |
| 495 | + $editor->refresh(); |
| 496 | + $this->assertTrue($editor->avatar()->exists()); |
| 497 | + } |
| 498 | + |
| 499 | + public function test_user_avatar_not_fetched_if_image_data_format_unknown() |
| 500 | + { |
| 501 | + config()->set(['oidc.fetch_avatar' => true]); |
| 502 | + |
| 503 | + $this->runLogin([ |
| 504 | + 'email' => 'avatar-format@example.com', |
| 505 | + 'picture' => 'https://example.com/my-avatar.jpg', |
| 506 | + ], [ |
| 507 | + new Response(200, ['Content-Type' => 'image/jpeg'], str_repeat('abc123', 5)) |
| 508 | + ]); |
| 509 | + |
| 510 | + $user = User::query()->where('email', '=', 'avatar-format@example.com')->first(); |
| 511 | + $this->assertNotNull($user); |
| 512 | + |
| 513 | + $this->assertFalse($user->avatar()->exists()); |
| 514 | + } |
| 515 | + |
| 516 | + public function test_user_avatar_not_fetched_when_avatar_already_assigned() |
| 517 | + { |
| 518 | + config()->set(['oidc.fetch_avatar' => true]); |
| 519 | + $editor = $this->users->editor(); |
| 520 | + $editor->external_auth_id = 'benny509'; |
| 521 | + $editor->save(); |
| 522 | + |
| 523 | + $avatars = $this->app->make(UserAvatars::class); |
| 524 | + $originalImageData = $this->files->pngImageData(); |
| 525 | + $avatars->assignToUserFromExistingData($editor, $originalImageData, 'png'); |
| 526 | + |
| 527 | + $this->runLogin([ |
| 528 | + 'picture' => 'https://example.com/my-avatar.jpg', |
| 529 | + 'sub' => 'benny509', |
| 530 | + ], [ |
| 531 | + new Response(200, ['Content-Type' => 'image/jpeg'], $this->files->jpegImageData()) |
| 532 | + ]); |
| 533 | + |
| 534 | + $editor->refresh(); |
| 535 | + $newAvatarData = file_get_contents($this->files->relativeToFullPath($editor->avatar->path)); |
| 536 | + $this->assertEquals($originalImageData, $newAvatarData); |
| 537 | + } |
| 538 | + |
| 539 | + public function test_user_avatar_fetch_follows_up_to_three_redirects() |
| 540 | + { |
| 541 | + config()->set(['oidc.fetch_avatar' => true]); |
| 542 | + |
| 543 | + $logger = $this->withTestLogger(); |
| 544 | + |
| 545 | + $this->runLogin([ |
| 546 | + 'email' => 'avatar@example.com', |
| 547 | + 'picture' => 'https://example.com/my-avatar.jpg', |
| 548 | + ], [ |
| 549 | + new Response(302, ['Location' => 'https://example.com/a']), |
| 550 | + new Response(302, ['Location' => 'https://example.com/b']), |
| 551 | + new Response(302, ['Location' => 'https://example.com/c']), |
| 552 | + new Response(302, ['Location' => 'https://example.com/d']), |
| 553 | + ]); |
| 554 | + |
| 555 | + $user = User::query()->where('email', '=', 'avatar@example.com')->first(); |
| 556 | + $this->assertFalse($user->avatar()->exists()); |
| 557 | + |
| 558 | + $this->assertStringContainsString('"Failed to fetch image, max redirect limit of 3 tries reached. Last fetched URL: https://example.com/c"', $logger->getRecords()[0]->formatted); |
| 559 | + } |
| 560 | + |
460 | 561 | public function test_login_group_sync() |
461 | 562 | { |
462 | 563 | config()->set([ |
|
0 commit comments