feat(停用): disabled_at 同挂登录与已发令牌两条闸,各沿用既有状态码
只挡请求不挡登录 = 被停用的人重登一次就把停用冲掉,所以四个入口(两张实体 × 两条闸)都要有。 停用判定排在到期之前:被停用的账号不该先收到续费提示。
This commit is contained in:
+93
@@ -0,0 +1,93 @@
|
||||
package com.nanri.aiimage.common.security;
|
||||
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.mapper.AdminUserMapper;
|
||||
import com.nanri.aiimage.common.mapper.ShopManageGroupMapper;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.modules.auth.model.entity.LoginUserEntity;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatCode;
|
||||
import static org.assertj.core.api.Assertions.catchThrowableOfType;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* 停用必须同时挡「重新登录」与「已发令牌」。只挡一边等于没停用:
|
||||
* 被停用的人重新登录一次,登录路径不看停用就会把吊销冲掉。
|
||||
* 状态码沿用各自闸门的既有口径(登录 403、会话 401),不新造第三种。
|
||||
*/
|
||||
class AccountDisabledGateTest {
|
||||
|
||||
private static final String MSG = "账号已停用,请联系管理员";
|
||||
|
||||
private AccountExpiryService service() {
|
||||
return new AccountExpiryService(mock(AdminUserMapper.class), mock(ShopManageGroupMapper.class));
|
||||
}
|
||||
|
||||
private AdminUserEntity admin(LocalDateTime disabledAt) {
|
||||
AdminUserEntity user = new AdminUserEntity();
|
||||
user.setId(9L);
|
||||
user.setUsername("小魏16");
|
||||
user.setCompanyId(2L);
|
||||
user.setRole("user");
|
||||
user.setIsAdmin(0);
|
||||
user.setDisabledAt(disabledAt);
|
||||
return user;
|
||||
}
|
||||
|
||||
private LoginUserEntity login(LocalDateTime disabledAt) {
|
||||
LoginUserEntity user = new LoginUserEntity();
|
||||
user.setId(9L);
|
||||
user.setUsername("小魏16");
|
||||
user.setCompanyId(2L);
|
||||
user.setRole("user");
|
||||
user.setIsAdmin(0);
|
||||
user.setDisabledAt(disabledAt);
|
||||
return user;
|
||||
}
|
||||
|
||||
@Test
|
||||
void disabledAdminAccountCannotLogInAgain() {
|
||||
BusinessException e = catchThrowableOfType(
|
||||
() -> service().assertLoginAllowed(admin(LocalDateTime.now().minusDays(1))),
|
||||
BusinessException.class);
|
||||
assertThat(e).isNotNull();
|
||||
assertThat(e.getCode()).isEqualTo(403);
|
||||
assertThat(e.getMessage()).isEqualTo(MSG);
|
||||
}
|
||||
|
||||
@Test
|
||||
void disabledAdminAccountLosesIssuedTokens() {
|
||||
BusinessException e = catchThrowableOfType(
|
||||
() -> service().assertSessionAllowed(admin(LocalDateTime.now())), BusinessException.class);
|
||||
assertThat(e).isNotNull();
|
||||
assertThat(e.getCode()).isEqualTo(401);
|
||||
assertThat(e.getMessage()).isEqualTo(MSG);
|
||||
}
|
||||
|
||||
/** 登录链路走的是另一张实体(同表同列),漏加就是「退出重登即复活」。 */
|
||||
@Test
|
||||
void disabledAccountOnLoginEntityIsAlsoBlocked() {
|
||||
BusinessException loginGate = catchThrowableOfType(
|
||||
() -> service().assertLoginAllowed(login(LocalDateTime.now())), BusinessException.class);
|
||||
assertThat(loginGate).isNotNull();
|
||||
assertThat(loginGate.getMessage()).isEqualTo(MSG);
|
||||
BusinessException sessionGate = catchThrowableOfType(
|
||||
() -> service().assertSessionAllowed(login(LocalDateTime.now())), BusinessException.class);
|
||||
assertThat(sessionGate).isNotNull();
|
||||
assertThat(sessionGate.getMessage()).isEqualTo(MSG);
|
||||
}
|
||||
|
||||
@Test
|
||||
void enabledAccountIsNotBlockedByDisabledGate() {
|
||||
AccountExpiryService s = service();
|
||||
// 未停用:不许出现停用文案(到期判定另有既有用例负责)
|
||||
assertThatCode(() -> s.assertSessionAllowed(admin(null))).doesNotThrowAnyException();
|
||||
BusinessException e = catchThrowableOfType(
|
||||
() -> s.assertLoginAllowed(admin(null)), BusinessException.class);
|
||||
assertThat(e == null || !MSG.equals(e.getMessage())).isTrue();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user