Fix & Complete tests

This commit is contained in:
Bubka
2024-07-03 11:16:08 +02:00
parent 8c89c6f0ab
commit e238e5121c
37 changed files with 1261 additions and 221 deletions

View File

@@ -7,6 +7,7 @@ use App\Models\Group;
use App\Models\TwoFAccount;
use App\Models\User;
use App\Observers\UserObserver;
use Database\Factories\AuthLogFactory;
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
@@ -187,4 +188,264 @@ class UserModelTest extends FeatureTestCase
$this->assertFalse($isDeleted);
}
#[Test]
public function test_getFromCredentialId_retreives_the_user()
{
$user = User::factory()->create();
DB::table('webauthn_credentials')->insert([
'id' => '-VOLFKPY-_FuMI_sJ7gMllK76L3VoRUINj6lL_Z3qDg',
'authenticatable_type' => \App\Models\User::class,
'authenticatable_id' => $user->id,
'user_id' => 'e8af6f703f8042aa91c30cf72289aa07',
'counter' => 0,
'rp_id' => 'http://localhost',
'origin' => 'http://localhost',
'aaguid' => '00000000-0000-0000-0000-000000000000',
'attestation_format' => 'none',
'public_key' => 'eyJpdiI6Imp0U0NVeFNNbW45KzEvMXpad2p2SUE9PSIsInZhbHVlIjoic0VxZ2I1WnlHM2lJakhkWHVkK2kzMWtibk1IN2ZlaExGT01qOElXMDdRTjhnVlR0TDgwOHk1S0xQUy9BQ1JCWHRLNzRtenNsMml1dVQydWtERjFEU0h0bkJGT2RwUXE1M1JCcVpablE2Y2VGV2YvVEE2RGFIRUE5L0x1K0JIQXhLVE1aNVNmN3AxeHdjRUo2V0hwREZSRTJYaThNNnB1VnozMlVXZEVPajhBL3d3ODlkTVN3bW54RTEwSG0ybzRQZFFNNEFrVytUYThub2IvMFRtUlBZamoyZElWKzR1bStZQ1IwU3FXbkYvSm1FU2FlMTFXYUo0SG9kc1BDME9CNUNKeE9IelE5d2dmNFNJRXBKNUdlVzJ3VHUrQWJZRFluK0hib0xvVTdWQ0ZISjZmOWF3by83aVJES1dxbU9Zd1lhRTlLVmhZSUdlWmlBOUFtcTM2ZVBaRWNKNEFSQUhENk5EaC9hN3REdnVFbm16WkRxekRWOXd4cVcvZFdKa2tlWWJqZWlmZnZLS0F1VEVCZEZQcXJkTExiNWRyQmxsZWtaSDRlT3VVS0ZBSXFBRG1JMjRUMnBKRXZxOUFUa2xxMjg2TEplUzdscVo2UytoVU5SdXk1OE1lcFN6aU05ZkVXTkdIM2tKM3Q5bmx1TGtYb1F5bGxxQVR3K3BVUVlia1VybDFKRm9lZDViNzYraGJRdmtUb2FNTEVGZmZYZ3lYRDRiOUVjRnJpcTVvWVExOHJHSTJpMnVBZ3E0TmljbUlKUUtXY2lSWDh1dE5MVDNRUzVRSkQrTjVJUU8rSGhpeFhRRjJvSEdQYjBoVT0iLCJtYWMiOiI5MTdmNWRkZGE5OTEwNzQ3MjhkYWVhYjRlNjk0MWZlMmI5OTQ4YzlmZWI1M2I4OGVkMjE1MjMxNjUwOWRmZTU2IiwidGFnIjoiIn0=',
'updated_at' => now(),
'created_at' => now(),
]);
$searched = User::getFromCredentialId('-VOLFKPY-_FuMI_sJ7gMllK76L3VoRUINj6lL_Z3qDg');
$this->assertEquals($user->id, $searched->id);
}
#[Test]
public function test_authentications_returns_user_auth_logs_sorted_by_latest_id()
{
$user = User::factory()->create();
$tenDaysAgoAuthLog = AuthLog::factory()->daysAgo(10)->for($user, 'authenticatable')->create();
$fiveDaysAgoAuthLog = AuthLog::factory()->daysAgo(5)->for($user, 'authenticatable')->create();
$lastAuthLog = AuthLog::factory()->daysAgo(1)->for($user, 'authenticatable')->create();
$authentications = $user->authentications()->get();
$this->assertCount(3, $authentications);
$this->assertEquals($lastAuthLog->id, $authentications[0]->id);
$this->assertEquals($fiveDaysAgoAuthLog->id, $authentications[1]->id);
$this->assertEquals($tenDaysAgoAuthLog->id, $authentications[2]->id);
}
#[Test]
public function test_authentications_returns_user_auth_logs_only()
{
$user = User::factory()->create();
$anotherUser = User::factory()->create();
$userAuthLog = AuthLog::factory()->daysAgo(10)->for($user, 'authenticatable')->create();
AuthLog::factory()->daysAgo(5)->for($anotherUser, 'authenticatable')->create();
$authentications = $user->authentications()->get();
$this->assertCount(1, $authentications);
$this->assertEquals($userAuthLog->id, $authentications[0]->id);
}
#[Test]
public function test_authenticationsByPeriod_returns_last_month_auth_logs()
{
$user = User::factory()->create();
$twoMonthsAgoAuthLog = AuthLog::factory()->duringLastThreeMonth()->for($user, 'authenticatable')->create();
$duringLastMonthAuthLog = AuthLog::factory()->duringLastMonth()->for($user, 'authenticatable')->create();
$authentications = $user->authenticationsByPeriod(1);
$this->assertCount(1, $authentications);
$this->assertEquals($duringLastMonthAuthLog->id, $authentications[0]->id);
}
#[Test]
public function test_authenticationsByPeriod_returns_last_three_months_auth_logs()
{
$user = User::factory()->create();
$sixMonthsAgoAuthLog = AuthLog::factory()->duringLastSixMonth()->for($user, 'authenticatable')->create();
$threeMonthsAgoAuthLog = AuthLog::factory()->duringLastThreeMonth()->for($user, 'authenticatable')->create();
$duringLastMonthAuthLog = AuthLog::factory()->duringLastMonth()->for($user, 'authenticatable')->create();
$authentications = $user->authenticationsByPeriod(3);
$this->assertCount(2, $authentications);
$this->assertEquals($duringLastMonthAuthLog->id, $authentications[0]->id);
$this->assertEquals($threeMonthsAgoAuthLog->id, $authentications[1]->id);
}
#[Test]
public function test_latestAuthentication_returns_user_latest_auth_logs()
{
$user = User::factory()->create();
$twoMonthsAgoAuthLog = AuthLog::factory()->duringLastThreeMonth()->for($user, 'authenticatable')->create();
$duringLastMonthAuthLog = AuthLog::factory()->duringLastMonth()->for($user, 'authenticatable')->create();
$authentications = $user->latestAuthentication()->get();
$this->assertCount(1, $authentications);
$this->assertEquals($duringLastMonthAuthLog->id, $authentications[0]->id);
}
#[Test]
public function test_latestAuthentication_returns_user_latest_auth_logs_only()
{
$user = User::factory()->create();
$anotherUser = User::factory()->create();
$userAuthLog = AuthLog::factory()->duringLastThreeMonth()->for($user, 'authenticatable')->create();
$anotherUserAuthLog = AuthLog::factory()->duringLastMonth()->for($anotherUser, 'authenticatable')->create();
$authentications = $user->latestAuthentication()->get();
$this->assertCount(1, $authentications);
$this->assertEquals($userAuthLog->id, $authentications[0]->id);
}
#[Test]
public function test_lastLoginAt_returns_user_last_auth_date()
{
$user = User::factory()->create();
$now = now();
$tenDaysAgoAuthLog = AuthLog::factory()->daysAgo(10)->for($user, 'authenticatable')->create();
$fiveDaysAgoAuthLog = AuthLog::factory()->daysAgo(5)->for($user, 'authenticatable')->create();
$lastAuthLog = AuthLog::factory()->at($now)->for($user, 'authenticatable')->create();
$lastLoginAt = $user->lastLoginAt();
$this->assertEquals($lastLoginAt->startOfSecond(), $now->startOfSecond());
}
#[Test]
public function test_lastLoginAt_returns_null_if_user_has_no_login()
{
$user = User::factory()->create();
AuthLog::factory()->logoutOnly()->for($user, 'authenticatable')->create();
$lastLoginAt = $user->lastLoginAt();
$this->assertNull($lastLoginAt);
}
#[Test]
public function test_lastSuccessfulLoginAt_returns_user_last_successful_login_date()
{
$user = User::factory()->create();
$now = now();
AuthLog::factory()->at($now)->for($user, 'authenticatable')->create();
$lastSuccessfulLoginAt = $user->lastSuccessfulLoginAt();
$this->assertEquals($lastSuccessfulLoginAt->startOfSecond(), $now->startOfSecond());
}
#[Test]
public function test_lastSuccessfulLoginAt_returns_null_if_user_has_no_successful_login()
{
$user = User::factory()->create();
$now = now();
AuthLog::factory()->at($now)->failedLogin()->for($user, 'authenticatable')->create();
$lastSuccessfulLoginAt = $user->lastSuccessfulLoginAt();
$this->assertNull($lastSuccessfulLoginAt);
}
#[Test]
public function test_lastLoginIp_returns_user_last_login_ip()
{
$user = User::factory()->create();
AuthLog::factory()->for($user, 'authenticatable')->create();
$lastLoginIp = $user->lastLoginIp();
$this->assertEquals(AuthLogFactory::IP, $lastLoginIp);
}
#[Test]
public function test_lastLoginIp_returns_null_if_user_has_no_auth_log()
{
$user = User::factory()->create();
$lastLoginIp = $user->lastLoginIp();
$this->assertNull($lastLoginIp);
}
#[Test]
public function test_lastSuccessfulLoginIp_returns_user_last_successful_login_ip()
{
$user = User::factory()->create();
AuthLog::factory()->for($user, 'authenticatable')->create();
$lastSuccessfulLoginIp = $user->lastSuccessfulLoginIp();
$this->assertEquals(AuthLogFactory::IP, $lastSuccessfulLoginIp);
}
#[Test]
public function test_lastSuccessfulLoginIp_returns_null_if_user_has_no_successful_login()
{
$user = User::factory()->create();
AuthLog::factory()->failedLogin()->for($user, 'authenticatable')->create();
$lastSuccessfulLoginIp = $user->lastSuccessfulLoginIp();
$this->assertNull($lastSuccessfulLoginIp);
}
#[Test]
public function test_previousLoginAt_returns_user_last_auth_date()
{
$user = User::factory()->create();
$now = now();
$yesterday = now()->subDay();
$yesterdayAuthLog = AuthLog::factory()->at($yesterday)->for($user, 'authenticatable')->create();
$lastAuthLog = AuthLog::factory()->at($now)->for($user, 'authenticatable')->create();
$previousLoginAt = $user->previousLoginAt();
$this->assertEquals($previousLoginAt->startOfSecond(), $yesterday->startOfSecond());
}
#[Test]
public function test_previousLoginAt_returns_null_if_user_has_no_auth_log()
{
$user = User::factory()->create();
$previousLoginAt = $user->previousLoginAt();
$this->assertNull($previousLoginAt);
}
#[Test]
public function test_previousLoginIp_returns_user_last_auth_ip()
{
$user = User::factory()->create();
$yesterday = now()->subDay();
AuthLog::factory()->for($user, 'authenticatable')->create();
AuthLog::factory()->at($yesterday)->for($user, 'authenticatable')->create();
$previousLoginIp = $user->previousLoginIp();
$this->assertEquals(AuthLogFactory::IP, $previousLoginIp);
}
#[Test]
public function test_previousLoginIp_returns_null_if_user_has_no_auth_log()
{
$user = User::factory()->create();
$previousLoginIp = $user->previousLoginIp();
$this->assertNull($previousLoginIp);
}
}