diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/social/impl/SysSocialServiceImpl.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/social/impl/SysSocialServiceImpl.java index e5102014a..6ead6f889 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/social/impl/SysSocialServiceImpl.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/social/impl/SysSocialServiceImpl.java @@ -9,6 +9,7 @@ import cn.iocoder.yudao.adminserver.modules.system.service.social.SysSocialServi import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; import cn.iocoder.yudao.framework.common.util.http.HttpUtils; +import com.google.common.annotations.VisibleForTesting; import com.xkcoding.justauth.AuthRequestFactory; import lombok.extern.slf4j.Slf4j; import me.zhyd.oauth.model.AuthCallback; @@ -138,10 +139,11 @@ public class SysSocialServiceImpl implements SysSocialService { socialUserMapper.deleteBatchIds(CollectionUtils.convertSet(socialUsers, SysSocialUserDO::getId)); } - private void unbindOldSocialUser(Long userId, Integer type, String newUnionId) { + @VisibleForTesting + public void unbindOldSocialUser(Long userId, Integer type, String newUnionId) { List types = SysSocialTypeEnum.getRelationTypes(type); - List oldSocialUsers = socialUserMapper.selectListByTypeAndUserId(UserTypeEnum.ADMIN.getValue(), - types, userId); + List oldSocialUsers = socialUserMapper.selectListByTypeAndUserId( + UserTypeEnum.ADMIN.getValue(), types, userId); // 如果新老的 unionId 是一致的,说明无需解绑 if (CollUtil.isEmpty(oldSocialUsers) || Objects.equals(newUnionId, oldSocialUsers.get(0).getUnionId())) { return; diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/BaseMockitoUnitTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/BaseMockitoUnitTest.java deleted file mode 100644 index 74e9d7fbe..000000000 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/BaseMockitoUnitTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package cn.iocoder.yudao.adminserver; - -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.junit.jupiter.MockitoExtension; - -/** - * 纯 Mockito 的单元测试 - * - * @author 芋道源码 - */ -@ExtendWith(MockitoExtension.class) -public class BaseMockitoUnitTest { -} diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/sms/SysSmsServiceTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/sms/SysSmsServiceTest.java index 2a25125b9..75b34afba 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/sms/SysSmsServiceTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/sms/SysSmsServiceTest.java @@ -1,7 +1,6 @@ package cn.iocoder.yudao.adminserver.modules.system.service.sms; import cn.hutool.core.map.MapUtil; -import cn.iocoder.yudao.adminserver.BaseMockitoUnitTest; import cn.iocoder.yudao.framework.common.core.KeyValue; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; @@ -14,6 +13,7 @@ import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.sms.SysSmsTemp import cn.iocoder.yudao.adminserver.modules.system.mq.message.sms.SysSmsSendMessage; import cn.iocoder.yudao.adminserver.modules.system.mq.producer.sms.SysSmsProducer; import cn.iocoder.yudao.adminserver.modules.system.service.sms.impl.SysSmsServiceImpl; +import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest; import org.assertj.core.util.Lists; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/social/SysSocialServiceTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/social/SysSocialServiceTest.java new file mode 100644 index 000000000..21fc7ee46 --- /dev/null +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/social/SysSocialServiceTest.java @@ -0,0 +1,94 @@ +package cn.iocoder.yudao.adminserver.modules.system.service.social; + +import cn.iocoder.yudao.adminserver.BaseDbAndRedisUnitTest; +import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.social.SysSocialUserDO; +import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.social.SysSocialUserMapper; +import cn.iocoder.yudao.adminserver.modules.system.dal.redis.social.SysSocialAuthUserRedisDAO; +import cn.iocoder.yudao.adminserver.modules.system.enums.user.SysSocialTypeEnum; +import cn.iocoder.yudao.adminserver.modules.system.service.social.impl.SysSocialServiceImpl; +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; +import cn.iocoder.yudao.framework.test.core.util.RandomUtils; +import com.xkcoding.justauth.AuthRequestFactory; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.context.annotation.Import; + +import javax.annotation.Resource; + +import static cn.hutool.core.util.RandomUtil.randomEle; +import static cn.hutool.core.util.RandomUtil.randomString; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * {@link SysSocialServiceImpl} 的单元测试类 + * + * @author 芋道源码 + */ +@Import({SysSocialServiceImpl.class, SysSocialAuthUserRedisDAO.class}) +public class SysSocialServiceTest extends BaseDbAndRedisUnitTest { + + @Resource + private SysSocialServiceImpl socialService; + + @Resource + private SysSocialUserMapper socialUserMapper; + + @MockBean + private AuthRequestFactory authRequestFactory; + + @Test + public void testBindSocialUser_create() { + // mock 数据 + // 准备参数 + // mock 方法 + + // 调用 + // 断言 + } + + /** + * 情况一,如果新老的 unionId 是一致的,无需解绑 + */ + @Test + public void testUnbindOldSocialUser_no() { + // mock 数据 + SysSocialUserDO oldSocialUser = RandomUtils.randomPojo(SysSocialUserDO.class, socialUserDO -> { + socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue()); + socialUserDO.setType(randomEle(SysSocialTypeEnum.values()).getType()); + }); + socialUserMapper.insert(oldSocialUser); + // 准备参数 + Long userId = oldSocialUser.getUserId(); + Integer type = oldSocialUser.getType(); + String newUnionId = oldSocialUser.getUnionId(); + + // 调用 + socialService.unbindOldSocialUser(userId, type, newUnionId); + // 断言 + assertEquals(1L, socialUserMapper.selectCount(null).longValue()); + } + + + /** + * 情况二,如果新老的 unionId 不一致的,需解绑 + */ + @Test + public void testUnbindOldSocialUser_yes() { + // mock 数据 + SysSocialUserDO oldSocialUser = RandomUtils.randomPojo(SysSocialUserDO.class, socialUserDO -> { + socialUserDO.setUserType(UserTypeEnum.ADMIN.getValue()); + socialUserDO.setType(randomEle(SysSocialTypeEnum.values()).getType()); + }); + socialUserMapper.insert(oldSocialUser); + // 准备参数 + Long userId = oldSocialUser.getUserId(); + Integer type = oldSocialUser.getType(); + String newUnionId = randomString(10); + + // 调用 + socialService.unbindOldSocialUser(userId, type, newUnionId); + // 断言 + assertEquals(0L, socialUserMapper.selectCount(null).longValue()); + } + +} diff --git a/yudao-admin-server/src/test/resources/sql/clean.sql b/yudao-admin-server/src/test/resources/sql/clean.sql index 78f02ee4f..75080b343 100644 --- a/yudao-admin-server/src/test/resources/sql/clean.sql +++ b/yudao-admin-server/src/test/resources/sql/clean.sql @@ -23,3 +23,4 @@ DELETE FROM "sys_sms_channel"; DELETE FROM "sys_sms_template"; DELETE FROM "sys_sms_log"; DELETE FROM "sys_error_code"; +DELETE FROM "sys_social_user"; diff --git a/yudao-admin-server/src/test/resources/sql/create_tables.sql b/yudao-admin-server/src/test/resources/sql/create_tables.sql index b9060373d..e575d184b 100644 --- a/yudao-admin-server/src/test/resources/sql/create_tables.sql +++ b/yudao-admin-server/src/test/resources/sql/create_tables.sql @@ -426,3 +426,23 @@ CREATE TABLE IF NOT EXISTS "sys_error_code" ( "deleted" bit NOT NULL DEFAULT FALSE, PRIMARY KEY ("id") ) COMMENT '错误码表'; + +CREATE TABLE IF NOT EXISTS "sys_social_user" ( + "id" number NOT NULL GENERATED BY DEFAULT AS IDENTITY, + "user_id" bigint NOT NULL, + "user_type" tinyint NOT NULL DEFAULT '0', + "type" tinyint NOT NULL, + "openid" varchar(32) NOT NULL, + "token" varchar(256) DEFAULT NULL, + "union_id" varchar(32) NOT NULL, + "raw_token_info" varchar(1024) NOT NULL, + "nickname" varchar(32) NOT NULL, + "avatar" varchar(255) DEFAULT NULL, + "raw_user_info" varchar(1024) 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 '社交用户';