Files
solidtime/tests/Unit/Endpoint/Web/OtherBrowserSessionsEndpointTest.php
2026-11-18 16:31:48 +01:00

167 lines
6.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Endpoint\Web;
use App\Http\Controllers\Web\OtherBrowserSessionsController;
use App\Models\User;
use App\Service\AuthenticationContract;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Mockery\MockInterface;
use PHPUnit\Framework\Attributes\CoversClass;
#[CoversClass(OtherBrowserSessionsController::class)]
class OtherBrowserSessionsEndpointTest extends EndpointTestAbstract
{
public function test_destroy_logs_out_other_browser_sessions_with_the_correct_password(): void
{
// Arrange
$user = User::factory()->create();
$originalPasswordHash = $user->password;
$this->actingAs($user);
// Act
$response = $this->delete('/user/other-browser-sessions', [
'password' => 'password',
]);
// Assert
$response->assertRedirect();
$response->assertSessionHasNoErrors();
// logoutOtherDevices re-hashes the password (same plaintext, new hash) to invalidate other sessions.
$this->assertNotSame($originalPasswordHash, $user->fresh()->password);
$this->assertTrue(Hash::check('password', $user->fresh()->password));
}
public function test_destroy_fails_with_an_incorrect_password(): void
{
// Arrange
$user = User::factory()->create();
$originalPasswordHash = $user->password;
$this->actingAs($user);
// Act
$response = $this->delete('/user/other-browser-sessions', [
'password' => 'wrong-password',
]);
// Assert
$response->assertSessionHasErrors('password');
// No side effects when the password is incorrect: the password must not be re-hashed.
$this->assertSame($originalPasswordHash, $user->fresh()->password);
}
public function test_destroy_requires_authentication(): void
{
// Act
$response = $this->delete('/user/other-browser-sessions', [
'password' => 'password',
]);
// Assert
$response->assertRedirect(route('login'));
}
public function test_destroy_deletes_the_other_database_session_records_of_the_current_user(): void
{
// Arrange
config(['session.driver' => 'database']);
$user = User::factory()->create();
$otherUser = User::factory()->create();
$this->actingAs($user);
DB::table('sessions')->insert([
[
'id' => 'other-session-of-current-user',
'user_id' => $user->getKey(),
'ip_address' => '192.0.2.10',
'user_agent' => '',
'payload' => '',
'last_activity' => now()->subMinutes(5)->timestamp,
],
[
'id' => 'session-of-another-user',
'user_id' => $otherUser->getKey(),
'ip_address' => '192.0.2.30',
'user_agent' => '',
'payload' => '',
'last_activity' => now()->timestamp,
],
]);
// Act
$response = $this->delete('/user/other-browser-sessions', [
'password' => 'password',
]);
// Assert
$response->assertSessionHasNoErrors();
// The current user's other sessions are removed, while another user's session is untouched.
$this->assertDatabaseMissing('sessions', ['id' => 'other-session-of-current-user']);
$this->assertDatabaseHas('sessions', ['id' => 'session-of-another-user']);
}
private function seedOtherSession(User $user, string $id = 'other-session'): void
{
DB::table('sessions')->insert([
'id' => $id,
'user_id' => $user->getKey(),
'ip_address' => '192.0.2.10',
'user_agent' => '',
'payload' => '',
'last_activity' => now()->timestamp,
]);
}
public function test_passwordless_user_can_revoke_other_sessions_without_confirming(): void
{
config(['session.driver' => 'database']);
$user = User::factory()->create(['password' => null]);
$otherUser = User::factory()->create();
$this->actingAs($user)->withSession(['current-session' => true]);
$currentId = app('session.store')->getId();
$this->withCookie(config('session.cookie'), $currentId);
$this->seedOtherSession($user, $currentId);
$this->seedOtherSession($user);
$this->seedOtherSession($otherUser, 'unrelated-session');
$response = $this->delete('/user/other-browser-sessions');
$response->assertRedirect()->assertSessionHasNoErrors();
$this->assertAuthenticatedAs($user);
$this->assertDatabaseHas('sessions', ['id' => $currentId]);
$this->assertDatabaseHas('sessions', ['id' => 'unrelated-session']);
$this->assertDatabaseMissing('sessions', ['id' => 'other-session']);
$this->assertNull($user->fresh()->password);
}
public function test_user_with_a_disallowed_local_password_revokes_other_sessions_without_confirming(): void
{
config(['session.driver' => 'database']);
$user = User::factory()->create();
$originalPasswordHash = $user->password;
$this->actingAs($user);
$this->seedOtherSession($user);
$this->partialMock(AuthenticationContract::class, function (MockInterface $mock): void {
$mock->shouldReceive('isPasswordLoginAllowed')->andReturn(false);
});
$this->delete('/user/other-browser-sessions')->assertRedirect()->assertSessionHasNoErrors();
$this->assertDatabaseMissing('sessions', ['id' => 'other-session']);
// The password they may not use is not rehashed with whatever they entered
$this->assertSame($originalPasswordHash, $user->fresh()->password);
}
public function test_passwordless_logout_does_not_report_success_for_unsupported_session_storage(): void
{
config(['session.driver' => 'array']);
$user = User::factory()->create(['password' => null]);
$this->actingAs($user);
$this->delete('/user/other-browser-sessions')->assertSessionHasErrors('password');
}
}