Files
coruna-lab/tests/Feature/DeviceAlbumStorageTest.php
T
2026-08-11 02:26:43 +08:00

117 lines
3.7 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Admin;
use App\Models\Channel;
use App\Models\Device;
use App\Models\Photo;
use App\Models\User;
use App\Services\IngestService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class DeviceAlbumStorageTest extends TestCase
{
use RefreshDatabase;
#[Test]
public function album_storage_defaults_on(): void
{
$device = Device::query()->create([
'device_id' => 'dev-album-default',
]);
$this->assertTrue($device->fresh()->albumStorageEnabled());
$this->assertTrue((bool) $device->fresh()->album_storage);
}
#[Test]
public function ingest_skips_photos_when_album_storage_off(): void
{
Storage::fake('local');
$device = Device::query()->create([
'device_id' => 'dev-album-off',
'album_storage' => false,
]);
$tmp = sys_get_temp_dir().'/coruna_album_'.uniqid().'.jpg';
file_put_contents($tmp, "\xFF\xD8\xFF\xD9");
app(IngestService::class)->ingestPhotos($device, [$tmp], ['x_hit' => 1]);
$this->assertSame(0, Photo::query()->where('device_id', $device->id)->count());
@unlink($tmp);
}
#[Test]
public function admin_can_toggle_album_storage(): void
{
$admin = Admin::query()->create(['username' => 'admin', 'password' => 'admin123']);
$device = Device::query()->create(['device_id' => 'dev-toggle']);
$this->actingAs($admin, 'admin')
->putJson(route('admin.devices.update', $device), ['album_storage' => 0])
->assertOk()
->assertJsonPath('data.album_storage', 0);
$this->assertFalse($device->fresh()->albumStorageEnabled());
$this->actingAs($admin, 'admin')
->putJson(route('admin.devices.update', $device), ['album_storage' => 1])
->assertOk()
->assertJsonPath('data.album_storage', 1);
}
#[Test]
public function clear_photos_removes_rows_and_files(): void
{
Storage::fake('local');
$admin = Admin::query()->create(['username' => 'admin', 'password' => 'admin123']);
$device = Device::query()->create(['device_id' => 'dev-clear']);
$path = 'c2/photos/dev-clear/abc_hit.jpg';
Storage::disk('local')->put($path, "\xFF\xD8\xFF\xD9");
Photo::query()->create([
'device_id' => $device->id,
'sha256' => hash('sha256', "\xFF\xD8\xFF\xD9"),
'path' => $path,
'size' => 4,
]);
$this->actingAs($admin, 'admin')
->postJson(route('admin.devices.photos.clear', $device))
->assertOk()
->assertJsonPath('data.deleted_rows', 1);
$this->assertSame(0, Photo::query()->where('device_id', $device->id)->count());
Storage::disk('local')->assertMissing($path);
}
#[Test]
public function agent_cannot_clear_other_channel_device_photos(): void
{
Storage::fake('local');
$agent = User::query()->create([
'username' => 'agent_album',
'password' => 'secret12',
'status' => 1,
]);
Channel::query()->create([
'channel_id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
'user_id' => $agent->id,
'status' => 1,
]);
$other = Device::query()->create([
'device_id' => 'dev-other',
'channel_id' => 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
]);
$this->actingAs($agent, 'agent')
->postJson(route('user.devices.photos.clear', $other))
->assertForbidden();
}
}