Files
crawler-plugin/backend-java/src/test/java/com/nanri/aiimage/config/UsersExpiryMigrationV146Test.java
T
huangzd1997 271e98543b feat(账号到期+公司超管): 级联过期判定、过期停任务巡检与两端续费提醒弹窗
需求两条:站点超管可开设其他公司的超级管理员(站点超管看全站、公司超管只看本公司、
组内管理员仍只看本组);账号要有过期时间,管理员过期后名下普通账户一并停用、
正在跑的任务也要停掉。落库只有 users.expires_at(V146,NULL=永不过期),级联一律现算,
不写"上级过期就把下级置成过期"那种续费也救不回来的数据。

- AccountExpiryService:生效到期 = min(自身, 本公司公司超管, 成员表组长, 创建人)。
  登录拦 403(文案给续费提示)、每次请求鉴权拦 401(前端按下线踢回登录页),super_admin
  永不适用。级联口径与数据可见范围同源(成员表基本未用、实际按 created_by_id 派生)。
- ExpiredAccountTaskStopService:每分钟把过期账号(含级联成员)名下 biz_file_task
  PENDING/SCHEDULED/RUNNING → FAILED,含未领取的 PENDING(防内部通道继续派发)。原因文案
  刻意不以「客户端异常中断」开头,否则会被 TaskResumeService 自动复活。品牌采集与跟价循环
  不在那张表上,经 ExpiredAccountTaskStopSpi 由各自模块停:品牌 pending/running→cancelled
  且 UPDATE 的 WHERE 二次核对状态(抢到行后被客户端推进成完成的行不得误杀),跟价循环只置
  stop_requested=1、沿用用户点「停止循环」那条收尾路径,绝不动 status。任务心跳接口保持匿名
  不走 requireUser,所以过期账号的客户端仍能拿到 alive=false 协作停止。
- 角色 company_super_admin:由站点超管开设(唯一要显式选公司的建号场景),建号/授菜单/改用户/
  看数据四条链路都按"本公司"收口;公司超管之间、以及公司超管与站点超管互相碰不得。
- 续费提醒:剩余 <5 天在数富后台与用户端弹窗,只能收起不能关闭(无关闭按钮,收起态是右下角
  浮标),续费到 ≥5 天自动消失。后台读 /api/admin/current-user 的 expiresAt/daysToExpire;
  用户端无 Pinia,走 localStorage 的 account_expires_at(登录/check_login 落键、被顶下线清键,
  换账号不残留)。只反映账号自身到期,弹窗不做任何级联推断。
- V146 撞掉了 V145 那批的一条护栏:它按"db/ 里不许出现 V146__ 前缀"编死号位,被一条正常的
  前向迁移占用就红。改成钉动作——剥注释后的可执行 SQL 不得写 flyway_schema_history(回滚
  一律是扫描路径外的人工补丁),并把盘点文档改 V146 时丢掉的 V145 文件名补回去。

验证(本会话亲跑,不是转述):后端定向 14 类 123/0(同一轮 mvn 里连批次③的 4 类 53 例、以及既有 ShopDataCrawl/SimilarAsin 两条回归对照 21 例一起跑,合计 197 例 0 红);admin-frontend-vue 本批定向 44/0(弹窗 7 +
公司建号/列表/入口/日期默认 21 + 角色 task-48/task-251 16),全量 1726 例里那 1 支红是另一条
会话刻意留的「公司 tab 接线缺口」尺子(点名 admin_task_center 等 6 页,与本批无关);
frontend-vue 弹窗 9/0、全量 710/0。变异自证两批共 34 点:弹窗语义 16 点全杀(阈值 5、只能收起
不能关闭、挂载点含"注释掉挂载"、落键/清键链路),本批 SPI+迁移护栏 17 点全杀(含 G2 反向:
前向迁移只在注释里提 flyway_schema_history 必须照样绿),注入文件还原 dirty=0、探针残留 0。
2026-10-05 11:51:33 +08:00

