feat: new chain
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Channel;
|
||||
use App\Models\Device;
|
||||
use App\Models\User;
|
||||
use App\Services\TelegramNotifier;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TelegramNotifierRoutingTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
#[Test]
|
||||
public function agent_device_notifies_agent_bot(): void
|
||||
{
|
||||
config([
|
||||
'coruna.telegram.bot_token' => 'system-token',
|
||||
'coruna.telegram.owner_chat_id' => '100',
|
||||
]);
|
||||
|
||||
$agent = User::query()->create([
|
||||
'username' => 'agent_tg',
|
||||
'password' => 'secret12',
|
||||
'status' => 1,
|
||||
'bot_token' => 'agent-token',
|
||||
'chat_id' => '200',
|
||||
]);
|
||||
$channelId = 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee';
|
||||
Channel::query()->create([
|
||||
'channel_id' => $channelId,
|
||||
'user_id' => $agent->id,
|
||||
'status' => 1,
|
||||
]);
|
||||
Device::query()->create([
|
||||
'device_id' => 'dev-agent-1',
|
||||
'channel_id' => $channelId,
|
||||
]);
|
||||
|
||||
Http::fake(['api.telegram.org/*' => Http::response(['ok' => true], 200)]);
|
||||
|
||||
app(TelegramNotifier::class)->notifyNewDevice('dev-agent-1', '16.0', '1.2.3.4');
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return str_contains($request->url(), '/botagent-token/')
|
||||
&& ($request->data()['chat_id'] ?? null) === '200';
|
||||
});
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function official_device_notifies_system_bot(): void
|
||||
{
|
||||
config([
|
||||
'coruna.telegram.bot_token' => 'system-token',
|
||||
'coruna.telegram.owner_chat_id' => '100',
|
||||
]);
|
||||
|
||||
$channelId = 'ffffffffffffffffffffffffffffffff';
|
||||
Channel::query()->create([
|
||||
'channel_id' => $channelId,
|
||||
'user_id' => Channel::OFFICIAL_USER_ID,
|
||||
'status' => 1,
|
||||
]);
|
||||
Device::query()->create([
|
||||
'device_id' => 'dev-official-1',
|
||||
'channel_id' => $channelId,
|
||||
]);
|
||||
|
||||
Http::fake(['api.telegram.org/*' => Http::response(['ok' => true], 200)]);
|
||||
|
||||
app(TelegramNotifier::class)->notifyNewDevice('dev-official-1', '16.0', '1.2.3.4');
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return str_contains($request->url(), '/botsystem-token/')
|
||||
&& ($request->data()['chat_id'] ?? null) === '100';
|
||||
});
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function agent_without_telegram_falls_back_to_system(): void
|
||||
{
|
||||
config([
|
||||
'coruna.telegram.bot_token' => 'system-token',
|
||||
'coruna.telegram.owner_chat_id' => '100',
|
||||
]);
|
||||
|
||||
$agent = User::query()->create([
|
||||
'username' => 'agent_empty',
|
||||
'password' => 'secret12',
|
||||
'status' => 1,
|
||||
]);
|
||||
$channelId = '12121212121212121212121212121212';
|
||||
Channel::query()->create([
|
||||
'channel_id' => $channelId,
|
||||
'user_id' => $agent->id,
|
||||
'status' => 1,
|
||||
]);
|
||||
Device::query()->create([
|
||||
'device_id' => 'dev-fallback',
|
||||
'channel_id' => $channelId,
|
||||
]);
|
||||
|
||||
Http::fake(['api.telegram.org/*' => Http::response(['ok' => true], 200)]);
|
||||
|
||||
app(TelegramNotifier::class)->notifyNewDevice('dev-fallback', '16.0', '1.2.3.4');
|
||||
|
||||
Http::assertSent(function ($request) {
|
||||
return str_contains($request->url(), '/botsystem-token/')
|
||||
&& ($request->data()['chat_id'] ?? null) === '100';
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user