user(). * * Auth/tenant как у CampaignApiTest: Tenant::factory + User(tenant_id) + actingAs. */ uses(RefreshDatabase::class); beforeEach(function () { $this->tenant = Tenant::factory()->create(); $this->user = User::factory()->create(['tenant_id' => $this->tenant->id]); $this->actingAs($this->user); }); /** @return array валидный payload сохранения правила. */ function autoRulePayload(array $overrides = []): array { return array_merge([ 'enabled' => true, 'ad_text' => 'Заходите в наш канал', 'ad_link' => 'https://t.me/example_channel', 'ord_category' => 'Размещение рекламы', 'budget_cap_rub' => '2500.00', 'daily_limit_rub' => '1000.00', 'batch_threshold' => 500, ], $overrides); } it('GET /auto-rule без правила отдаёт безопасные значения по умолчанию (авто выключено)', function () { $this->getJson('/api/telegram/auto-rule') ->assertOk() ->assertJsonPath('enabled', false) ->assertJsonPath('daily_limit_rub', '0.00'); }); it('PUT /auto-rule создаёт правило тенанта', function () { $this->putJson('/api/telegram/auto-rule', autoRulePayload()) ->assertOk() ->assertJsonPath('enabled', true) ->assertJsonPath('daily_limit_rub', '1000.00') ->assertJsonPath('batch_threshold', 500); $rule = AutoRule::where('tenant_id', $this->tenant->id)->first(); expect($rule)->not->toBeNull(); expect((string) $rule->daily_limit_rub)->toBe('1000.00'); expect($rule->batch_threshold)->toBe(500); expect($rule->enabled)->toBeTrue(); }); it('PUT /auto-rule обновляет существующее правило, не плодит второе', function () { $this->putJson('/api/telegram/auto-rule', autoRulePayload())->assertOk(); $this->putJson('/api/telegram/auto-rule', autoRulePayload(['enabled' => false, 'daily_limit_rub' => '0.00'])) ->assertOk() ->assertJsonPath('enabled', false); expect(AutoRule::where('tenant_id', $this->tenant->id)->count())->toBe(1); }); it('PUT /auto-rule: порог ниже минимума МТС (367) — 422', function () { $this->putJson('/api/telegram/auto-rule', autoRulePayload(['batch_threshold' => 100])) ->assertStatus(422) ->assertJsonValidationErrors(['batch_threshold']); }); it('PUT /auto-rule: отрицательный дневной лимит — 422', function () { $this->putJson('/api/telegram/auto-rule', autoRulePayload(['daily_limit_rub' => '-5'])) ->assertStatus(422) ->assertJsonValidationErrors(['daily_limit_rub']); }); it('PUT /auto-rule: при включении без текста объявления — 422', function () { $this->putJson('/api/telegram/auto-rule', autoRulePayload(['ad_text' => ''])) ->assertStatus(422) ->assertJsonValidationErrors(['ad_text']); }); it('GET /auto-rule не видит правило чужого тенанта', function () { $other = Tenant::factory()->create(); AutoRule::create([ 'tenant_id' => $other->id, 'enabled' => true, 'ad_text' => 'Чужой', 'ad_link' => 'https://t.me/x', 'ord_category' => 'Размещение рекламы', 'budget_cap_rub' => '2500.00', 'daily_limit_rub' => '999.00', ]); $this->getJson('/api/telegram/auto-rule') ->assertOk() ->assertJsonPath('enabled', false) ->assertJsonPath('daily_limit_rub', '0.00'); // видит СВОИ дефолты, не чужие 999 });