From 70682e9f790e84624b7b26ccff9a6b9d4409be11 Mon Sep 17 00:00:00 2001 From: KirinZero0 Date: Tue, 7 Jan 2025 16:34:17 +0800 Subject: [PATCH] #1289: Credentials update rules fix and test coverage --- app/Actions/SysAdmin/Guest/UpdateGuest.php | 9 ++++--- tests/Feature/AikuSections/SysAdminTest.php | 29 ++++++++++++++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/app/Actions/SysAdmin/Guest/UpdateGuest.php b/app/Actions/SysAdmin/Guest/UpdateGuest.php index 57d4f5126..35efe678d 100644 --- a/app/Actions/SysAdmin/Guest/UpdateGuest.php +++ b/app/Actions/SysAdmin/Guest/UpdateGuest.php @@ -48,8 +48,7 @@ public function handle(Guest $guest, array $modelData): Guest } } } - - if ($user = $guest->getUser()) { + if (!empty($credentials) && $user = $guest->getUser()) { UpdateUser::run($user, $credentials); } @@ -122,7 +121,11 @@ public function rules(): array ]; - $rules['password'] = ['sometimes', 'required', app()->isLocal() || app()->environment('testing') ? null : Password::min(8)->uncompromised()]; + $rules['password'] = [ + 'sometimes', + 'required', + app()->isLocal() || app()->environment('testing') ? 'min:8' : Password::min(8)->uncompromised(), + ]; } diff --git a/tests/Feature/AikuSections/SysAdminTest.php b/tests/Feature/AikuSections/SysAdminTest.php index 83abd9950..e774ee628 100644 --- a/tests/Feature/AikuSections/SysAdminTest.php +++ b/tests/Feature/AikuSections/SysAdminTest.php @@ -344,6 +344,33 @@ return $guest; })->depends('create guest from command'); +test('update guest credentials', function ($guest) { + app()->instance('group', $guest->group); + setPermissionsTeamId($guest->group->id); + + $guest = UpdateGuest::make()->action($guest, ['username' => 'testuser']); + expect($guest->getUser()->username)->toBe('testuser') + ->and(Hash::check('hello1234', $guest->getUser()->password))->toBeTrue(); + + $guest = UpdateGuest::make()->action($guest, ['password' => 'testoooo']); + expect($guest->getUser()->username)->toBe('testuser') + ->and(Hash::check('testoooo', $guest->getUser()->password))->toBeTrue(); + + return $guest; +})->depends('update guest'); + +test('update guest status', function ($guest) { + app()->instance('group', $guest->group); + setPermissionsTeamId($guest->group->id); + + $guest = UpdateGuest::make()->action($guest, ['status' => true]); + expect($guest->getUser()->username)->toBe('testuser') + ->and(Hash::check('testoooo', $guest->getUser()->password))->toBeTrue() + ->and($guest->status)->toBeTrue(); + + return $guest; +})->depends('update guest credentials'); + test('fail to create guest with invalid usernames', function (Group $group) { app()->instance('group', $group); setPermissionsTeamId($group->id); @@ -380,7 +407,7 @@ })->depends('update guest'); test('update user username', function (User $user) { - expect($user->username)->toBe('pika'); + expect($user->username)->toBe('testuser'); $user = UpdateUser::make()->action($user, [ 'username' => 'new-username' ]);