mirror of
http://192.168.1.205:9980/cf_devdept2/cf_imes_server.git
synced 2026-08-12 21:02:08 +08:00
1、完善DeptServiceImpl单元测试覆盖率90%;2、新增security mock能力;3、移除postservice相关代码;4、移除部分无用模块的单测代码;
This commit is contained in:
+3
-3
@@ -111,9 +111,6 @@ public class DefaultDBFieldHandler implements MetaObjectHandler {
|
||||
setFieldValByName("updater", loginUserName, metaObject);
|
||||
}
|
||||
|
||||
// 从请求中获取到 Token
|
||||
String authorization = obtainAuthorization(AUTHORIZATION_HEADER_NAME, TOKEN_PARAM_NAME);
|
||||
|
||||
Long organId = null;
|
||||
|
||||
/**
|
||||
@@ -121,6 +118,9 @@ public class DefaultDBFieldHandler implements MetaObjectHandler {
|
||||
* 这里的组织ID字段填入即使为空也可,后续的请求中都会带有token,再从缓存中获取到 token 数据,再获取组织ID,填入即可
|
||||
*/
|
||||
try {
|
||||
// 从请求中获取到 Token
|
||||
String authorization = obtainAuthorization(AUTHORIZATION_HEADER_NAME, TOKEN_PARAM_NAME);
|
||||
|
||||
// 获取 token 对应的缓存数据,并得到组织 ID
|
||||
OAuth2AccessTokenDO oAuth2AccessTokenDO = get(authorization);
|
||||
organId = oAuth2AccessTokenDO.getOrganId();
|
||||
|
||||
@@ -73,6 +73,11 @@
|
||||
<artifactId>bizlog-sdk</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-test</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
+12
@@ -138,6 +138,18 @@ public class SecurityFrameworkUtils {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户的部门ID
|
||||
*
|
||||
*/
|
||||
public static Long getUserDeptId() {
|
||||
LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
|
||||
if (loginUser == null) {
|
||||
throw new ServiceException(GlobalErrorCodeConstants.UNAUTHORIZED);
|
||||
}
|
||||
return loginUser.getDeptId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前用户是否超级管理员
|
||||
*
|
||||
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.cf.imes.framework.security.test;
|
||||
|
||||
import org.springframework.security.test.context.support.WithSecurityContext;
|
||||
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
|
||||
/**
|
||||
* security mock loginuser annotation
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2025/9/8 11:16
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@WithSecurityContext(factory = WithMockLoginUserSecurityContextFactory.class)
|
||||
public @interface WithMockLoginUser {
|
||||
String username() default "";
|
||||
|
||||
long deptId() default 0L;
|
||||
|
||||
boolean isSuperAdmin() default false;
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package com.cf.imes.framework.security.test;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.cf.imes.framework.security.core.LoginUser;
|
||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||
import org.springframework.security.core.context.SecurityContext;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.security.test.context.support.WithSecurityContextFactory;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* security mock loginuser
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2025/9/8 11:17
|
||||
*/
|
||||
public class WithMockLoginUserSecurityContextFactory implements WithSecurityContextFactory<WithMockLoginUser> {
|
||||
|
||||
@Override
|
||||
public SecurityContext createSecurityContext(WithMockLoginUser annotation) {
|
||||
SecurityContext context = SecurityContextHolder.createEmptyContext();
|
||||
|
||||
LoginUser loginUser = new LoginUser();
|
||||
loginUser.setNickname(RandomUtil.randomString(10));
|
||||
loginUser.setDeptId(RandomUtil.randomLong());
|
||||
loginUser.setOrganId(RandomUtil.randomLong());
|
||||
loginUser.setIsSupAdmin(annotation.isSuperAdmin());
|
||||
|
||||
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
|
||||
loginUser, null, Collections.emptyList());
|
||||
context.setAuthentication(authenticationToken);
|
||||
|
||||
return context;
|
||||
}
|
||||
}
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* 提供random mock对象策略 基类
|
||||
*/
|
||||
package com.cf.imes.framework.test.core.podam;
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.cf.imes.framework.test.core.podam.strategy;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import uk.co.jemos.podam.common.AttributeStrategy;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* random mock对象 @NumberValid策略
|
||||
*
|
||||
* @author Gqr
|
||||
* @since 2025/9/9 14:17
|
||||
*/
|
||||
public class NumberValidStrategy implements AttributeStrategy<Integer> {
|
||||
|
||||
@Override
|
||||
public Integer getValue(Class<?> aClass, List<Annotation> list) {
|
||||
return RandomUtil.randomInt(0, Integer.MAX_VALUE);
|
||||
}
|
||||
}
|
||||
+4
@@ -5,8 +5,11 @@ import cn.hutool.core.text.CharSequenceUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.cf.imes.framework.common.enums.CommonStatusEnum;
|
||||
import com.cf.imes.framework.common.validation.NumberValid;
|
||||
import com.cf.imes.framework.test.core.podam.strategy.NumberValidStrategy;
|
||||
import uk.co.jemos.podam.api.PodamFactory;
|
||||
import uk.co.jemos.podam.api.PodamFactoryImpl;
|
||||
import uk.co.jemos.podam.api.RandomDataProviderStrategy;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.time.LocalDateTime;
|
||||
@@ -63,6 +66,7 @@ public class RandomUtils {
|
||||
}
|
||||
return RandomUtil.randomBoolean();
|
||||
});
|
||||
((RandomDataProviderStrategy) PODAM_FACTORY.getStrategy()).addOrReplaceAttributeStrategy(NumberValid.class, new NumberValidStrategy());
|
||||
}
|
||||
|
||||
public static String randomString() {
|
||||
|
||||
Reference in New Issue
Block a user