Skip to content

Commit

Permalink
Enhance test coverage & Fix small issues & Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Bubka committed Jun 28, 2024
1 parent 413e2c4 commit 0f1372e
Show file tree
Hide file tree
Showing 38 changed files with 1,274 additions and 163 deletions.
4 changes: 0 additions & 4 deletions app/Api/v1/Controllers/TwoFAccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ public function index(TwoFAccountIndexRequest $request)

$validated = $request->validated();

// if ($request->has('withOtp')) {
// $request->merge(['at' => time()]);
// }

return Arr::has($validated, 'ids')
? new TwoFAccountCollection($request->user()->twofaccounts()->whereIn('id', Helpers::commaSeparatedToArray($validated['ids']))->get()->sortBy('order_column'))
: new TwoFAccountCollection($request->user()->twofaccounts->sortBy('order_column'));
Expand Down
4 changes: 2 additions & 2 deletions app/Api/v1/Controllers/UserManagerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Api\v1\Requests\UserManagerPromoteRequest;
use App\Api\v1\Requests\UserManagerStoreRequest;
use App\Api\v1\Resources\UserAuthentication;
use App\Api\v1\Resources\UserAuthenticationResource;
use App\Api\v1\Resources\UserManagerResource;
use App\Http\Controllers\Controller;
use App\Models\User;
Expand Down Expand Up @@ -231,7 +231,7 @@ public function authentications(Request $request, User $user)
$authentications = $request->has('period') ? $user->authenticationsByPeriod($validated['period']) : $user->authentications;
$authentications = $request->has('limit') ? $authentications->take($validated['limit']) : $authentications;

return UserAuthentication::collection($authentications);
return UserAuthenticationResource::collection($authentications);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Api/v1/Requests/IconFetchRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function authorize()
public function rules()
{
return [
'service' => 'string|regex:/^[^:]+$/i',
'service' => 'string',
];
}

Expand All @@ -39,7 +39,7 @@ public function rules()
protected function prepareForValidation()
{
$this->merge([
'service' => strip_tags($this->service),
'service' => strip_tags(strval($this->service)),
]);
}
}
5 changes: 4 additions & 1 deletion app/Api/v1/Resources/TwoFAccountCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ public function toArray($request)
$request->merge(['withSecret' => false]);
}

// Here we add a timestamp to the request if OTPs have to be in the response.
// The 'at' parameter is used by the TwoFAccountReadResource class to obtain
// all OTPs at the same timestamps
if ($request->has('withOtp')) {
$request->merge(['at' => time()]);
$request->merge(['at' => now()->timestamp]);
}

