56 lines
1.4 KiB
PHP
56 lines
1.4 KiB
PHP
<?php
|
|
|
|
use App\Http\Resources\SessionResource;
|
|
use App\Models\User;
|
|
use App\Repositories\Session\SessionRepository;
|
|
use Carbon\Carbon;
|
|
use Faker\Generator;
|
|
use Illuminate\Support\Str;
|
|
|
|
test('get user sessions unauthenticated', function () {
|
|
$this->getJson('/api/me/sessions')->assertStatus(401);
|
|
});
|
|
|
|
test('get sessions if non database driver is used', function () {
|
|
config(['session.driver' => 'array']);
|
|
|
|
$this->login();
|
|
|
|
$this->getJson('/api/me/sessions')->assertStatus(404);
|
|
});
|
|
|
|
test('get user sessions', function () {
|
|
config(['session.driver' => 'database']);
|
|
|
|
$user = $this->login();
|
|
|
|
$sessions = generateProfileApiSessions($user);
|
|
|
|
$this->getJson('/api/me/sessions')
|
|
->assertOk()
|
|
->assertJsonFragment([
|
|
'data' => SessionResource::collection($sessions)->resolve(),
|
|
]);
|
|
});
|
|
|
|
function generateProfileApiSessions(User $user, $count = 5)
|
|
{
|
|
$sessions = [];
|
|
$faker = app(Generator::class);
|
|
|
|
for ($i = 0; $i < $count; $i++) {
|
|
array_push($sessions, [
|
|
'id' => Str::random(40),
|
|
'user_id' => $user->id,
|
|
'ip_address' => $faker->ipv4,
|
|
'user_agent' => $faker->userAgent,
|
|
'payload' => Str::random(),
|
|
'last_activity' => Carbon::now()->subMinute()->timestamp,
|
|
]);
|
|
}
|
|
|
|
DB::table('sessions')->insert($sessions);
|
|
|
|
return app(SessionRepository::class)->getUserSessions($user->id);
|
|
}
|