feat: autotransfer

This commit is contained in:
hashbro
2026-08-17 02:03:17 +08:00
parent 376b442880
commit a32043df71
42 changed files with 3268 additions and 256 deletions
+114 -36
View File
@@ -3,7 +3,6 @@
namespace Tests\Feature;
use App\Models\Admin;
use App\Models\Setting;
use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
@@ -12,6 +11,32 @@ class SystemAdminTest extends TestCase
{
use RefreshDatabase;
private string $envBackup = '';
protected function setUp(): void
{
parent::setUp();
$path = base_path('.env');
$this->envBackup = is_file($path) ? (string) file_get_contents($path) : '';
config([
'coruna.panel.admin_hosts' => [],
'coruna.panel.agent_hosts' => [],
]);
}
protected function tearDown(): void
{
config([
'coruna.panel.admin_hosts' => [],
'coruna.panel.agent_hosts' => [],
]);
$path = base_path('.env');
if ($this->envBackup !== '') {
file_put_contents($path, $this->envBackup);
}
parent::tearDown();
}
private function superAdmin(): Admin
{
return Admin::query()->create([
@@ -31,21 +56,38 @@ class SystemAdminTest extends TestCase
}
#[Test]
public function normal_admin_cannot_access_system_routes(): void
public function normal_admin_cannot_access_super_only_routes(): void
{
$staff = $this->normalAdmin();
$this->actingAs($staff, 'admin')
->get(route('admin.system.admins.index'))
->assertForbidden();
$this->actingAs($staff, 'admin')
->get(route('admin.logs.index'))
->assertForbidden();
}
#[Test]
public function normal_admin_can_access_system_settings(): void
{
$staff = $this->normalAdmin();
$this->actingAs($staff, 'admin')
->get(route('admin.system.settings.index'))
->assertForbidden();
->assertOk()
->assertSee('系统设置');
$this->actingAs($staff, 'admin')
->get(route('admin.system.admins.index'))
->assertForbidden();
->get(route('admin.home'))
->assertOk()
->assertSee('lay-href="'.route('admin.system.settings.index').'"', false)
->assertDontSee('lay-href="'.route('admin.system.admins.index').'"', false);
}
#[Test]
public function super_admin_sees_system_menu_normal_does_not(): void
public function super_admin_sees_full_system_menu(): void
{
$this->actingAs($this->superAdmin(), 'admin')
->get(route('admin.home'))
@@ -53,17 +95,16 @@ class SystemAdminTest extends TestCase
->assertSee('系统')
->assertSee('设置')
->assertSee('管理员');
$this->actingAs($this->normalAdmin(), 'admin')
->get(route('admin.home'))
->assertOk()
->assertDontSee('lay-href="'.route('admin.system.settings.index').'"', false);
}
#[Test]
public function super_admin_can_save_settings(): void
{
$super = $this->superAdmin();
config([
'coruna.telegram.bot_token' => 'bot:token',
'coruna.telegram.bot_username' => '',
]);
\Illuminate\Support\Facades\Http::fake(function ($request) {
if (str_contains($request->url(), 'getMe')) {
return \Illuminate\Support\Facades\Http::response([
@@ -77,33 +118,45 @@ class SystemAdminTest extends TestCase
$this->actingAs($super, 'admin')
->post(route('admin.system.settings.update'), [
'telegram_bot_token' => 'bot:token',
'telegram_owner_chat_id' => '-1001',
'channels_max_per_agent' => 5,
'channels_domains' => "cdn1.example.com\nhttps://cdn2.example.com/",
'panel_admin_hosts' => "Admin.Example.com\nhttps://ops.example.com:443/",
'panel_agent_hosts' => 'agent.example.com',
'auto_transfer_enabled' => '0',
])
->assertOk()
->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'));
$env = (string) file_get_contents(base_path('.env'));
$this->assertStringContainsString('TELEGRAM_OWNER_CHAT_ID=-1001', $env);
$this->assertStringContainsString('TELEGRAM_BOT_USERNAME=test_bot', $env);
$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(
'cdn1.example.com,cdn2.example.com',
Setting::query()->where('key', 'channels.domains')->value('value')
);
$this->assertSame(['admin.example.com', 'ops.example.com'], config('coruna.panel.admin_hosts'));
$this->assertSame(['agent.example.com'], config('coruna.panel.agent_hosts'));
$this->assertSame(
'admin.example.com,ops.example.com',
Setting::query()->where('key', 'panel.admin_hosts')->value('value')
);
$this->assertSame('-1001', config('coruna.telegram.owner_chat_id'));
}
#[Test]
public function settings_page_hides_token_and_transfer_to_addresses(): void
{
config([
'coruna.telegram.bot_token' => 'secret-token-should-not-render',
'coruna.telegram.bot_username' => 'ops_bot',
'coruna.transfer.to_address' => 'THiddenToAddressShouldNotRender',
'coruna.panel.admin_hosts' => [],
'coruna.channel_domains' => ['hidden-cdn.example.com'],
]);
$this->actingAs($this->superAdmin(), 'admin')
->get(route('admin.system.settings.index'))
->assertOk()
->assertDontSee('secret-token-should-not-render')
->assertDontSee('name="telegram_bot_token"', false)
->assertDontSee('name="transfer_to_address"', false)
->assertDontSee('name="panel_admin_hosts"', false)
->assertDontSee('name="channels_domains"', false)
->assertDontSee('后台访问域名')
->assertDontSee('渠道链接')
->assertDontSee('THiddenToAddressShouldNotRender')
->assertDontSee('hidden-cdn.example.com')
->assertSee('TELEGRAM_BOT_TOKEN')
->assertSee('@ops_bot');
}
#[Test]
@@ -166,13 +219,12 @@ class SystemAdminTest extends TestCase
$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/')
return str_contains($request->url(), '/botsaved-token/')
&& ($request->data()['chat_id'] ?? null) === '-1009'
&& str_contains((string) ($request->data()['text'] ?? ''), 'Telegram 测试');
});
@@ -199,10 +251,32 @@ class SystemAdminTest extends TestCase
}
#[Test]
public function normal_admin_cannot_test_official_telegram(): void
public function normal_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->normalAdmin(), 'admin')
->post(route('admin.system.settings.telegramTest'), [
'chat_id' => '-1009',
])
->assertOk()
->assertJsonPath('code', 0);
}
#[Test]
public function normal_admin_cannot_access_admins_crud(): void
{
$this->actingAs($this->normalAdmin(), 'admin')
->post(route('admin.system.settings.telegramTest'), ['chat_id' => '1'])
->post(route('admin.system.admins.store'), [
'username' => 'x',
'password' => 'secret12',
])
->assertForbidden();
}
@@ -230,6 +304,10 @@ class SystemAdminTest extends TestCase
->assertSee('@ops_bot')
->assertSee('t.me/ops_bot?startgroup=1', false);
$this->assertSame('ops_bot', Setting::query()->where('key', 'telegram.bot_username')->value('value'));
$this->assertSame('ops_bot', config('coruna.telegram.bot_username'));
$this->assertStringContainsString(
'TELEGRAM_BOT_USERNAME=ops_bot',
(string) file_get_contents(base_path('.env'))
);
}
}