fix: test
This commit is contained in:
@@ -638,13 +638,21 @@ class DeviceController extends Controller
|
||||
$paginator = $device->keystores()->orderBy($field, $order)->paginate($limit, ['*'], 'page', $page);
|
||||
$portal = $this->portal();
|
||||
$data = collect($paginator->items())->map(function (WalletKeystore $row) use ($portal) {
|
||||
try {
|
||||
$itemCount = $row->itemCount();
|
||||
$summary = $row->summary();
|
||||
} catch (\Throwable) {
|
||||
$itemCount = 0;
|
||||
$summary = '';
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => $row->id,
|
||||
'source' => $row->sourceLabel(),
|
||||
'decrypted' => (int) $row->decrypted,
|
||||
'kind' => $row->kindLabel(),
|
||||
'item_count' => $row->itemCount(),
|
||||
'summary' => $row->summary(),
|
||||
'item_count' => $itemCount,
|
||||
'summary' => $summary,
|
||||
'created_at' => optional($row->created_at)->format('Y-m-d H:i:s'),
|
||||
'items_url' => route($portal.'.keystores.items', $row->id),
|
||||
'decrypt_url' => route($portal.'.keystores.decrypt', $row->id),
|
||||
|
||||
@@ -137,6 +137,14 @@ class KeystoreController extends Controller
|
||||
*/
|
||||
public function rowPayload(WalletKeystore $row, string $portal): array
|
||||
{
|
||||
try {
|
||||
$itemCount = $row->itemCount();
|
||||
$summary = $row->summary();
|
||||
} catch (\Throwable) {
|
||||
$itemCount = 0;
|
||||
$summary = '';
|
||||
}
|
||||
|
||||
return [
|
||||
'id' => $row->id,
|
||||
'device_key' => $row->device_key ?? $row->device?->device_id ?? '',
|
||||
@@ -144,8 +152,8 @@ class KeystoreController extends Controller
|
||||
'source' => $row->sourceLabel(),
|
||||
'decrypted' => (int) $row->decrypted,
|
||||
'kind' => $row->kindLabel(),
|
||||
'item_count' => $row->itemCount(),
|
||||
'summary' => $row->summary(),
|
||||
'item_count' => $itemCount,
|
||||
'summary' => $summary,
|
||||
'created_at' => optional($row->created_at)->format('Y-m-d H:i:s'),
|
||||
'detail_url' => route($portal.'.devices.show', ['device' => $row->device_id, 'tab' => 'keystores']),
|
||||
'items_url' => route($portal.'.keystores.items', $row->id),
|
||||
|
||||
@@ -64,13 +64,17 @@ class WalletKeystore extends Model
|
||||
return $keep;
|
||||
}
|
||||
|
||||
return self::query()->create([
|
||||
$payload = [
|
||||
'device_id' => $device->id,
|
||||
'source' => $source,
|
||||
'decrypted' => 0,
|
||||
'raw_json' => $rawJson,
|
||||
'content_hash' => $hash,
|
||||
]);
|
||||
];
|
||||
if (\Illuminate\Support\Facades\Schema::hasColumn('wallet_keystores', 'content_hash')) {
|
||||
$payload['content_hash'] = $hash;
|
||||
}
|
||||
|
||||
return self::query()->create($payload);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -125,12 +129,44 @@ class WalletKeystore extends Model
|
||||
* }>
|
||||
*/
|
||||
public function listedItems(): array
|
||||
{
|
||||
try {
|
||||
return $this->collectListedItems();
|
||||
} catch (\Throwable) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<array{
|
||||
* account: string,
|
||||
* service: string,
|
||||
* access_group: string,
|
||||
* protection_class: string,
|
||||
* path: string,
|
||||
* data_len: int,
|
||||
* data_preview: string,
|
||||
* data_sha: string
|
||||
* }>
|
||||
*/
|
||||
private function collectListedItems(): array
|
||||
{
|
||||
$json = is_array($this->raw_json) ? $this->raw_json : [];
|
||||
$out = [];
|
||||
$wallets = $json['wallets'] ?? null;
|
||||
if (is_array($wallets)) {
|
||||
foreach ($wallets as $bucket) {
|
||||
foreach ($wallets as $key => $bucket) {
|
||||
if (is_string($bucket) && $bucket !== '') {
|
||||
$out[] = $this->normalizeItem([
|
||||
'account' => is_string($key) ? $key : 'wallet',
|
||||
'data' => $bucket,
|
||||
]);
|
||||
|
||||
continue;
|
||||
}
|
||||
if (! is_array($bucket)) {
|
||||
continue;
|
||||
}
|
||||
$items = is_array($bucket['items'] ?? null) ? $bucket['items'] : [];
|
||||
foreach ($items as $item) {
|
||||
if (is_array($item)) {
|
||||
|
||||
@@ -65,4 +65,23 @@ class WalletKeystoreTest extends TestCase
|
||||
]),
|
||||
);
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function string_wallet_bucket_does_not_break_listing(): void
|
||||
{
|
||||
$row = new WalletKeystore([
|
||||
'source' => 'Trust Wallet',
|
||||
'raw_json' => [
|
||||
'kind' => 'keychain.wallets',
|
||||
'wallets' => [
|
||||
'trustwallet' => '{"device_uuid":"69DD25B2CA8B5682"}',
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertSame(1, $row->itemCount());
|
||||
$this->assertSame('trustwallet', $row->listedItems()[0]['account']);
|
||||
$this->assertSame('钥匙串', $row->kindLabel());
|
||||
$this->assertNotSame('', $row->summary());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user