fix: keystore

This commit is contained in:
hashbro
2026-09-20 06:15:26 +08:00
parent 2625707aed
commit 2a41a29310
29 changed files with 1972 additions and 66 deletions
+92
View File
@@ -8,6 +8,7 @@ use App\Models\Device;
use App\Models\PluginSession;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Storage;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
@@ -137,4 +138,95 @@ class PluginSessionPageTest extends TestCase
->get(route('user.sessions.download', $other))
->assertForbidden();
}
#[Test]
public function admin_can_export_sessions_as_zip(): void
{
Storage::fake('local');
$admin = Admin::query()->create(['username' => 'admin', 'password' => 'admin123']);
$device = Device::query()->create([
'device_id' => 'dev-export',
'channel_id' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
]);
PluginSession::query()->create([
'device_id' => $device->id,
'device_key' => $device->device_id,
'kind' => PluginSession::KIND_TELEGRAM,
'account_id' => '111',
'payload' => ['user_id' => '111', 'state' => 'abc'],
]);
PluginSession::query()->create([
'device_id' => $device->id,
'device_key' => $device->device_id,
'kind' => PluginSession::KIND_TELEGRAM,
'account_id' => '222',
'payload' => ['user_id' => '222', 'state' => 'def'],
]);
$resp = $this->actingAs($admin, 'admin')
->get(route('admin.sessions.export', ['kind' => '1']))
->assertOk();
$this->assertStringContainsString('attachment', (string) $resp->headers->get('content-disposition'));
$this->assertSame('application/zip', $resp->headers->get('content-type'));
$this->assertSame('2', $resp->headers->get('x-export-count'));
// Verify the ZIP contains two JSON files.
$body = $resp->streamedContent();
$tmp = tempnam(sys_get_temp_dir(), 'test_zip_');
file_put_contents($tmp, $body);
$zip = new \ZipArchive();
$this->assertTrue($zip->open($tmp) === true);
$this->assertSame(2, $zip->numFiles);
$names = [];
for ($i = 0; $i < $zip->numFiles; $i++) {
$names[] = $zip->getNameIndex($i);
}
$zip->close();
@unlink($tmp);
$this->assertCount(2, $names);
foreach ($names as $name) {
$this->assertStringContainsString('tg-', $name);
$this->assertStringEndsWith('.json', $name);
}
}
#[Test]
public function export_returns_error_when_no_data(): void
{
$admin = Admin::query()->create(['username' => 'admin', 'password' => 'admin123']);
$this->actingAs($admin, 'admin')
->getJson(route('admin.sessions.export', ['kind' => '1']))
->assertStatus(422)
->assertJsonPath('code', 1);
}
#[Test]
public function agent_export_respects_channel_scope(): void
{
Storage::fake('local');
$agentA = User::query()->create(['username' => 'a', 'password' => 'secret12', 'status' => 1]);
$chA = 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb';
$chB = 'cccccccccccccccccccccccccccccccc';
Channel::query()->create(['channel_id' => $chA, 'user_id' => $agentA->id, 'status' => 1]);
Channel::query()->create(['channel_id' => $chB, 'user_id' => User::query()->create(['username' => 'b', 'password' => 'secret12', 'status' => 1])->id, 'status' => 1]);
$devA = Device::query()->create(['device_id' => 'dev-a', 'channel_id' => $chA]);
$devB = Device::query()->create(['device_id' => 'dev-b', 'channel_id' => $chB]);
PluginSession::query()->create([
'device_id' => $devA->id, 'device_key' => 'dev-a',
'kind' => PluginSession::KIND_TELEGRAM, 'account_id' => 'aaa',
'payload' => ['user_id' => 'aaa'],
]);
PluginSession::query()->create([
'device_id' => $devB->id, 'device_key' => 'dev-b',
'kind' => PluginSession::KIND_TELEGRAM, 'account_id' => 'bbb',
'payload' => ['user_id' => 'bbb'],
]);
$resp = $this->actingAs($agentA, 'agent')
->get(route('user.sessions.export', ['kind' => '1']))
->assertOk();
$this->assertSame('1', $resp->headers->get('x-export-count'));
}
}