From f823f69dd2d1c728a26ba436fa153995bae4a2d9 Mon Sep 17 00:00:00 2001 From: Bubka <858858+Bubka@users.noreply.github.com> Date: Sat, 20 Apr 2024 19:03:44 +0200 Subject: [PATCH] Fix phpstan & pint issues --- .../v1/Controllers/TwoFAccountController.php | 2 +- .../v1/Controllers/UserManagerController.php | 10 +- app/Api/v1/Resources/UserAuthentication.php | 30 +++--- app/Api/v1/Resources/UserManagerResource.php | 2 +- app/Extensions/RemoteUserProvider.php | 2 +- app/Factories/MigratorFactory.php | 12 +-- app/Factories/MigratorFactoryInterface.php | 2 +- app/Models/Group.php | 4 +- app/Models/Option.php | 2 +- app/Models/TwoFAccount.php | 8 +- app/Models/User.php | 18 +--- app/Notifications/SignedInWithNewDevice.php | 9 +- app/Providers/Socialite/OpenId.php | 2 +- app/Services/GroupService.php | 4 +- app/Services/LogoService.php | 6 +- app/Services/Migrators/GoogleAuthMigrator.php | 4 +- app/Services/QrCodeService.php | 9 +- app/Services/SettingService.php | 10 +- app/Services/TwoFAccountService.php | 8 +- .../Controllers/UserManagerControllerTest.php | 87 ++++++++------- tests/Data/AuthenticationLogData.php | 102 +++++++++--------- 21 files changed, 159 insertions(+), 174 deletions(-) diff --git a/app/Api/v1/Controllers/TwoFAccountController.php b/app/Api/v1/Controllers/TwoFAccountController.php index 32d387bb..959c643a 100644 --- a/app/Api/v1/Controllers/TwoFAccountController.php +++ b/app/Api/v1/Controllers/TwoFAccountController.php @@ -311,7 +311,7 @@ public function batchDestroy(TwoFAccountBatchRequest $request) /** * Checks ids length * - * @param string $ids comma-separated ids + * @param string $ids comma-separated ids * @return bool whether or not the number of ids is acceptable */ private function tooManyIds(string $ids) : bool diff --git a/app/Api/v1/Controllers/UserManagerController.php b/app/Api/v1/Controllers/UserManagerController.php index a65c2a9e..779a6432 100644 --- a/app/Api/v1/Controllers/UserManagerController.php +++ b/app/Api/v1/Controllers/UserManagerController.php @@ -130,9 +130,9 @@ public function revokePATs(Request $request, User $user, TokenRepository $tokenR $tokens = $tokenRepository->forUser($user->getAuthIdentifier()); $tokens->load('client')->filter(function ($token) { - return $token->client->personal_access_client && ! $token->revoked; + return $token->client->personal_access_client && ! $token->revoked; /** @phpstan-ignore-line */ })->each(function ($token) { - $token->revoke(); + $token->revoke(); /** @phpstan-ignore-line */ }); Log::info(sprintf('All personal access tokens for User ID #%s have been revoked', $user->id)); @@ -188,7 +188,7 @@ public function destroy(Request $request, User $user) /** * Promote (or demote) a user * - * @return \App\Api\v1\Resources\UserManagerResource + * @return \App\Api\v1\Resources\UserManagerResource|\Illuminate\Http\JsonResponse */ public function promote(UserManagerPromoteRequest $request, User $user) { @@ -209,7 +209,7 @@ public function promote(UserManagerPromoteRequest $request, User $user) /** * Get the user's authentication logs * - * @return \Illuminate\Http\JsonResponse + * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection */ public function authentications(Request $request, User $user) { @@ -217,7 +217,7 @@ public function authentications(Request $request, User $user) $validated = $this->validate($request, [ 'period' => 'sometimes|numeric', - 'limit' => 'sometimes|numeric', + 'limit' => 'sometimes|numeric', ]); $authentications = $request->has('period') ? $user->authenticationsByPeriod($validated['period']) : $user->authentications; diff --git a/app/Api/v1/Resources/UserAuthentication.php b/app/Api/v1/Resources/UserAuthentication.php index 65585551..240cd457 100644 --- a/app/Api/v1/Resources/UserAuthentication.php +++ b/app/Api/v1/Resources/UserAuthentication.php @@ -9,11 +9,15 @@ /** * @property mixed $id - * @property string $name - * @property string $email - * @property string $oauth_provider - * @property \Illuminate\Support\Collection $preferences - * @property string $is_admin + * @property string $ip_address + * @property string $user_agent + * @property string $browser + * @property string $platform + * @property string $device + * @property Carbon|null $login_at + * @property Carbon|null $logout_at + * @property bool $login_successful + * @property string|null $duration */ class UserAuthentication extends JsonResource { @@ -47,16 +51,16 @@ public function __construct($resource) public function toArray($request) { return [ - 'id' => $this->id, - 'ip_address' => $this->ip_address, - 'user_agent' => $this->user_agent, - 'browser' => $this->agent->browser(), - 'platform' => $this->agent->platform(), - 'device' => $this->agent->deviceType(), - 'login_at' => $this->login_at + 'id' => $this->id, + 'ip_address' => $this->ip_address, + 'user_agent' => $this->user_agent, + 'browser' => $this->agent->browser(), + 'platform' => $this->agent->platform(), + 'device' => $this->agent->deviceType(), + 'login_at' => $this->login_at ? Carbon::parse($this->login_at)->toDayDateTimeString() : null, - 'logout_at' => $this->logout_at + 'logout_at' => $this->logout_at ? Carbon::parse($this->logout_at)->toDayDateTimeString() : null, 'login_successful' => $this->login_successful, diff --git a/app/Api/v1/Resources/UserManagerResource.php b/app/Api/v1/Resources/UserManagerResource.php index ee3d2413..a5689f7e 100644 --- a/app/Api/v1/Resources/UserManagerResource.php +++ b/app/Api/v1/Resources/UserManagerResource.php @@ -55,7 +55,7 @@ public function __construct($resource) $tokens = $tokenRepository->forUser($this->resource->getAuthIdentifier()); $PATs_count = $tokens->load('client')->filter(function ($token) { - return $token->client->personal_access_client && ! $token->revoked; + return $token->client->personal_access_client && ! $token->revoked; /** @phpstan-ignore-line */ })->count(); $this->with = [ diff --git a/app/Extensions/RemoteUserProvider.php b/app/Extensions/RemoteUserProvider.php index b43e7f5a..e7ce6df9 100644 --- a/app/Extensions/RemoteUserProvider.php +++ b/app/Extensions/RemoteUserProvider.php @@ -80,7 +80,7 @@ public function retrieveById($identifier) /** * Set a fake email address * - * @param $id mixed + * @param $id mixed * @return string */ protected function fakeRemoteEmail(mixed $id) diff --git a/app/Factories/MigratorFactory.php b/app/Factories/MigratorFactory.php index 1b71df20..0f104515 100644 --- a/app/Factories/MigratorFactory.php +++ b/app/Factories/MigratorFactory.php @@ -19,7 +19,7 @@ class MigratorFactory implements MigratorFactoryInterface /** * Infer the type of migrator needed from a payload and create the migrator * - * @param string $migrationPayload The migration payload used to infer the migrator type + * @param string $migrationPayload The migration payload used to infer the migrator type */ public function create(string $migrationPayload) : Migrator { @@ -41,7 +41,7 @@ public function create(string $migrationPayload) : Migrator /** * Determine if a payload comes from Google Authenticator * - * @param string $migrationPayload The payload to analyse + * @param string $migrationPayload The payload to analyse */ private function isGoogleAuth(string $migrationPayload) : bool { @@ -59,7 +59,7 @@ private function isGoogleAuth(string $migrationPayload) : bool /** * Determine if a payload is a plain text content * - * @param string $migrationPayload The payload to analyse + * @param string $migrationPayload The payload to analyse */ private function isPlainText(string $migrationPayload) : bool { @@ -77,7 +77,7 @@ private function isPlainText(string $migrationPayload) : bool /** * Determine if a payload comes from 2FAuth in JSON format * - * @param string $migrationPayload The payload to analyse + * @param string $migrationPayload The payload to analyse */ private function isTwoFAuthJSON(string $migrationPayload) : bool { @@ -105,7 +105,7 @@ private function isTwoFAuthJSON(string $migrationPayload) : bool /** * Determine if a payload comes from Aegis Authenticator in JSON format * - * @param string $migrationPayload The payload to analyse + * @param string $migrationPayload The payload to analyse * @return bool */ private function isAegisJSON(string $migrationPayload) : mixed @@ -150,7 +150,7 @@ private function isAegisJSON(string $migrationPayload) : mixed /** * Determine if a payload comes from 2FAS Authenticator * - * @param string $migrationPayload The payload to analyse + * @param string $migrationPayload The payload to analyse * @return bool */ private function is2FASv2(string $migrationPayload) : mixed diff --git a/app/Factories/MigratorFactoryInterface.php b/app/Factories/MigratorFactoryInterface.php index b5751ff4..e29134bb 100644 --- a/app/Factories/MigratorFactoryInterface.php +++ b/app/Factories/MigratorFactoryInterface.php @@ -9,7 +9,7 @@ interface MigratorFactoryInterface /** * Infer the type of migrator needed from a payload and create the migrator * - * @param string $migrationPayload The migration payload used to infer the migrator type + * @param string $migrationPayload The migration payload used to infer the migrator type */ public function create(string $migrationPayload) : Migrator; } diff --git a/app/Models/Group.php b/app/Models/Group.php index 5af0b0ba..e8e0006d 100644 --- a/app/Models/Group.php +++ b/app/Models/Group.php @@ -27,14 +27,14 @@ class Group extends Model /** * model's array form. * - * @var string[] + * @var array */ protected $fillable = ['name']; /** * The accessors to append to the model's array form. * - * @var array + * @var array */ protected $appends = []; diff --git a/app/Models/Option.php b/app/Models/Option.php index 5c290614..69ba8c0d 100644 --- a/app/Models/Option.php +++ b/app/Models/Option.php @@ -16,7 +16,7 @@ class Option extends Model /** * The attributes that are mass assignable. * - * @var string[] + * @var array */ protected $fillable = [ 'key', diff --git a/app/Models/TwoFAccount.php b/app/Models/TwoFAccount.php index b616ee7c..54abb1a8 100644 --- a/app/Models/TwoFAccount.php +++ b/app/Models/TwoFAccount.php @@ -95,7 +95,7 @@ class TwoFAccount extends Model implements Sortable /** * model's array form. * - * @var string[] + * @var array */ protected $fillable = [ // 'service', @@ -119,7 +119,7 @@ class TwoFAccount extends Model implements Sortable /** * The accessors to append to the model's array form. * - * @var array + * @var array */ public $appends = []; @@ -611,7 +611,7 @@ private function initGenerator() : void * Store and set the provided icon * * @param \Psr\Http\Message\StreamInterface|\Illuminate\Http\File|\Illuminate\Http\UploadedFile|string|resource $data - * @param string|null $extension The resource extension, without the dot + * @param string|null $extension The resource extension, without the dot */ public function setIcon($data, $extension = null) : void { @@ -633,7 +633,7 @@ public function setIcon($data, $extension = null) : void * Store img data as an icon file. * * @param \Psr\Http\Message\StreamInterface|\Illuminate\Http\File|\Illuminate\Http\UploadedFile|string|resource $content - * @param string $extension The file extension, without the dot + * @param string $extension The file extension, without the dot * @return string|null The filename of the stored icon or null if the operation fails */ private function storeFileDataAsIcon($content, $extension) : ?string diff --git a/app/Models/User.php b/app/Models/User.php index d19aa188..cf8402e7 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -46,15 +46,9 @@ * @property-read \Illuminate\Database\Eloquent\Collection $authentications * @property-read int|null $authentications_count * @property-read \Bubka\LaravelAuthenticationLog\Models\AuthenticationLog|null $latestAuthentication + * * @method static \Illuminate\Database\Eloquent\Builder|User admins() - * @method \Illuminate\Support\Carbon|null latestAuthentication() - * @method \Illuminate\Support\Carbon|null lastLoginAt() - * @method \Illuminate\Support\Carbon|null lastSuccessfulLoginAt() - * @method \Illuminate\Support\Carbon|null lastLoginIp() - * @method \Illuminate\Support\Carbon|null lastSuccessfulLoginIp() - * @method \Illuminate\Support\Carbon|null previousLoginAt() - * @method \Illuminate\Support\Carbon|null previousLoginIp() - * @method \Illuminate\Support\Collection authenticationsByPeriod() + * * @mixin \Eloquent */ class User extends Authenticatable implements HasLocalePreference, WebAuthnAuthenticatable @@ -66,7 +60,7 @@ class User extends Authenticatable implements HasLocalePreference, WebAuthnAuthe /** * The attributes that are mass assignable. * - * @var string[] + * @var array */ protected $fillable = [ 'name', 'email', 'password', 'oauth_id', 'oauth_provider', @@ -148,15 +142,13 @@ public function promoteToAdministrator(bool $promote = true) : bool /** * Say if the user is the only registered administrator * - * @return void + * @return bool */ public function isLastAdministrator() { $admins = User::admins()->get(); - $toto = $admins->contains($this->id) && $admins->count() === 1; - - return $toto; + return $admins->contains($this->id) && $admins->count() === 1; } /** diff --git a/app/Notifications/SignedInWithNewDevice.php b/app/Notifications/SignedInWithNewDevice.php index 0e401ca4..59ad12c6 100644 --- a/app/Notifications/SignedInWithNewDevice.php +++ b/app/Notifications/SignedInWithNewDevice.php @@ -2,12 +2,12 @@ namespace App\Notifications; +use Bubka\LaravelAuthenticationLog\Models\AuthenticationLog; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification; use Jenssegers\Agent\Agent; -use Bubka\LaravelAuthenticationLog\Models\AuthenticationLog; class SignedInWithNewDevice extends Notification implements ShouldQueue { @@ -32,10 +32,7 @@ public function __construct(AuthenticationLog $authenticationLog) $this->agent->setUserAgent($authenticationLog->user_agent); } - /** - * - */ - public function via($notifiable) + public function via(mixed $notifiable) : array|string { return $notifiable->notifyAuthenticationLogVia(); } @@ -43,7 +40,7 @@ public function via($notifiable) /** * Wrap the notification to a mail envelop */ - public function toMail($notifiable) + public function toMail(mixed $notifiable) : MailMessage { return (new MailMessage()) ->subject(__('notifications.new_device.subject')) diff --git a/app/Providers/Socialite/OpenId.php b/app/Providers/Socialite/OpenId.php index d0786abe..668d1b90 100644 --- a/app/Providers/Socialite/OpenId.php +++ b/app/Providers/Socialite/OpenId.php @@ -58,7 +58,7 @@ protected function getUserByToken($token) */ public function refreshToken($refreshToken) { - return $this->getHttpClient()->post($this->getTokenUrl(), [ + return $this->getHttpClient()->post($this->getTokenUrl(), [/** @phpstan-ignore-line */ RequestOptions::FORM_PARAMS => [ 'client_id' => $this->clientId, 'client_secret' => $this->clientSecret, diff --git a/app/Services/GroupService.php b/app/Services/GroupService.php index dd9a71eb..b9761bbb 100644 --- a/app/Services/GroupService.php +++ b/app/Services/GroupService.php @@ -14,8 +14,8 @@ class GroupService /** * Assign one or more accounts to a group * - * @param array|int $ids accounts ids to assign - * @param \App\Models\Group|null $group The group the accounts will be assigned to + * @param array|int $ids accounts ids to assign + * @param \App\Models\Group|null $group The group the accounts will be assigned to * * @throws \Illuminate\Auth\Access\AuthorizationException */ diff --git a/app/Services/LogoService.php b/app/Services/LogoService.php index 13094a98..764fcf3d 100644 --- a/app/Services/LogoService.php +++ b/app/Services/LogoService.php @@ -33,7 +33,7 @@ public function __construct() /** * Fetch a logo for the given service and save it as an icon * - * @param string $serviceName Name of the service to fetch a logo for + * @param string $serviceName Name of the service to fetch a logo for * @return string|null The icon filename or null if no logo has been found */ public function getIcon($serviceName) @@ -52,7 +52,7 @@ public function getIcon($serviceName) /** * Return the logo's filename for a given service * - * @param string $serviceName Name of the service to fetch a logo for + * @param string $serviceName Name of the service to fetch a logo for * @return string|null The logo filename or null if no logo has been found */ protected function getLogo($serviceName) @@ -114,7 +114,7 @@ protected function cacheTfaDirectorySource() : void /** * Fetch and cache a logo from 2fa.Directory repository * - * @param string $logoFile Logo filename to fetch + * @param string $logoFile Logo filename to fetch */ protected function fetchLogo(string $logoFile) : void { diff --git a/app/Services/Migrators/GoogleAuthMigrator.php b/app/Services/Migrators/GoogleAuthMigrator.php index c998b573..2c4d0d7e 100644 --- a/app/Services/Migrators/GoogleAuthMigrator.php +++ b/app/Services/Migrators/GoogleAuthMigrator.php @@ -20,13 +20,13 @@ class GoogleAuthMigrator extends Migrator /** * Convert Google Authenticator migration URI to a set of TwoFAccount objects. * - * @param mixed $migrationPayload migration uri provided by Google Authenticator export feature + * @param mixed $migrationPayload migration uri provided by Google Authenticator export feature * @return \Illuminate\Support\Collection The converted accounts */ public function migrate(mixed $migrationPayload) : Collection { try { - $migrationData = base64_decode(urldecode(Str::replace('otpauth-migration://offline?data=', '', $migrationPayload))); + $migrationData = base64_decode(urldecode(Str::replace('otpauth-migration://offline?data=', '', strval($migrationPayload)))); $protobuf = new Payload(); $protobuf->mergeFromString($migrationData); $otpParameters = $protobuf->getOtpParameters(); diff --git a/app/Services/QrCodeService.php b/app/Services/QrCodeService.php index db9b69c5..aa195cbc 100644 --- a/app/Services/QrCodeService.php +++ b/app/Services/QrCodeService.php @@ -15,7 +15,7 @@ class QrCodeService /** * Encode a string into a QR code image * - * @param string $data The string to encode + * @param string $data The string to encode * @return mixed */ public static function encode(string $data) @@ -55,19 +55,12 @@ public static function decode(\Illuminate\Http\UploadedFile $file) switch (get_class($qrcode->getError())) { case NotFoundException::class: throw new \App\Exceptions\InvalidQrCodeException(__('errors.cannot_detect_qrcode_in_image')); - break; - case FormatException::class: throw new \App\Exceptions\InvalidQrCodeException(__('errors.cannot_decode_detected_qrcode')); - break; - case ChecksumException::class: throw new \App\Exceptions\InvalidQrCodeException(__('errors.qrcode_has_invalid_checksum')); - break; - default: throw new \App\Exceptions\InvalidQrCodeException(__('errors.no_readable_qrcode')); - break; } } diff --git a/app/Services/SettingService.php b/app/Services/SettingService.php index 02a433e2..5964cae7 100644 --- a/app/Services/SettingService.php +++ b/app/Services/SettingService.php @@ -46,7 +46,7 @@ public function __construct() /** * Get a setting * - * @param string $setting A single setting name + * @param string $setting A single setting name * @return mixed string|int|boolean|null */ public function get($setting) @@ -67,8 +67,8 @@ public function all() : Collection /** * Set a setting * - * @param string|array $setting A single setting name or an associative array of name:value settings - * @param string|int|bool|null $value The value for single setting + * @param string|array $setting A single setting name or an associative array of name:value settings + * @param string|int|bool|null $value The value for single setting */ public function set($setting, $value = null) : void { @@ -93,7 +93,7 @@ public function set($setting, $value = null) : void /** * Delete a setting * - * @param string $name The setting name + * @param string $name The setting name */ public function delete(string $name) : void { @@ -200,7 +200,7 @@ private function setEncryptionTo(bool $state) : void /** * Encrypt/Decrypt accounts in database * - * @param bool $encrypted Whether the record should be encrypted or not + * @param bool $encrypted Whether the record should be encrypted or not * @return bool Whether the operation completed successfully */ private function updateRecords(bool $encrypted) : bool diff --git a/app/Services/TwoFAccountService.php b/app/Services/TwoFAccountService.php index fe11ded5..451621f7 100644 --- a/app/Services/TwoFAccountService.php +++ b/app/Services/TwoFAccountService.php @@ -28,7 +28,7 @@ public function __construct(MigratorFactoryInterface $migratorFactory) /** * Withdraw one or more twofaccounts from their group * - * @param int|array|string $ids twofaccount ids to free + * @param int|array|string $ids twofaccount ids to free */ public static function withdraw($ids) : void { @@ -50,7 +50,7 @@ public static function withdraw($ids) : void /** * Convert a migration payload to a set of TwoFAccount objects * - * @param string $migrationPayload Migration payload from 2FA apps export feature + * @param string $migrationPayload Migration payload from 2FA apps export feature * @return \Illuminate\Support\Collection The converted accounts */ public function migrate(string $migrationPayload) : Collection @@ -64,7 +64,7 @@ public function migrate(string $migrationPayload) : Collection /** * Export one or more twofaccounts * - * @param int|array|string $ids twofaccount ids to delete + * @param int|array|string $ids twofaccount ids to delete * @return \Illuminate\Support\Collection The converted accounts */ public static function export($ids) : Collection @@ -80,7 +80,7 @@ public static function export($ids) : Collection /** * Delete one or more twofaccounts * - * @param int|array|string $ids twofaccount ids to delete + * @param int|array|string $ids twofaccount ids to delete * @return int The number of deleted */ public static function delete($ids) : int diff --git a/tests/Api/v1/Controllers/UserManagerControllerTest.php b/tests/Api/v1/Controllers/UserManagerControllerTest.php index a927df9c..dfc40872 100644 --- a/tests/Api/v1/Controllers/UserManagerControllerTest.php +++ b/tests/Api/v1/Controllers/UserManagerControllerTest.php @@ -561,7 +561,7 @@ protected function feedAuthenticationLog() : int public function test_authentications_returns_all_entries() : void { $created = $this->feedAuthenticationLog(); - + $this->actingAs($this->admin, 'api-guard') ->json('GET', '/api/v1/users/' . $this->user->id . '/authentications') ->assertOk() @@ -574,7 +574,7 @@ public function test_authentications_returns_all_entries() : void public function test_authentications_returns_expected_resource() : void { $this->user->authentications()->create(AuthenticationLogData::duringLastMonth()); - + $this->actingAs($this->admin, 'api-guard') ->json('GET', '/api/v1/users/' . $this->user->id . '/authentications') ->assertJsonStructure([ @@ -589,7 +589,7 @@ public function test_authentications_returns_expected_resource() : void 'logout_at', 'login_successful', 'duration', - ] + ], ]); } @@ -599,12 +599,12 @@ public function test_authentications_returns_expected_resource() : void public function test_authentications_returns_no_login_entry() : void { $this->user->authentications()->create(AuthenticationLogData::noLogin()); - + $this->actingAs($this->admin, 'api-guard') ->json('GET', '/api/v1/users/' . $this->user->id . '/authentications?period=1') ->assertJsonCount(1) ->assertJsonFragment([ - 'login_at' => null + 'login_at' => null, ]); } @@ -614,12 +614,12 @@ public function test_authentications_returns_no_login_entry() : void public function test_authentications_returns_no_logout_entry() : void { $this->user->authentications()->create(AuthenticationLogData::noLogout()); - + $this->actingAs($this->admin, 'api-guard') ->json('GET', '/api/v1/users/' . $this->user->id . '/authentications?period=1') ->assertJsonCount(1) ->assertJsonFragment([ - 'logout_at' => null + 'logout_at' => null, ]); } @@ -630,12 +630,12 @@ public function test_authentications_returns_failed_entry() : void { $this->user->authentications()->create(AuthenticationLogData::failedLogin()); $expected = Carbon::parse(AuthenticationLogData::failedLogin()['login_at'])->toDayDateTimeString(); - + $this->actingAs($this->admin, 'api-guard') ->json('GET', '/api/v1/users/' . $this->user->id . '/authentications?period=1') ->assertJsonCount(1) ->assertJsonFragment([ - 'login_at' => $expected, + 'login_at' => $expected, 'login_successful' => false, ]); } @@ -647,12 +647,12 @@ public function test_authentications_returns_last_month_entries() : void { $this->feedAuthenticationLog(); $expected = Carbon::parse(AuthenticationLogData::duringLastMonth()['login_at'])->toDayDateTimeString(); - + $this->actingAs($this->admin, 'api-guard') ->json('GET', '/api/v1/users/' . $this->user->id . '/authentications?period=1') ->assertJsonCount(3) ->assertJsonFragment([ - 'login_at' => $expected + 'login_at' => $expected, ]); } @@ -662,17 +662,17 @@ public function test_authentications_returns_last_month_entries() : void public function test_authentications_returns_last_three_months_entries() : void { $this->feedAuthenticationLog(); - $expectedOneMonth = Carbon::parse(AuthenticationLogData::duringLastMonth()['login_at'])->toDayDateTimeString(); + $expectedOneMonth = Carbon::parse(AuthenticationLogData::duringLastMonth()['login_at'])->toDayDateTimeString(); $expectedThreeMonth = Carbon::parse(AuthenticationLogData::duringLastThreeMonth()['login_at'])->toDayDateTimeString(); - + $this->actingAs($this->admin, 'api-guard') ->json('GET', '/api/v1/users/' . $this->user->id . '/authentications?period=3') ->assertJsonCount(4) ->assertJsonFragment([ - 'login_at' => $expectedOneMonth + 'login_at' => $expectedOneMonth, ]) ->assertJsonFragment([ - 'login_at' => $expectedThreeMonth + 'login_at' => $expectedThreeMonth, ]); } @@ -682,21 +682,21 @@ public function test_authentications_returns_last_three_months_entries() : void public function test_authentications_returns_last_six_months_entries() : void { $this->feedAuthenticationLog(); - $expectedOneMonth = Carbon::parse(AuthenticationLogData::duringLastMonth()['login_at'])->toDayDateTimeString(); + $expectedOneMonth = Carbon::parse(AuthenticationLogData::duringLastMonth()['login_at'])->toDayDateTimeString(); $expectedThreeMonth = Carbon::parse(AuthenticationLogData::duringLastThreeMonth()['login_at'])->toDayDateTimeString(); - $expectedSixMonth = Carbon::parse(AuthenticationLogData::duringLastSixMonth()['login_at'])->toDayDateTimeString(); - + $expectedSixMonth = Carbon::parse(AuthenticationLogData::duringLastSixMonth()['login_at'])->toDayDateTimeString(); + $this->actingAs($this->admin, 'api-guard') ->json('GET', '/api/v1/users/' . $this->user->id . '/authentications?period=6') ->assertJsonCount(5) ->assertJsonFragment([ - 'login_at' => $expectedOneMonth + 'login_at' => $expectedOneMonth, ]) ->assertJsonFragment([ - 'login_at' => $expectedThreeMonth + 'login_at' => $expectedThreeMonth, ]) ->assertJsonFragment([ - 'login_at' => $expectedSixMonth + 'login_at' => $expectedSixMonth, ]); } @@ -706,25 +706,25 @@ public function test_authentications_returns_last_six_months_entries() : void public function test_authentications_returns_last_year_entries() : void { $this->feedAuthenticationLog(); - $expectedOneMonth = Carbon::parse(AuthenticationLogData::duringLastMonth()['login_at'])->toDayDateTimeString(); + $expectedOneMonth = Carbon::parse(AuthenticationLogData::duringLastMonth()['login_at'])->toDayDateTimeString(); $expectedThreeMonth = Carbon::parse(AuthenticationLogData::duringLastThreeMonth()['login_at'])->toDayDateTimeString(); - $expectedSixMonth = Carbon::parse(AuthenticationLogData::duringLastSixMonth()['login_at'])->toDayDateTimeString(); - $expectedYear = Carbon::parse(AuthenticationLogData::duringLastYear()['login_at'])->toDayDateTimeString(); - + $expectedSixMonth = Carbon::parse(AuthenticationLogData::duringLastSixMonth()['login_at'])->toDayDateTimeString(); + $expectedYear = Carbon::parse(AuthenticationLogData::duringLastYear()['login_at'])->toDayDateTimeString(); + $this->actingAs($this->admin, 'api-guard') ->json('GET', '/api/v1/users/' . $this->user->id . '/authentications?period=12') ->assertJsonCount(6) ->assertJsonFragment([ - 'login_at' => $expectedOneMonth + 'login_at' => $expectedOneMonth, ]) ->assertJsonFragment([ - 'login_at' => $expectedThreeMonth + 'login_at' => $expectedThreeMonth, ]) ->assertJsonFragment([ - 'login_at' => $expectedSixMonth + 'login_at' => $expectedSixMonth, ]) ->assertJsonFragment([ - 'login_at' => $expectedYear + 'login_at' => $expectedYear, ]); } @@ -735,7 +735,7 @@ public function test_authentications_returns_last_year_entries() : void public function test_authentications_returns_limited_entries($limit) : void { $this->feedAuthenticationLog(); - + $this->actingAs($this->admin, 'api-guard') ->json('GET', '/api/v1/users/' . $this->user->id . '/authentications?limit=' . $limit) ->assertOk() @@ -760,21 +760,21 @@ public static function LimitProvider() public function test_authentications_returns_expected_ip_and_useragent_chunks() : void { $this->user->authentications()->create([ - 'ip_address' => '127.0.0.1', - 'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0', - 'login_at' => now(), + 'ip_address' => '127.0.0.1', + 'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0', + 'login_at' => now(), 'login_successful' => true, - 'logout_at' => null, - 'location' => null, + 'logout_at' => null, + 'location' => null, ]); - + $this->actingAs($this->admin, 'api-guard') ->json('GET', '/api/v1/users/' . $this->user->id . '/authentications?period=1') ->assertJsonFragment([ 'ip_address' => '127.0.0.1', - 'browser' => 'Firefox', - 'platform' => 'Windows', - 'device' => 'desktop', + 'browser' => 'Firefox', + 'platform' => 'Windows', + 'device' => 'desktop', ]); } @@ -806,12 +806,11 @@ public function test_authentications_with_invalid_period_returns_validation_erro public static function invalidQueryParameterProvider() { return [ - 'empty' => [''], - 'null' => ['null'], + 'empty' => [''], + 'null' => ['null'], 'boolean' => ['true'], - 'string' => ['string'], - 'array' => ['[]'], + 'string' => ['string'], + 'array' => ['[]'], ]; } - } diff --git a/tests/Data/AuthenticationLogData.php b/tests/Data/AuthenticationLogData.php index e6de0f26..334cda23 100644 --- a/tests/Data/AuthenticationLogData.php +++ b/tests/Data/AuthenticationLogData.php @@ -12,14 +12,14 @@ class AuthenticationLogData public static function failedLogin() { $loginDate = now()->subDays(15); - + return [ - 'ip_address' => fake()->ipv4(), - 'user_agent' => fake()->userAgent(), - 'login_at' => $loginDate, + 'ip_address' => fake()->ipv4(), + 'user_agent' => fake()->userAgent(), + 'login_at' => $loginDate, 'login_successful' => false, - 'logout_at' => null, - 'location' => null, + 'logout_at' => null, + 'location' => null, ]; } @@ -31,12 +31,12 @@ public static function failedLogin() public static function noLogin() { return [ - 'ip_address' => fake()->ipv4(), - 'user_agent' => fake()->userAgent(), - 'login_at' => null, + 'ip_address' => fake()->ipv4(), + 'user_agent' => fake()->userAgent(), + 'login_at' => null, 'login_successful' => false, - 'logout_at' => now(), - 'location' => null, + 'logout_at' => now(), + 'location' => null, ]; } @@ -48,12 +48,12 @@ public static function noLogin() public static function noLogout() { return [ - 'ip_address' => fake()->ipv4(), - 'user_agent' => fake()->userAgent(), - 'login_at' => now(), + 'ip_address' => fake()->ipv4(), + 'user_agent' => fake()->userAgent(), + 'login_at' => now(), 'login_successful' => true, - 'logout_at' => null, - 'location' => null, + 'logout_at' => null, + 'location' => null, ]; } @@ -64,16 +64,16 @@ public static function noLogout() */ public static function duringLastMonth() { - $loginDate = now()->subDays(15); + $loginDate = now()->subDays(15); $logoutDate = $loginDate->addHours(1); - + return [ - 'ip_address' => '127.0.0.1', - 'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0', - 'login_at' => $loginDate, + 'ip_address' => '127.0.0.1', + 'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0', + 'login_at' => $loginDate, 'login_successful' => true, - 'logout_at' => $logoutDate, - 'location' => null, + 'logout_at' => $logoutDate, + 'location' => null, ]; } @@ -84,16 +84,16 @@ public static function duringLastMonth() */ public static function duringLastThreeMonth() { - $loginDate = now()->subMonths(2); + $loginDate = now()->subMonths(2); $logoutDate = $loginDate->addHours(1); - + return [ - 'ip_address' => fake()->ipv4(), - 'user_agent' => fake()->userAgent(), - 'login_at' => $loginDate, + 'ip_address' => fake()->ipv4(), + 'user_agent' => fake()->userAgent(), + 'login_at' => $loginDate, 'login_successful' => true, - 'logout_at' => $logoutDate, - 'location' => null, + 'logout_at' => $logoutDate, + 'location' => null, ]; } @@ -104,16 +104,16 @@ public static function duringLastThreeMonth() */ public static function duringLastSixMonth() { - $loginDate = now()->subMonths(4); + $loginDate = now()->subMonths(4); $logoutDate = $loginDate->addHours(1); - + return [ - 'ip_address' => fake()->ipv4(), - 'user_agent' => fake()->userAgent(), - 'login_at' => $loginDate, + 'ip_address' => fake()->ipv4(), + 'user_agent' => fake()->userAgent(), + 'login_at' => $loginDate, 'login_successful' => true, - 'logout_at' => $logoutDate, - 'location' => null, + 'logout_at' => $logoutDate, + 'location' => null, ]; } @@ -124,16 +124,16 @@ public static function duringLastSixMonth() */ public static function duringLastYear() { - $loginDate = now()->subMonths(10); + $loginDate = now()->subMonths(10); $logoutDate = $loginDate->addHours(1); - + return [ - 'ip_address' => fake()->ipv4(), - 'user_agent' => fake()->userAgent(), - 'login_at' => $loginDate, + 'ip_address' => fake()->ipv4(), + 'user_agent' => fake()->userAgent(), + 'login_at' => $loginDate, 'login_successful' => true, - 'logout_at' => $logoutDate, - 'location' => null, + 'logout_at' => $logoutDate, + 'location' => null, ]; } @@ -144,16 +144,16 @@ public static function duringLastYear() */ public static function beforeLastYear() { - $loginDate = now()->subYears(2); + $loginDate = now()->subYears(2); $logoutDate = $loginDate->addHours(1); - + return [ - 'ip_address' => fake()->ipv4(), - 'user_agent' => fake()->userAgent(), - 'login_at' => $loginDate, + 'ip_address' => fake()->ipv4(), + 'user_agent' => fake()->userAgent(), + 'login_at' => $loginDate, 'login_successful' => true, - 'logout_at' => $logoutDate, - 'location' => null, + 'logout_at' => $logoutDate, + 'location' => null, ]; } }