From e5a96b0cb0788f44b331afa83f1279314a7bafd1 Mon Sep 17 00:00:00 2001
From: Dan Brown <ssddanbrown@googlemail.com>
Date: Sun, 10 Jan 2021 13:29:13 +0000
Subject: [PATCH] Added test case for avatar failed fetch

Fixed non-imported log issue while there.
For #2449
---
 app/Uploads/UserAvatars.php  |  1 +
 tests/Uploads/AvatarTest.php | 27 ++++++++++++++++++++++-----
 2 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/app/Uploads/UserAvatars.php b/app/Uploads/UserAvatars.php
index 92b06bc8a..b3b9d5951 100644
--- a/app/Uploads/UserAvatars.php
+++ b/app/Uploads/UserAvatars.php
@@ -3,6 +3,7 @@
 use BookStack\Auth\User;
 use BookStack\Exceptions\HttpFetchException;
 use Exception;
+use Illuminate\Support\Facades\Log;
 
 class UserAvatars
 {
diff --git a/tests/Uploads/AvatarTest.php b/tests/Uploads/AvatarTest.php
index ecf7037a9..efaa016dd 100644
--- a/tests/Uploads/AvatarTest.php
+++ b/tests/Uploads/AvatarTest.php
@@ -1,7 +1,9 @@
 <?php namespace Tests\Uploads;
 
 use BookStack\Auth\User;
+use BookStack\Exceptions\HttpFetchException;
 use BookStack\Uploads\HttpFetcher;
+use Illuminate\Support\Facades\Log;
 use Tests\TestCase;
 
 class AvatarTest extends TestCase
@@ -11,7 +13,7 @@ class AvatarTest extends TestCase
 
     protected function createUserRequest($user)
     {
-        $resp = $this->asAdmin()->post('/settings/users/create', [
+        $this->asAdmin()->post('/settings/users/create', [
             'name' => $user->name,
             'email' => $user->email,
             'password' => 'testing',
@@ -22,8 +24,7 @@ class AvatarTest extends TestCase
 
     protected function assertImageFetchFrom(string $url)
     {
-        $http = \Mockery::mock(HttpFetcher::class);
-        $this->app->instance(HttpFetcher::class, $http);
+        $http = $this->mock(HttpFetcher::class);
 
         $http->shouldReceive('fetch')
             ->once()->with($url)
@@ -55,6 +56,7 @@ class AvatarTest extends TestCase
     public function test_custom_url_used_if_set()
     {
         config()->set([
+            'services.disable_services' => false,
             'services.avatar_url' => 'https://example.com/${email}/${hash}/${size}',
         ]);
 
@@ -74,11 +76,26 @@ class AvatarTest extends TestCase
 
         $user = factory(User::class)->make();
 
-        $http = \Mockery::mock(HttpFetcher::class);
-        $this->app->instance(HttpFetcher::class, $http);
+        $http = $this->mock(HttpFetcher::class);
         $http->shouldNotReceive('fetch');
 
         $this->createUserRequest($user);
     }
 
+    public function test_no_failure_but_error_logged_on_failed_avatar_fetch()
+    {
+        config()->set([
+            'services.disable_services' => false,
+        ]);
+
+        $http = $this->mock(HttpFetcher::class);
+        $http->shouldReceive('fetch')->andThrow(new HttpFetchException());
+
+        $logger = $this->withTestLogger();
+
+        $user = factory(User::class)->make();
+        $this->createUserRequest($user);
+        $this->assertTrue($logger->hasError('Failed to save user avatar image'));
+    }
+
 }