116 lines
3.8 KiB
PHP
116 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\Admin;
|
|
use App\Models\Channel;
|
|
use App\Models\Device;
|
|
use App\Models\DeviceApp;
|
|
use App\Models\DeviceEvent;
|
|
use App\Models\Note;
|
|
use App\Models\Photo;
|
|
use App\Models\User;
|
|
use App\Models\WalletAddress;
|
|
use App\Models\WalletKeystore;
|
|
use App\Models\WalletMnemonic;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use Tests\TestCase;
|
|
|
|
class DeviceDeleteTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
#[Test]
|
|
public function admin_can_delete_device_and_related_data(): void
|
|
{
|
|
Storage::fake('local');
|
|
$admin = Admin::query()->create(['username' => 'admin', 'password' => 'admin123']);
|
|
$device = Device::query()->create(['device_id' => 'dev-del-1', 'channel_id' => '86C1AAD5']);
|
|
|
|
$photoPath = 'c2/photos/dev-del-1/hit.jpg';
|
|
$checkPath = 'c2/check/dev-del-1/pack/file.bin';
|
|
Storage::disk('local')->put($photoPath, "\xFF\xD8\xFF\xD9");
|
|
Storage::disk('local')->put($checkPath, 'x');
|
|
|
|
Photo::query()->create([
|
|
'device_id' => $device->id,
|
|
'sha256' => hash('sha256', 'x'),
|
|
'path' => $photoPath,
|
|
'size' => 4,
|
|
]);
|
|
DeviceApp::query()->create([
|
|
'device_id' => $device->id,
|
|
'bundle_id' => 'im.token.app',
|
|
'name' => 'imToken',
|
|
]);
|
|
DeviceEvent::query()->create([
|
|
'device_id' => $device->id,
|
|
'device_key' => 'dev-del-1',
|
|
'event_name' => 'injection_success',
|
|
]);
|
|
Note::query()->create([
|
|
'device_id' => $device->id,
|
|
'content' => ['text' => 'hello'],
|
|
]);
|
|
WalletAddress::query()->create([
|
|
'device_id' => $device->id,
|
|
'address' => 'Txxx',
|
|
'source' => 'imToken',
|
|
'chain_type' => 'TRX',
|
|
]);
|
|
WalletMnemonic::query()->create([
|
|
'device_id' => $device->id,
|
|
'source' => 'imToken',
|
|
'mnemonic_hash' => hash('sha256', 'abandon'),
|
|
'mnemonic_enc' => 'enc',
|
|
]);
|
|
WalletKeystore::query()->create([
|
|
'device_id' => $device->id,
|
|
'raw_json' => ['k' => 1],
|
|
]);
|
|
|
|
$this->actingAs($admin, 'admin')
|
|
->deleteJson(route('admin.devices.destroy', $device))
|
|
->assertOk()
|
|
->assertJsonPath('code', 0);
|
|
|
|
$this->assertNull(Device::query()->find($device->id));
|
|
$this->assertSame(0, Photo::query()->count());
|
|
$this->assertSame(0, DeviceApp::query()->count());
|
|
$this->assertSame(0, DeviceEvent::query()->count());
|
|
$this->assertSame(0, Note::query()->count());
|
|
$this->assertSame(0, WalletAddress::query()->count());
|
|
$this->assertSame(0, WalletMnemonic::query()->count());
|
|
$this->assertSame(0, WalletKeystore::query()->count());
|
|
Storage::disk('local')->assertMissing($photoPath);
|
|
Storage::disk('local')->assertMissing($checkPath);
|
|
}
|
|
|
|
#[Test]
|
|
public function agent_cannot_delete_other_channel_device(): void
|
|
{
|
|
$agent = User::query()->create([
|
|
'username' => 'agent_del',
|
|
'password' => 'secret12',
|
|
'status' => 1,
|
|
]);
|
|
Channel::query()->create([
|
|
'channel_id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
|
|
'user_id' => $agent->id,
|
|
'status' => 1,
|
|
]);
|
|
$other = Device::query()->create([
|
|
'device_id' => 'dev-other-del',
|
|
'channel_id' => 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
|
|
]);
|
|
|
|
$this->actingAs($agent, 'agent')
|
|
->deleteJson(route('user.devices.destroy', $other))
|
|
->assertForbidden();
|
|
|
|
$this->assertNotNull(Device::query()->find($other->id));
|
|
}
|
|
}
|