From 80723d94ba634686632efd95cbee8fb92705ea96 Mon Sep 17 00:00:00 2001 From: Bubka <858858+Bubka@users.noreply.github.com> Date: Fri, 10 Mar 2023 16:03:42 +0100 Subject: [PATCH] Update Feature tests --- .../Auth/ForgotPasswordControllerTest.php | 3 + tests/Feature/Http/Auth/LoginTest.php | 3 +- .../Http/Auth/PasswordControllerTest.php | 2 +- .../Http/Auth/RegisterControllerTest.php | 33 +++++++ .../Http/Auth/ResetPasswordControllerTest.php | 2 +- .../Feature/Http/Auth/UserControllerTest.php | 98 +++++++++++++++++-- .../Http/Auth/WebAuthnLoginControllerTest.php | 61 ++++++++++-- .../Auth/WebAuthnManageControllerTest.php | 2 +- .../Auth/WebAuthnRegisterControllerTest.php | 2 +- .../Http/Requests/UserStoreRequestTest.php | 29 +++--- .../Http/Requests/UserUpdateRequestTest.php | 20 +++- tests/Feature/Http/SystemControllerTest.php | 74 +++++++++----- tests/Feature/Models/TwoFAccountModelTest.php | 81 +++++++-------- .../Services/ReleaseRadarServiceTest.php | 84 ++++++++++------ 14 files changed, 366 insertions(+), 128 deletions(-) diff --git a/tests/Feature/Http/Auth/ForgotPasswordControllerTest.php b/tests/Feature/Http/Auth/ForgotPasswordControllerTest.php index cf0ac678..2b2800f5 100644 --- a/tests/Feature/Http/Auth/ForgotPasswordControllerTest.php +++ b/tests/Feature/Http/Auth/ForgotPasswordControllerTest.php @@ -103,6 +103,9 @@ public function test_submit_email_password_request_in_demo_mode_returns_unauthor */ public function test_submit_email_password_request_when_authenticated_returns_bad_request() { + /** + * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable + */ $user = User::factory()->create(); $this->actingAs($user, 'web-guard') diff --git a/tests/Feature/Http/Auth/LoginTest.php b/tests/Feature/Http/Auth/LoginTest.php index b3c07787..c3139c2b 100644 --- a/tests/Feature/Http/Auth/LoginTest.php +++ b/tests/Feature/Http/Auth/LoginTest.php @@ -191,7 +191,8 @@ public function test_user_logout_returns_validation_success() public function test_user_logout_after_inactivity_returns_teapot() { // Set the autolock period to 1 minute - Settings::set('kickUserAfter', 1); + $this->user['preferences->kickUserAfter'] = 1; + $this->user->save(); $response = $this->json('POST', '/user/login', [ 'email' => $this->user->email, diff --git a/tests/Feature/Http/Auth/PasswordControllerTest.php b/tests/Feature/Http/Auth/PasswordControllerTest.php index 7319e1f8..55e964ea 100644 --- a/tests/Feature/Http/Auth/PasswordControllerTest.php +++ b/tests/Feature/Http/Auth/PasswordControllerTest.php @@ -11,7 +11,7 @@ class PasswordControllerTest extends FeatureTestCase { /** - * @var \App\Models\User + * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable */ protected $user; diff --git a/tests/Feature/Http/Auth/RegisterControllerTest.php b/tests/Feature/Http/Auth/RegisterControllerTest.php index c360c312..eb8025fe 100644 --- a/tests/Feature/Http/Auth/RegisterControllerTest.php +++ b/tests/Feature/Http/Auth/RegisterControllerTest.php @@ -45,6 +45,39 @@ public function test_register_returns_success() ->assertJsonFragment([ 'name' => self::USERNAME, ]); + + $this->assertDatabaseHas('users', [ + 'name' => self::USERNAME, + 'email' => self::EMAIL, + ]); + } + + /** + * @test + */ + public function test_register_with_uppercased_email_returns_success() + { + DB::table('users')->delete(); + + $response = $this->json('POST', '/user', [ + 'name' => self::USERNAME, + 'email' => strtoupper(self::EMAIL), + 'password' => self::PASSWORD, + 'password_confirmation' => self::PASSWORD, + ]) + ->assertCreated() + ->assertJsonStructure([ + 'message', + 'name', + ]) + ->assertJsonFragment([ + 'name' => self::USERNAME, + ]); + + $this->assertDatabaseHas('users', [ + 'name' => self::USERNAME, + 'email' => self::EMAIL, + ]); } /** diff --git a/tests/Feature/Http/Auth/ResetPasswordControllerTest.php b/tests/Feature/Http/Auth/ResetPasswordControllerTest.php index ed563009..f9bb5e56 100644 --- a/tests/Feature/Http/Auth/ResetPasswordControllerTest.php +++ b/tests/Feature/Http/Auth/ResetPasswordControllerTest.php @@ -15,7 +15,7 @@ class ResetPasswordControllerTest extends FeatureTestCase { /** - * @var \App\Models\User + * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable */ protected $user; diff --git a/tests/Feature/Http/Auth/UserControllerTest.php b/tests/Feature/Http/Auth/UserControllerTest.php index c87fd833..2533011a 100644 --- a/tests/Feature/Http/Auth/UserControllerTest.php +++ b/tests/Feature/Http/Auth/UserControllerTest.php @@ -3,6 +3,8 @@ namespace Tests\Feature\Http\Auth; use App\Facades\Settings; +use App\Models\Group; +use App\Models\TwoFAccount; use App\Models\User; use Illuminate\Support\Facades\Config; use Tests\FeatureTestCase; @@ -14,7 +16,7 @@ class UserControllerTest extends FeatureTestCase { /** - * @var \App\Models\User + * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable */ protected $user; @@ -47,10 +49,18 @@ public function test_update_user_returns_success() ]) ->assertOk() ->assertExactJson([ - 'name' => self::NEW_USERNAME, - 'id' => $this->user->id, - 'email' => self::NEW_EMAIL, + 'name' => self::NEW_USERNAME, + 'id' => $this->user->id, + 'email' => self::NEW_EMAIL, + 'is_admin' => false, ]); + + $this->assertDatabaseHas('users', [ + 'name' => self::NEW_USERNAME, + 'id' => $this->user->id, + 'email' => self::NEW_EMAIL, + 'is_admin' => false, + ]); } /** @@ -68,10 +78,18 @@ public function test_update_user_in_demo_mode_returns_unchanged_user() ]) ->assertOk() ->assertExactJson([ - 'name' => $this->user->name, - 'id' => $this->user->id, - 'email' => $this->user->email, + 'name' => $this->user->name, + 'id' => $this->user->id, + 'email' => $this->user->email, + 'is_admin' => $this->user->is_admin, ]); + + $this->assertDatabaseHas('users', [ + 'name' => $this->user->name, + 'id' => $this->user->id, + 'email' => $this->user->email, + 'is_admin' => $this->user->is_admin, + ]); } /** @@ -107,11 +125,43 @@ public function test_update_user_with_invalid_data_returns_validation_error() */ public function test_delete_user_returns_success() { - $response = $this->actingAs($this->user, 'web-guard') + TwoFAccount::factory()->for($this->user)->create(); + Group::factory()->for($this->user)->create(); + + $admin = User::factory()->administrator()->create(); + $this->assertDatabaseCount('users', 2); + + $this->actingAs($this->user, 'web-guard') ->json('DELETE', '/user', [ 'password' => self::PASSWORD, ]) ->assertNoContent(); + + $this->assertDatabaseMissing('users', [ + 'id' => $this->user->id + ]); + $this->assertDatabaseHas('users', [ + 'id' => $admin->id + ]); + $this->assertDatabaseCount('users', 1); + $this->assertDatabaseMissing('twofaccounts', [ + 'user_id' => $this->user->id + ]); + $this->assertDatabaseMissing('groups', [ + 'user_id' => $this->user->id + ]); + $this->assertDatabaseMissing('webauthn_credentials', [ + 'authenticatable_id' => $this->user->id + ]); + $this->assertDatabaseMissing('webauthn_recoveries', [ + 'email' => $this->user->email + ]); + $this->assertDatabaseMissing('oauth_access_tokens', [ + 'user_id' => $this->user->id + ]); + $this->assertDatabaseMissing('password_resets', [ + 'email' => $this->user->email + ]); } /** @@ -130,6 +180,10 @@ public function test_delete_user_in_demo_mode_returns_unauthorized() ->assertJsonStructure([ 'message', ]); + + $this->assertDatabaseHas('users', [ + 'id' => $this->user->id + ]); } /** @@ -142,5 +196,33 @@ public function test_delete_user_passing_wrong_password_returns_bad_request() 'password' => 'wrongPassword', ]) ->assertStatus(400); + + $this->assertDatabaseHas('users', [ + 'id' => $this->user->id + ]); + } + + /** + * @test + */ + public function test_delete_the_only_admin_returns_bad_request() + { + /** + * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable + */ + $admin = User::factory()->administrator()->create(); + + $this->assertDatabaseCount('users', 2); + $this->assertEquals(1, User::admins()->count()); + + $response = $this->actingAs($admin, 'web-guard') + ->json('DELETE', '/user', [ + 'password' => self::PASSWORD, + ]) + ->assertStatus(400); + + $this->assertDatabaseHas('users', [ + 'id' => $admin->id + ]); } } diff --git a/tests/Feature/Http/Auth/WebAuthnLoginControllerTest.php b/tests/Feature/Http/Auth/WebAuthnLoginControllerTest.php index 87356c5c..13902737 100644 --- a/tests/Feature/Http/Auth/WebAuthnLoginControllerTest.php +++ b/tests/Feature/Http/Auth/WebAuthnLoginControllerTest.php @@ -194,7 +194,9 @@ public function test_get_options_for_securelogin_returns_success() 'created_at' => now(), ]); - $response = $this->json('POST', '/webauthn/login/options') + $response = $this->json('POST', '/webauthn/login/options', [ + 'email' => $this->user->email, + ]) ->assertOk() ->assertJsonStructure([ 'challenge', @@ -234,7 +236,9 @@ public function test_get_options_for_fastlogin_returns_success() 'created_at' => now(), ]); - $response = $this->json('POST', '/webauthn/login/options') + $response = $this->json('POST', '/webauthn/login/options', [ + 'email' => $this->user->email, + ]) ->assertOk() ->assertJsonStructure([ 'challenge', @@ -253,12 +257,55 @@ public function test_get_options_for_fastlogin_returns_success() /** * @test */ - public function test_get_options_with_no_registred_user_returns_error() + public function test_get_options_with_capitalized_email_returns_success() { - $this->json('POST', '/webauthn/login/options') - ->assertStatus(400) - ->assertJsonStructure([ - 'message', + $this->user = User::factory()->create(); + + $this->json('POST', '/webauthn/login/options', [ + 'email' => strtoupper($this->user->email), + ]) + ->assertOk(); + } + + /** + * @test + */ + public function test_get_options_with_missing_email_returns_validation_errors() + { + $this->json('POST', '/webauthn/login/options', [ + 'email' => null, + ]) + ->assertStatus(422) + ->assertJsonValidationErrors([ + 'email', + ]); + } + + /** + * @test + */ + public function test_get_options_with_invalid_email_returns_validation_errors() + { + $this->json('POST', '/webauthn/login/options', [ + 'email' => 'invalid', + ]) + ->assertStatus(422) + ->assertJsonValidationErrors([ + 'email', + ]); + } + + /** + * @test + */ + public function test_get_options_with_unknown_email_returns_validation_errors() + { + $this->json('POST', '/webauthn/login/options', [ + 'email' => 'john@example.com', + ]) + ->assertStatus(422) + ->assertJsonValidationErrors([ + 'email', ]); } } diff --git a/tests/Feature/Http/Auth/WebAuthnManageControllerTest.php b/tests/Feature/Http/Auth/WebAuthnManageControllerTest.php index 48c2fcd3..247d3eaf 100644 --- a/tests/Feature/Http/Auth/WebAuthnManageControllerTest.php +++ b/tests/Feature/Http/Auth/WebAuthnManageControllerTest.php @@ -17,7 +17,7 @@ class WebAuthnManageControllerTest extends FeatureTestCase // use WithoutMiddleware; /** - * @var \App\Models\User + * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable */ protected $user; diff --git a/tests/Feature/Http/Auth/WebAuthnRegisterControllerTest.php b/tests/Feature/Http/Auth/WebAuthnRegisterControllerTest.php index 7b9a24fb..d6b8059e 100644 --- a/tests/Feature/Http/Auth/WebAuthnRegisterControllerTest.php +++ b/tests/Feature/Http/Auth/WebAuthnRegisterControllerTest.php @@ -16,7 +16,7 @@ class WebAuthnRegisterControllerTest extends FeatureTestCase { /** - * @var \App\Models\User + * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable */ protected $user; diff --git a/tests/Feature/Http/Requests/UserStoreRequestTest.php b/tests/Feature/Http/Requests/UserStoreRequestTest.php index 0866db23..1f9e4ef4 100644 --- a/tests/Feature/Http/Requests/UserStoreRequestTest.php +++ b/tests/Feature/Http/Requests/UserStoreRequestTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Http\Requests; use App\Http\Requests\UserStoreRequest; +use App\Models\User; use Illuminate\Foundation\Testing\WithoutMiddleware; use Illuminate\Support\Facades\Validator; use Tests\FeatureTestCase; @@ -29,6 +30,11 @@ public function test_user_is_authorized() */ public function test_valid_data(array $data) : void { + User::factory()->create([ + 'name' => 'Jane', + 'email' => 'jane@example.com', + ]); + $request = new UserStoreRequest(); $validator = Validator::make($data, $request->rules()); @@ -47,6 +53,12 @@ public function provideValidData() : array 'password' => 'MyPassword', 'password_confirmation' => 'MyPassword', ]], + [[ + 'name' => 'John', + 'email' => 'JOHN@example.com', + 'password' => 'MyPassword', + 'password_confirmation' => 'MyPassword', + ]], ]; } @@ -55,15 +67,10 @@ public function provideValidData() : array */ public function test_invalid_data(array $data) : void { - $user = new \App\Models\User( - [ - 'name' => 'John', - 'email' => 'john@example.com', - 'password' => 'MyPassword', - 'password_confirmation' => 'MyPassword', - ] - ); - $user->save(); + User::factory()->create([ + 'name' => 'John', + 'email' => 'john@example.com', + ]); $request = new UserStoreRequest(); $validator = Validator::make($data, $request->rules()); @@ -78,8 +85,8 @@ public function provideInvalidData() : array { return [ [[ - 'name' => 'John', // unique - 'email' => 'john@example.com', + 'name' => 'John', + 'email' => 'john@example.com', // unique 'password' => 'MyPassword', 'password_confirmation' => 'MyPassword', ]], diff --git a/tests/Feature/Http/Requests/UserUpdateRequestTest.php b/tests/Feature/Http/Requests/UserUpdateRequestTest.php index eaf1374f..9095e8a7 100644 --- a/tests/Feature/Http/Requests/UserUpdateRequestTest.php +++ b/tests/Feature/Http/Requests/UserUpdateRequestTest.php @@ -3,15 +3,16 @@ namespace Tests\Feature\Http\Requests; use App\Http\Requests\UserUpdateRequest; +use App\Models\User; use Illuminate\Foundation\Testing\WithoutMiddleware; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Validator; -use Tests\TestCase; +use Tests\FeatureTestCase; /** * @covers \App\Http\Requests\UserUpdateRequest */ -class UserUpdateRequestTest extends TestCase +class UserUpdateRequestTest extends FeatureTestCase { use WithoutMiddleware; @@ -34,6 +35,11 @@ public function test_user_is_authorized() */ public function test_valid_data(array $data) : void { + User::factory()->create([ + 'name' => 'Jane', + 'email' => 'jane@example.com', + ]); + $request = new UserUpdateRequest(); $validator = Validator::make($data, $request->rules()); @@ -59,6 +65,11 @@ public function provideValidData() : array */ public function test_invalid_data(array $data) : void { + User::factory()->create([ + 'name' => 'Jane', + 'email' => 'jane@example.com', + ]); + $request = new UserUpdateRequest(); $validator = Validator::make($data, $request->rules()); @@ -71,6 +82,11 @@ public function test_invalid_data(array $data) : void public function provideInvalidData() : array { return [ + [[ + 'name' => 'John', + 'email' => 'jane@example.com', // unique + 'password' => 'MyPassword', + ]], [[ 'name' => '', // required 'email' => 'john@example.com', diff --git a/tests/Feature/Http/SystemControllerTest.php b/tests/Feature/Http/SystemControllerTest.php index de701889..a0c28615 100644 --- a/tests/Feature/Http/SystemControllerTest.php +++ b/tests/Feature/Http/SystemControllerTest.php @@ -15,7 +15,7 @@ class SystemControllerTest extends FeatureTestCase use WithoutMiddleware; /** - * @var \App\Models\User + * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable */ protected $user; @@ -37,34 +37,38 @@ public function test_infos_returns_only_base_collection() $response = $this->json('GET', '/infos') ->assertOk() ->assertJsonStructure([ - 'Date', - 'userAgent', - 'Version', - 'Environment', - 'Debug', - 'Cache driver', - 'Log channel', - 'Log level', - 'DB driver', - 'PHP version', - 'Operating system', - 'interface', + 'common' => [ + 'Date', + 'userAgent', + 'Version', + 'Environment', + 'Install path', + 'Debug', + 'Cache driver', + 'Log channel', + 'Log level', + 'DB driver', + 'PHP version', + 'Operating system', + 'interface', + ] + ]) + ->assertJsonMissing([ + 'user_preferences', + 'admin_settings', ]); } /** * @test */ - public function test_infos_returns_full_collection_when_signed_in() + public function test_infos_returns_user_preferences_when_signed_in() { $response = $this->actingAs($this->user, 'api-guard') ->json('GET', '/infos') ->assertOk() ->assertJsonStructure([ - 'Auth guard', - 'webauthn user verification', - 'Trusted proxies', - 'options' => [ + 'user_preferences' => [ 'showTokenAsDot', 'closeOtpOnCopy', 'copyOtpOnDisplay', @@ -75,15 +79,11 @@ public function test_infos_returns_full_collection_when_signed_in() 'activeGroup', 'rememberActiveGroup', 'defaultGroup', - 'useEncryption', 'defaultCaptureMode', 'useDirectCapture', 'useWebauthnAsDefault', 'useWebauthnOnly', 'getOfficialIcons', - 'checkForUpdate', - 'lastRadarScan', - 'latestRelease', 'lang', ], ]); @@ -92,14 +92,38 @@ public function test_infos_returns_full_collection_when_signed_in() /** * @test */ - public function test_infos_returns_full_collection_when_signed_in_behind_proxy() + public function test_infos_returns_admin_settings_when_signed_in_as_admin() + { + /** + * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable + */ + $admin = User::factory()->administrator()->create(); + + $response = $this->actingAs($admin, 'api-guard') + ->json('GET', '/infos') + ->assertOk() + ->assertJsonStructure([ + 'admin_settings' => [ + 'useEncryption', + 'lastRadarScan', + 'checkForUpdate', + ] + ]); + } + + /** + * @test + */ + public function test_infos_returns_proxy_collection_when_signed_in_behind_proxy() { $response = $this->actingAs($this->user, 'reverse-proxy-guard') ->json('GET', '/infos') ->assertOk() ->assertJsonStructure([ - 'Auth proxy header for user', - 'Auth proxy header for email', + 'common' => [ + 'Auth proxy header for user', + 'Auth proxy header for email', + ] ]); } diff --git a/tests/Feature/Models/TwoFAccountModelTest.php b/tests/Feature/Models/TwoFAccountModelTest.php index 28ef21d1..9658c330 100644 --- a/tests/Feature/Models/TwoFAccountModelTest.php +++ b/tests/Feature/Models/TwoFAccountModelTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Models; use App\Models\TwoFAccount; +use App\Models\User; use Illuminate\Http\Testing\FileFactory; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Storage; @@ -16,14 +17,14 @@ class TwoFAccountModelTest extends FeatureTestCase { /** - * App\Models\TwoFAccount $customTotpTwofaccount + * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable */ - protected $customTotpTwofaccount; + protected $user, $anotherUser; /** - * App\Models\TwoFAccount $customTotpTwofaccount + * @var \App\Models\TwoFAccount */ - protected $customHotpTwofaccount; + protected $customTotpTwofaccount, $customHotpTwofaccount, $customSteamTotpTwofaccount; /** * Helpers $helpers; @@ -37,43 +38,45 @@ public function setUp() : void { parent::setUp(); - $this->customTotpTwofaccount = new TwoFAccount; - $this->customTotpTwofaccount->legacy_uri = OtpTestData::TOTP_FULL_CUSTOM_URI; - $this->customTotpTwofaccount->service = OtpTestData::SERVICE; - $this->customTotpTwofaccount->account = OtpTestData::ACCOUNT; - $this->customTotpTwofaccount->icon = OtpTestData::ICON_PNG; - $this->customTotpTwofaccount->otp_type = 'totp'; - $this->customTotpTwofaccount->secret = OtpTestData::SECRET; - $this->customTotpTwofaccount->digits = OtpTestData::DIGITS_CUSTOM; - $this->customTotpTwofaccount->algorithm = OtpTestData::ALGORITHM_CUSTOM; - $this->customTotpTwofaccount->period = OtpTestData::PERIOD_CUSTOM; - $this->customTotpTwofaccount->counter = null; - $this->customTotpTwofaccount->save(); + $this->user = User::factory()->create(); - $this->customHotpTwofaccount = new TwoFAccount; - $this->customHotpTwofaccount->legacy_uri = OtpTestData::HOTP_FULL_CUSTOM_URI; - $this->customHotpTwofaccount->service = OtpTestData::SERVICE; - $this->customHotpTwofaccount->account = OtpTestData::ACCOUNT; - $this->customHotpTwofaccount->icon = OtpTestData::ICON_PNG; - $this->customHotpTwofaccount->otp_type = 'hotp'; - $this->customHotpTwofaccount->secret = OtpTestData::SECRET; - $this->customHotpTwofaccount->digits = OtpTestData::DIGITS_CUSTOM; - $this->customHotpTwofaccount->algorithm = OtpTestData::ALGORITHM_CUSTOM; - $this->customHotpTwofaccount->period = null; - $this->customHotpTwofaccount->counter = OtpTestData::COUNTER_CUSTOM; - $this->customHotpTwofaccount->save(); + $this->customTotpTwofaccount = TwoFAccount::factory()->for($this->user)->create([ + 'legacy_uri' => OtpTestData::TOTP_FULL_CUSTOM_URI, + 'service' => OtpTestData::SERVICE, + 'account' => OtpTestData::ACCOUNT, + 'icon' => OtpTestData::ICON_PNG, + 'otp_type' => 'totp', + 'secret' => OtpTestData::SECRET, + 'digits' => OtpTestData::DIGITS_CUSTOM, + 'algorithm' => OtpTestData::ALGORITHM_CUSTOM, + 'period' => OtpTestData::PERIOD_CUSTOM, + 'counter' => null, + ]); - $this->customSteamTotpTwofaccount = new TwoFAccount; - $this->customSteamTotpTwofaccount->legacy_uri = OtpTestData::STEAM_TOTP_URI; - $this->customSteamTotpTwofaccount->service = OtpTestData::STEAM; - $this->customSteamTotpTwofaccount->account = OtpTestData::ACCOUNT; - $this->customSteamTotpTwofaccount->otp_type = 'steamtotp'; - $this->customSteamTotpTwofaccount->secret = OtpTestData::STEAM_SECRET; - $this->customSteamTotpTwofaccount->digits = OtpTestData::DIGITS_STEAM; - $this->customSteamTotpTwofaccount->algorithm = OtpTestData::ALGORITHM_DEFAULT; - $this->customSteamTotpTwofaccount->period = OtpTestData::PERIOD_DEFAULT; - $this->customSteamTotpTwofaccount->counter = null; - $this->customSteamTotpTwofaccount->save(); + $this->customHotpTwofaccount = TwoFAccount::factory()->for($this->user)->create([ + 'legacy_uri' => OtpTestData::HOTP_FULL_CUSTOM_URI, + 'service' => OtpTestData::SERVICE, + 'account' => OtpTestData::ACCOUNT, + 'icon' => OtpTestData::ICON_PNG, + 'otp_type' => 'hotp', + 'secret' => OtpTestData::SECRET, + 'digits' => OtpTestData::DIGITS_CUSTOM, + 'algorithm' => OtpTestData::ALGORITHM_CUSTOM, + 'period' => null, + 'counter' => OtpTestData::COUNTER_CUSTOM, + ]); + + $this->customSteamTotpTwofaccount = TwoFAccount::factory()->for($this->user)->create([ + 'legacy_uri' => OtpTestData::STEAM_TOTP_URI, + 'service' => OtpTestData::STEAM, + 'account' => OtpTestData::ACCOUNT, + 'otp_type' => 'steamtotp', + 'secret' => OtpTestData::STEAM_SECRET, + 'digits' => OtpTestData::DIGITS_STEAM, + 'algorithm' => OtpTestData::ALGORITHM_DEFAULT, + 'period' => OtpTestData::PERIOD_DEFAULT, + 'counter' => null, + ]); } /** diff --git a/tests/Feature/Services/ReleaseRadarServiceTest.php b/tests/Feature/Services/ReleaseRadarServiceTest.php index ad7bcbcb..22c3e5dc 100644 --- a/tests/Feature/Services/ReleaseRadarServiceTest.php +++ b/tests/Feature/Services/ReleaseRadarServiceTest.php @@ -3,7 +3,8 @@ namespace Tests\Feature\Services; use App\Facades\Settings; -use App\Services\ReleaseRadarService; +// use App\Services\ReleaseRadarService; +use Facades\App\Services\ReleaseRadarService; use Illuminate\Foundation\Testing\WithoutMiddleware; use Illuminate\Support\Facades\Http; use Tests\Data\HttpRequestTestData; @@ -28,10 +29,7 @@ public function test_manualScan_returns_no_new_release() $url => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NO_NEW_RELEASE, 200), ]); - $releaseRadarService = new ReleaseRadarService(); - $release = $releaseRadarService->manualScan(); - - $this->assertFalse($release); + $this->assertFalse(ReleaseRadarService::manualScan()); $this->assertDatabaseHas('options', [ 'key' => 'lastRadarScan', ]); @@ -53,10 +51,7 @@ public function test_manualScan_returns_new_release() $url => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NEW_RELEASE, 200), ]); - $releaseRadarService = new ReleaseRadarService(); - $release = $releaseRadarService->manualScan(); - - $this->assertEquals(HttpRequestTestData::NEW_TAG_NAME, $release); + $this->assertEquals(HttpRequestTestData::NEW_TAG_NAME, ReleaseRadarService::manualScan()); $this->assertDatabaseHas('options', [ 'key' => 'latestRelease', 'value' => HttpRequestTestData::NEW_TAG_NAME, @@ -69,17 +64,12 @@ public function test_manualScan_returns_new_release() /** * @test */ - public function test_manualScan_succeed_when_something_fails() + public function test_manualScan_complete_when_http_call_fails() { - $url = config('2fauth.latestReleaseUrl'); - // We do not fake the http request so an exception will be thrown Http::preventStrayRequests(); - $releaseRadarService = new ReleaseRadarService(); - $release = $releaseRadarService->manualScan(); - - $this->assertFalse($release); + $this->assertFalse(ReleaseRadarService::manualScan()); } /** @@ -94,10 +84,7 @@ public function test_manualScan_succeed_when_github_is_unreachable() $url => Http::response(null, 400), ]); - $releaseRadarService = new ReleaseRadarService(); - $release = $releaseRadarService->manualScan(); - - $this->assertFalse($release); + $this->assertFalse(ReleaseRadarService::manualScan()); } /** @@ -112,14 +99,22 @@ public function test_scheduleScan_runs_after_one_week() $url => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NEW_RELEASE, 200), ]); - Settings::set('lastRadarScan', time() - (60 * 60 * 24 * 7) - 1); + $time = time() - (60 * 60 * 24 * 7) - 1; - $releaseRadarService = $this->mock(ReleaseRadarService::class)->makePartial(); - $releaseRadarService->shouldAllowMockingProtectedMethods() - ->shouldReceive('newRelease') - ->once(); + Settings::set('lastRadarScan', $time); + Settings::delete('latestRelease'); - $releaseRadarService->scheduledScan(); + ReleaseRadarService::scheduledScan(); + + $this->assertDatabaseHas('options', [ + 'key' => 'latestRelease', + 'value' => HttpRequestTestData::NEW_TAG_NAME, + ]); + + $this->assertDatabaseMissing('options', [ + 'key' => 'lastRadarScan', + 'value' => $time, + ]); } /** @@ -127,12 +122,39 @@ public function test_scheduleScan_runs_after_one_week() */ public function test_scheduleScan_does_not_run_before_one_week() { - Settings::set('lastRadarScan', time() - (60 * 60 * 24 * 7) + 2); + $url = config('2fauth.latestReleaseUrl'); - $releaseRadarService = $this->mock(ReleaseRadarService::class)->makePartial(); - $releaseRadarService->shouldAllowMockingProtectedMethods() - ->shouldNotReceive('newRelease'); + Http::preventStrayRequests(); + Http::fake([ + $url => Http::response(HttpRequestTestData::LATEST_RELEASE_BODY_NEW_RELEASE, 200), + ]); - $releaseRadarService->scheduledScan(); + $time = time() - (60 * 60 * 24 * 7) + 1; + + Settings::set('latestRelease', 'v1'); + Settings::set('lastRadarScan', $time); + + ReleaseRadarService::scheduledScan(); + + $this->assertDatabaseHas('options', [ + 'key' => 'latestRelease', + 'value' => 'v1', + ]); + + $this->assertDatabaseHas('options', [ + 'key' => 'lastRadarScan', + 'value' => $time, + ]); + } + + /** + * @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(); + + $this->assertNull(ReleaseRadarService::scheduledScan()); } }