From 15ee7d2e24d60e9f8c3cfb806c3e363fc777709c Mon Sep 17 00:00:00 2001 From: Bubka <858858+Bubka@users.noreply.github.com> Date: Fri, 25 Oct 2024 14:58:58 +0200 Subject: [PATCH] Add missing Storage & Http facade fakes --- .../Controllers/TwoFAccountControllerTest.php | 5 +++- tests/Data/OtpTestData.php | 4 +-- tests/Feature/Models/TwoFAccountModelTest.php | 30 ++++++------------- tests/Feature/Services/IconServiceTest.php | 6 +++- .../Services/ReleaseRadarServiceTest.php | 26 +++++++++------- tests/Feature/Services/SettingServiceTest.php | 4 +-- tests/Feature/ViewTest.php | 12 ++++++++ 7 files changed, 49 insertions(+), 38 deletions(-) diff --git a/tests/Api/v1/Controllers/TwoFAccountControllerTest.php b/tests/Api/v1/Controllers/TwoFAccountControllerTest.php index 8dc69075..a91a08ad 100644 --- a/tests/Api/v1/Controllers/TwoFAccountControllerTest.php +++ b/tests/Api/v1/Controllers/TwoFAccountControllerTest.php @@ -235,6 +235,9 @@ public function setUp() : void Http::fake([ OtpTestData::EXTERNAL_IMAGE_URL_DECODED => Http::response((new FileFactory)->image('file.png', 10, 10)->tempFile, 200), ]); + Http::fake([ + 'example.com/*' => Http::response(null, 400), + ]); $this->user = User::factory()->create(); $this->userGroupA = Group::factory()->for($this->user)->create(); @@ -1193,7 +1196,7 @@ public function test_preview_with_unreachable_image_but_official_logo_returns_su public function test_preview_with_unreachable_image_returns_success_with_no_icon() { $this->user['preferences->getOfficialIcons'] = false; - + $response = $this->actingAs($this->user, 'api-guard') ->json('POST', '/api/v1/twofaccounts/preview', [ 'uri' => OtpTestData::TOTP_URI_WITH_UNREACHABLE_IMAGE, diff --git a/tests/Data/OtpTestData.php b/tests/Data/OtpTestData.php index c4bd9ca5..f0f25d6a 100644 --- a/tests/Data/OtpTestData.php +++ b/tests/Data/OtpTestData.php @@ -80,9 +80,9 @@ class OtpTestData const HOTP_SHORT_URI = 'otpauth://hotp/' . self::ACCOUNT . '?secret=' . self::SECRET; - const UNREACHABLE_IMAGE_URL = 'https%3A%2F%2Fen.opensuse.org%2Fimage.png'; + const UNREACHABLE_IMAGE_URL = 'https%3A%2F%2Fwww.example.com%2Fimage.png'; - const UNREACHABLE_IMAGE_URL_DECODED = 'https://en.opensuse.or/image.png'; + const UNREACHABLE_IMAGE_URL_DECODED = 'https://www.example.com/image.png'; const TOTP_URI_WITH_UNREACHABLE_IMAGE = 'otpauth://totp/service:account?secret=A4GRFHVVRBGY7UIW&image=' . self::UNREACHABLE_IMAGE_URL; diff --git a/tests/Feature/Models/TwoFAccountModelTest.php b/tests/Feature/Models/TwoFAccountModelTest.php index 02734a7f..d802d50e 100644 --- a/tests/Feature/Models/TwoFAccountModelTest.php +++ b/tests/Feature/Models/TwoFAccountModelTest.php @@ -47,6 +47,11 @@ public function setUp() : void { parent::setUp(); + Storage::fake('imagesLink'); + Storage::fake('icons'); + + Http::preventStrayRequests(); + /** @var \Illuminate\Contracts\Auth\Authenticatable $user */ $this->user = User::factory()->create(); $this->actingAs($this->user, 'api-guard'); @@ -95,15 +100,10 @@ public function test_fill_with_custom_totp_uri_returns_correct_value() { $file = (new FileFactory)->image('file.png', 10, 10); - // TODO: set preventStrayRequests in parent class to ensure all tests use a fake - Http::preventStrayRequests(); Http::fake([ OtpTestData::EXTERNAL_IMAGE_URL_DECODED => Http::response($file->tempFile, 200), ]); - Storage::fake('imagesLink'); - Storage::fake('icons'); - $twofaccount = new TwoFAccount; $twofaccount->fillWithURI(OtpTestData::TOTP_FULL_CUSTOM_URI); @@ -162,14 +162,10 @@ public function test_fill_with_custom_hotp_uri_returns_correct_value() { $file = (new FileFactory)->image('file.png', 10, 10); - Http::preventStrayRequests(); Http::fake([ OtpTestData::EXTERNAL_IMAGE_URL_DECODED => Http::response($file->tempFile, 200), ]); - Storage::fake('imagesLink'); - Storage::fake('icons'); - $twofaccount = new TwoFAccount; $twofaccount->fillWithURI(OtpTestData::HOTP_FULL_CUSTOM_URI); @@ -464,14 +460,10 @@ public function test_update_totp_persists_updated_model() #[Test] public function test_getOTP_for_totp_returns_the_same_password() { - Http::preventStrayRequests(); Http::fake([ OtpTestData::EXTERNAL_IMAGE_URL_DECODED => Http::response(HttpRequestTestData::ICON_PNG, 200), ]); - Storage::fake('imagesLink'); - Storage::fake('icons'); - $twofaccount = new TwoFAccount; $otp_from_model = $this->customTotpTwofaccount->getOTP(); @@ -492,14 +484,10 @@ public function test_getOTP_for_totp_returns_the_same_password() #[Test] public function test_getOTP_for_hotp_returns_the_same_password() { - Http::preventStrayRequests(); Http::fake([ OtpTestData::EXTERNAL_IMAGE_URL_DECODED => Http::response(HttpRequestTestData::ICON_PNG, 200), ]); - Storage::fake('imagesLink'); - Storage::fake('icons'); - $twofaccount = new TwoFAccount; $otp_from_model = $this->customHotpTwofaccount->getOTP(); @@ -585,14 +573,10 @@ public function test_getURI_for_custom_hotp_model_returns_uri() #[Test] public function test_fill_succeed_when_image_fetching_fails() { - Http::preventStrayRequests(); Http::fake([ OtpTestData::EXTERNAL_IMAGE_URL_DECODED => new \Exception, ]); - Storage::fake('imagesLink'); - Storage::fake('icons'); - $twofaccount = new TwoFAccount; $twofaccount->fillWithURI(OtpTestData::TOTP_FULL_CUSTOM_URI); @@ -684,6 +668,10 @@ public function test_equals_returns_false() #[Test] public function test_scopeOrphans_retreives_accounts_without_owner() { + Http::fake([ + OtpTestData::EXTERNAL_IMAGE_URL_DECODED => Http::response(HttpRequestTestData::ICON_PNG, 200), + ]); + $orphan = new TwoFAccount; $orphan->fillWithURI(OtpTestData::HOTP_FULL_CUSTOM_URI); $orphan->save(); diff --git a/tests/Feature/Services/IconServiceTest.php b/tests/Feature/Services/IconServiceTest.php index 536f2a3b..10081dee 100644 --- a/tests/Feature/Services/IconServiceTest.php +++ b/tests/Feature/Services/IconServiceTest.php @@ -200,7 +200,11 @@ public static function buildFromRemoteImageInvalidUrlProvider() #[Test] public function test_buildFromRemoteImage_returns_null_when_remote_img_is_unreachable() { - $imageUrl = 'https://icon.png'; + Http::fake([ + 'example.com/*' => Http::response(null, 400), + ]); + + $imageUrl = 'https://www.example.com/image.png'; $this->iconService = $this->app->make(IconService::class); $iconName = $this->iconService->buildFromRemoteImage($imageUrl); diff --git a/tests/Feature/Services/ReleaseRadarServiceTest.php b/tests/Feature/Services/ReleaseRadarServiceTest.php index f473bd92..67883f38 100644 --- a/tests/Feature/Services/ReleaseRadarServiceTest.php +++ b/tests/Feature/Services/ReleaseRadarServiceTest.php @@ -23,12 +23,18 @@ class ReleaseRadarServiceTest extends FeatureTestCase { use WithoutMiddleware; + public function setUp() : void + { + parent::setUp(); + + Http::preventStrayRequests(); + } + #[Test] public function test_manualScan_returns_no_new_release() { $url = config('2fauth.latestReleaseUrl'); - Http::preventStrayRequests(); Http::fake([ $url => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NO_NEW_RELEASE, 200), ]); @@ -48,7 +54,6 @@ public function test_manualScan_returns_new_release() { $url = config('2fauth.latestReleaseUrl'); - Http::preventStrayRequests(); Http::fake([ $url => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NEW_RELEASE, 200), ]); @@ -66,8 +71,9 @@ public function test_manualScan_returns_new_release() #[Test] public function test_manualScan_complete_when_http_call_fails() { - // We do not fake the http request so an exception will be thrown - Http::preventStrayRequests(); + Http::fake([ + config('2fauth.latestReleaseUrl') => Http::response('not found', 404), + ]); $this->assertNull(ReleaseRadarService::manualScan()); } @@ -77,9 +83,8 @@ public function test_manualScan_succeed_when_github_is_unreachable() { $url = config('2fauth.latestReleaseUrl'); - Http::preventStrayRequests(); Http::fake([ - $url => Http::response(null, 400), + $url => Http::response(null, 404), ]); $this->assertNull(ReleaseRadarService::manualScan()); @@ -90,7 +95,6 @@ public function test_scheduleScan_runs_after_one_week() { $url = config('2fauth.latestReleaseUrl'); - Http::preventStrayRequests(); Http::fake([ $url => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NEW_RELEASE, 200), ]); @@ -118,7 +122,6 @@ public function test_scheduleScan_does_not_run_before_one_week() { $url = config('2fauth.latestReleaseUrl'); - Http::preventStrayRequests(); Http::fake([ $url => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NEW_RELEASE, 200), ]); @@ -144,8 +147,11 @@ public function test_scheduleScan_does_not_run_before_one_week() #[Test] public function test_scheduleScan_complete_when_http_call_fails() { - // We do not fake the http request so an exception will be thrown - Http::preventStrayRequests(); + $url = config('2fauth.latestReleaseUrl'); + + Http::fake([ + $url => Http::response(null, 404), + ]); $this->assertNull(ReleaseRadarService::scheduledScan()); } diff --git a/tests/Feature/Services/SettingServiceTest.php b/tests/Feature/Services/SettingServiceTest.php index 1d592171..c61443eb 100644 --- a/tests/Feature/Services/SettingServiceTest.php +++ b/tests/Feature/Services/SettingServiceTest.php @@ -65,11 +65,9 @@ class SettingServiceTest extends FeatureTestCase private const PERIOD_CUSTOM = 40; - private const EXTERNAL_IMAGE_URL_ENCODED = 'https%3A%2F%2Fen.opensuse.org%2Fimages%2F4%2F44%2FButton-filled-colour.png'; - private const ICON = 'test.png'; - private const TOTP_FULL_CUSTOM_URI = 'otpauth://totp/' . self::SERVICE . ':' . self::ACCOUNT . '?secret=' . self::SECRET . '&issuer=' . self::SERVICE . '&digits=' . self::DIGITS_CUSTOM . '&period=' . self::PERIOD_CUSTOM . '&algorithm=' . self::ALGORITHM_CUSTOM . '&image=' . self::EXTERNAL_IMAGE_URL_ENCODED; + private const TOTP_FULL_CUSTOM_URI = 'otpauth://totp/' . self::SERVICE . ':' . self::ACCOUNT . '?secret=' . self::SECRET . '&issuer=' . self::SERVICE . '&digits=' . self::DIGITS_CUSTOM . '&period=' . self::PERIOD_CUSTOM . '&algorithm=' . self::ALGORITHM_CUSTOM . '&image=' . OtpTestData::EXTERNAL_IMAGE_URL_ENCODED; public function setUp() : void { diff --git a/tests/Feature/ViewTest.php b/tests/Feature/ViewTest.php index c4876691..2c0cf149 100644 --- a/tests/Feature/ViewTest.php +++ b/tests/Feature/ViewTest.php @@ -5,13 +5,25 @@ use App\Events\ScanForNewReleaseCalled; use App\Http\Controllers\SinglePageController; use Illuminate\Support\Facades\Event; +use Illuminate\Support\Facades\Http; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Test; +use Tests\Data\HttpRequestTestData; use Tests\FeatureTestCase; #[CoversClass(SinglePageController::class)] class ViewTest extends FeatureTestCase { + public function setUp() : void + { + parent::setUp(); + + Http::preventStrayRequests(); + Http::fake([ + config('2fauth.latestReleaseUrl') => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NO_NEW_RELEASE), + ]); + } + #[Test] public function test_landing_view_is_returned() {