houzhongyi
2024-07-11 e7c1260db32209a078a962aaa0ad5492c35774fb
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.system.service.social;
H 2
3 import com.iailab.framework.common.enums.UserTypeEnum;
4 import com.iailab.framework.common.pojo.PageResult;
5 import com.iailab.framework.test.core.ut.BaseDbUnitTest;
6 import com.iailab.module.system.api.social.dto.SocialUserBindReqDTO;
7 import com.iailab.module.system.api.social.dto.SocialUserRespDTO;
8 import com.iailab.module.system.controller.admin.socail.vo.user.SocialUserPageReqVO;
9 import com.iailab.module.system.dal.dataobject.social.SocialUserBindDO;
10 import com.iailab.module.system.dal.dataobject.social.SocialUserDO;
11 import com.iailab.module.system.dal.mysql.social.SocialUserBindMapper;
12 import com.iailab.module.system.dal.mysql.social.SocialUserMapper;
13 import com.iailab.module.system.enums.social.SocialTypeEnum;
14 import com.xingyuv.jushauth.model.AuthUser;
15 import org.junit.jupiter.api.Test;
16 import org.springframework.boot.test.mock.mockito.MockBean;
17 import org.springframework.context.annotation.Import;
18
19 import javax.annotation.Resource;
20 import java.util.List;
21
22 import static cn.hutool.core.util.RandomUtil.randomEle;
23 import static cn.hutool.core.util.RandomUtil.randomLong;
24 import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
25 import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildTime;
26 import static com.iailab.framework.common.util.json.JsonUtils.toJsonString;
27 import static com.iailab.framework.common.util.object.ObjectUtils.cloneIgnoreId;
28 import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals;
29 import static com.iailab.framework.test.core.util.AssertUtils.assertServiceException;
30 import static com.iailab.framework.test.core.util.RandomUtils.randomPojo;
31 import static com.iailab.framework.test.core.util.RandomUtils.randomString;
32 import static com.iailab.module.system.enums.ErrorCodeConstants.SOCIAL_USER_NOT_FOUND;
33 import static org.junit.jupiter.api.Assertions.assertEquals;
34 import static org.mockito.Mockito.eq;
35 import static org.mockito.Mockito.when;
36
37 /**
38  * {@link SocialUserServiceImpl} 的单元测试类
39  *
40  * @author iailab
41  */
42 @Import(SocialUserServiceImpl.class)
43 public class SocialUserServiceImplTest extends BaseDbUnitTest {
44
45     @Resource
46     private SocialUserServiceImpl socialUserService;
47
48     @Resource
49     private SocialUserMapper socialUserMapper;
50     @Resource
51     private SocialUserBindMapper socialUserBindMapper;
52
53     @MockBean
54     private SocialClientService socialClientService;
55
56     @Test
57     public void testGetSocialUserList() {
58         Long userId = 1L;
59         Integer userType = UserTypeEnum.ADMIN.getValue();
60         // mock 获得社交用户
61         SocialUserDO socialUser = randomPojo(SocialUserDO.class).setType(SocialTypeEnum.GITEE.getType());
62         socialUserMapper.insert(socialUser); // 可被查到
63         socialUserMapper.insert(randomPojo(SocialUserDO.class)); // 不可被查到
64         // mock 获得绑定
65         socialUserBindMapper.insert(randomPojo(SocialUserBindDO.class) // 可被查询到
66                 .setUserId(userId).setUserType(userType).setSocialType(SocialTypeEnum.GITEE.getType())
67                 .setSocialUserId(socialUser.getId()));
68         socialUserBindMapper.insert(randomPojo(SocialUserBindDO.class) // 不可被查询到
69                 .setUserId(2L).setUserType(userType).setSocialType(SocialTypeEnum.DINGTALK.getType()));
70
71         // 调用
72         List<SocialUserDO> result = socialUserService.getSocialUserList(userId, userType);
73         // 断言
74         assertEquals(1, result.size());
75         assertPojoEquals(socialUser, result.get(0));
76     }
77
78     @Test
79     public void testBindSocialUser() {
80         // 准备参数
81         SocialUserBindReqDTO reqDTO = new SocialUserBindReqDTO()
82                 .setUserId(1L).setUserType(UserTypeEnum.ADMIN.getValue())
83                 .setSocialType(SocialTypeEnum.GITEE.getType()).setCode("test_code").setState("test_state");
84         // mock 数据:获得社交用户
85         SocialUserDO socialUser = randomPojo(SocialUserDO.class).setType(reqDTO.getSocialType())
86                 .setCode(reqDTO.getCode()).setState(reqDTO.getState());
87         socialUserMapper.insert(socialUser);
88         // mock 数据:用户可能之前已经绑定过该社交类型
89         socialUserBindMapper.insert(randomPojo(SocialUserBindDO.class).setUserId(1L).setUserType(UserTypeEnum.ADMIN.getValue())
90                 .setSocialType(SocialTypeEnum.GITEE.getType()).setSocialUserId(-1L));
91         // mock 数据:社交用户可能之前绑定过别的用户
92         socialUserBindMapper.insert(randomPojo(SocialUserBindDO.class).setUserType(UserTypeEnum.ADMIN.getValue())
93                 .setSocialType(SocialTypeEnum.GITEE.getType()).setSocialUserId(socialUser.getId()));
94
95         // 调用
96         String openid = socialUserService.bindSocialUser(reqDTO);
97         // 断言
98         List<SocialUserBindDO> socialUserBinds = socialUserBindMapper.selectList();
99         assertEquals(1, socialUserBinds.size());
100         assertEquals(socialUser.getOpenid(), openid);
101     }
102
103     @Test
104     public void testUnbindSocialUser_success() {
105         // 准备参数
106         Long userId = 1L;
107         Integer userType = UserTypeEnum.ADMIN.getValue();
108         Integer type = SocialTypeEnum.GITEE.getType();
109         String openid = "test_openid";
110         // mock 数据:社交用户
111         SocialUserDO socialUser = randomPojo(SocialUserDO.class).setType(type).setOpenid(openid);
112         socialUserMapper.insert(socialUser);
113         // mock 数据:社交绑定关系
114         SocialUserBindDO socialUserBind = randomPojo(SocialUserBindDO.class).setUserType(userType)
115                 .setUserId(userId).setSocialType(type);
116         socialUserBindMapper.insert(socialUserBind);
117
118         // 调用
119         socialUserService.unbindSocialUser(userId, userType, type, openid);
120         // 断言
121         assertEquals(0, socialUserBindMapper.selectCount(null).intValue());
122     }
123
124     @Test
125     public void testUnbindSocialUser_notFound() {
126         // 调用,并断言
127         assertServiceException(
128                 () -> socialUserService.unbindSocialUser(randomLong(), UserTypeEnum.ADMIN.getValue(),
129                         SocialTypeEnum.GITEE.getType(), "test_openid"),
130                 SOCIAL_USER_NOT_FOUND);
131     }
132
133     @Test
134     public void testGetSocialUser() {
135         // 准备参数
136         Integer userType = UserTypeEnum.ADMIN.getValue();
137         Integer type = SocialTypeEnum.GITEE.getType();
138         String code = "tudou";
139         String state = "yuanma";
140         // mock 社交用户
141         SocialUserDO socialUserDO = randomPojo(SocialUserDO.class).setType(type).setCode(code).setState(state);
142         socialUserMapper.insert(socialUserDO);
143         // mock 社交用户的绑定
144         Long userId = randomLong();
145         SocialUserBindDO socialUserBind = randomPojo(SocialUserBindDO.class).setUserType(userType).setUserId(userId)
146                 .setSocialType(type).setSocialUserId(socialUserDO.getId());
147         socialUserBindMapper.insert(socialUserBind);
148
149         // 调用
150         SocialUserRespDTO socialUser = socialUserService.getSocialUserByCode(userType, type, code, state);
151         // 断言
152         assertEquals(userId, socialUser.getUserId());
153         assertEquals(socialUserDO.getOpenid(), socialUser.getOpenid());
154     }
155
156     @Test
157     public void testAuthSocialUser_exists() {
158         // 准备参数
159         Integer socialType = SocialTypeEnum.GITEE.getType();
160         Integer userType = randomEle(SocialTypeEnum.values()).getType();
161         String code = "tudou";
162         String state = "yuanma";
163         // mock 方法
164         SocialUserDO socialUser = randomPojo(SocialUserDO.class).setType(socialType).setCode(code).setState(state);
165         socialUserMapper.insert(socialUser);
166
167         // 调用
168         SocialUserDO result = socialUserService.authSocialUser(socialType, userType, code, state);
169         // 断言
170         assertPojoEquals(socialUser, result);
171     }
172
173     @Test
174     public void testAuthSocialUser_notNull() {
175         // mock 数据
176         SocialUserDO socialUser = randomPojo(SocialUserDO.class,
177                 o -> o.setType(SocialTypeEnum.GITEE.getType()).setCode("tudou").setState("yuanma"));
178         socialUserMapper.insert(socialUser);
179         // 准备参数
180         Integer socialType = SocialTypeEnum.GITEE.getType();
181         Integer userType = randomEle(SocialTypeEnum.values()).getType();
182         String code = "tudou";
183         String state = "yuanma";
184
185         // 调用
186         SocialUserDO result = socialUserService.authSocialUser(socialType, userType, code, state);
187         // 断言
188         assertPojoEquals(socialUser, result);
189     }
190
191     @Test
192     public void testAuthSocialUser_insert() {
193         // 准备参数
194         Integer socialType = SocialTypeEnum.GITEE.getType();
195         Integer userType = randomEle(SocialTypeEnum.values()).getType();
196         String code = "tudou";
197         String state = "yuanma";
198         // mock 方法
199         AuthUser authUser = randomPojo(AuthUser.class);
200         when(socialClientService.getAuthUser(eq(socialType), eq(userType), eq(code), eq(state))).thenReturn(authUser);
201
202         // 调用
203         SocialUserDO result = socialUserService.authSocialUser(socialType, userType, code, state);
204         // 断言
205         assertBindSocialUser(socialType, result, authUser);
206         assertEquals(code, result.getCode());
207         assertEquals(state, result.getState());
208     }
209
210     @Test
211     public void testAuthSocialUser_update() {
212         // 准备参数
213         Integer socialType = SocialTypeEnum.GITEE.getType();
214         Integer userType = randomEle(SocialTypeEnum.values()).getType();
215         String code = "tudou";
216         String state = "yuanma";
217         // mock 数据
218         socialUserMapper.insert(randomPojo(SocialUserDO.class).setType(socialType).setOpenid("test_openid"));
219         // mock 方法
220         AuthUser authUser = randomPojo(AuthUser.class);
221         when(socialClientService.getAuthUser(eq(socialType), eq(userType), eq(code), eq(state))).thenReturn(authUser);
222
223         // 调用
224         SocialUserDO result = socialUserService.authSocialUser(socialType, userType, code, state);
225         // 断言
226         assertBindSocialUser(socialType, result, authUser);
227         assertEquals(code, result.getCode());
228         assertEquals(state, result.getState());
229     }
230
231     private void assertBindSocialUser(Integer type, SocialUserDO socialUser, AuthUser authUser) {
232         assertEquals(authUser.getToken().getAccessToken(), socialUser.getToken());
233         assertEquals(toJsonString(authUser.getToken()), socialUser.getRawTokenInfo());
234         assertEquals(authUser.getNickname(), socialUser.getNickname());
235         assertEquals(authUser.getAvatar(), socialUser.getAvatar());
236         assertEquals(toJsonString(authUser.getRawUserInfo()), socialUser.getRawUserInfo());
237         assertEquals(type, socialUser.getType());
238         assertEquals(authUser.getUuid(), socialUser.getOpenid());
239     }
240
241     @Test
242     public void testGetSocialUser_id() {
243         // mock 数据
244         SocialUserDO socialUserDO = randomPojo(SocialUserDO.class);
245         socialUserMapper.insert(socialUserDO);
246         // 参数准备
247         Long id = socialUserDO.getId();
248
249         // 调用
250         SocialUserDO dbSocialUserDO = socialUserService.getSocialUser(id);
251         // 断言
252         assertPojoEquals(socialUserDO, dbSocialUserDO);
253     }
254
255     @Test
256     public void testGetSocialUserPage() {
257         // mock 数据
258         SocialUserDO dbSocialUser = randomPojo(SocialUserDO.class, o -> { // 等会查询到
259             o.setType(SocialTypeEnum.GITEE.getType());
260             o.setNickname("iailab");
261             o.setOpenid("iailabyuanma");
262             o.setCreateTime(buildTime(2020, 1, 15));
263         });
264         socialUserMapper.insert(dbSocialUser);
265         // 测试 type 不匹配
266         socialUserMapper.insert(cloneIgnoreId(dbSocialUser, o -> o.setType(SocialTypeEnum.DINGTALK.getType())));
267         // 测试 nickname 不匹配
268         socialUserMapper.insert(cloneIgnoreId(dbSocialUser, o -> o.setNickname(randomString())));
269         // 测试 openid 不匹配
270         socialUserMapper.insert(cloneIgnoreId(dbSocialUser, o -> o.setOpenid("java")));
271         // 测试 createTime 不匹配
272         socialUserMapper.insert(cloneIgnoreId(dbSocialUser, o -> o.setCreateTime(buildTime(2020, 1, 21))));
273         // 准备参数
274         SocialUserPageReqVO reqVO = new SocialUserPageReqVO();
275         reqVO.setType(SocialTypeEnum.GITEE.getType());
276         reqVO.setNickname("芋");
277         reqVO.setOpenid("iailab");
278         reqVO.setCreateTime(buildBetweenTime(2020, 1, 10, 2020, 1, 20));
279
280         // 调用
281         PageResult<SocialUserDO> pageResult = socialUserService.getSocialUserPage(reqVO);
282         // 断言
283         assertEquals(1, pageResult.getTotal());
284         assertEquals(1, pageResult.getList().size());
285         assertPojoEquals(dbSocialUser, pageResult.getList().get(0));
286     }
287
288 }