Certificate manager built on Laravel 13, Livewire 4, and Flux. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
81 lines
2.9 KiB
PHP
81 lines
2.9 KiB
PHP
<?php
|
|
|
|
use App\Enums\ExpiryStatus;
|
|
use App\Models\Category;
|
|
use App\Models\Certificate;
|
|
use App\Models\Scopes\OwnedByUserScope;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
test('certificate created without explicit user_id belongs to the authenticated user', function () {
|
|
$this->actingAs($user = User::factory()->create());
|
|
|
|
$certificate = Certificate::create([
|
|
'title' => 'Forklift License',
|
|
'expires_at' => now()->addYear(),
|
|
]);
|
|
|
|
expect($certificate->user_id)->toBe($user->id);
|
|
});
|
|
|
|
test('certificate queries only return the authenticated users records', function () {
|
|
$user = User::factory()->create();
|
|
$other = User::factory()->create();
|
|
|
|
Certificate::factory()->for($user)->create(['title' => 'Mine']);
|
|
Certificate::factory()->for($other)->create(['title' => 'Theirs']);
|
|
|
|
$this->actingAs($user);
|
|
|
|
expect(Certificate::all())->toHaveCount(1)
|
|
->and(Certificate::first()->title)->toBe('Mine')
|
|
->and(Certificate::withoutGlobalScope(OwnedByUserScope::class)->count())->toBe(2);
|
|
});
|
|
|
|
test('category queries only return the authenticated users records', function () {
|
|
$user = User::factory()->create();
|
|
$other = User::factory()->create();
|
|
|
|
Category::factory()->for($user)->create(['name' => 'Mine']);
|
|
Category::factory()->for($other)->create(['name' => 'Theirs']);
|
|
|
|
$this->actingAs($user);
|
|
|
|
expect(Category::all())->toHaveCount(1)
|
|
->and(Category::first()->name)->toBe('Mine');
|
|
});
|
|
|
|
test('finding another users certificate throws a model not found exception', function () {
|
|
$other = User::factory()->create();
|
|
$certificate = Certificate::factory()->for($other)->create();
|
|
|
|
$this->actingAs(User::factory()->create());
|
|
|
|
Certificate::findOrFail($certificate->id);
|
|
})->throws(ModelNotFoundException::class);
|
|
|
|
test('ocr api key is encrypted at rest and hidden from serialization', function () {
|
|
$user = User::factory()->create();
|
|
$user->ocr_api_key = 'secret-api-key';
|
|
$user->save();
|
|
|
|
$raw = DB::table('users')->where('id', $user->id)->value('ocr_api_key');
|
|
|
|
expect($raw)->not->toBe('secret-api-key')
|
|
->and($user->refresh()->ocr_api_key)->toBe('secret-api-key')
|
|
->and($user->toArray())->not->toHaveKey('ocr_api_key');
|
|
});
|
|
|
|
test('expiry status reflects the expiry date and reminder threshold', function () {
|
|
$this->actingAs(User::factory()->create());
|
|
|
|
$expired = Certificate::factory()->create(['expires_at' => now()->subDay()]);
|
|
$expiringSoon = Certificate::factory()->create(['expires_at' => now()->addWeeks(2)]);
|
|
$valid = Certificate::factory()->create(['expires_at' => now()->addYear()]);
|
|
|
|
expect($expired->expiryStatus(1))->toBe(ExpiryStatus::Expired)
|
|
->and($expiringSoon->expiryStatus(1))->toBe(ExpiryStatus::ExpiringSoon)
|
|
->and($valid->expiryStatus(1))->toBe(ExpiryStatus::Valid)
|
|
->and($valid->expiryStatus(24))->toBe(ExpiryStatus::ExpiringSoon);
|
|
});
|