2026-05-09 06:58:49 +03:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
|
|
use App\Models\Tenant;
|
|
|
|
|
|
use App\Models\User;
|
2026-05-14 05:29:34 +03:00
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
2026-05-09 06:58:49 +03:00
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
|
|
|
2026-05-14 05:29:34 +03:00
|
|
|
|
uses(RefreshDatabase::class);
|
2026-05-09 06:58:49 +03:00
|
|
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
|
|
$this->tenant = Tenant::factory()->create();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('GET /api/managers возвращает active users тенанта', function () {
|
|
|
|
|
|
DB::statement('SET app.current_tenant_id = '.$this->tenant->id);
|
2026-05-21 19:14:11 +03:00
|
|
|
|
// actingAs одного из активных пользователей тенанта — он сам входит в список.
|
|
|
|
|
|
$ivan = User::factory()->for($this->tenant)->create([
|
2026-05-09 06:58:49 +03:00
|
|
|
|
'first_name' => 'Иван', 'last_name' => 'Петров', 'is_active' => true,
|
|
|
|
|
|
]);
|
|
|
|
|
|
User::factory()->for($this->tenant)->create([
|
|
|
|
|
|
'first_name' => 'Ольга', 'last_name' => 'Романова', 'is_active' => true,
|
|
|
|
|
|
]);
|
|
|
|
|
|
User::factory()->for($this->tenant)->create([
|
|
|
|
|
|
'first_name' => 'Удалённый', 'is_active' => false,
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
2026-05-21 19:14:11 +03:00
|
|
|
|
$this->actingAs($ivan);
|
|
|
|
|
|
$r = $this->getJson('/api/managers');
|
2026-05-09 06:58:49 +03:00
|
|
|
|
$r->assertStatus(200);
|
|
|
|
|
|
$managers = $r->json('managers');
|
|
|
|
|
|
expect($managers)->toHaveCount(2);
|
|
|
|
|
|
$names = array_column($managers, 'name');
|
|
|
|
|
|
expect($names)->toContain('Иван П.')->toContain('Ольга Р.');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('GET /api/managers возвращает initials с fallback на email', function () {
|
|
|
|
|
|
DB::statement('SET app.current_tenant_id = '.$this->tenant->id);
|
2026-05-21 19:14:11 +03:00
|
|
|
|
$admin = User::factory()->for($this->tenant)->create([
|
2026-05-09 06:58:49 +03:00
|
|
|
|
'email' => 'admin@example.ru',
|
|
|
|
|
|
'first_name' => null,
|
|
|
|
|
|
'last_name' => null,
|
|
|
|
|
|
'is_active' => true,
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
2026-05-21 19:14:11 +03:00
|
|
|
|
$this->actingAs($admin);
|
|
|
|
|
|
$r = $this->getJson('/api/managers');
|
2026-05-09 06:58:49 +03:00
|
|
|
|
$r->assertStatus(200);
|
|
|
|
|
|
$manager = $r->json('managers.0');
|
|
|
|
|
|
expect($manager['name'])->toBe('admin@example.ru');
|
|
|
|
|
|
expect($manager['initials'])->toBe('AD');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2026-05-21 19:14:11 +03:00
|
|
|
|
test('GET /api/managers без авторизации возвращает 401', function () {
|
|
|
|
|
|
$this->getJson('/api/managers')->assertStatus(401);
|
2026-05-09 06:58:49 +03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('POST /api/deals 422 если manager_id не принадлежит tenant\'у', function () {
|
|
|
|
|
|
$otherTenant = Tenant::factory()->create();
|
|
|
|
|
|
DB::statement('SET app.current_tenant_id = '.$otherTenant->id);
|
|
|
|
|
|
$otherManager = User::factory()->for($otherTenant)->create(['is_active' => true]);
|
|
|
|
|
|
|
|
|
|
|
|
// Назначаем чужого менеджера на свою сделку — должен быть 422.
|
2026-05-16 15:14:17 +03:00
|
|
|
|
$this->actingAs(User::factory()->for($this->tenant)->create());
|
2026-05-09 06:58:49 +03:00
|
|
|
|
$r = $this->postJson('/api/deals', [
|
|
|
|
|
|
'project_name' => 'X',
|
|
|
|
|
|
'phone' => '+7 (999) 000-00-00',
|
|
|
|
|
|
'manager_id' => $otherManager->id,
|
|
|
|
|
|
]);
|
|
|
|
|
|
$r->assertStatus(422);
|
|
|
|
|
|
expect($r->json('errors'))->toHaveKey('manager_id');
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('POST /api/deals 422 если manager_id не активен (is_active=false)', function () {
|
|
|
|
|
|
DB::statement('SET app.current_tenant_id = '.$this->tenant->id);
|
|
|
|
|
|
$inactive = User::factory()->for($this->tenant)->create(['is_active' => false]);
|
|
|
|
|
|
|
2026-05-16 15:14:17 +03:00
|
|
|
|
$this->actingAs(User::factory()->for($this->tenant)->create());
|
2026-05-09 06:58:49 +03:00
|
|
|
|
$r = $this->postJson('/api/deals', [
|
|
|
|
|
|
'project_name' => 'X',
|
|
|
|
|
|
'phone' => '+7 (999) 000-00-00',
|
|
|
|
|
|
'manager_id' => $inactive->id,
|
|
|
|
|
|
]);
|
|
|
|
|
|
$r->assertStatus(422);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('POST /api/deals принимает manager_id из своего tenant\'а', function () {
|
|
|
|
|
|
DB::statement('SET app.current_tenant_id = '.$this->tenant->id);
|
|
|
|
|
|
$manager = User::factory()->for($this->tenant)->create(['is_active' => true]);
|
|
|
|
|
|
|
2026-05-16 15:14:17 +03:00
|
|
|
|
$this->actingAs(User::factory()->for($this->tenant)->create());
|
2026-05-09 06:58:49 +03:00
|
|
|
|
$r = $this->postJson('/api/deals', [
|
|
|
|
|
|
'project_name' => 'X',
|
|
|
|
|
|
'phone' => '+7 (999) 000-00-00',
|
|
|
|
|
|
'manager_id' => $manager->id,
|
|
|
|
|
|
]);
|
|
|
|
|
|
$r->assertStatus(201);
|
|
|
|
|
|
expect($r->json('deal.manager_id'))->toBe($manager->id);
|
|
|
|
|
|
});
|