CaiWuXiTong/tests/Feature/Console/CmbMailFailureNotificationTest.php

93 lines
3.5 KiB
PHP

<?php
namespace Tests\Feature\Console;
use App\Models\Auth\DingTalkIdentity;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Tests\Feature\FeatureTestCase;
class CmbMailFailureNotificationTest extends FeatureTestCase
{
public function test_it_sends_a_server_side_dingtalk_failure_notice(): void
{
DingTalkIdentity::query()->updateOrCreate(
['user_id' => $this->user->id],
[
'union_id' => 'cmb-alert-union',
'dingtalk_user_id' => 'cmb-alert-user',
'nickname' => '山时',
],
);
config([
'dingtalk.client_id' => 'test-client-id',
'dingtalk.client_secret' => 'test-client-secret',
'dingtalk.robot.code' => 'test-robot-code',
'dingtalk.robot.access_token_url' => 'https://dingtalk.test/access-token',
'dingtalk.robot.send_url' => 'https://dingtalk.test/robot/send',
]);
Http::fake([
'https://dingtalk.test/access-token' => Http::response(['accessToken' => 'test-token']),
'https://dingtalk.test/robot/send' => Http::response(['processQueryKey' => 'sent']),
]);
$this->artisan('cmb-mail:notify-failure', [
'--date' => '2026-11-24',
'--type' => 'auth',
'--reason' => '邮箱登录失败,账号 finance@example.com 需要重新授权。',
'--recipient' => '山时',
])->expectsOutput('钉钉失败提醒已发送。')->assertSuccessful();
Http::assertSent(function (Request $request): bool {
if ($request->url() !== 'https://dingtalk.test/robot/send') {
return false;
}
$body = $request->data();
return $body['robotCode'] === 'test-robot-code'
&& $body['userIds'] === ['cmb-alert-user']
&& str_contains($body['msgParam'], '邮箱授权失效或登录失败')
&& str_contains($body['msgParam'], '07:00、07:10 两次均失败')
&& str_contains($body['msgParam'], '[邮箱已隐藏]')
&& ! str_contains($body['msgParam'], 'finance@example.com');
});
}
public function test_it_refuses_to_guess_when_the_recipient_is_ambiguous(): void
{
DingTalkIdentity::query()->updateOrCreate(
['user_id' => $this->user->id],
[
'union_id' => 'cmb-alert-union-1',
'dingtalk_user_id' => 'cmb-alert-user-1',
'nickname' => '重名成员',
],
);
$second = user_model_class()::create([
'name' => '重名测试账号',
'email' => 'cmb-alert-second@example.test',
'password' => 'fixture-unused-password',
'enabled' => true,
'locale' => 'zh-CN',
'landing_page' => 'dashboard',
]);
DingTalkIdentity::query()->create([
'user_id' => $second->id,
'union_id' => 'cmb-alert-union-2',
'dingtalk_user_id' => 'cmb-alert-user-2',
'nickname' => '重名成员',
]);
Http::fake();
$this->artisan('cmb-mail:notify-failure', [
'--date' => '2026-11-24',
'--type' => 'auth',
'--recipient' => '重名成员',
])->expectsOutput('该昵称对应多个钉钉企业成员,无法安全确定接收人。')
->assertFailed();
Http::assertNothingSent();
}
}