feat(多公司): 第二批 —— 登录带出公司身份、移人仅超管、启动期 DEFAULT 哨兵、列表公司列

company 模块落地(目录读写收窄 + 菜单闸门),并把公司维度接进四个已有出口:
- 登录/current-user 回包带 companyId/companyName,且取名失败不得挡住登录;
- 移人仅超管,且校验「创建人同公司或为超管」不变式(二期免改 77 处过滤点的前提);
- users.company_id 的 DEFAULT 由启动期哨兵盯着,多公司已上线仍未摘则打
  COMPANY_DEFAULT_STILL_PRESENT(ASCII 标记串,供 docker logs grep);
- 账号列表加 company_id/company_name 与可选按公司筛选:超管可筛任意公司,
  公司内 admin 传别家公司视为无结果(不报错也不越权)。

39 例护栏,29 条变异自证(首轮 25/28 咬死,两条存活都是用例侧的洞:
namesByIds 被 mock 掉所以内部去重从未被证明、contains("公司") 分不清闸门与
不变式;修补后 4/4 KILLED)。业务表零 DDL、零回填。
This commit is contained in:
2026-10-04 07:11:13 +08:00
parent 5633898a0d
commit 061147c603
33 changed files with 2454 additions and 12 deletions
@@ -0,0 +1,146 @@
package com.nanri.aiimage.modules.auth.service;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.security.AuthProperties;
import com.nanri.aiimage.common.security.JwtService;
import com.nanri.aiimage.modules.auth.mapper.LoginUserMapper;
import com.nanri.aiimage.modules.auth.model.dto.LoginRequest;
import com.nanri.aiimage.modules.auth.model.entity.LoginUserEntity;
import com.nanri.aiimage.modules.auth.model.vo.LoginResultVo;
import com.nanri.aiimage.modules.auth.util.WerkzeugPasswordEncoder;
import com.nanri.aiimage.modules.company.service.CompanyService;
import com.nanri.aiimage.modules.permission.service.PermissionMenuService;
import io.jsonwebtoken.Claims;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* 用户端(app-web)登录回包里的公司身份(设计文档 §2.4)。
*
* <p>前端 {@code src/shared/auth/company-identity.ts} 读的是 {@code data.companyName},
* 拿到空串就清 localStorage 键。所以这里要保证两件事:① 有公司时名字带得出去;
* ② <b>公司这一环永远不能把登录本身弄失败</b>——公司查不到、公司停用、账号压根没有公司,
* 都只能让公司名变空。②用一条"把 requireActive 桩成必抛"的用例钉住:
* 谁把显示口径改成准入口径,这条立刻红。</p>
*/
class AuthServiceCompanyTest {
private LoginUserMapper loginUserMapper;
private WerkzeugPasswordEncoder passwordEncoder;
private JwtService jwtService;
private CompanyService companyService;
private AuthService service;
@BeforeEach
void setUp() {
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""),
LoginUserEntity.class);
loginUserMapper = mock(LoginUserMapper.class);
passwordEncoder = mock(WerkzeugPasswordEncoder.class);
jwtService = mock(JwtService.class);
companyService = mock(CompanyService.class);
AuthProperties authProperties = mock(AuthProperties.class);
// 本组用例只关心公司身份,单设备互踢那条路径留给 AuthServiceTest
when(authProperties.isSingleDeviceEnabled()).thenReturn(false);
when(jwtService.ttlSeconds()).thenReturn(604800L);
service = new AuthService(loginUserMapper, passwordEncoder, jwtService,
mock(PermissionMenuService.class), authProperties, companyService);
}
private LoginUserEntity user(Long companyId) {
LoginUserEntity user = new LoginUserEntity();
user.setId(7L);
user.setUsername("u7");
user.setPasswordHash("hash");
user.setIsAdmin(0);
user.setRole("normal");
user.setCompanyId(companyId);
user.setMachine("devA");
return user;
}
private LoginRequest loginRequest() {
LoginRequest request = new LoginRequest();
request.setUsername("u7");
request.setPassword("pwd");
request.setDeviceId("devA");
return request;
}
private void stubSuccessfulLogin(LoginUserEntity user) {
when(loginUserMapper.selectOne(any())).thenReturn(user);
when(passwordEncoder.matches("pwd", "hash")).thenReturn(true);
when(jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
}
@Test
@DisplayName("登录回包带 companyId 与公司名")
void loginCarriesCompanyIdentity() {
stubSuccessfulLogin(user(5L));
when(companyService.nameOrEmpty(5L)).thenReturn("厦门米椿花");
LoginResultVo vo = service.login(loginRequest());
assertThat(vo.getCompanyId()).isEqualTo(5L);
assertThat(vo.getCompanyName()).isEqualTo("厦门米椿花");
}
@Test
@DisplayName("公司行查不到:登录照样成功,只是公司名为空")
void loginSucceedsWhenTheCompanyRowIsGone() {
stubSuccessfulLogin(user(9L));
when(companyService.nameOrEmpty(9L)).thenReturn("");
// 实现一旦改成"登录要校验公司可用"(requireActive),这条立刻红:
// 停用一家公司就等于把它的账号全部锁在门外。
when(companyService.requireActive(any())).thenThrow(new BusinessException("公司已停用"));
LoginResultVo vo = service.login(loginRequest());
assertThat(vo.getToken()).isEqualTo("jwt-token");
assertThat(vo.getCompanyName()).isEmpty();
verify(companyService, never()).requireActive(any());
}
@Test
@DisplayName("账号没有公司归属:companyId 为 null、公司名为空串,不是 null 字符串")
void userWithoutACompanyLogsInWithEmptyCompanyName() {
stubSuccessfulLogin(user(null));
when(companyService.nameOrEmpty(null)).thenReturn("");
LoginResultVo vo = service.login(loginRequest());
assertThat(vo.getCompanyId()).isNull();
assertThat(vo.getCompanyName()).isEmpty();
}
@Test
@DisplayName("check_login 同样带公司身份:刷新页面后顶栏不能空")
void checkLoginCarriesCompanyIdentity() {
LoginUserEntity user = user(5L);
Claims claims = mock(Claims.class);
when(claims.getSubject()).thenReturn("7");
when(claims.get("deviceId")).thenReturn("devA");
when(jwtService.parse("t")).thenReturn(claims);
when(loginUserMapper.selectById(7L)).thenReturn(user);
when(jwtService.issue(eq(7L), anyString(), anyString())).thenReturn("jwt-token");
when(companyService.nameOrEmpty(5L)).thenReturn("厦门米椿花");
LoginResultVo vo = service.checkLogin("t", null);
assertThat(vo.getCompanyId()).isEqualTo(5L);
assertThat(vo.getCompanyName()).isEqualTo("厦门米椿花");
}
}
@@ -9,6 +9,7 @@ import com.nanri.aiimage.modules.auth.model.dto.LoginRequest;
import com.nanri.aiimage.modules.auth.model.entity.LoginUserEntity;
import com.nanri.aiimage.modules.auth.model.vo.LoginResultVo;
import com.nanri.aiimage.modules.auth.util.WerkzeugPasswordEncoder;
import com.nanri.aiimage.modules.company.service.CompanyService;
import com.nanri.aiimage.modules.permission.service.PermissionMenuService;
import io.jsonwebtoken.Claims;
import org.apache.ibatis.builder.MapperBuilderAssistant;
@@ -157,7 +158,7 @@ class AuthServiceTest {
when(authProperties.isSingleDeviceEnabled()).thenReturn(singleDeviceEnabled);
when(jwtService.ttlSeconds()).thenReturn(604800L);
AuthService service = new AuthService(loginUserMapper, passwordEncoder, jwtService,
permissionMenuService, authProperties);
permissionMenuService, authProperties, mock(CompanyService.class));
return new Fixture(service, loginUserMapper, passwordEncoder, jwtService);
}