82 lines
2.7 KiB
PHP
82 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Models\BotDialog;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Queue;
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
/** Разговор, где уже писал вошедший клиент. */
|
|
function ownedChat(User $owner): string
|
|
{
|
|
$chat = str_repeat('9', 32);
|
|
BotDialog::create([
|
|
'chat_id' => $chat, 'direction' => 'in', 'message' => 'мой баланс?',
|
|
'source' => 'portal', 'user_id' => $owner->id, 'created_at' => now(),
|
|
]);
|
|
BotDialog::create([
|
|
'chat_id' => $chat, 'direction' => 'out', 'message' => 'Баланс 3 400 ₽.',
|
|
'created_at' => now(),
|
|
]);
|
|
|
|
return $chat;
|
|
}
|
|
|
|
it('владелец читает свой разговор', function () {
|
|
$owner = User::factory()->create();
|
|
$chat = ownedChat($owner);
|
|
|
|
$this->actingAs($owner)->getJson("/api/chat/{$chat}/messages?after=0")
|
|
->assertOk()
|
|
->assertJsonCount(2, 'messages');
|
|
});
|
|
|
|
it('чужой вошедший не читает разговор владельца', function () {
|
|
$owner = User::factory()->create();
|
|
$stranger = User::factory()->create();
|
|
$chat = ownedChat($owner);
|
|
|
|
$this->actingAs($stranger)->getJson("/api/chat/{$chat}/messages?after=0")->assertStatus(404);
|
|
});
|
|
|
|
it('гость не читает разговор вошедшего клиента', function () {
|
|
$owner = User::factory()->create();
|
|
$chat = ownedChat($owner);
|
|
|
|
$this->getJson("/api/chat/{$chat}/messages?after=0")->assertStatus(404);
|
|
});
|
|
|
|
it('чужой не может дописать в разговор владельца', function () {
|
|
Queue::fake();
|
|
$owner = User::factory()->create();
|
|
$stranger = User::factory()->create();
|
|
$chat = ownedChat($owner);
|
|
|
|
$this->actingAs($stranger)
|
|
->postJson('/api/chat/message', ['chat_id' => $chat, 'text' => 'а сколько у него денег?'])
|
|
->assertStatus(404);
|
|
|
|
Queue::assertNothingPushed();
|
|
});
|
|
|
|
it('гость не может дописать в разговор вошедшего клиента', function () {
|
|
Queue::fake();
|
|
$owner = User::factory()->create();
|
|
$chat = ownedChat($owner);
|
|
|
|
$this->postJson('/api/chat/message', ['chat_id' => $chat, 'text' => 'подслушиваю'])
|
|
->assertStatus(404);
|
|
|
|
Queue::assertNothingPushed();
|
|
});
|
|
|
|
it('гостевой разговор остаётся доступен как раньше', function () {
|
|
$chat = str_repeat('8', 32);
|
|
BotDialog::create(['chat_id' => $chat, 'direction' => 'out', 'message' => 'общий ответ', 'created_at' => now()]);
|
|
|
|
$this->getJson("/api/chat/{$chat}/messages?after=0")->assertOk()->assertJsonCount(1, 'messages');
|
|
});
|