Browse Source

Add missing Storage & Http facade fakes

Bubka 9 months ago
parent
commit
15ee7d2e24

+ 4 - 1
tests/Api/v1/Controllers/TwoFAccountControllerTest.php

@@ -235,6 +235,9 @@ class TwoFAccountControllerTest extends FeatureTestCase
         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 @@ class TwoFAccountControllerTest extends FeatureTestCase
     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,

+ 2 - 2
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;
 

+ 9 - 21
tests/Feature/Models/TwoFAccountModelTest.php

@@ -47,6 +47,11 @@ class TwoFAccountModelTest extends FeatureTestCase
     {
         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 @@ class TwoFAccountModelTest extends FeatureTestCase
     {
         $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 @@ class TwoFAccountModelTest extends FeatureTestCase
     {
         $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 @@ class TwoFAccountModelTest extends FeatureTestCase
     #[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 @@ class TwoFAccountModelTest extends FeatureTestCase
     #[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 @@ class TwoFAccountModelTest extends FeatureTestCase
     #[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 @@ class TwoFAccountModelTest extends FeatureTestCase
     #[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();

+ 5 - 1
tests/Feature/Services/IconServiceTest.php

@@ -200,7 +200,11 @@ class IconServiceTest extends FeatureTestCase
     #[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);

+ 16 - 10
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 @@ class ReleaseRadarServiceTest extends FeatureTestCase
     {
         $url = config('2fauth.latestReleaseUrl');
 
-        Http::preventStrayRequests();
         Http::fake([
             $url => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NEW_RELEASE, 200),
         ]);
@@ -66,8 +71,9 @@ class ReleaseRadarServiceTest extends FeatureTestCase
     #[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 @@ class ReleaseRadarServiceTest extends FeatureTestCase
     {
         $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 @@ class ReleaseRadarServiceTest extends FeatureTestCase
     {
         $url = config('2fauth.latestReleaseUrl');
 
-        Http::preventStrayRequests();
         Http::fake([
             $url => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NEW_RELEASE, 200),
         ]);
@@ -118,7 +122,6 @@ class ReleaseRadarServiceTest extends FeatureTestCase
     {
         $url = config('2fauth.latestReleaseUrl');
 
-        Http::preventStrayRequests();
         Http::fake([
             $url => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NEW_RELEASE, 200),
         ]);
@@ -144,8 +147,11 @@ class ReleaseRadarServiceTest extends FeatureTestCase
     #[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());
     }

+ 1 - 3
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
     {

+ 12 - 0
tests/Feature/ViewTest.php

@@ -5,13 +5,25 @@ namespace Tests\Feature;
 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()
     {