From f7ac1e96c3e13c73b80c33d832c61bd8cb9c5ba5 Mon Sep 17 00:00:00 2001 From: Bubka <858858+Bubka@users.noreply.github.com> Date: Sat, 30 Jul 2022 17:51:02 +0200 Subject: [PATCH] Set SettingService behind a Facade --- app/Api/v1/Controllers/SettingController.php | 32 +++-------- app/Extensions/EloquentTwoFAuthProvider.php | 4 +- app/Facades/Settings.php | 14 +++++ app/Http/Controllers/SinglePageController.php | 19 +------ app/Http/Controllers/SystemController.php | 19 +------ app/Http/Middleware/KickOutInactiveUser.php | 4 +- app/Http/Middleware/SetLanguage.php | 6 +-- app/Models/TwoFAccount.php | 17 +++--- app/Services/GroupService.php | 15 +++--- ..._change_nullable_in_twofaccounts_table.php | 6 +-- .../v1/Controllers/SettingControllerTest.php | 18 +++---- .../Controllers/TwoFAccountControllerTest.php | 22 +++----- .../v1/Requests/SettingStoreRequestTest.php | 4 +- tests/Feature/Http/Auth/LoginTest.php | 4 +- .../Feature/Http/Auth/UserControllerTest.php | 8 ++- tests/Feature/Services/GroupServiceTest.php | 26 ++++----- tests/Feature/Services/SettingServiceTest.php | 54 ++++++++----------- .../v1/Controllers/GroupControllerTest.php | 5 +- tests/Unit/Events/TwoFAccountDeletedTest.php | 15 ------ tests/Unit/Listeners/CleanIconStorageTest.php | 5 -- tests/Unit/TwoFAccountModelTest.php | 12 ----- 21 files changed, 101 insertions(+), 208 deletions(-) create mode 100644 app/Facades/Settings.php diff --git a/app/Api/v1/Controllers/SettingController.php b/app/Api/v1/Controllers/SettingController.php index a70564be..1bebd783 100644 --- a/app/Api/v1/Controllers/SettingController.php +++ b/app/Api/v1/Controllers/SettingController.php @@ -2,9 +2,7 @@ namespace App\Api\v1\Controllers; -use App\Exceptions\DbEncryptionException; -use App\Services\DbEncryptionService; -use App\Services\SettingService; +use App\Facades\Settings; use App\Api\v1\Requests\SettingStoreRequest; use App\Api\v1\Requests\SettingUpdateRequest; use App\Http\Controllers\Controller; @@ -12,22 +10,6 @@ class SettingController extends Controller { - - /** - * The Settings Service instance. - */ - protected SettingService $settingService; - - - /** - * Create a new controller instance. - */ - public function __construct(SettingService $settingService) - { - $this->settingService = $settingService; - } - - /** * List all settings * @@ -35,7 +17,7 @@ public function __construct(SettingService $settingService) */ public function index() { - $settings = $this->settingService->all(); + $settings = Settings::all(); $settingsResources = collect(); $settings->each(function ($item, $key) use ($settingsResources) { $settingsResources->push([ @@ -57,7 +39,7 @@ public function index() */ public function show($settingName) { - $setting = $this->settingService->get($settingName); + $setting = Settings::get($settingName); if (is_null($setting)) { abort(404); @@ -80,7 +62,7 @@ public function store(SettingStoreRequest $request) { $validated = $request->validated(); - $this->settingService->set($validated['key'], $validated['value']); + Settings::set($validated['key'], $validated['value']); return response()->json([ 'key' => $validated['key'], @@ -99,7 +81,7 @@ public function update(SettingUpdateRequest $request, $settingName) { $validated = $request->validated(); - $this->settingService->set($settingName, $validated['value']); + Settings::set($settingName, $validated['value']); return response()->json([ 'key' => $settingName, @@ -117,7 +99,7 @@ public function update(SettingUpdateRequest $request, $settingName) */ public function destroy($settingName) { - $setting = $this->settingService->get($settingName); + $setting = Settings::get($settingName); if (is_null($setting)) { abort(404); @@ -131,7 +113,7 @@ public function destroy($settingName) ], 400); } - $this->settingService->delete($settingName); + Settings::delete($settingName); return response()->json(null, 204); } diff --git a/app/Extensions/EloquentTwoFAuthProvider.php b/app/Extensions/EloquentTwoFAuthProvider.php index fac1ccf5..2bcf833a 100644 --- a/app/Extensions/EloquentTwoFAuthProvider.php +++ b/app/Extensions/EloquentTwoFAuthProvider.php @@ -6,7 +6,7 @@ use DarkGhostHunter\Larapass\WebAuthn\WebAuthnAssertValidator; use Illuminate\Contracts\Config\Repository as ConfigContract; use Illuminate\Contracts\Hashing\Hasher as HasherContract; -use Facades\App\Services\SettingService; +use App\Facades\Settings; class EloquentTwoFAuthProvider extends EloquentWebAuthnProvider { @@ -26,6 +26,6 @@ public function __construct( ) { parent::__construct($config, $validator, $hasher, $model); - $this->fallback = !SettingService::get('useWebauthnOnly'); + $this->fallback = !Settings::get('useWebauthnOnly'); } } diff --git a/app/Facades/Settings.php b/app/Facades/Settings.php new file mode 100644 index 00000000..3822eda4 --- /dev/null +++ b/app/Facades/Settings.php @@ -0,0 +1,14 @@ +settingService = $settingService; - } - /** * return the main view @@ -31,7 +16,7 @@ public function __construct(SettingService $settingService) public function index() { return view('landing')->with([ - 'appSettings' => $this->settingService->all()->toJson(), + 'appSettings' => Settings::all()->toJson(), 'appConfig' => collect([ 'proxyAuth' => config("auth.defaults.guard") === 'reverse-proxy-guard' ? true : false, 'proxyLogoutUrl' => config("2fauth.config.proxyLogoutUrl") ? config("2fauth.config.proxyLogoutUrl") : false, diff --git a/app/Http/Controllers/SystemController.php b/app/Http/Controllers/SystemController.php index 14b8fc15..5a676f6f 100644 --- a/app/Http/Controllers/SystemController.php +++ b/app/Http/Controllers/SystemController.php @@ -3,27 +3,12 @@ namespace App\Http\Controllers; use App\Http\Controllers\Controller; -use App\Services\SettingService; +use App\Facades\Settings; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; class SystemController extends Controller { - /** - * The Settings Service instance. - */ - protected SettingService $settingService; - - - /** - * Create a new controller instance. - */ - public function __construct(SettingService $settingService) - { - $this->settingService = $settingService; - } - - /** * Get detailed information about the current installation * @@ -55,7 +40,7 @@ public function infos(Request $request) $infos['Trusted proxies'] = config('2fauth.trustedProxies') ?: 'none'; // User info if ($request->user()) { - $infos['options'] = $this->settingService->all()->toArray(); + $infos['options'] = Settings::all()->toArray(); } return response()->json($infos); diff --git a/app/Http/Middleware/KickOutInactiveUser.php b/app/Http/Middleware/KickOutInactiveUser.php index 8c7b8d74..ba7a0737 100644 --- a/app/Http/Middleware/KickOutInactiveUser.php +++ b/app/Http/Middleware/KickOutInactiveUser.php @@ -7,6 +7,7 @@ use Illuminate\Http\Response; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Log; +use App\Facades\Settings; class KickOutInactiveUser { @@ -32,8 +33,7 @@ public function handle($request, Closure $next, ...$quards) $inactiveFor = $now->diffInSeconds(Carbon::parse($user->last_seen_at)); // Fetch all setting values - $settingService = resolve('App\Services\SettingService'); - $kickUserAfterXSecond = intval($settingService->get('kickUserAfter')) * 60; + $kickUserAfterXSecond = intval(Settings::get('kickUserAfter')) * 60; // If user has been inactive longer than the allowed inactivity period if ($kickUserAfterXSecond > 0 && $inactiveFor > $kickUserAfterXSecond) { diff --git a/app/Http/Middleware/SetLanguage.php b/app/Http/Middleware/SetLanguage.php index 22635df2..7a89b0eb 100644 --- a/app/Http/Middleware/SetLanguage.php +++ b/app/Http/Middleware/SetLanguage.php @@ -4,7 +4,7 @@ use Closure; use Illuminate\Support\Facades\App; -use Facades\App\Services\SettingService; +use App\Facades\Settings; class SetLanguage { @@ -23,8 +23,8 @@ public function handle($request, Closure $next) // - No language is passed from the client // // We prioritize the user defined one, then the request header one, and finally the fallback one. - // FI: SettingService::get() always returns a fallback value - $lang = SettingService::get('lang'); + // FI: Settings::get() always returns a fallback value + $lang = Settings::get('lang'); if($lang === 'browser') { $lang = config('app.fallback_locale'); diff --git a/app/Models/TwoFAccount.php b/app/Models/TwoFAccount.php index bb8a619c..3ccaf87d 100644 --- a/app/Models/TwoFAccount.php +++ b/app/Models/TwoFAccount.php @@ -4,7 +4,7 @@ use Exception; use App\Services\LogoService; -use App\Services\SettingService; +use App\Facades\Settings; use App\Models\Dto\TotpDto; use App\Models\Dto\HotpDto; use App\Events\TwoFAccountDeleted; @@ -395,8 +395,7 @@ public function fillWithOtpParameters(array $parameters, bool $skipIconFetching $this->icon = $this->getDefaultIcon(); } - $settingService = App::make(SettingService::class); - if (!$this->icon && $settingService->get('getOfficialIcons') && !$skipIconFetching) { + if (!$this->icon && Settings::get('getOfficialIcons') && !$skipIconFetching) { $this->icon = $this->getDefaultIcon(); } @@ -450,8 +449,7 @@ public function fillWithURI(string $uri, bool $isSteamTotp = false, bool $skipIc $this->icon = $this->storeImageAsIcon($this->generator->getParameter('image')); } - $settingService = App::make(SettingService::class); - if (!$this->icon && $settingService->get('getOfficialIcons') && !$skipIconFetching) { + if (!$this->icon && Settings::get('getOfficialIcons') && !$skipIconFetching) { $this->icon = $this->getDefaultIcon(); } @@ -598,9 +596,8 @@ private function storeImageAsIcon(string $url) private function getDefaultIcon() { $logoService = App::make(LogoService::class); - $settingService = App::make(SettingService::class); - return $settingService->get('getOfficialIcons') ? $logoService->getIcon($this->service) : null; + return Settings::get('getOfficialIcons') ? $logoService->getIcon($this->service) : null; } @@ -609,9 +606,8 @@ private function getDefaultIcon() */ private function decryptOrReturn($value) { - $settingService = App::make(SettingService::class); // Decipher when needed - if ( $settingService->get('useEncryption') ) + if ( Settings::get('useEncryption') ) { try { return Crypt::decryptString($value); @@ -631,9 +627,8 @@ private function decryptOrReturn($value) */ private function encryptOrReturn($value) { - $settingService = App::make(SettingService::class); // should be replaced by laravel 8 attribute encryption casting - return $settingService->get('useEncryption') ? Crypt::encryptString($value) : $value; + return Settings::get('useEncryption') ? Crypt::encryptString($value) : $value; } } \ No newline at end of file diff --git a/app/Services/GroupService.php b/app/Services/GroupService.php index 36134f75..46fe024c 100644 --- a/app/Services/GroupService.php +++ b/app/Services/GroupService.php @@ -4,7 +4,7 @@ use App\Models\Group; use App\Models\TwoFAccount; -use App\Services\SettingService; +use App\Facades\Settings; use Illuminate\Database\Eloquent\Collection; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\App; @@ -86,25 +86,23 @@ public static function update(Group $group, array $data) : Group */ public static function delete($ids) : int { - $settingService = App::make(SettingService::class); - $ids = is_array($ids) ? $ids : func_get_args(); // A group is possibly set as the default group in Settings. // In this case we reset the setting to "No group" (groupId = 0) - $defaultGroupId = $settingService->get('defaultGroup'); + $defaultGroupId = Settings::get('defaultGroup'); if (in_array($defaultGroupId, $ids)) { - $settingService->set('defaultGroup', 0); + Settings::set('defaultGroup', 0); } // A group is also possibly set as the active group if the user // configured 2FAuth to memorize the active group. // In this case we reset the setting to the pseudo "All" group (groupId = 0) - $activeGroupId = $settingService->get('activeGroup'); + $activeGroupId = Settings::get('activeGroup'); if (in_array($activeGroupId, $ids)) { - $settingService->set('activeGroup', 0); + Settings::set('activeGroup', 0); } $deleted = Group::destroy($ids); @@ -166,8 +164,7 @@ public static function getAccounts(Group $group) : Collection */ private static function defaultGroup() { - $settingService = App::make(SettingService::class); - $id = $settingService->get('defaultGroup') === -1 ? (int) $settingService->get('activeGroup') : (int) $settingService->get('defaultGroup'); + $id = Settings::get('defaultGroup') === -1 ? (int) Settings::get('activeGroup') : (int) Settings::get('defaultGroup'); return Group::find($id); } diff --git a/database/migrations/2021_09_14_195451_change_nullable_in_twofaccounts_table.php b/database/migrations/2021_09_14_195451_change_nullable_in_twofaccounts_table.php index 5fd2cfb2..15a0f5ee 100644 --- a/database/migrations/2021_09_14_195451_change_nullable_in_twofaccounts_table.php +++ b/database/migrations/2021_09_14_195451_change_nullable_in_twofaccounts_table.php @@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\Log; +use App\Facades\Settings; class ChangeNullableInTwofaccountsTable extends Migration { @@ -17,18 +18,17 @@ class ChangeNullableInTwofaccountsTable extends Migration public function up() { $twofaccounts = DB::table('twofaccounts')->select('id', 'legacy_uri')->get(); - $settingService = resolve('App\Services\SettingService'); foreach ($twofaccounts as $twofaccount) { try { - $legacy_uri = $settingService->get('useEncryption') ? Crypt::decryptString($twofaccount->legacy_uri) : $twofaccount->legacy_uri; + $legacy_uri = Settings::get('useEncryption') ? Crypt::decryptString($twofaccount->legacy_uri) : $twofaccount->legacy_uri; $token = \OTPHP\Factory::loadFromProvisioningUri($legacy_uri); $affected = DB::table('twofaccounts') ->where('id', $twofaccount->id) ->update([ 'otp_type' => get_class($token) === 'OTPHP\TOTP' ? 'totp' : 'hotp', - 'secret' => $settingService->get('useEncryption') ? Crypt::encryptString($token->getSecret()) : $token->getSecret(), + 'secret' => Settings::get('useEncryption') ? Crypt::encryptString($token->getSecret()) : $token->getSecret(), 'algorithm' => $token->getDigest(), 'digits' => $token->getDigits(), 'period' => $token->hasParameter('period') ? $token->getParameter('period') : null, diff --git a/tests/Api/v1/Controllers/SettingControllerTest.php b/tests/Api/v1/Controllers/SettingControllerTest.php index 9d6c9350..4eeb61d2 100644 --- a/tests/Api/v1/Controllers/SettingControllerTest.php +++ b/tests/Api/v1/Controllers/SettingControllerTest.php @@ -3,9 +3,8 @@ namespace Tests\Api\v1\Controllers; use App\Models\User; -use App\Models\Group; use Tests\FeatureTestCase; -use App\Models\TwoFAccount; +use App\Facades\Settings; /** @@ -74,8 +73,7 @@ public function test_show_native_unchanged_setting_returns_consistent_value() */ public function test_show_native_changed_setting_returns_consistent_value() { - $settingService = resolve('App\Services\SettingService'); - $settingService->set(self::TWOFAUTH_NATIVE_SETTING, self::TWOFAUTH_NATIVE_SETTING_CHANGED_VALUE); + Settings::set(self::TWOFAUTH_NATIVE_SETTING, self::TWOFAUTH_NATIVE_SETTING_CHANGED_VALUE); $response = $this->actingAs($this->user, 'api-guard') ->json('GET', '/api/v1/settings/' . self::TWOFAUTH_NATIVE_SETTING) @@ -92,8 +90,7 @@ public function test_show_native_changed_setting_returns_consistent_value() */ public function test_show_custom_user_setting_returns_consistent_value() { - $settingService = resolve('App\Services\SettingService'); - $settingService->set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE); + Settings::set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE); $response = $this->actingAs($this->user, 'api-guard') ->json('GET', '/api/v1/settings/' . self::USER_DEFINED_SETTING) @@ -153,8 +150,7 @@ public function test_store_invalid_custom_user_setting_returns_validation_error( */ public function test_store_existing_custom_user_setting_returns_validation_error() { - $settingService = resolve('App\Services\SettingService'); - $settingService->set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE); + Settings::set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE); $response = $this->actingAs($this->user, 'api-guard') ->json('POST', '/api/v1/settings', [ @@ -187,8 +183,7 @@ public function test_update_unchanged_native_setting_returns_updated_setting() */ public function test_update_custom_user_setting_returns_updated_setting() { - $settingService = resolve('App\Services\SettingService'); - $settingService->set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE); + Settings::set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE); $response = $this->actingAs($this->user, 'api-guard') ->json('PUT', '/api/v1/settings/' . self::USER_DEFINED_SETTING, [ @@ -224,8 +219,7 @@ public function test_update_missing_user_setting_returns_created_setting() */ public function test_destroy_user_setting_returns_success() { - $settingService = resolve('App\Services\SettingService'); - $settingService->set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE); + Settings::set(self::USER_DEFINED_SETTING, self::USER_DEFINED_SETTING_VALUE); $response = $this->actingAs($this->user, 'api-guard') ->json('DELETE', '/api/v1/settings/' . self::USER_DEFINED_SETTING) diff --git a/tests/Api/v1/Controllers/TwoFAccountControllerTest.php b/tests/Api/v1/Controllers/TwoFAccountControllerTest.php index 0ec60c5b..affc8fe5 100644 --- a/tests/Api/v1/Controllers/TwoFAccountControllerTest.php +++ b/tests/Api/v1/Controllers/TwoFAccountControllerTest.php @@ -4,12 +4,12 @@ use App\Models\User; use App\Models\Group; +use App\Facades\Settings; use Tests\FeatureTestCase; use Tests\Classes\OtpTestData; use App\Models\TwoFAccount; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Storage; -use Illuminate\Http\UploadedFile; /** @@ -393,8 +393,7 @@ public function test_store_with_invalid_uri_returns_validation_error() public function test_store_assigns_created_account_when_default_group_is_a_specific_one() { // Set the default group to a specific one - $settingService = resolve('App\Services\SettingService'); - $settingService->set('defaultGroup', $this->group->id); + Settings::set('defaultGroup', $this->group->id); $response = $this->actingAs($this->user, 'api-guard') ->json('POST', '/api/v1/twofaccounts', [ @@ -411,12 +410,10 @@ public function test_store_assigns_created_account_when_default_group_is_a_speci */ public function test_store_assigns_created_account_when_default_group_is_the_active_one() { - $settingService = resolve('App\Services\SettingService'); - // Set the default group to be the active one - $settingService->set('defaultGroup', -1); + Settings::set('defaultGroup', -1); // Set the active group - $settingService->set('activeGroup', $this->group->id); + Settings::set('activeGroup', $this->group->id); $response = $this->actingAs($this->user, 'api-guard') ->json('POST', '/api/v1/twofaccounts', [ @@ -433,10 +430,8 @@ public function test_store_assigns_created_account_when_default_group_is_the_act */ public function test_store_assigns_created_account_when_default_group_is_no_group() { - $settingService = resolve('App\Services\SettingService'); - // Set the default group to No group - $settingService->set('defaultGroup', 0); + Settings::set('defaultGroup', 0); $response = $this->actingAs($this->user, 'api-guard') ->json('POST', '/api/v1/twofaccounts', [ @@ -453,10 +448,8 @@ public function test_store_assigns_created_account_when_default_group_is_no_grou */ public function test_store_assigns_created_account_when_default_group_does_not_exist() { - $settingService = resolve('App\Services\SettingService'); - // Set the default group to a non-existing one - $settingService->set('defaultGroup', 1000); + Settings::set('defaultGroup', 1000); $response = $this->actingAs($this->user, 'api-guard') ->json('POST', '/api/v1/twofaccounts', [ @@ -836,8 +829,7 @@ public function test_get_otp_by_posting_multiple_inputs_returns_bad_request() */ public function test_get_otp_using_indecipherable_twofaccount_id_returns_bad_request() { - $settingService = resolve('App\Services\SettingService'); - $settingService->set('useEncryption', true); + Settings::set('useEncryption', true); $twofaccount = TwoFAccount::factory()->create(); diff --git a/tests/Api/v1/Requests/SettingStoreRequestTest.php b/tests/Api/v1/Requests/SettingStoreRequestTest.php index 184eec1b..fc04535c 100644 --- a/tests/Api/v1/Requests/SettingStoreRequestTest.php +++ b/tests/Api/v1/Requests/SettingStoreRequestTest.php @@ -7,6 +7,7 @@ use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Auth; use Tests\FeatureTestCase; +use App\Facades\Settings; class SettingStoreRequestTest extends FeatureTestCase { @@ -69,8 +70,7 @@ public function provideValidData() : array */ public function test_invalid_data(array $data) : void { - $settingService = resolve('App\Services\SettingService'); - $settingService->set($this->uniqueKey, 'uniqueValue'); + Settings::set($this->uniqueKey, 'uniqueValue'); $request = new SettingStoreRequest(); $validator = Validator::make($data, $request->rules()); diff --git a/tests/Feature/Http/Auth/LoginTest.php b/tests/Feature/Http/Auth/LoginTest.php index 32763d4f..be69d66a 100644 --- a/tests/Feature/Http/Auth/LoginTest.php +++ b/tests/Feature/Http/Auth/LoginTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Http\Auth; use App\Models\User; +use App\Facades\Settings; use Tests\FeatureTestCase; class LoginTest extends FeatureTestCase @@ -163,8 +164,7 @@ public function test_user_logout_returns_validation_success() public function test_user_logout_after_inactivity_returns_teapot() { // Set the autolock period to 1 minute - $settingService = resolve('App\Services\SettingService'); - $settingService->set('kickUserAfter', 1); + Settings::set('kickUserAfter', 1); $response = $this->json('POST', '/user/login', [ 'email' => $this->user->email, diff --git a/tests/Feature/Http/Auth/UserControllerTest.php b/tests/Feature/Http/Auth/UserControllerTest.php index 763e2f85..db81bf6b 100644 --- a/tests/Feature/Http/Auth/UserControllerTest.php +++ b/tests/Feature/Http/Auth/UserControllerTest.php @@ -3,6 +3,7 @@ namespace Tests\Feature\Http\Auth; use App\Models\User; +use App\Facades\Settings; use Tests\FeatureTestCase; use Illuminate\Support\Facades\Config; @@ -53,8 +54,7 @@ public function test_update_user_returns_success() */ public function test_update_user_in_demo_mode_returns_unchanged_user() { - $settingService = resolve('App\Services\SettingService'); - $settingService->set('isDemoApp', true); + Settings::set('isDemoApp', true); $response = $this->actingAs($this->user, 'web-guard') ->json('PUT', '/user', [ @@ -120,9 +120,7 @@ public function test_delete_user_returns_success() public function test_delete_user_in_demo_mode_returns_unauthorized() { Config::set('2fauth.config.isDemoApp', true); - - $settingService = resolve('App\Services\SettingService'); - $settingService->set('isDemoApp', true); + Settings::set('isDemoApp', true); $response = $this->actingAs($this->user, 'web-guard') ->json('DELETE', '/user', [ diff --git a/tests/Feature/Services/GroupServiceTest.php b/tests/Feature/Services/GroupServiceTest.php index 6a8c55ca..b5502565 100644 --- a/tests/Feature/Services/GroupServiceTest.php +++ b/tests/Feature/Services/GroupServiceTest.php @@ -6,7 +6,7 @@ use App\Models\TwoFAccount; use Tests\FeatureTestCase; use App\Facades\Groups; -use App\Services\SettingService; +use App\Facades\Settings; /** @@ -14,12 +14,6 @@ */ class GroupServiceTest extends FeatureTestCase { - /** - * App\Services\SettingService $settingService - */ - protected $settingService; - - /** * App\Models\Group $groupOne, $groupTwo */ @@ -52,8 +46,6 @@ public function setUp() : void { parent::setUp(); - $this->settingService = $this->app->make(SettingService::class); - $this->groupOne = new Group; $this->groupOne->name = 'MyGroupOne'; $this->groupOne->save(); @@ -178,7 +170,7 @@ public function test_delete_an_array_of_ids_clear_db_and_returns_deleted_count() */ public function test_delete_default_group_reset_defaultGroup_setting() { - $this->settingService->set('defaultGroup', $this->groupOne->id); + Settings::set('defaultGroup', $this->groupOne->id); $deleted = Groups::delete($this->groupOne->id); @@ -194,8 +186,8 @@ public function test_delete_default_group_reset_defaultGroup_setting() */ public function test_delete_active_group_reset_activeGroup_setting() { - $this->settingService->set('rememberActiveGroup', true); - $this->settingService->set('activeGroup', $this->groupOne->id); + Settings::set('rememberActiveGroup', true); + Settings::set('activeGroup', $this->groupOne->id); $deleted = Groups::delete($this->groupOne->id); @@ -244,7 +236,7 @@ public function test_assign_multiple_twofaccountid_to_a_specified_group_persists */ public function test_assign_a_twofaccountid_to_no_group_assigns_to_default_group() { - $this->settingService->set('defaultGroup', $this->groupTwo->id); + Settings::set('defaultGroup', $this->groupTwo->id); Groups::assign($this->twofaccountOne->id); @@ -260,8 +252,8 @@ public function test_assign_a_twofaccountid_to_no_group_assigns_to_default_group */ public function test_assign_a_twofaccountid_to_no_group_assigns_to_active_group() { - $this->settingService->set('defaultGroup', -1); - $this->settingService->set('activeGroup', $this->groupTwo->id); + Settings::set('defaultGroup', -1); + Settings::set('activeGroup', $this->groupTwo->id); Groups::assign($this->twofaccountOne->id); @@ -277,8 +269,8 @@ public function test_assign_a_twofaccountid_to_no_group_assigns_to_active_group( */ public function test_assign_a_twofaccountid_to_missing_active_group_does_not_fails() { - $this->settingService->set('defaultGroup', -1); - $this->settingService->set('activeGroup', 100000); + Settings::set('defaultGroup', -1); + Settings::set('activeGroup', 100000); Groups::assign($this->twofaccountOne->id); diff --git a/tests/Feature/Services/SettingServiceTest.php b/tests/Feature/Services/SettingServiceTest.php index e307563b..72ab5e3a 100644 --- a/tests/Feature/Services/SettingServiceTest.php +++ b/tests/Feature/Services/SettingServiceTest.php @@ -6,7 +6,7 @@ use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\DB; use App\Models\TwoFAccount; -use App\Services\SettingService; +use App\Facades\Settings; /** @@ -14,12 +14,6 @@ */ class SettingServiceTest extends FeatureTestCase { - /** - * App\Services\SettingService $settingService - */ - protected $settingService; - - /** * App\Models\Group $groupOne, $groupTwo */ @@ -51,8 +45,6 @@ public function setUp() : void { parent::setUp(); - $this->settingService = $this->app->make(SettingService::class); - $this->twofaccountOne = new TwoFAccount; $this->twofaccountOne->legacy_uri = self::TOTP_FULL_CUSTOM_URI; $this->twofaccountOne->service = self::SERVICE; @@ -86,9 +78,9 @@ public function setUp() : void */ public function test_get_string_setting_returns_correct_value() { - $this->settingService->set(self::SETTING_NAME, self::SETTING_VALUE_STRING); + Settings::set(self::SETTING_NAME, self::SETTING_VALUE_STRING); - $this->assertEquals(self::SETTING_VALUE_STRING, $this->settingService->get(self::SETTING_NAME)); + $this->assertEquals(self::SETTING_VALUE_STRING, Settings::get(self::SETTING_NAME)); } @@ -97,9 +89,9 @@ public function test_get_string_setting_returns_correct_value() */ public function test_get_boolean_setting_returns_true() { - $this->settingService->set(self::SETTING_NAME, self::SETTING_VALUE_TRUE_TRANSFORMED); + Settings::set(self::SETTING_NAME, self::SETTING_VALUE_TRUE_TRANSFORMED); - $this->assertEquals(true, $this->settingService->get(self::SETTING_NAME)); + $this->assertEquals(true, Settings::get(self::SETTING_NAME)); } @@ -108,9 +100,9 @@ public function test_get_boolean_setting_returns_true() */ public function test_get_boolean_setting_returns_false() { - $this->settingService->set(self::SETTING_NAME, self::SETTING_VALUE_FALSE_TRANSFORMED); + Settings::set(self::SETTING_NAME, self::SETTING_VALUE_FALSE_TRANSFORMED); - $this->assertEquals(false, $this->settingService->get(self::SETTING_NAME)); + $this->assertEquals(false, Settings::get(self::SETTING_NAME)); } @@ -119,9 +111,9 @@ public function test_get_boolean_setting_returns_false() */ public function test_get_int_setting_returns_int() { - $this->settingService->set(self::SETTING_NAME, self::SETTING_VALUE_INT); + Settings::set(self::SETTING_NAME, self::SETTING_VALUE_INT); - $value = $this->settingService->get(self::SETTING_NAME); + $value = Settings::get(self::SETTING_NAME); $this->assertEquals(self::SETTING_VALUE_INT, $value); $this->assertIsInt($value); @@ -135,9 +127,9 @@ public function test_all_returns_native_and_user_settings() { $native_options = config('2fauth.options'); - $this->settingService->set(self::SETTING_NAME, self::SETTING_VALUE_STRING); + Settings::set(self::SETTING_NAME, self::SETTING_VALUE_STRING); - $all = $this->settingService->all(); + $all = Settings::all(); $this->assertArrayHasKey(self::SETTING_NAME, $all); $this->assertEquals($all[self::SETTING_NAME], self::SETTING_VALUE_STRING); @@ -157,7 +149,7 @@ public function test_all_returns_native_and_user_settings() */ public function test_set_setting_persist_correct_value() { - $value = $this->settingService->set(self::SETTING_NAME, self::SETTING_VALUE_STRING); + $value = Settings::set(self::SETTING_NAME, self::SETTING_VALUE_STRING); $this->assertDatabaseHas('options', [ self::KEY => self::SETTING_NAME, @@ -171,7 +163,7 @@ public function test_set_setting_persist_correct_value() */ public function test_set_useEncryption_on_encrypts_all_accounts() { - $this->settingService->set('useEncryption', true); + Settings::set('useEncryption', true); $twofaccounts = DB::table('twofaccounts')->get(); @@ -188,8 +180,8 @@ public function test_set_useEncryption_on_encrypts_all_accounts() */ public function test_set_useEncryption_on_twice_prevents_successive_encryption() { - $this->settingService->set('useEncryption', true); - $this->settingService->set('useEncryption', true); + Settings::set('useEncryption', true); + Settings::set('useEncryption', true); $twofaccounts = DB::table('twofaccounts')->get(); @@ -206,8 +198,8 @@ public function test_set_useEncryption_on_twice_prevents_successive_encryption() */ public function test_set_useEncryption_off_decrypts_all_accounts() { - $this->settingService->set('useEncryption', true); - $this->settingService->set('useEncryption', false); + Settings::set('useEncryption', true); + Settings::set('useEncryption', false); $twofaccounts = DB::table('twofaccounts')->get(); @@ -227,13 +219,13 @@ public function test_set_useEncryption_off_returns_exception_when_data_are_undec { $this->expectException(\App\Exceptions\DbEncryptionException::class); - $this->settingService->set('useEncryption', true); + Settings::set('useEncryption', true); $affected = DB::table('twofaccounts') ->where('id', $this->twofaccountOne->id) ->update($data); - $this->settingService->set('useEncryption', false); + Settings::set('useEncryption', false); $twofaccount = TwoFAccount::find($this->twofaccountOne->id); } @@ -263,7 +255,7 @@ public function provideUndecipherableData() : array */ public function test_set_array_of_settings_persist_correct_values() { - $value = $this->settingService->set([ + $value = Settings::set([ self::SETTING_NAME => self::SETTING_VALUE_STRING, self::SETTING_NAME_ALT => self::SETTING_VALUE_INT, ]); @@ -285,7 +277,7 @@ public function test_set_array_of_settings_persist_correct_values() */ public function test_set_true_setting_persist_transformed_boolean() { - $value = $this->settingService->set(self::SETTING_NAME, true); + $value = Settings::set(self::SETTING_NAME, true); $this->assertDatabaseHas('options', [ self::KEY => self::SETTING_NAME, @@ -299,7 +291,7 @@ public function test_set_true_setting_persist_transformed_boolean() */ public function test_set_false_setting_persist_transformed_boolean() { - $value = $this->settingService->set(self::SETTING_NAME, false); + $value = Settings::set(self::SETTING_NAME, false); $this->assertDatabaseHas('options', [ self::KEY => self::SETTING_NAME, @@ -317,7 +309,7 @@ public function test_del_remove_setting_from_db() [self::KEY => self::SETTING_NAME, self::VALUE => strval(self::SETTING_VALUE_STRING)] ); - $value = $this->settingService->delete(self::SETTING_NAME); + $value = Settings::delete(self::SETTING_NAME); $this->assertDatabaseMissing('options', [ self::KEY => self::SETTING_NAME, diff --git a/tests/Unit/Api/v1/Controllers/GroupControllerTest.php b/tests/Unit/Api/v1/Controllers/GroupControllerTest.php index cc23cf79..8a74a229 100644 --- a/tests/Unit/Api/v1/Controllers/GroupControllerTest.php +++ b/tests/Unit/Api/v1/Controllers/GroupControllerTest.php @@ -142,9 +142,8 @@ public function test_accounts_returns_api_resources_fetched_using_groupService() { $group = Group::factory()->make(); - $this->mock(SettingService::class, function (MockInterface $mock) { - $mock->shouldReceive('get') - ->with('useEncryption') + $settingService = $this->mock(SettingService::class, function (MockInterface $settingService) { + $settingService->shouldReceive('get') ->andReturn(false); }); diff --git a/tests/Unit/Events/TwoFAccountDeletedTest.php b/tests/Unit/Events/TwoFAccountDeletedTest.php index 799c1e70..f2166b26 100644 --- a/tests/Unit/Events/TwoFAccountDeletedTest.php +++ b/tests/Unit/Events/TwoFAccountDeletedTest.php @@ -23,21 +23,6 @@ public function test_event_constructor() ->andReturn(false); }); - // SettingService::shouldReceive('get') - // ->andReturn(false); - - // $settingService->shouldReceive('get') - // ->with('useEncryption') - // ->andReturn(false); - - // $settingService->shouldReceive('get') - // ->with('getOfficialIcons') - // ->andReturn(false); - - // \Facades\App\Services\SettingService::shouldReceive('get') - // ->with('useEncryption') - // ->andReturn(false); - $twofaccount = TwoFAccount::factory()->make(); $event = new TwoFAccountDeleted($twofaccount); diff --git a/tests/Unit/Listeners/CleanIconStorageTest.php b/tests/Unit/Listeners/CleanIconStorageTest.php index 7422ba66..40eaa7ed 100644 --- a/tests/Unit/Listeners/CleanIconStorageTest.php +++ b/tests/Unit/Listeners/CleanIconStorageTest.php @@ -21,14 +21,9 @@ public function test_it_deletes_icon_file_on_twofaccount_deletion() { $settingService = $this->mock(SettingService::class, function (MockInterface $settingService) { $settingService->shouldReceive('get') - ->with('useEncryption') ->andReturn(false); }); - // \Facades\App\Services\SettingService::shouldReceive('get') - // ->with('useEncryption') - // ->andReturn(false); - $twofaccount = TwoFAccount::factory()->make(); $event = new TwoFAccountDeleted($twofaccount); $listener = new CleanIconStorage(); diff --git a/tests/Unit/TwoFAccountModelTest.php b/tests/Unit/TwoFAccountModelTest.php index 4458d8b4..d8633ae6 100644 --- a/tests/Unit/TwoFAccountModelTest.php +++ b/tests/Unit/TwoFAccountModelTest.php @@ -50,10 +50,6 @@ public function test_sensitive_attributes_are_stored_encrypted(string $attribute ->andReturn(true); }); - // \Facades\App\Services\SettingService::shouldReceive('get') - // ->with('useEncryption') - // ->andReturn(true); - $twofaccount = TwoFAccount::factory()->make([ $attribute => 'string', ]); @@ -92,10 +88,6 @@ public function test_sensitive_attributes_are_returned_clear(string $attribute) ->andReturn(false); }); - // \Facades\App\Services\SettingService::shouldReceive('get') - // ->with('useEncryption') - // ->andReturn(false); - $twofaccount = TwoFAccount::factory()->make(); $this->assertEquals($twofaccount->getAttributes()[$attribute], $twofaccount->$attribute); @@ -115,10 +107,6 @@ public function test_indecipherable_attributes_returns_masked_value(string $attr ->andReturn(true); }); - // \Facades\App\Services\SettingService::shouldReceive('get') - // ->with('useEncryption') - // ->andReturn(true); - Crypt::shouldReceive('encryptString') ->andReturn('indecipherableString');