重构社交登录,完善单元测试
parent
89df5b3cf6
commit
878445a238
|
@ -23,33 +23,33 @@ public enum SocialTypeEnum implements IntArrayValuable {
|
||||||
* Gitee
|
* Gitee
|
||||||
* 文档链接:https://gitee.com/api/v5/oauth_doc#/
|
* 文档链接:https://gitee.com/api/v5/oauth_doc#/
|
||||||
*/
|
*/
|
||||||
GITEE(10, 1, "GITEE"),
|
GITEE(10, "GITEE"),
|
||||||
/**
|
/**
|
||||||
* 钉钉
|
* 钉钉
|
||||||
* 文档链接:https://developers.dingtalk.com/document/app/obtain-identity-credentials
|
* 文档链接:https://developers.dingtalk.com/document/app/obtain-identity-credentials
|
||||||
*/
|
*/
|
||||||
DINGTALK(20, 2, "DINGTALK"),
|
DINGTALK(20, "DINGTALK"),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 企业微信
|
* 企业微信
|
||||||
* 文档链接:https://xkcoding.com/2019/08/06/use-justauth-integration-wechat-enterprise.html
|
* 文档链接:https://xkcoding.com/2019/08/06/use-justauth-integration-wechat-enterprise.html
|
||||||
*/
|
*/
|
||||||
WECHAT_ENTERPRISE(30, 3, "WECHAT_ENTERPRISE"),
|
WECHAT_ENTERPRISE(30, "WECHAT_ENTERPRISE"),
|
||||||
/**
|
/**
|
||||||
* 微信公众平台 - 移动端 H5
|
* 微信公众平台 - 移动端 H5
|
||||||
* 文档链接:https://www.cnblogs.com/juewuzhe/p/11905461.html
|
* 文档链接:https://www.cnblogs.com/juewuzhe/p/11905461.html
|
||||||
*/
|
*/
|
||||||
WECHAT_MP(31, 3, "WECHAT_MP"),
|
WECHAT_MP(31, "WECHAT_MP"),
|
||||||
/**
|
/**
|
||||||
* 微信开放平台 - 网站应用 PC 端扫码授权登录
|
* 微信开放平台 - 网站应用 PC 端扫码授权登录
|
||||||
* 文档链接:https://justauth.wiki/guide/oauth/wechat_open/#_2-申请开发者资质认证
|
* 文档链接:https://justauth.wiki/guide/oauth/wechat_open/#_2-申请开发者资质认证
|
||||||
*/
|
*/
|
||||||
WECHAT_OPEN(32, 3, "WECHAT_OPEN"),
|
WECHAT_OPEN(32, "WECHAT_OPEN"),
|
||||||
/**
|
/**
|
||||||
* 微信小程序
|
* 微信小程序
|
||||||
* 文档链接:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html
|
* 文档链接:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html
|
||||||
*/
|
*/
|
||||||
WECHAT_MINI_PROGRAM(33, 3, "WECHAT_MINI_PROGRAM"),
|
WECHAT_MINI_PROGRAM(33, "WECHAT_MINI_PROGRAM"),
|
||||||
;
|
;
|
||||||
|
|
||||||
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(SocialTypeEnum::getType).toArray();
|
public static final int[] ARRAYS = Arrays.stream(values()).mapToInt(SocialTypeEnum::getType).toArray();
|
||||||
|
@ -58,12 +58,6 @@ public enum SocialTypeEnum implements IntArrayValuable {
|
||||||
* 类型
|
* 类型
|
||||||
*/
|
*/
|
||||||
private final Integer type;
|
private final Integer type;
|
||||||
/**
|
|
||||||
* 平台
|
|
||||||
*
|
|
||||||
* 例如说,微信平台下,有企业微信、公众平台、开放平台、小程序等
|
|
||||||
*/
|
|
||||||
private final Integer platform;
|
|
||||||
/**
|
/**
|
||||||
* 类型的标识
|
* 类型的标识
|
||||||
*/
|
*/
|
||||||
|
@ -78,11 +72,4 @@ public enum SocialTypeEnum implements IntArrayValuable {
|
||||||
return ArrayUtil.firstMatch(o -> o.getType().equals(type), values());
|
return ArrayUtil.firstMatch(o -> o.getType().equals(type), values());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Set<Integer> getTypes(Collection<Integer> platforms) {
|
|
||||||
return Arrays.stream(values())
|
|
||||||
.filter(socialTypeEnum -> platforms.contains(socialTypeEnum.getPlatform()))
|
|
||||||
.map(SocialTypeEnum::getType)
|
|
||||||
.collect(Collectors.toSet());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,8 @@ public class SocialUserBindDO extends BaseDO {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 关联的用户编号
|
* 关联的用户编号
|
||||||
|
*
|
||||||
|
* 关联 UserDO 的编号
|
||||||
*/
|
*/
|
||||||
private Long userId;
|
private Long userId;
|
||||||
/**
|
/**
|
||||||
|
@ -32,14 +34,16 @@ public class SocialUserBindDO extends BaseDO {
|
||||||
private Integer userType;
|
private Integer userType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 社交平台
|
* 社交平台的用户编号
|
||||||
*
|
*
|
||||||
* 枚举 {@link SocialTypeEnum#getPlatform()}
|
* 关联 {@link SocialUserDO#getId()}
|
||||||
*/
|
*/
|
||||||
private Integer platform;
|
private Long socialUserId;
|
||||||
/**
|
/**
|
||||||
* 社交的全局编号
|
* 社交平台的类型
|
||||||
|
*
|
||||||
|
* 冗余 {@link SocialUserDO#getType()}
|
||||||
*/
|
*/
|
||||||
private String unionId;
|
private Integer socialType;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package cn.iocoder.yudao.module.system.dal.dataobject.social;
|
package cn.iocoder.yudao.module.system.dal.dataobject.social;
|
||||||
|
|
||||||
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
|
|
||||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
|
||||||
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
|
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
|
||||||
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
|
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
@ -10,7 +8,6 @@ import lombok.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 社交用户
|
* 社交用户
|
||||||
* 通过 {@link SocialUserDO#getUserId()} 关联到对应的 {@link AdminUserDO}
|
|
||||||
*
|
*
|
||||||
* @author weir
|
* @author weir
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -10,26 +10,23 @@ import java.util.List;
|
||||||
@Mapper
|
@Mapper
|
||||||
public interface SocialUserBindMapper extends BaseMapperX<SocialUserBindDO> {
|
public interface SocialUserBindMapper extends BaseMapperX<SocialUserBindDO> {
|
||||||
|
|
||||||
default void deleteByUserTypeAndUserIdAndUnionId(Integer userType, Long userId, Integer platform) {
|
default void deleteByUserTypeAndUserIdAndSocialType(Integer userType, Long userId, Integer socialType) {
|
||||||
delete(new LambdaQueryWrapperX<SocialUserBindDO>()
|
delete(new LambdaQueryWrapperX<SocialUserBindDO>()
|
||||||
.eq(SocialUserBindDO::getUserType, userType)
|
.eq(SocialUserBindDO::getUserType, userType)
|
||||||
.eq(SocialUserBindDO::getUserId, userId)
|
.eq(SocialUserBindDO::getUserId, userId)
|
||||||
.eq(SocialUserBindDO::getPlatform, platform));
|
.eq(SocialUserBindDO::getSocialType, socialType));
|
||||||
}
|
}
|
||||||
|
|
||||||
default void deleteByUserTypeAndPlatformAndUnionId(Integer userType, Integer platform, String unionId) {
|
default void deleteByUserTypeAndSocialUserId(Integer userType, Long socialUserId) {
|
||||||
delete(new LambdaQueryWrapperX<SocialUserBindDO>()
|
delete(new LambdaQueryWrapperX<SocialUserBindDO>()
|
||||||
.eq(SocialUserBindDO::getUserType, userType)
|
.eq(SocialUserBindDO::getUserType, userType)
|
||||||
.eq(SocialUserBindDO::getPlatform, platform)
|
.eq(SocialUserBindDO::getSocialUserId, socialUserId));
|
||||||
.eq(SocialUserBindDO::getUnionId, unionId));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
default SocialUserBindDO selectByUserTypeAndPlatformAndUnionId(Integer userType,
|
default SocialUserBindDO selectByUserTypeAndSocialUserId(Integer userType, Long socialUserId) {
|
||||||
Integer platform, String unionId) {
|
|
||||||
return selectOne(new LambdaQueryWrapperX<SocialUserBindDO>()
|
return selectOne(new LambdaQueryWrapperX<SocialUserBindDO>()
|
||||||
.eq(SocialUserBindDO::getUserType, userType)
|
.eq(SocialUserBindDO::getUserType, userType)
|
||||||
.eq(SocialUserBindDO::getPlatform, platform)
|
.eq(SocialUserBindDO::getSocialUserId, socialUserId));
|
||||||
.eq(SocialUserBindDO::getUnionId, unionId));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
default List<SocialUserBindDO> selectListByUserIdAndUserType(Long userId, Integer userType) {
|
default List<SocialUserBindDO> selectListByUserIdAndUserType(Long userId, Integer userType) {
|
||||||
|
|
|
@ -22,13 +22,7 @@ public interface SocialUserMapper extends BaseMapperX<SocialUserDO> {
|
||||||
default SocialUserDO selectByTypeAndOpenid(Integer type, String openid) {
|
default SocialUserDO selectByTypeAndOpenid(Integer type, String openid) {
|
||||||
return selectOne(new LambdaQueryWrapper<SocialUserDO>()
|
return selectOne(new LambdaQueryWrapper<SocialUserDO>()
|
||||||
.eq(SocialUserDO::getType, type)
|
.eq(SocialUserDO::getType, type)
|
||||||
.eq(SocialUserDO::getCode, openid));
|
.eq(SocialUserDO::getOpenid, openid));
|
||||||
}
|
|
||||||
|
|
||||||
default List<SocialUserDO> selectListByUnionIdAndType(Collection<String> unionIds, Collection<Integer> types) {
|
|
||||||
return selectList(new LambdaQueryWrapper<SocialUserDO>()
|
|
||||||
.in(SocialUserDO::getUnionId, unionIds)
|
|
||||||
.in(SocialUserDO::getType, types));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,8 +2,6 @@ package cn.iocoder.yudao.module.system.service.social;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.util.StrUtil;
|
|
||||||
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
|
||||||
import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
|
import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
|
||||||
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
|
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
|
||||||
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserBindDO;
|
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserBindDO;
|
||||||
|
@ -25,9 +23,9 @@ import org.springframework.validation.annotation.Validated;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||||
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
|
||||||
import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString;
|
import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString;
|
||||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
||||||
|
|
||||||
|
@ -78,7 +76,6 @@ public class SocialUserServiceImpl implements SocialUserService {
|
||||||
}
|
}
|
||||||
socialUser.setType(type).setCode(code).setState(state) // 需要保存 code + state 字段,保证后续可查询
|
socialUser.setType(type).setCode(code).setState(state) // 需要保存 code + state 字段,保证后续可查询
|
||||||
.setOpenid(authUser.getUuid()).setToken(authUser.getToken().getAccessToken()).setRawTokenInfo((toJsonString(authUser.getToken())))
|
.setOpenid(authUser.getUuid()).setToken(authUser.getToken().getAccessToken()).setRawTokenInfo((toJsonString(authUser.getToken())))
|
||||||
.setUnionId(StrUtil.blankToDefault(authUser.getToken().getUnionId(), authUser.getUuid())) // unionId 识别多个用户
|
|
||||||
.setNickname(authUser.getNickname()).setAvatar(authUser.getAvatar()).setRawUserInfo(toJsonString(authUser.getRawUserInfo()));
|
.setNickname(authUser.getNickname()).setAvatar(authUser.getAvatar()).setRawUserInfo(toJsonString(authUser.getRawUserInfo()));
|
||||||
if (socialUser.getId() == null) {
|
if (socialUser.getId() == null) {
|
||||||
socialUserMapper.insert(socialUser);
|
socialUserMapper.insert(socialUser);
|
||||||
|
@ -96,9 +93,7 @@ public class SocialUserServiceImpl implements SocialUserService {
|
||||||
return Collections.emptyList();
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
// 获得社交用户
|
// 获得社交用户
|
||||||
Set<Integer> platforms = CollectionUtils.convertSet(socialUserBinds, SocialUserBindDO::getPlatform);
|
return socialUserMapper.selectBatchIds(convertSet(socialUserBinds, SocialUserBindDO::getSocialUserId));
|
||||||
return socialUserMapper.selectListByUnionIdAndType(CollectionUtils.convertSet(socialUserBinds, SocialUserBindDO::getUnionId),
|
|
||||||
SocialTypeEnum.getTypes(platforms));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -108,19 +103,17 @@ public class SocialUserServiceImpl implements SocialUserService {
|
||||||
SocialUserDO socialUser = authSocialUser(reqDTO.getType(), reqDTO.getCode(), reqDTO.getState());
|
SocialUserDO socialUser = authSocialUser(reqDTO.getType(), reqDTO.getCode(), reqDTO.getState());
|
||||||
Assert.notNull(socialUser, "社交用户不能为空");
|
Assert.notNull(socialUser, "社交用户不能为空");
|
||||||
|
|
||||||
// 如果 unionId 之前被绑定过,需要进行解绑
|
// 社交用户可能之前绑定过别的用户,需要进行解绑
|
||||||
Integer platform = SocialTypeEnum.valueOfType(socialUser.getType()).getPlatform();
|
socialUserBindMapper.deleteByUserTypeAndSocialUserId(reqDTO.getUserType(), socialUser.getId());
|
||||||
socialUserBindMapper.deleteByUserTypeAndPlatformAndUnionId(reqDTO.getUserType(), platform,
|
|
||||||
socialUser.getUnionId());
|
|
||||||
|
|
||||||
// 如果 userId 之前绑定过该 type 的其它账号,需要进行解绑
|
// 用户可能之前已经绑定过该社交类型,需要进行解绑
|
||||||
socialUserBindMapper.deleteByUserTypeAndUserIdAndUnionId(reqDTO.getUserType(), reqDTO.getUserId(),
|
socialUserBindMapper.deleteByUserTypeAndUserIdAndSocialType(reqDTO.getUserType(), reqDTO.getUserId(),
|
||||||
socialUser.getUnionId());
|
socialUser.getType());
|
||||||
|
|
||||||
// 绑定当前登录的社交用户
|
// 绑定当前登录的社交用户
|
||||||
SocialUserBindDO socialUserBind = SocialUserBindDO.builder()
|
SocialUserBindDO socialUserBind = SocialUserBindDO.builder()
|
||||||
.userId(reqDTO.getUserId()).userType(reqDTO.getUserType())
|
.userId(reqDTO.getUserId()).userType(reqDTO.getUserType())
|
||||||
.platform(platform).unionId(socialUser.getUnionId()).build();
|
.socialUserId(socialUser.getId()).socialType(socialUser.getType()).build();
|
||||||
socialUserBindMapper.insert(socialUserBind);
|
socialUserBindMapper.insert(socialUserBind);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,8 +126,7 @@ public class SocialUserServiceImpl implements SocialUserService {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获得对应的社交绑定关系
|
// 获得对应的社交绑定关系
|
||||||
socialUserBindMapper.deleteByUserTypeAndUserIdAndPlatformAndUnionId(userType, userId,
|
socialUserBindMapper.deleteByUserTypeAndUserIdAndSocialType(userType, userId, socialUser.getType());
|
||||||
SocialTypeEnum.valueOfType(socialUser.getType()).getPlatform(), socialUser.getUnionId());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -144,8 +136,8 @@ public class SocialUserServiceImpl implements SocialUserService {
|
||||||
Assert.notNull(socialUser, "社交用户不能为空");
|
Assert.notNull(socialUser, "社交用户不能为空");
|
||||||
|
|
||||||
// 如果未绑定的社交用户,则无法自动登录,进行报错
|
// 如果未绑定的社交用户,则无法自动登录,进行报错
|
||||||
SocialUserBindDO socialUserBind = socialUserBindMapper.selectByUserTypeAndPlatformAndUnionId(userType,
|
SocialUserBindDO socialUserBind = socialUserBindMapper.selectByUserTypeAndSocialUserId(userType,
|
||||||
SocialTypeEnum.valueOfType(socialUser.getType()).getPlatform(), socialUser.getUnionId());
|
socialUser.getId());
|
||||||
if (socialUserBind == null) {
|
if (socialUserBind == null) {
|
||||||
throw exception(AUTH_THIRD_LOGIN_NOT_BIND);
|
throw exception(AUTH_THIRD_LOGIN_NOT_BIND);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,13 @@
|
||||||
package cn.iocoder.yudao.module.system.service.social;
|
package cn.iocoder.yudao.module.system.service.social;
|
||||||
|
|
||||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||||
import cn.iocoder.yudao.framework.test.core.util.AssertUtils;
|
import cn.iocoder.yudao.framework.test.core.ut.BaseDbAndRedisUnitTest;
|
||||||
import cn.iocoder.yudao.framework.test.core.util.RandomUtils;
|
|
||||||
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
|
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
|
||||||
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserBindDO;
|
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserBindDO;
|
||||||
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO;
|
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialUserDO;
|
||||||
import cn.iocoder.yudao.module.system.dal.mysql.social.SocialUserBindMapper;
|
import cn.iocoder.yudao.module.system.dal.mysql.social.SocialUserBindMapper;
|
||||||
import cn.iocoder.yudao.module.system.dal.mysql.social.SocialUserMapper;
|
import cn.iocoder.yudao.module.system.dal.mysql.social.SocialUserMapper;
|
||||||
import cn.iocoder.yudao.framework.test.core.ut.BaseDbAndRedisUnitTest;
|
|
||||||
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
|
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
|
|
||||||
import com.xkcoding.justauth.AuthRequestFactory;
|
import com.xkcoding.justauth.AuthRequestFactory;
|
||||||
import me.zhyd.oauth.enums.AuthResponseStatus;
|
import me.zhyd.oauth.enums.AuthResponseStatus;
|
||||||
import me.zhyd.oauth.model.AuthCallback;
|
import me.zhyd.oauth.model.AuthCallback;
|
||||||
|
@ -19,16 +16,15 @@ import me.zhyd.oauth.model.AuthUser;
|
||||||
import me.zhyd.oauth.request.AuthRequest;
|
import me.zhyd.oauth.request.AuthRequest;
|
||||||
import me.zhyd.oauth.utils.AuthStateUtils;
|
import me.zhyd.oauth.utils.AuthStateUtils;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.function.Executable;
|
|
||||||
import org.mockito.MockedStatic;
|
import org.mockito.MockedStatic;
|
||||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static cn.hutool.core.util.RandomUtil.*;
|
import static cn.hutool.core.util.RandomUtil.randomLong;
|
||||||
|
import static cn.hutool.core.util.RandomUtil.randomString;
|
||||||
import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString;
|
import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString;
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
|
||||||
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
||||||
|
@ -157,24 +153,22 @@ public class SocialUserServiceTest extends BaseDbAndRedisUnitTest {
|
||||||
assertEquals(toJsonString(authUser.getRawUserInfo()), socialUser.getRawUserInfo());
|
assertEquals(toJsonString(authUser.getRawUserInfo()), socialUser.getRawUserInfo());
|
||||||
assertEquals(type, socialUser.getType());
|
assertEquals(type, socialUser.getType());
|
||||||
assertEquals(authUser.getUuid(), socialUser.getOpenid());
|
assertEquals(authUser.getUuid(), socialUser.getOpenid());
|
||||||
assertEquals(authUser.getToken().getUnionId(), socialUser.getUnionId());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetSocialUserList() {
|
public void testGetSocialUserList() {
|
||||||
Long userId = 1L;
|
Long userId = 1L;
|
||||||
Integer userType = UserTypeEnum.ADMIN.getValue();
|
Integer userType = UserTypeEnum.ADMIN.getValue();
|
||||||
// mock 获得绑定
|
|
||||||
socialUserBindMapper.insert(randomPojo(SocialUserBindDO.class) // 可被查询到
|
|
||||||
.setUserId(userId).setUserType(userType).setPlatform(SocialTypeEnum.GITEE.getPlatform())
|
|
||||||
.setUnionId("test_unionId"));
|
|
||||||
socialUserBindMapper.insert(randomPojo(SocialUserBindDO.class) // 不可被查询到
|
|
||||||
.setUserId(2L).setUserType(userType).setPlatform(SocialTypeEnum.DINGTALK.getPlatform()));
|
|
||||||
// mock 获得社交用户
|
// mock 获得社交用户
|
||||||
SocialUserDO socialUser = randomPojo(SocialUserDO.class).setType(SocialTypeEnum.GITEE.getType())
|
SocialUserDO socialUser = randomPojo(SocialUserDO.class).setType(SocialTypeEnum.GITEE.getType());
|
||||||
.setUnionId("test_unionId");
|
|
||||||
socialUserMapper.insert(socialUser); // 可被查到
|
socialUserMapper.insert(socialUser); // 可被查到
|
||||||
socialUserMapper.insert(randomPojo(SocialUserDO.class)); // 不可被查到
|
socialUserMapper.insert(randomPojo(SocialUserDO.class)); // 不可被查到
|
||||||
|
// mock 获得绑定
|
||||||
|
socialUserBindMapper.insert(randomPojo(SocialUserBindDO.class) // 可被查询到
|
||||||
|
.setUserId(userId).setUserType(userType).setSocialType(SocialTypeEnum.GITEE.getType())
|
||||||
|
.setSocialUserId(socialUser.getId()));
|
||||||
|
socialUserBindMapper.insert(randomPojo(SocialUserBindDO.class) // 不可被查询到
|
||||||
|
.setUserId(2L).setUserType(userType).setSocialType(SocialTypeEnum.DINGTALK.getType()));
|
||||||
|
|
||||||
// 调用
|
// 调用
|
||||||
List<SocialUserDO> result = socialUserService.getSocialUserList(userId, userType);
|
List<SocialUserDO> result = socialUserService.getSocialUserList(userId, userType);
|
||||||
|
@ -184,120 +178,79 @@ public class SocialUserServiceTest extends BaseDbAndRedisUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBindSocialUser_create() {
|
public void testBindSocialUser() {
|
||||||
// 准备参数
|
// 准备参数
|
||||||
SocialUserBindReqDTO reqDTO = new SocialUserBindReqDTO()
|
SocialUserBindReqDTO reqDTO = new SocialUserBindReqDTO()
|
||||||
.setUserId(1L).setUserType(UserTypeEnum.ADMIN.getValue())
|
.setUserId(1L).setUserType(UserTypeEnum.ADMIN.getValue())
|
||||||
.setType(SocialTypeEnum.GITEE.getType()).setCode("test_code").setState("test_state");
|
.setType(SocialTypeEnum.GITEE.getType()).setCode("test_code").setState("test_state");
|
||||||
// mock 数据:获得社交用户
|
// mock 数据:获得社交用户
|
||||||
SocialUserDO socialUser = randomPojo(SocialUserDO.class).setType(reqDTO.getType())
|
SocialUserDO socialUser = randomPojo(SocialUserDO.class).setType(reqDTO.getType())
|
||||||
.setCode(reqDTO.getCode()).setState(reqDTO.getState()).setUnionId("test_unionId");
|
.setCode(reqDTO.getCode()).setState(reqDTO.getState());
|
||||||
socialUserMapper.insert(socialUser);
|
socialUserMapper.insert(socialUser);
|
||||||
// mock 数据:解绑其它账号
|
// mock 数据:用户可能之前已经绑定过该社交类型
|
||||||
socialUserBindMapper.insert(randomPojo(SocialUserBindDO.class).setUserId(1L).setUserType(UserTypeEnum.ADMIN.getValue())
|
socialUserBindMapper.insert(randomPojo(SocialUserBindDO.class).setUserId(1L).setUserType(UserTypeEnum.ADMIN.getValue())
|
||||||
.setPlatform(SocialTypeEnum.GITEE.getPlatform()).setUnionId("test_delete_unionId"));
|
.setSocialType(SocialTypeEnum.GITEE.getType()).setSocialUserId(-1L));
|
||||||
|
// mock 数据:社交用户可能之前绑定过别的用户
|
||||||
|
socialUserBindMapper.insert(randomPojo(SocialUserBindDO.class).setUserType(UserTypeEnum.ADMIN.getValue())
|
||||||
|
.setSocialType(SocialTypeEnum.GITEE.getType()).setSocialUserId(socialUser.getId()));
|
||||||
|
|
||||||
// 调用
|
// 调用
|
||||||
socialUserService.bindSocialUser(reqDTO);
|
socialUserService.bindSocialUser(reqDTO);
|
||||||
// 断言
|
// 断言
|
||||||
List<SocialUserBindDO> socialUserBinds = socialUserBindMapper.selectList();
|
List<SocialUserBindDO> socialUserBinds = socialUserBindMapper.selectList();
|
||||||
assertEquals(1, socialUserBinds.size());
|
assertEquals(1, socialUserBinds.size());
|
||||||
|
|
||||||
}
|
}
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * 情况二,更新 SocialUserDO 的情况
|
|
||||||
// */
|
|
||||||
// @Test
|
|
||||||
// public void testBindSocialUser_update() {
|
|
||||||
// // mock 数据
|
|
||||||
// SocialUserDO dbSocialUser = randomPojo(SocialUserDO.class, socialUserDO -> {
|
|
||||||
// socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue());
|
|
||||||
// socialUserDO.setType(randomEle(SocialTypeEnum.values()).getType());
|
|
||||||
// });
|
|
||||||
// socialUserMapper.insert(dbSocialUser);
|
|
||||||
// // 准备参数
|
|
||||||
// Long userId = dbSocialUser.getUserId();
|
|
||||||
// Integer type = dbSocialUser.getType();
|
|
||||||
// AuthUser authUser = randomPojo(AuthUser.class);
|
|
||||||
// // mock 方法
|
|
||||||
//
|
|
||||||
// // 调用
|
|
||||||
// socialService.bindSocialUser(userId, UserTypeEnum.ADMIN.getValue(), type, authUser);
|
|
||||||
// // 断言
|
|
||||||
// List<SocialUserDO> socialUsers = socialUserMapper.selectList("user_id", userId);
|
|
||||||
// assertEquals(1, socialUsers.size());
|
|
||||||
// assertBindSocialUser(socialUsers.get(0), authUser, userId, type);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// /**
|
|
||||||
// * 情况一和二都存在的,逻辑二的场景
|
|
||||||
// */
|
|
||||||
// @Test
|
|
||||||
// public void testBindSocialUser_userId() {
|
|
||||||
// // mock 数据
|
|
||||||
// SocialUserDO dbSocialUser = randomPojo(SocialUserDO.class, socialUserDO -> {
|
|
||||||
// socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue());
|
|
||||||
// socialUserDO.setType(randomEle(SocialTypeEnum.values()).getType());
|
|
||||||
// });
|
|
||||||
// socialUserMapper.insert(dbSocialUser);
|
|
||||||
// // 准备参数
|
|
||||||
// Long userId = randomLongId();
|
|
||||||
// Integer type = dbSocialUser.getType();
|
|
||||||
// AuthUser authUser = randomPojo(AuthUser.class);
|
|
||||||
// // mock 方法
|
|
||||||
//
|
|
||||||
// // 调用
|
|
||||||
// socialService.bindSocialUser(userId, UserTypeEnum.ADMIN.getValue(), type, authUser);
|
|
||||||
// // 断言
|
|
||||||
// List<SocialUserDO> socialUsers = socialUserMapper.selectList("user_id", userId);
|
|
||||||
// assertEquals(1, socialUsers.size());
|
|
||||||
// }
|
|
||||||
|
|
||||||
//
|
@Test
|
||||||
// /**
|
public void testUnbindSocialUser_success() {
|
||||||
// * 情况一,如果新老的 unionId 是一致的,无需解绑
|
// 准备参数
|
||||||
// */
|
Long userId = 1L;
|
||||||
// @Test
|
Integer userType = UserTypeEnum.ADMIN.getValue();
|
||||||
// public void testUnbindOldSocialUser_no() {
|
Integer type = SocialTypeEnum.GITEE.getType();
|
||||||
// // mock 数据
|
String openid = "test_openid";
|
||||||
// SocialUserDO oldSocialUser = randomPojo(SocialUserDO.class, socialUserDO -> {
|
// mock 数据:社交用户
|
||||||
// socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue());
|
SocialUserDO socialUser = randomPojo(SocialUserDO.class).setType(type).setOpenid(openid);
|
||||||
// socialUserDO.setType(randomEle(SocialTypeEnum.values()).getType());
|
socialUserMapper.insert(socialUser);
|
||||||
// });
|
// mock 数据:社交绑定关系
|
||||||
// socialUserMapper.insert(oldSocialUser);
|
SocialUserBindDO socialUserBind = randomPojo(SocialUserBindDO.class).setUserType(userType)
|
||||||
// // 准备参数
|
.setUserId(userId).setSocialType(type);
|
||||||
// Long userId = oldSocialUser.getUserId();
|
socialUserBindMapper.insert(socialUserBind);
|
||||||
// Integer type = oldSocialUser.getType();
|
|
||||||
// String newUnionId = oldSocialUser.getUnionId();
|
// 调用
|
||||||
//
|
socialUserService.unbindSocialUser(userId, userType, type, openid);
|
||||||
// // 调用
|
// 断言
|
||||||
// socialService.unbindOldSocialUser(userId, UserTypeEnum.ADMIN.getValue(), type, newUnionId);
|
assertEquals(0, socialUserBindMapper.selectCount(null).intValue());
|
||||||
// // 断言
|
}
|
||||||
// assertEquals(1L, socialUserMapper.selectCount(null).longValue());
|
|
||||||
// }
|
@Test
|
||||||
//
|
public void testUnbindSocialUser_notFound() {
|
||||||
//
|
// 调用,并断言
|
||||||
// /**
|
assertServiceException(
|
||||||
// * 情况二,如果新老的 unionId 不一致的,需解绑
|
() -> socialUserService.unbindSocialUser(randomLong(), UserTypeEnum.ADMIN.getValue(),
|
||||||
// */
|
SocialTypeEnum.GITEE.getType(), "test_openid"),
|
||||||
// @Test
|
SOCIAL_USER_NOT_FOUND);
|
||||||
// public void testUnbindOldSocialUser_yes() {
|
}
|
||||||
// // mock 数据
|
|
||||||
// SocialUserDO oldSocialUser = randomPojo(SocialUserDO.class, socialUserDO -> {
|
@Test
|
||||||
// socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue());
|
public void testGetBindUserId() {
|
||||||
// socialUserDO.setType(randomEle(SocialTypeEnum.values()).getType());
|
// 准备参数
|
||||||
// });
|
Integer userType = UserTypeEnum.ADMIN.getValue();
|
||||||
// socialUserMapper.insert(oldSocialUser);
|
Integer type = SocialTypeEnum.GITEE.getType();
|
||||||
// // 准备参数
|
String code = "tudou";
|
||||||
// Long userId = oldSocialUser.getUserId();
|
String state = "yuanma";
|
||||||
// Integer type = oldSocialUser.getType();
|
// mock 社交用户
|
||||||
// String newUnionId = randomString(10);
|
SocialUserDO socialUser = randomPojo(SocialUserDO.class).setType(type).setCode(code).setState(state);
|
||||||
//
|
socialUserMapper.insert(socialUser);
|
||||||
// // 调用
|
// mock 社交用户的绑定
|
||||||
// socialService.unbindOldSocialUser(userId, UserTypeEnum.ADMIN.getValue(), type, newUnionId);
|
Long userId = randomLong();
|
||||||
// // 断言
|
SocialUserBindDO socialUserBind = randomPojo(SocialUserBindDO.class).setUserType(userType).setUserId(userId)
|
||||||
// assertEquals(0L, socialUserMapper.selectCount(null).longValue());
|
.setSocialType(type).setSocialUserId(socialUser.getId());
|
||||||
// }
|
socialUserBindMapper.insert(socialUserBind);
|
||||||
|
|
||||||
|
// 调用
|
||||||
|
Long result = socialUserService.getBindUserId(userType, type, code, state);
|
||||||
|
// 断言
|
||||||
|
assertEquals(userId, result);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -381,7 +381,6 @@ CREATE TABLE IF NOT EXISTS "system_social_user" (
|
||||||
"type" tinyint NOT NULL,
|
"type" tinyint NOT NULL,
|
||||||
"openid" varchar(64) NOT NULL,
|
"openid" varchar(64) NOT NULL,
|
||||||
"token" varchar(256) DEFAULT NULL,
|
"token" varchar(256) DEFAULT NULL,
|
||||||
"union_id" varchar(64) NOT NULL,
|
|
||||||
"raw_token_info" varchar(1024) NOT NULL,
|
"raw_token_info" varchar(1024) NOT NULL,
|
||||||
"nickname" varchar(32) NOT NULL,
|
"nickname" varchar(32) NOT NULL,
|
||||||
"avatar" varchar(255) DEFAULT NULL,
|
"avatar" varchar(255) DEFAULT NULL,
|
||||||
|
@ -399,9 +398,9 @@ CREATE TABLE IF NOT EXISTS "system_social_user" (
|
||||||
CREATE TABLE IF NOT EXISTS "system_social_user_bind" (
|
CREATE TABLE IF NOT EXISTS "system_social_user_bind" (
|
||||||
"id" number NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
"id" number NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
||||||
"user_id" bigint NOT NULL,
|
"user_id" bigint NOT NULL,
|
||||||
"user_type" tinyint NOT NULL DEFAULT '0',
|
"user_type" tinyint NOT NULL,
|
||||||
"platform" tinyint NOT NULL,
|
"social_type" tinyint NOT NULL,
|
||||||
"union_id" varchar(64) NOT NULL,
|
"social_user_id" number NOT NULL,
|
||||||
"creator" varchar(64) DEFAULT '',
|
"creator" varchar(64) DEFAULT '',
|
||||||
"create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
"create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
"updater" varchar(64) DEFAULT '',
|
"updater" varchar(64) DEFAULT '',
|
||||||
|
|
Loading…
Reference in New Issue