Added test case for avatar failed fetch
Fixed non-imported log issue while there. For #2449
This commit is contained in:
		
							parent
							
								
									526be33ab2
								
							
						
					
					
						commit
						e5a96b0cb0
					
				| 
						 | 
					@ -3,6 +3,7 @@
 | 
				
			||||||
use BookStack\Auth\User;
 | 
					use BookStack\Auth\User;
 | 
				
			||||||
use BookStack\Exceptions\HttpFetchException;
 | 
					use BookStack\Exceptions\HttpFetchException;
 | 
				
			||||||
use Exception;
 | 
					use Exception;
 | 
				
			||||||
 | 
					use Illuminate\Support\Facades\Log;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class UserAvatars
 | 
					class UserAvatars
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,7 +1,9 @@
 | 
				
			||||||
<?php namespace Tests\Uploads;
 | 
					<?php namespace Tests\Uploads;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use BookStack\Auth\User;
 | 
					use BookStack\Auth\User;
 | 
				
			||||||
 | 
					use BookStack\Exceptions\HttpFetchException;
 | 
				
			||||||
use BookStack\Uploads\HttpFetcher;
 | 
					use BookStack\Uploads\HttpFetcher;
 | 
				
			||||||
 | 
					use Illuminate\Support\Facades\Log;
 | 
				
			||||||
use Tests\TestCase;
 | 
					use Tests\TestCase;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class AvatarTest extends TestCase
 | 
					class AvatarTest extends TestCase
 | 
				
			||||||
| 
						 | 
					@ -11,7 +13,7 @@ class AvatarTest extends TestCase
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    protected function createUserRequest($user)
 | 
					    protected function createUserRequest($user)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $resp = $this->asAdmin()->post('/settings/users/create', [
 | 
					        $this->asAdmin()->post('/settings/users/create', [
 | 
				
			||||||
            'name' => $user->name,
 | 
					            'name' => $user->name,
 | 
				
			||||||
            'email' => $user->email,
 | 
					            'email' => $user->email,
 | 
				
			||||||
            'password' => 'testing',
 | 
					            'password' => 'testing',
 | 
				
			||||||
| 
						 | 
					@ -22,8 +24,7 @@ class AvatarTest extends TestCase
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    protected function assertImageFetchFrom(string $url)
 | 
					    protected function assertImageFetchFrom(string $url)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $http = \Mockery::mock(HttpFetcher::class);
 | 
					        $http = $this->mock(HttpFetcher::class);
 | 
				
			||||||
        $this->app->instance(HttpFetcher::class, $http);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $http->shouldReceive('fetch')
 | 
					        $http->shouldReceive('fetch')
 | 
				
			||||||
            ->once()->with($url)
 | 
					            ->once()->with($url)
 | 
				
			||||||
| 
						 | 
					@ -55,6 +56,7 @@ class AvatarTest extends TestCase
 | 
				
			||||||
    public function test_custom_url_used_if_set()
 | 
					    public function test_custom_url_used_if_set()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        config()->set([
 | 
					        config()->set([
 | 
				
			||||||
 | 
					            'services.disable_services' => false,
 | 
				
			||||||
            'services.avatar_url' => 'https://example.com/${email}/${hash}/${size}',
 | 
					            'services.avatar_url' => 'https://example.com/${email}/${hash}/${size}',
 | 
				
			||||||
        ]);
 | 
					        ]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -74,11 +76,26 @@ class AvatarTest extends TestCase
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $user = factory(User::class)->make();
 | 
					        $user = factory(User::class)->make();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $http = \Mockery::mock(HttpFetcher::class);
 | 
					        $http = $this->mock(HttpFetcher::class);
 | 
				
			||||||
        $this->app->instance(HttpFetcher::class, $http);
 | 
					 | 
				
			||||||
        $http->shouldNotReceive('fetch');
 | 
					        $http->shouldNotReceive('fetch');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        $this->createUserRequest($user);
 | 
					        $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'));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue