]> BookStack Code Mirror - bookstack/blobdiff - tests/Uploads/AvatarTest.php
Fixed OIDC Logout
[bookstack] / tests / Uploads / AvatarTest.php
index 3e27e53446b9046cea4a429ec7241f59b73ea3be..363c1fa95420b858d42980f27e05d8e38e10a07a 100644 (file)
@@ -2,15 +2,14 @@
 
 namespace Tests\Uploads;
 
-use BookStack\Auth\User;
 use BookStack\Exceptions\HttpFetchException;
 use BookStack\Uploads\HttpFetcher;
+use BookStack\Uploads\UserAvatars;
+use BookStack\Users\Models\User;
 use Tests\TestCase;
 
 class AvatarTest extends TestCase
 {
-    use UsesImages;
-
     protected function createUserRequest($user): User
     {
         $this->asAdmin()->post('/settings/users/create', [
@@ -29,12 +28,12 @@ class AvatarTest extends TestCase
 
         $http->shouldReceive('fetch')
             ->once()->with($url)
-            ->andReturn($this->getTestImageContent());
+            ->andReturn($this->files->pngImageData());
     }
 
     protected function deleteUserImage(User $user)
     {
-        $this->deleteImage($user->avatar->path);
+        $this->files->deleteAtRelativePath($user->avatar->path);
     }
 
     public function test_gravatar_fetched_on_user_create()
@@ -86,7 +85,7 @@ class AvatarTest extends TestCase
     {
         config()->set([
             'services.disable_services' => false,
-            'services.avatar_url' => false,
+            'services.avatar_url'       => false,
         ]);
 
         $user = User::factory()->make();
@@ -112,4 +111,28 @@ class AvatarTest extends TestCase
         $this->createUserRequest($user);
         $this->assertTrue($logger->hasError('Failed to save user avatar image'));
     }
+
+    public function test_exception_message_on_failed_fetch()
+    {
+        // set wrong url
+        config()->set([
+            'services.disable_services' => false,
+            'services.avatar_url'       => 'http_malformed_url/${email}/${hash}/${size}',
+        ]);
+
+        $user = User::factory()->make();
+        $avatar = app()->make(UserAvatars::class);
+        $url = 'http_malformed_url/' . urlencode(strtolower($user->email)) . '/' . md5(strtolower($user->email)) . '/500';
+        $logger = $this->withTestLogger();
+
+        $avatar->fetchAndAssignToUser($user);
+
+        $this->assertTrue($logger->hasError('Failed to save user avatar image'));
+        $exception = $logger->getRecords()[0]['context']['exception'];
+        $this->assertEquals(new HttpFetchException(
+            'Cannot get image from ' . $url,
+            6,
+            (new HttpFetchException('Could not resolve host: http_malformed_url', 6))
+        ), $exception);
+    }
 }