55use BookStack \Activity \ActivityType ;
66use BookStack \Facades \Theme ;
77use BookStack \Theming \ThemeEvents ;
8+ use BookStack \Uploads \UserAvatars ;
89use BookStack \Users \Models \Role ;
910use BookStack \Users \Models \User ;
1011use GuzzleHttp \Psr7 \Response ;
@@ -475,6 +476,26 @@ public function test_user_avatar_fetched_from_picture_on_first_login_if_enabled(
475476 $ this ->assertTrue ($ user ->avatar ()->exists ());
476477 }
477478
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+
478499 public function test_user_avatar_not_fetched_if_image_data_format_unknown ()
479500 {
480501 config ()->set (['oidc.fetch_avatar ' => true ]);
@@ -492,11 +513,16 @@ public function test_user_avatar_not_fetched_if_image_data_format_unknown()
492513 $ this ->assertFalse ($ user ->avatar ()->exists ());
493514 }
494515
495- public function test_user_avatar_not_fetched_when_user_already_exists ()
516+ public function test_user_avatar_not_fetched_when_avatar_already_assigned ()
496517 {
497518 config ()->set (['oidc.fetch_avatar ' => true ]);
498519 $ editor = $ this ->users ->editor ();
499520 $ 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 ' );
500526
501527 $ this ->runLogin ([
502528 'picture ' => 'https://example.com/my-avatar.jpg ' ,
@@ -506,7 +532,8 @@ public function test_user_avatar_not_fetched_when_user_already_exists()
506532 ]);
507533
508534 $ editor ->refresh ();
509- $ this ->assertFalse ($ editor ->avatar ()->exists ());
535+ $ newAvatarData = file_get_contents ($ this ->files ->relativeToFullPath ($ editor ->avatar ->path ));
536+ $ this ->assertEquals ($ originalImageData , $ newAvatarData );
510537 }
511538
512539 public function test_login_group_sync ()
0 commit comments