From 7b1d04547e6147c8530449755d2c8f8892796556 Mon Sep 17 00:00:00 2001 From: hellcat Date: Wed, 4 Nov 2026 18:36:34 +0800 Subject: [PATCH] test(fuc-xb): add bounded real-mail acceptance harness --- scripts/fuc-xb-acceptance/README.md | 9 ++++ scripts/fuc-xb-acceptance/mail.php | 76 +++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 scripts/fuc-xb-acceptance/README.md create mode 100644 scripts/fuc-xb-acceptance/mail.php diff --git a/scripts/fuc-xb-acceptance/README.md b/scripts/fuc-xb-acceptance/README.md new file mode 100644 index 00000000..4dc3f9de --- /dev/null +++ b/scripts/fuc-xb-acceptance/README.md @@ -0,0 +1,9 @@ +# FUC-XB 外部联调验收 + +仅适用于已登记隔离测试站,不切正式站,不适用于DPL/HN。按精确Git版本执行;输出、凭据及临时状态只在测试站private,失败或结果未知先回读,不重发。 + +2026-11-04用户要求继续真实邮件/节点/客户端验收;随后明确不实际付款,但尽量验证不扣款的支付链路。收件地址指定mukaitao(既定mukaitao@proton.me)。 + +mail.php 从同宿主旧FUC只读取得原邮件传输配置;密钥只交给同一服务认证组件,不输出、不传到本机。在当前PHP进程的MySQL事务内临时配置原生邮件流程和同步任务连接及进程内缓存(不测试常驻Redis队列),调用真实HTTP内核验证码接口及原生MailService通知,只发指定收件人两封,验证同消息键不重发。不启动常驻worker或自动群发、不改FPM环境;回滚配置/日志并清理本次验证码。区分provider提交/投递、SMTP提交与收件箱实收。 + +节点使用专用QA组、独立端口及进程,原节点不切换。客户端应使用原生订阅、严格TLS、预先登记的预期出口并核对上报。额外访问门保持;仅环回受限桥可携带既有访问Cookie,XB原生节点令牌仍须验证。 diff --git a/scripts/fuc-xb-acceptance/mail.php b/scripts/fuc-xb-acceptance/mail.php new file mode 100644 index 00000000..ffe3b787 --- /dev/null +++ b/scripts/fuc-xb-acceptance/mail.php @@ -0,0 +1,76 @@ +PDO::ERRMODE_EXCEPTION,PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC]); + $p->exec('SET TRANSACTION READ ONLY');$p->beginTransaction(); + $s=$p->query("SELECT item,value FROM config WHERE item IN ('smtp_host','smtp_port','smtp_username','smtp_password','smtp_sender','smtp_ssl')")->fetchAll(PDO::FETCH_KEY_PAIR); + $v=$p->query("SELECT apikey,auther FROM mail_must WHERE client='resend' AND status=1 LIMIT 1")->fetch(); + $p->rollBack();chdir($cwd); + if(($s['smtp_host']??'')!=='mail.privateemail.com'||($s['smtp_sender']??'')!=='fu@gemcook.net'||empty($s['smtp_password'])||empty($v['apikey']))throw new RuntimeException('Registered FUC transports changed'); + return ['smtp'=>$s,'resend'=>$v]; +} +$legacy=legacyMailConfig(); +putenv('XB_INSTANCE=fuc-xb-test');$_SERVER['XB_INSTANCE']=$_ENV['XB_INSTANCE']='fuc-xb-test'; +require $root.'/vendor/autoload.php';$app=require $root.'/bootstrap/app.php';$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); +use Illuminate\Support\Facades\{DB,Cache}; +use App\Models\MailMessage; +if(DB::connection()->getDatabaseName()!=='fuc_xb_test')throw new RuntimeException('Wrong database'); +// Isolate settings cache too: never publish uncommitted secrets/settings to shared Redis. +config(['cache.stores.redis'=>['driver'=>'array'],'cache.default'=>'redis']); +Cache::purge('redis');$app->forgetInstance(App\Support\Setting::class); +$recipient='mukaitao@proton.me';$key='fuc-xb-live-mail-20261104'; +if(MailMessage::count()!==0)throw new RuntimeException('Unexpected existing mail; stop before sending'); +$result=['recipient'=>$recipient,'sender'=>'fu@gemcook.net','real_deliveries'=>[],'simulated'=>false,'native_http_kernel'=>true,'production_site_changed'=>false]; +$baseline=['users'=>App\Models\User::count(),'orders'=>App\Models\Order::count(),'balance'=>(int)App\Models\User::sum('balance'),'settings_hash'=>hash('sha256',DB::table('v2_settings')->orderBy('id')->get()->toJson())]; +$db=DB::connection();$db->beginTransaction(); +try { + $flow=array_replace((array)admin_setting('mail_flow',[]),['enabled'=>true,'resend_enabled'=>true,'resend_key'=>$legacy['resend']['apikey'],'resend_from'=>$legacy['resend']['auther'],'reply_to'=>'fu@gemcook.net','inbound_enabled'=>false,'auto_reply_enabled'=>false,'report_enabled'=>false,'logo_path'=>'','daily_limit'=>100,'monthly_limit'=>3000]); + // Uncommitted settings remain invisible to other PHP/FPM requests. + app(App\Support\Setting::class)->save(['mail_flow'=>$flow,'automatic_mail_policy'=>['level'=>3,'since'=>null]]); + config(['queue.default'=>'sync','mail.driver'=>'smtp','mail.host'=>$legacy['smtp']['smtp_host'],'mail.port'=>(int)$legacy['smtp']['smtp_port'],'mail.username'=>$legacy['smtp']['smtp_username'],'mail.password'=>$legacy['smtp']['smtp_password'],'mail.encryption'=>'ssl','mail.from.address'=>'fu@gemcook.net','mail.from.name'=>'FUC-XB_TEST','mailflow.logo_path'=>'']); + $r=Illuminate\Http\Request::create('https://fuc-xb-test.gemcook.net/api/v1/passport/comm/sendEmailVerify','POST',['email'=>$recipient],[],[],['HTTP_ACCEPT'=>'application/json','REMOTE_ADDR'=>'127.0.0.1']); + app()->instance('request',$r);$response=app(Illuminate\Contracts\Http\Kernel::class)->handle($r); + if($response->getStatusCode()!==200)throw new RuntimeException('Native verification request failed'); + $first=MailMessage::where('email',$recipient)->where('purpose','verification')->firstOrFail(); + if(!in_array($first->status,['submitted','delivered'],true)||!in_array($first->channel,['resend','conventional'],true))throw new RuntimeException('Verification not submitted'); + $code=Cache::get(App\Utils\CacheKey::get('EMAIL_VERIFY_CODE',$recipient)); + if(!preg_match('/^\d{6}$/',(string)$code))throw new RuntimeException('Verification cache contract failed'); + $result['verification_http_status']=200;$result['verification_code_cached_in_isolated_process']=true; + $before=$first->attempts; + (new App\Jobs\SendEmailJob(['email'=>$recipient,'subject'=>'FUC-XB verification idempotency check','template_name'=>'verify','purpose'=>'verification','message_key'=>$first->message_key,'template_value'=>['name'=>'FUC-XB_TEST','code'=>$code,'url'=>'https://fuc-xb-test.tomtojapan.online']]))->handle(); + $first->refresh();if($first->attempts!==$before)throw new RuntimeException('Mail duplicate submitted again'); + $result['same_message_key_does_not_resend']=true; + $second=App\Services\MailService::sendEmail(['email'=>$recipient,'subject'=>'[FUC-XB隔离验收 20261104] 通知邮件','template_name'=>'notify','purpose'=>'admin_test','message_key'=>$key.'-notification','template_value'=>['name'=>'FUC-XB_TEST','content'=>'隔离测试站真实通知邮件,仅用于验收。不涉及正式切换、扣款或原用户通知。','url'=>'https://fuc-xb-test.tomtojapan.online']]); + if(!in_array($second['status']??null,['submitted','delivered'],true))throw new RuntimeException('Notification not submitted'); + foreach(MailMessage::where('email',$recipient)->get()as$m){ + $event=null; + if($m->channel==='resend'){ + try{$detail=app(App\Services\MailFlow\ResendClient::class)->retrieve($m->provider_id);$event=$detail['last_event']??null;}catch(Throwable){$event='provider_status_unavailable';} + } + $result['real_deliveries'][]=['purpose'=>$m->purpose,'channel'=>$m->channel,'status'=>$m->status,'provider_event'=>$event,'fallback_reason'=>$m->fallback_reason,'attempts'=>$m->attempts,'provider_id'=>$m->provider_id]; + } + $result['both_messages_submitted']=count($result['real_deliveries'])===2; + file_put_contents($out,json_encode($result,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT));chmod($out,0600); +} catch(Throwable $e) { + $result['failed']=get_class($e);$result['failure_stage']=$e->getMessage(); + file_put_contents($out,json_encode($result,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT));chmod($out,0600); + throw $e; +} finally { + while($db->transactionLevel()>0)$db->rollBack(); + Cache::store('redis')->forget(App\Support\Setting::CACHE_KEY); + Cache::forget(App\Utils\CacheKey::get('EMAIL_VERIFY_CODE',$recipient)); + Cache::forget(App\Utils\CacheKey::get('LAST_SEND_EMAIL_VERIFY_TIMESTAMP',$recipient)); + Cache::forget('mailflow:resend:usage'); +} +$after=['users'=>App\Models\User::count(),'orders'=>App\Models\Order::count(),'balance'=>(int)App\Models\User::sum('balance'),'settings_hash'=>hash('sha256',DB::table('v2_settings')->orderBy('id')->get()->toJson())]; +$result['temporary_settings_and_database_rows_rolled_back']=$baseline===$after&&MailMessage::count()===0; +$result['verification_code_cleared']=true;$result['passed']=($result['both_messages_submitted']??false)&&$result['temporary_settings_and_database_rows_rolled_back']; +file_put_contents($out,json_encode($result,JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT));echo json_encode($result,JSON_UNESCAPED_UNICODE).PHP_EOL;