return $this->collection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @property string|null $duration
* @property string|null $login_method
*/
class UserAuthentication extends JsonResource
class UserAuthenticationResource extends JsonResource
{
/**
* A user agent parser instance.
Expand Down
4 changes: 2 additions & 2 deletions app/Listeners/Authentication/FailedLoginListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace App\Listeners\Authentication;

use App\Notifications\FailedLogin;
use App\Notifications\FailedLoginNotification;
use Illuminate\Auth\Events\Failed;

class FailedLoginListener extends AbstractAccessListener
Expand Down Expand Up @@ -56,7 +56,7 @@ public function handle(mixed $event) : void
]);

if ($user->preferences['notifyOnFailedLogin'] == true) {
$user->notify(new FailedLogin($log));
$user->notify(new FailedLoginNotification($log));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions app/Listeners/Authentication/LoginListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace App\Listeners\Authentication;

use App\Notifications\SignedInWithNewDevice;
use App\Notifications\SignedInWithNewDeviceNotification;
use Illuminate\Auth\Events\Login;
use Illuminate\Support\Carbon;

Expand Down Expand Up @@ -59,7 +59,7 @@ public function handle(mixed $event) : void
]);

if (! $known && ! $newUser && $user->preferences['notifyOnNewAuthDevice'] == true) {
$user->notify(new SignedInWithNewDevice($log));
$user->notify(new SignedInWithNewDeviceNotification($log));
}
}
}
4 changes: 2 additions & 2 deletions app/Listeners/Authentication/VisitedByProxyUserListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Events\VisitedByProxyUser;
use App\Extensions\RemoteUserProvider;
use App\Notifications\SignedInWithNewDevice;
use App\Notifications\SignedInWithNewDeviceNotification;
use Illuminate\Support\Carbon;

class VisitedByProxyUserListener extends AbstractAccessListener
Expand Down Expand Up @@ -37,7 +37,7 @@ public function handle(mixed $event) : void
]);

if (! $known && ! $newUser && ! str_ends_with($user->email, RemoteUserProvider::FAKE_REMOTE_DOMAIN) && $user->preferences['notifyOnNewAuthDevice']) {
$user->notify(new SignedInWithNewDevice($log));
$user->notify(new SignedInWithNewDeviceNotification($log));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Notifications\Events\NotificationSent;
use Illuminate\Support\Facades\Log;

class LogNotification
class LogNotificationListener
{
/**
* Create the event listener.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Illuminate\Notifications\Notification;
use Jenssegers\Agent\Agent;

class FailedLogin extends Notification implements ShouldQueue
class FailedLoginNotification extends Notification implements ShouldQueue
{
use Queueable;

Expand All @@ -26,7 +26,7 @@ class FailedLogin extends Notification implements ShouldQueue
public AuthLog $authLog;

/**
* Create a new FailedLogin instance
* Create a new FailedLoginNotification instance
*/
public function __construct(AuthLog $authLog)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Illuminate\Notifications\Notification;
use Jenssegers\Agent\Agent;

class SignedInWithNewDevice extends Notification implements ShouldQueue
class SignedInWithNewDeviceNotification extends Notification implements ShouldQueue
{
use Queueable;

Expand All @@ -26,7 +26,7 @@ class SignedInWithNewDevice extends Notification implements ShouldQueue
protected $agent;

/**
* Create a new SignedInWithNewDevice instance
* Create a new SignedInWithNewDeviceNotification instance
*/
public function __construct(AuthLog $authLog)
{
Expand All @@ -47,7 +47,7 @@ public function toMail(mixed $notifiable) : MailMessage
{
return (new MailMessage())
->subject(__('notifications.new_device.subject'))
->markdown('emails.SignedInWithNewDevice', [
->markdown('emails.signedInWithNewDevice', [
'account' => $notifiable,
'time' => $this->authLog->login_at,
'ipAddress' => $this->authLog->ip_address,
Expand Down
22 changes: 18 additions & 4 deletions app/Policies/UserPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ public function before(User $user, string $ability) : ?bool
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user) : bool
{
return false;
}
// public function viewAny(User $user) : bool
// {
// return false;
// }

/**
* Determine whether the user can view the model.
*
* @codeCoverageIgnore
* Ignored as long as the before() method restrict the access to admins only
*/
public function view(User $user, User $model) : bool
{
Expand All @@ -45,6 +48,9 @@ public function view(User $user, User $model) : bool

/**
* Determine whether the user can create models.
*
* @codeCoverageIgnore
* Ignored as long as the before() method restrict the access to admins only
*/
public function create(?User $user) : bool
{
Expand All @@ -53,6 +59,8 @@ public function create(?User $user) : bool

/**
* Determine whether the user can update the model.
*
* Not ignored because the user can update itself
*/
public function update(User $user, User $model) : bool
{
Expand All @@ -67,6 +75,9 @@ public function update(User $user, User $model) : bool

/**
* Determine whether the user can delete the model.
*
* @codeCoverageIgnore
* Ignored as long as the before() method restrict the access to admins only
*/
public function delete(User $user, User $model) : bool
{
Expand All @@ -81,6 +92,9 @@ public function delete(User $user, User $model) : bool

/**
* Determine whether the user can promote the model.
*
* @codeCoverageIgnore
* Ignored as long as the before() method restrict the access to admins only
*/
public function promote(User $user) : bool
{
Expand Down
4 changes: 2 additions & 2 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use App\Listeners\Authentication\VisitedByProxyUserListener;
use App\Listeners\CleanIconStorage;
use App\Listeners\DissociateTwofaccountFromGroup;
use App\Listeners\LogNotification;
use App\Listeners\LogNotificationListener;
use App\Listeners\RegisterOpenId;
use App\Listeners\ReleaseRadar;
use App\Listeners\ResetUsersPreference;
Expand Down Expand Up @@ -55,7 +55,7 @@ class EventServiceProvider extends ServiceProvider
RegisterOpenId::class,
],
NotificationSent::class => [
LogNotification::class,
LogNotificationListener::class,
],
Login::class => [
LoginListener::class,
Expand Down
10 changes: 6 additions & 4 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ fi

echo "${COMMIT}" > /2fauth/installed
php artisan storage:link --quiet
php artisan optimize:clear
php artisan config:cache
php artisan route:cache
php artisan view:cache

# Clearing compiled, cache has already been cleared
php artisan clear-compiled

# Clearing and Caching config, events, routes, views
php artisan optimize

supervisord
4 changes: 2 additions & 2 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use Laravel\Passport\Http\Controllers\PersonalAccessTokenController;

// use App\Models\User;
// use App\Notifications\SignedInWithNewDevice;
// use App\Notifications\SignedInWithNewDeviceNotification;
// use App\Models\AuthLog;

/*
Expand Down Expand Up @@ -109,7 +109,7 @@

// Route::get('/notification', function () {
// $user = User::find(1);
// return (new SignedInWithNewDevice(AuthLog::find(9)))
// return (new SignedInWithNewDeviceNotification(AuthLog::find(9)))
// ->toMail($user);
// });

Expand Down
23 changes: 23 additions & 0 deletions tests/Api/v1/Controllers/GroupControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Api\v1\Controllers\GroupController;
use App\Api\v1\Resources\GroupResource;
use App\Listeners\DissociateTwofaccountFromGroup;
use App\Listeners\ResetUsersPreference;
use App\Models\Group;
use App\Models\TwoFAccount;
Expand All @@ -18,6 +19,7 @@
#[CoversClass(ResetUsersPreference::class)]
#[CoversClass(GroupPolicy::class)]
#[CoversClass(Group::class)]
#[CoversClass(DissociateTwofaccountFromGroup::class)]
class GroupControllerTest extends FeatureTestCase
{
/**
Expand Down Expand Up @@ -103,6 +105,27 @@ public function test_index_returns_user_groups_only_with_pseudo_group()
]);
}

#[Test]
public function test_orphan_groups_are_reassign_to_the_only_user()
{
config(['auth.defaults.guard' => 'reverse-proxy-guard']);

$this->anotherUser->delete();
$this->userGroupA->user_id = null;
$this->userGroupA->save();

$this->assertCount(1, User::all());
$this->assertNull($this->userGroupA->user_id);

$this->actingAs($this->user, 'reverse-proxy-guard')
->json('GET', '/api/v1/groups')
->assertOk();

$this->userGroupA->refresh();

$this->assertNotNull($this->userGroupA->user_id);
}

#[Test]
public function test_store_returns_created_group_resource()
{
Expand Down
13 changes: 13 additions & 0 deletions tests/Api/v1/Controllers/SettingControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
namespace Tests\Api\v1\Controllers;

use App\Api\v1\Controllers\SettingController;
use App\Api\v1\Requests\SettingUpdateRequest;
use App\Facades\Settings;
use App\Models\User;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\Test;
use Tests\FeatureTestCase;

/**
* SettingController test class
*/
#[CoversClass(SettingController::class)]
#[CoversMethod(SettingUpdateRequest::class, 'rules')]
class SettingControllerTest extends FeatureTestCase
{
/**
Expand Down Expand Up @@ -230,6 +233,16 @@ public function test_update_missing_user_setting_returns_created_setting()
]);
}

#[Test]
public function test_update_restrictList_setting_rejects_invalid_email_list()
{
$response = $this->actingAs($this->admin, 'api-guard')
->json('PUT', '/api/v1/settings/restrictList', [
'value' => '[email protected]|janedoeexamplecom',
])
->assertJsonValidationErrorFor('value');
}

#[Test]
public function test_destroy_user_setting_returns_success()
{
Expand Down
Loading

0 comments on commit 0f1372e

Please sign in to comment.