Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BCrwHHnGCB5XH968Nxokqw
64 lines
2.6 KiB
PHP
64 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Enums\ChannelType;
|
|
use App\Models\Channel;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Tests\TestCase;
|
|
|
|
class ChannelsApiTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private array $as = ['X-Remote-User' => 'nikita'];
|
|
|
|
public function test_lists_channels_including_auto_web(): void
|
|
{
|
|
$this->getJson('/me/channels', $this->as)->assertOk()
|
|
->assertJsonCount(1, 'channels')
|
|
->assertJsonPath('channels.0.type', 'web');
|
|
}
|
|
|
|
public function test_adds_and_deletes_webhook_channel(): void
|
|
{
|
|
$r = $this->postJson('/me/channels', ['type' => 'webhook', 'config' => ['deliver_url' => 'https://ha.test/api/webhook/hado', 'presence_url' => 'https://ha.test/api/presence']], $this->as)
|
|
->assertCreated()->assertJsonPath('type', 'webhook');
|
|
|
|
$this->deleteJson('/me/channels/'.$r->json('id'), [], $this->as)->assertNoContent();
|
|
$this->assertSame(1, Channel::count());
|
|
}
|
|
|
|
public function test_validates_webhook_config(): void
|
|
{
|
|
$this->postJson('/me/channels', ['type' => 'webhook', 'config' => ['deliver_url' => 'not a url']], $this->as)
|
|
->assertStatus(422)->assertJsonValidationErrors(['config.deliver_url', 'config.presence_url']);
|
|
// telegram подключается только через /start, не через API
|
|
$this->postJson('/me/channels', ['type' => 'telegram', 'config' => ['chat_id' => 1]], $this->as)
|
|
->assertStatus(422);
|
|
}
|
|
|
|
public function test_web_channel_cannot_be_deleted_and_foreign_channel_is_404(): void
|
|
{
|
|
$this->getJson('/me', $this->as);
|
|
$web = User::sole()->channels()->sole();
|
|
$this->deleteJson('/me/channels/'.$web->id, [], $this->as)->assertStatus(422);
|
|
|
|
$this->getJson('/me', ['X-Remote-User' => 'other']);
|
|
$foreign = Channel::create(['user_id' => User::where('login', 'other')->sole()->id, 'type' => ChannelType::Webhook, 'config' => ['deliver_url' => 'u', 'presence_url' => 'p'], 'enabled' => true]);
|
|
$this->deleteJson('/me/channels/'.$foreign->id, [], $this->as)->assertNotFound();
|
|
}
|
|
|
|
public function test_telegram_link_code(): void
|
|
{
|
|
$r = $this->postJson('/me/channels/telegram/link', [], $this->as)->assertOk();
|
|
$code = $r->json('code');
|
|
|
|
$this->assertSame(6, strlen($code));
|
|
$this->assertSame('https://t.me/hado_test_bot?start='.$code, $r->json('bot_url'));
|
|
$this->assertSame(User::sole()->id, Cache::get("tg:link:$code"));
|
|
}
|
|
}
|