From 878445a238cd2f47c6cdfbe6735a16bbdd9c653f Mon Sep 17 00:00:00 2001 From: YunaiV Date: Tue, 26 Apr 2022 02:17:38 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E7=A4=BE=E4=BA=A4=E7=99=BB?= =?UTF-8?q?=E5=BD=95=EF=BC=8C=E5=AE=8C=E5=96=84=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/enums/social/SocialTypeEnum.java | 25 +-- .../dataobject/social/SocialUserBindDO.java | 14 +- .../dal/dataobject/social/SocialUserDO.java | 3 - .../mysql/social/SocialUserBindMapper.java | 15 +- .../dal/mysql/social/SocialUserMapper.java | 8 +- .../service/social/SocialUserServiceImpl.java | 30 ++- .../service/social/SocialUserServiceTest.java | 183 +++++++----------- .../src/test/resources/sql/create_tables.sql | 19 +- 8 files changed, 110 insertions(+), 187 deletions(-) diff --git a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/social/SocialTypeEnum.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/social/SocialTypeEnum.java index 81b4e47e96..77833b2e6c 100644 --- a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/social/SocialTypeEnum.java +++ b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/social/SocialTypeEnum.java @@ -23,33 +23,33 @@ public enum SocialTypeEnum implements IntArrayValuable { * Gitee * 文档链接:https://gitee.com/api/v5/oauth_doc#/ */ - GITEE(10, 1, "GITEE"), + GITEE(10, "GITEE"), /** * 钉钉 * 文档链接: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 */ - WECHAT_ENTERPRISE(30, 3, "WECHAT_ENTERPRISE"), + WECHAT_ENTERPRISE(30, "WECHAT_ENTERPRISE"), /** * 微信公众平台 - 移动端 H5 * 文档链接:https://www.cnblogs.com/juewuzhe/p/11905461.html */ - WECHAT_MP(31, 3, "WECHAT_MP"), + WECHAT_MP(31, "WECHAT_MP"), /** * 微信开放平台 - 网站应用 PC 端扫码授权登录 * 文档链接: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 */ - 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(); @@ -58,12 +58,6 @@ public enum SocialTypeEnum implements IntArrayValuable { * 类型 */ 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()); } - public static Set getTypes(Collection platforms) { - return Arrays.stream(values()) - .filter(socialTypeEnum -> platforms.contains(socialTypeEnum.getPlatform())) - .map(SocialTypeEnum::getType) - .collect(Collectors.toSet()); - } - } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/social/SocialUserBindDO.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/social/SocialUserBindDO.java index d75f895385..09626621e1 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/social/SocialUserBindDO.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/social/SocialUserBindDO.java @@ -22,6 +22,8 @@ public class SocialUserBindDO extends BaseDO { /** * 关联的用户编号 + * + * 关联 UserDO 的编号 */ private Long userId; /** @@ -32,14 +34,16 @@ public class SocialUserBindDO extends BaseDO { private Integer userType; /** - * 社交平台 + * 社交平台的用户编号 * - * 枚举 {@link SocialTypeEnum#getPlatform()} + * 关联 {@link SocialUserDO#getId()} */ - private Integer platform; + private Long socialUserId; /** - * 社交的全局编号 + * 社交平台的类型 + * + * 冗余 {@link SocialUserDO#getType()} */ - private String unionId; + private Integer socialType; } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/social/SocialUserDO.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/social/SocialUserDO.java index 7d9babf57e..b59553ca15 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/social/SocialUserDO.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/social/SocialUserDO.java @@ -1,7 +1,5 @@ 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.module.system.enums.social.SocialTypeEnum; import com.baomidou.mybatisplus.annotation.TableId; @@ -10,7 +8,6 @@ import lombok.*; /** * 社交用户 - * 通过 {@link SocialUserDO#getUserId()} 关联到对应的 {@link AdminUserDO} * * @author weir */ diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/social/SocialUserBindMapper.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/social/SocialUserBindMapper.java index 016a7fcf82..88d0c9227f 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/social/SocialUserBindMapper.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/social/SocialUserBindMapper.java @@ -10,26 +10,23 @@ import java.util.List; @Mapper public interface SocialUserBindMapper extends BaseMapperX { - default void deleteByUserTypeAndUserIdAndUnionId(Integer userType, Long userId, Integer platform) { + default void deleteByUserTypeAndUserIdAndSocialType(Integer userType, Long userId, Integer socialType) { delete(new LambdaQueryWrapperX() .eq(SocialUserBindDO::getUserType, userType) .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() .eq(SocialUserBindDO::getUserType, userType) - .eq(SocialUserBindDO::getPlatform, platform) - .eq(SocialUserBindDO::getUnionId, unionId)); + .eq(SocialUserBindDO::getSocialUserId, socialUserId)); } - default SocialUserBindDO selectByUserTypeAndPlatformAndUnionId(Integer userType, - Integer platform, String unionId) { + default SocialUserBindDO selectByUserTypeAndSocialUserId(Integer userType, Long socialUserId) { return selectOne(new LambdaQueryWrapperX() .eq(SocialUserBindDO::getUserType, userType) - .eq(SocialUserBindDO::getPlatform, platform) - .eq(SocialUserBindDO::getUnionId, unionId)); + .eq(SocialUserBindDO::getSocialUserId, socialUserId)); } default List selectListByUserIdAndUserType(Long userId, Integer userType) { diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/social/SocialUserMapper.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/social/SocialUserMapper.java index 4c1d854a73..442cc45760 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/social/SocialUserMapper.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/social/SocialUserMapper.java @@ -22,13 +22,7 @@ public interface SocialUserMapper extends BaseMapperX { default SocialUserDO selectByTypeAndOpenid(Integer type, String openid) { return selectOne(new LambdaQueryWrapper() .eq(SocialUserDO::getType, type) - .eq(SocialUserDO::getCode, openid)); - } - - default List selectListByUnionIdAndType(Collection unionIds, Collection types) { - return selectList(new LambdaQueryWrapper() - .in(SocialUserDO::getUnionId, unionIds) - .in(SocialUserDO::getType, types)); + .eq(SocialUserDO::getOpenid, openid)); } } diff --git a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceImpl.java b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceImpl.java index dd4dc0f5a8..723e507c41 100644 --- a/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceImpl.java +++ b/yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceImpl.java @@ -2,8 +2,6 @@ package cn.iocoder.yudao.module.system.service.social; import cn.hutool.core.collection.CollUtil; 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.module.system.api.social.dto.SocialUserBindReqDTO; 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 java.util.Collections; 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.util.collection.CollectionUtils.convertSet; import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; 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 字段,保证后续可查询 .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())); if (socialUser.getId() == null) { socialUserMapper.insert(socialUser); @@ -96,9 +93,7 @@ public class SocialUserServiceImpl implements SocialUserService { return Collections.emptyList(); } // 获得社交用户 - Set platforms = CollectionUtils.convertSet(socialUserBinds, SocialUserBindDO::getPlatform); - return socialUserMapper.selectListByUnionIdAndType(CollectionUtils.convertSet(socialUserBinds, SocialUserBindDO::getUnionId), - SocialTypeEnum.getTypes(platforms)); + return socialUserMapper.selectBatchIds(convertSet(socialUserBinds, SocialUserBindDO::getSocialUserId)); } @Override @@ -108,19 +103,17 @@ public class SocialUserServiceImpl implements SocialUserService { SocialUserDO socialUser = authSocialUser(reqDTO.getType(), reqDTO.getCode(), reqDTO.getState()); Assert.notNull(socialUser, "社交用户不能为空"); - // 如果 unionId 之前被绑定过,需要进行解绑 - Integer platform = SocialTypeEnum.valueOfType(socialUser.getType()).getPlatform(); - socialUserBindMapper.deleteByUserTypeAndPlatformAndUnionId(reqDTO.getUserType(), platform, - socialUser.getUnionId()); + // 社交用户可能之前绑定过别的用户,需要进行解绑 + socialUserBindMapper.deleteByUserTypeAndSocialUserId(reqDTO.getUserType(), socialUser.getId()); - // 如果 userId 之前绑定过该 type 的其它账号,需要进行解绑 - socialUserBindMapper.deleteByUserTypeAndUserIdAndUnionId(reqDTO.getUserType(), reqDTO.getUserId(), - socialUser.getUnionId()); + // 用户可能之前已经绑定过该社交类型,需要进行解绑 + socialUserBindMapper.deleteByUserTypeAndUserIdAndSocialType(reqDTO.getUserType(), reqDTO.getUserId(), + socialUser.getType()); // 绑定当前登录的社交用户 SocialUserBindDO socialUserBind = SocialUserBindDO.builder() .userId(reqDTO.getUserId()).userType(reqDTO.getUserType()) - .platform(platform).unionId(socialUser.getUnionId()).build(); + .socialUserId(socialUser.getId()).socialType(socialUser.getType()).build(); socialUserBindMapper.insert(socialUserBind); } @@ -133,8 +126,7 @@ public class SocialUserServiceImpl implements SocialUserService { } // 获得对应的社交绑定关系 - socialUserBindMapper.deleteByUserTypeAndUserIdAndPlatformAndUnionId(userType, userId, - SocialTypeEnum.valueOfType(socialUser.getType()).getPlatform(), socialUser.getUnionId()); + socialUserBindMapper.deleteByUserTypeAndUserIdAndSocialType(userType, userId, socialUser.getType()); } @Override @@ -144,8 +136,8 @@ public class SocialUserServiceImpl implements SocialUserService { Assert.notNull(socialUser, "社交用户不能为空"); // 如果未绑定的社交用户,则无法自动登录,进行报错 - SocialUserBindDO socialUserBind = socialUserBindMapper.selectByUserTypeAndPlatformAndUnionId(userType, - SocialTypeEnum.valueOfType(socialUser.getType()).getPlatform(), socialUser.getUnionId()); + SocialUserBindDO socialUserBind = socialUserBindMapper.selectByUserTypeAndSocialUserId(userType, + socialUser.getId()); if (socialUserBind == null) { throw exception(AUTH_THIRD_LOGIN_NOT_BIND); } diff --git a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceTest.java b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceTest.java index b7302173db..36d6b3b974 100644 --- a/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceTest.java +++ b/yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/social/SocialUserServiceTest.java @@ -1,16 +1,13 @@ package cn.iocoder.yudao.module.system.service.social; import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; -import cn.iocoder.yudao.framework.test.core.util.AssertUtils; -import cn.iocoder.yudao.framework.test.core.util.RandomUtils; +import cn.iocoder.yudao.framework.test.core.ut.BaseDbAndRedisUnitTest; 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.SocialUserDO; import cn.iocoder.yudao.module.system.dal.mysql.social.SocialUserBindMapper; 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 com.baomidou.mybatisplus.core.toolkit.PluginUtils; import com.xkcoding.justauth.AuthRequestFactory; import me.zhyd.oauth.enums.AuthResponseStatus; 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.utils.AuthStateUtils; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.function.Executable; import org.mockito.MockedStatic; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; import javax.annotation.Resource; - 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.test.core.util.AssertUtils.assertPojoEquals; 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(type, socialUser.getType()); assertEquals(authUser.getUuid(), socialUser.getOpenid()); - assertEquals(authUser.getToken().getUnionId(), socialUser.getUnionId()); } @Test public void testGetSocialUserList() { Long userId = 1L; 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 获得社交用户 - SocialUserDO socialUser = randomPojo(SocialUserDO.class).setType(SocialTypeEnum.GITEE.getType()) - .setUnionId("test_unionId"); + SocialUserDO socialUser = randomPojo(SocialUserDO.class).setType(SocialTypeEnum.GITEE.getType()); socialUserMapper.insert(socialUser); // 可被查到 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 result = socialUserService.getSocialUserList(userId, userType); @@ -184,120 +178,79 @@ public class SocialUserServiceTest extends BaseDbAndRedisUnitTest { } @Test - public void testBindSocialUser_create() { + public void testBindSocialUser() { // 准备参数 SocialUserBindReqDTO reqDTO = new SocialUserBindReqDTO() .setUserId(1L).setUserType(UserTypeEnum.ADMIN.getValue()) .setType(SocialTypeEnum.GITEE.getType()).setCode("test_code").setState("test_state"); // mock 数据:获得社交用户 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); - // mock 数据:解绑其它账号 + // mock 数据:用户可能之前已经绑定过该社交类型 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); // 断言 List socialUserBinds = socialUserBindMapper.selectList(); 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 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 socialUsers = socialUserMapper.selectList("user_id", userId); -// assertEquals(1, socialUsers.size()); -// } -// -// /** -// * 情况一,如果新老的 unionId 是一致的,无需解绑 -// */ -// @Test -// public void testUnbindOldSocialUser_no() { -// // mock 数据 -// SocialUserDO oldSocialUser = randomPojo(SocialUserDO.class, socialUserDO -> { -// socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue()); -// socialUserDO.setType(randomEle(SocialTypeEnum.values()).getType()); -// }); -// socialUserMapper.insert(oldSocialUser); -// // 准备参数 -// Long userId = oldSocialUser.getUserId(); -// Integer type = oldSocialUser.getType(); -// String newUnionId = oldSocialUser.getUnionId(); -// -// // 调用 -// socialService.unbindOldSocialUser(userId, UserTypeEnum.ADMIN.getValue(), type, newUnionId); -// // 断言 -// assertEquals(1L, socialUserMapper.selectCount(null).longValue()); -// } -// -// -// /** -// * 情况二,如果新老的 unionId 不一致的,需解绑 -// */ -// @Test -// public void testUnbindOldSocialUser_yes() { -// // mock 数据 -// SocialUserDO oldSocialUser = randomPojo(SocialUserDO.class, socialUserDO -> { -// socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue()); -// socialUserDO.setType(randomEle(SocialTypeEnum.values()).getType()); -// }); -// socialUserMapper.insert(oldSocialUser); -// // 准备参数 -// Long userId = oldSocialUser.getUserId(); -// Integer type = oldSocialUser.getType(); -// String newUnionId = randomString(10); -// -// // 调用 -// socialService.unbindOldSocialUser(userId, UserTypeEnum.ADMIN.getValue(), type, newUnionId); -// // 断言 -// assertEquals(0L, socialUserMapper.selectCount(null).longValue()); -// } + @Test + public void testUnbindSocialUser_success() { + // 准备参数 + Long userId = 1L; + Integer userType = UserTypeEnum.ADMIN.getValue(); + Integer type = SocialTypeEnum.GITEE.getType(); + String openid = "test_openid"; + // mock 数据:社交用户 + SocialUserDO socialUser = randomPojo(SocialUserDO.class).setType(type).setOpenid(openid); + socialUserMapper.insert(socialUser); + // mock 数据:社交绑定关系 + SocialUserBindDO socialUserBind = randomPojo(SocialUserBindDO.class).setUserType(userType) + .setUserId(userId).setSocialType(type); + socialUserBindMapper.insert(socialUserBind); + + // 调用 + socialUserService.unbindSocialUser(userId, userType, type, openid); + // 断言 + assertEquals(0, socialUserBindMapper.selectCount(null).intValue()); + } + + @Test + public void testUnbindSocialUser_notFound() { + // 调用,并断言 + assertServiceException( + () -> socialUserService.unbindSocialUser(randomLong(), UserTypeEnum.ADMIN.getValue(), + SocialTypeEnum.GITEE.getType(), "test_openid"), + SOCIAL_USER_NOT_FOUND); + } + + @Test + public void testGetBindUserId() { + // 准备参数 + Integer userType = UserTypeEnum.ADMIN.getValue(); + Integer type = SocialTypeEnum.GITEE.getType(); + String code = "tudou"; + String state = "yuanma"; + // mock 社交用户 + SocialUserDO socialUser = randomPojo(SocialUserDO.class).setType(type).setCode(code).setState(state); + socialUserMapper.insert(socialUser); + // mock 社交用户的绑定 + Long userId = randomLong(); + SocialUserBindDO socialUserBind = randomPojo(SocialUserBindDO.class).setUserType(userType).setUserId(userId) + .setSocialType(type).setSocialUserId(socialUser.getId()); + socialUserBindMapper.insert(socialUserBind); + + // 调用 + Long result = socialUserService.getBindUserId(userType, type, code, state); + // 断言 + assertEquals(userId, result); + } } diff --git a/yudao-module-system/yudao-module-system-biz/src/test/resources/sql/create_tables.sql b/yudao-module-system/yudao-module-system-biz/src/test/resources/sql/create_tables.sql index 9f88f8b5b3..709de08fa3 100644 --- a/yudao-module-system/yudao-module-system-biz/src/test/resources/sql/create_tables.sql +++ b/yudao-module-system/yudao-module-system-biz/src/test/resources/sql/create_tables.sql @@ -381,7 +381,6 @@ CREATE TABLE IF NOT EXISTS "system_social_user" ( "type" tinyint NOT NULL, "openid" varchar(64) NOT NULL, "token" varchar(256) DEFAULT NULL, - "union_id" varchar(64) NOT NULL, "raw_token_info" varchar(1024) NOT NULL, "nickname" varchar(32) NOT NULL, "avatar" varchar(255) DEFAULT NULL, @@ -399,15 +398,15 @@ CREATE TABLE IF NOT EXISTS "system_social_user" ( CREATE TABLE IF NOT EXISTS "system_social_user_bind" ( "id" number NOT NULL GENERATED BY DEFAULT AS IDENTITY, "user_id" bigint NOT NULL, - "user_type" tinyint NOT NULL DEFAULT '0', - "platform" tinyint NOT NULL, - "union_id" varchar(64) NOT NULL, - "creator" varchar(64) DEFAULT '', - "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - "updater" varchar(64) DEFAULT '', - "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, - "deleted" bit NOT NULL DEFAULT FALSE, - PRIMARY KEY ("id") + "user_type" tinyint NOT NULL, + "social_type" tinyint NOT NULL, + "social_user_id" number NOT NULL, + "creator" varchar(64) DEFAULT '', + "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updater" varchar(64) DEFAULT '', + "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + "deleted" bit NOT NULL DEFAULT FALSE, + PRIMARY KEY ("id") ) COMMENT '社交用户的绑定'; CREATE TABLE IF NOT EXISTS "system_tenant" (