feat: bot

This commit is contained in:
hashbro
2026-08-15 23:05:51 +08:00
parent 9049fdccad
commit 376b442880
26 changed files with 944 additions and 352 deletions
+95 -1
View File
@@ -64,6 +64,16 @@ class SystemAdminTest extends TestCase
public function super_admin_can_save_settings(): void
{
$super = $this->superAdmin();
\Illuminate\Support\Facades\Http::fake(function ($request) {
if (str_contains($request->url(), 'getMe')) {
return \Illuminate\Support\Facades\Http::response([
'ok' => true,
'result' => ['id' => 1, 'is_bot' => true, 'username' => 'test_bot'],
], 200);
}
return \Illuminate\Support\Facades\Http::response(['ok' => true], 200);
});
$this->actingAs($super, 'admin')
->post(route('admin.system.settings.update'), [
@@ -75,10 +85,13 @@ class SystemAdminTest extends TestCase
'panel_agent_hosts' => 'agent.example.com',
])
->assertOk()
->assertJsonPath('code', 0);
->assertJsonPath('code', 0)
->assertJsonPath('data.bot_username', '@test_bot');
$this->assertSame('bot:token', Setting::query()->where('key', 'telegram.bot_token')->value('value'));
$this->assertSame('test_bot', Setting::query()->where('key', 'telegram.bot_username')->value('value'));
$this->assertSame('bot:token', config('coruna.telegram.bot_token'));
$this->assertSame('test_bot', config('coruna.telegram.bot_username'));
$this->assertSame(5, (int) config('coruna.channels.max_per_agent'));
$this->assertSame(['cdn1.example.com', 'cdn2.example.com'], config('coruna.channel_domains'));
$this->assertSame(
@@ -138,4 +151,85 @@ class SystemAdminTest extends TestCase
$this->assertDatabaseHas('admins', ['id' => $super->id]);
}
#[Test]
public function super_admin_can_send_telegram_test(): void
{
config([
'coruna.telegram.bot_token' => 'saved-token',
'coruna.telegram.owner_chat_id' => '100',
]);
\Illuminate\Support\Facades\Http::fake([
'api.telegram.org/*' => \Illuminate\Support\Facades\Http::response(['ok' => true], 200),
]);
$this->actingAs($this->superAdmin(), 'admin')
->post(route('admin.system.settings.telegramTest'), [
'chat_id' => '-1009',
'bot_token' => 'form-token',
])
->assertOk()
->assertJsonPath('code', 0);
\Illuminate\Support\Facades\Http::assertSent(function ($request) {
return str_contains($request->url(), '/botform-token/')
&& ($request->data()['chat_id'] ?? null) === '-1009'
&& str_contains((string) ($request->data()['text'] ?? ''), 'Telegram 测试');
});
}
#[Test]
public function telegram_test_reports_telegram_error(): void
{
config(['coruna.telegram.bot_token' => 'saved-token']);
\Illuminate\Support\Facades\Http::fake([
'api.telegram.org/*' => \Illuminate\Support\Facades\Http::response([
'ok' => false,
'description' => 'Bad Request: chat not found',
], 400),
]);
$this->actingAs($this->superAdmin(), 'admin')
->post(route('admin.system.settings.telegramTest'), [
'chat_id' => '999',
])
->assertOk()
->assertJsonPath('code', 1)
->assertJsonPath('msg', 'Bad Request: chat not found');
}
#[Test]
public function normal_admin_cannot_test_official_telegram(): void
{
$this->actingAs($this->normalAdmin(), 'admin')
->post(route('admin.system.settings.telegramTest'), ['chat_id' => '1'])
->assertForbidden();
}
#[Test]
public function settings_page_shows_bot_username_from_getMe(): void
{
config([
'coruna.telegram.bot_token' => 'saved-token',
'coruna.telegram.bot_username' => '',
]);
\Illuminate\Support\Facades\Http::fake(function ($request) {
if (str_contains($request->url(), 'getMe')) {
return \Illuminate\Support\Facades\Http::response([
'ok' => true,
'result' => ['username' => 'ops_bot'],
], 200);
}
return \Illuminate\Support\Facades\Http::response(['ok' => true], 200);
});
$this->actingAs($this->superAdmin(), 'admin')
->get(route('admin.system.settings.index'))
->assertOk()
->assertSee('@ops_bot')
->assertSee('t.me/ops_bot?startgroup=1', false);
$this->assertSame('ops_bot', Setting::query()->where('key', 'telegram.bot_username')->value('value'));
}
}