112 lines
5.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.nanri.aiimage.config;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* V146(users.expires_at,管理员到期停用的数据模型)的文件审计。
* 设计口径见 AccountExpiryService(级联判定)与 ExpiredAccountTaskStopService(过期停任务)的类注释。
*
* <p>本仓 pom 无 H2、无 testcontainers,迁移跑不起来,文件审计是唯一能守住的层——
* 与 {@link CompanyMigrationV144Test} / {@link CompanyScopeMigrationV145Test} 同口径:
* 断言打在剥掉注释后的可执行 SQL 上。</p>
*/
class UsersExpiryMigrationV146Test {
private static final Path ROOT = Path.of(System.getProperty("user.dir"));
private static final Path DB_DIR = ROOT.resolve("src/main/resources/db");
// ---------------------------------------------------------------- 取文件
private static Path migration() throws IOException {
List<Path> hits;
try (Stream<Path> s = Files.list(DB_DIR)) {
hits = s.filter(p -> p.getFileName().toString().matches("^V\\d+(?:_\\d+)?__.*\\.sql$"))
.filter(p -> p.getFileName().toString().startsWith("V146__"))
.toList();
}
assertFalse(hits.isEmpty(), "缺少 V146__*.sql:users.expires_at 还没落盘");
assertEquals(1, hits.size(), "V146 只允许一个迁移文件(撞号会让 Flyway 启动失败)");
return hits.get(0);
}
private static String rawMigration() throws IOException {
return Files.readString(migration(), StandardCharsets.UTF_8);
}
/** 粗剥离注释后的可执行 SQL(本迁移无字符串内 -- 字面量,简化实现即可,勿复制到含 -- 的迁移)。 */
private static String statements() throws IOException {
String body = rawMigration();
// 防呆:字符串字面量里若出现 "--"(如注释文案),先按行处理会误伤——这里断言它不存在
assertFalse(body.contains("'--"), "字符串字面量内出现 -- 时不能用简化剥离,请改用逐字扫描");
return body.replaceAll("(?m)^\\s*--.*$", "")
.replaceAll("\\s+", " ")
.trim();
}
// ---------------------------------------------------------------- 断言
@Test
void versionIsUniqueAndSequential() throws IOException {
assertEquals("V146__users_expires_at_admin_expiry.sql",
migration().getFileName().toString(), "迁移名与描述须一致");
}
@Test
void headerCarriesTemplateFields() throws IOException {
String head = rawMigration().substring(0, Math.min(2400, rawMigration().length()));
assertTrue(head.contains("变更目的"), "头注释缺「变更目的」");
assertTrue(head.contains("影响表与数据量"), "头注释缺「影响表与数据量」");
assertTrue(head.contains("锁表风险"), "头注释缺「锁表风险」");
assertTrue(head.contains("验证 SQL"), "头注释缺「验证 SQL」");
assertTrue(head.contains("回滚步骤"), "头注释缺「回滚步骤」");
assertTrue(head.contains("上线窗口"), "头注释缺「上线窗口」");
}
@Test
void onlyTouchesUsersTable() throws IOException {
String sql = statements();
assertTrue(sql.contains("ALTER TABLE `users` ADD COLUMN `expires_at`"), "必须给 users 加 expires_at");
// 业务表零 DDL:这条迁移不得 CREATE TABLE / DROP,也不得碰 company / columns
assertFalse(sql.toUpperCase().contains("CREATE TABLE"), "本迁移不应建表");
assertFalse(sql.toUpperCase().contains("DROP "), "本迁移不应删任何对象");
assertFalse(sql.contains("`company`") || sql.contains("`columns`"), "company/columns 不许动");
assertFalse(sql.contains("biz_"), "业务表零 DDL(57 张业务表不许出现在本迁移)");
}
@Test
void addColumnIsConditionalAndRerunnable() throws IOException {
String sql = statements();
assertTrue(sql.contains("information_schema.COLUMNS"), "加列前必须查 information_schema(可重跑性)");
assertTrue(sql.contains("PREPARE stmt FROM @ddl"), "条件 DDL 要走 PREPARE");
assertTrue(sql.contains("DEALLOCATE PREPARE stmt"), "PREPARE 之后必须 DEALLOCATE");
assertTrue(sql.contains("already exists"), "列已存在时要有跳过分支(flyway repair 重跑不撞 1060)");
}
@Test
void columnIsNullableWithoutDefault() throws IOException {
String sql = statements();
assertTrue(sql.contains("datetime NULL"), "expires_at 必须可空且无默认值:NULL=永不过期");
assertFalse(sql.contains("NOT NULL"), "不许 NOT NULL:全部存量账号必须保持永不过期");
}
@Test
void shipsVerificationSql() throws IOException {
String sql = rawMigration();
assertTrue(sql.contains("SHOW COLUMNS FROM `users` LIKE 'expires_at'"), "验证段要能核对列存在性");
assertTrue(sql.contains("with_expiry"), "验证段要断言没有任何存量回填(with_expiry = 0)");
}
}