潘志宝
2024-08-21 c39abccd937de093fc067abffac5f66b758bc97b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package com.iailab.module.system.service.social;
 
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaUserService;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.hutool.core.util.ReflectUtil;
import com.iailab.framework.common.enums.CommonStatusEnum;
import com.iailab.framework.common.enums.UserTypeEnum;
import com.iailab.framework.common.pojo.PageResult;
import com.iailab.framework.test.core.ut.BaseDbUnitTest;
import com.iailab.module.system.controller.admin.socail.vo.client.SocialClientPageReqVO;
import com.iailab.module.system.controller.admin.socail.vo.client.SocialClientSaveReqVO;
import com.iailab.module.system.dal.dataobject.social.SocialClientDO;
import com.iailab.module.system.dal.mysql.social.SocialClientMapper;
import com.iailab.module.system.enums.social.SocialTypeEnum;
import com.binarywang.spring.starter.wxjava.miniapp.properties.WxMaProperties;
import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties;
import com.xingyuv.jushauth.config.AuthConfig;
import com.xingyuv.jushauth.model.AuthResponse;
import com.xingyuv.jushauth.model.AuthUser;
import com.xingyuv.jushauth.request.AuthDefaultRequest;
import com.xingyuv.jushauth.request.AuthRequest;
import com.xingyuv.jushauth.utils.AuthStateUtils;
import com.xingyuv.justauth.AuthRequestFactory;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.core.StringRedisTemplate;
 
import javax.annotation.Resource;
 
import static cn.hutool.core.util.RandomUtil.randomEle;
import static com.iailab.framework.common.util.object.ObjectUtils.cloneIgnoreId;
import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals;
import static com.iailab.framework.test.core.util.AssertUtils.assertServiceException;
import static com.iailab.framework.test.core.util.RandomUtils.*;
import static com.iailab.module.system.enums.ErrorCodeConstants.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
 
/**
 * {@link SocialClientServiceImpl} 的单元测试类
 *
 * @author iailab
 */
@Import(SocialClientServiceImpl.class)
public class SocialClientServiceImplTest extends BaseDbUnitTest {
 
    @Resource
    private SocialClientServiceImpl socialClientService;
 
    @Resource
    private SocialClientMapper socialClientMapper;
 
    @MockBean
    private AuthRequestFactory authRequestFactory;
 
    @MockBean
    private WxMpService wxMpService;
    @MockBean
    private WxMpProperties wxMpProperties;
    @MockBean
    private StringRedisTemplate stringRedisTemplate;
    @MockBean
    private WxMaService wxMaService;
    @MockBean
    private WxMaProperties wxMaProperties;
 
    @Test
    public void testGetAuthorizeUrl() {
        try (MockedStatic<AuthStateUtils> authStateUtilsMock = mockStatic(AuthStateUtils.class)) {
            // 准备参数
            Integer socialType = SocialTypeEnum.WECHAT_MP.getType();
            Integer userType = randomPojo(UserTypeEnum.class).getValue();
            String redirectUri = "sss";
            // mock 获得对应的 AuthRequest 实现
            AuthRequest authRequest = mock(AuthRequest.class);
            when(authRequestFactory.get(eq("WECHAT_MP"))).thenReturn(authRequest);
            // mock 方法
            authStateUtilsMock.when(AuthStateUtils::createState).thenReturn("aoteman");
            when(authRequest.authorize(eq("aoteman"))).thenReturn("https://www.baidu.com?redirect_uri=yyy");
 
            // 调用
            String url = socialClientService.getAuthorizeUrl(socialType, userType, redirectUri);
            // 断言
            assertEquals("https://www.baidu.com?redirect_uri=sss", url);
        }
    }
 
    @Test
    public void testAuthSocialUser_success() {
        // 准备参数
        Integer socialType = SocialTypeEnum.WECHAT_MP.getType();
        Integer userType = randomPojo(UserTypeEnum.class).getValue();
        String code = randomString();
        String state = randomString();
        // mock 方法(AuthRequest)
        AuthRequest authRequest = mock(AuthRequest.class);
        when(authRequestFactory.get(eq("WECHAT_MP"))).thenReturn(authRequest);
        // mock 方法(AuthResponse)
        AuthUser authUser = randomPojo(AuthUser.class);
        AuthResponse<?> authResponse = new AuthResponse<>(2000, null, authUser);
        when(authRequest.login(argThat(authCallback -> {
            assertEquals(code, authCallback.getCode());
            assertEquals(state, authCallback.getState());
            return true;
        }))).thenReturn(authResponse);
 
        // 调用
        AuthUser result = socialClientService.getAuthUser(socialType, userType, code, state);
        // 断言
        assertSame(authUser, result);
    }
 
    @Test
    public void testAuthSocialUser_fail() {
        // 准备参数
        Integer socialType = SocialTypeEnum.WECHAT_MP.getType();
        Integer userType = randomPojo(UserTypeEnum.class).getValue();
        String code = randomString();
        String state = randomString();
        // mock 方法(AuthRequest)
        AuthRequest authRequest = mock(AuthRequest.class);
        when(authRequestFactory.get(eq("WECHAT_MP"))).thenReturn(authRequest);
        // mock 方法(AuthResponse)
        AuthResponse<?> authResponse = new AuthResponse<>(0, "模拟失败", null);
        when(authRequest.login(argThat(authCallback -> {
            assertEquals(code, authCallback.getCode());
            assertEquals(state, authCallback.getState());
            return true;
        }))).thenReturn(authResponse);
 
        // 调用并断言
        assertServiceException(
                () -> socialClientService.getAuthUser(socialType, userType, code, state),
                SOCIAL_USER_AUTH_FAILURE, "模拟失败");
    }
 
    @Test
    public void testBuildAuthRequest_clientNull() {
        // 准备参数
        Integer socialType = SocialTypeEnum.WECHAT_MP.getType();
        Integer userType = randomPojo(SocialTypeEnum.class).getType();
        // mock 获得对应的 AuthRequest 实现
        AuthRequest authRequest = mock(AuthDefaultRequest.class);
        AuthConfig authConfig = (AuthConfig) ReflectUtil.getFieldValue(authRequest, "config");
        when(authRequestFactory.get(eq("WECHAT_MP"))).thenReturn(authRequest);
 
        // 调用
        AuthRequest result = socialClientService.buildAuthRequest(socialType, userType);
        // 断言
        assertSame(authRequest, result);
        assertSame(authConfig, ReflectUtil.getFieldValue(authConfig, "config"));
    }
 
    @Test
    public void testBuildAuthRequest_clientDisable() {
        // 准备参数
        Integer socialType = SocialTypeEnum.WECHAT_MP.getType();
        Integer userType = randomPojo(SocialTypeEnum.class).getType();
        // mock 获得对应的 AuthRequest 实现
        AuthRequest authRequest = mock(AuthDefaultRequest.class);
        AuthConfig authConfig = (AuthConfig) ReflectUtil.getFieldValue(authRequest, "config");
        when(authRequestFactory.get(eq("WECHAT_MP"))).thenReturn(authRequest);
        // mock 数据
        SocialClientDO client = randomPojo(SocialClientDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())
                .setUserType(userType).setSocialType(socialType));
        socialClientMapper.insert(client);
 
        // 调用
        AuthRequest result = socialClientService.buildAuthRequest(socialType, userType);
        // 断言
        assertSame(authRequest, result);
        assertSame(authConfig, ReflectUtil.getFieldValue(authConfig, "config"));
    }
 
    @Test
    public void testBuildAuthRequest_clientEnable() {
        // 准备参数
        Integer socialType = SocialTypeEnum.WECHAT_MP.getType();
        Integer userType = randomPojo(SocialTypeEnum.class).getType();
        // mock 获得对应的 AuthRequest 实现
        AuthConfig authConfig = mock(AuthConfig.class);
        AuthRequest authRequest = mock(AuthDefaultRequest.class);
        ReflectUtil.setFieldValue(authRequest, "config", authConfig);
        when(authRequestFactory.get(eq("WECHAT_MP"))).thenReturn(authRequest);
        // mock 数据
        SocialClientDO client = randomPojo(SocialClientDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())
                .setUserType(userType).setSocialType(socialType));
        socialClientMapper.insert(client);
 
        // 调用
        AuthRequest result = socialClientService.buildAuthRequest(socialType, userType);
        // 断言
        assertSame(authRequest, result);
        assertNotSame(authConfig, ReflectUtil.getFieldValue(authRequest, "config"));
    }
 
    // =================== 微信公众号独有 ===================
 
    @Test
    public void testCreateWxMpJsapiSignature() throws WxErrorException {
        // 准备参数
        Integer userType = randomPojo(UserTypeEnum.class).getValue();
        String url = randomString();
        // mock 方法
        WxJsapiSignature signature = randomPojo(WxJsapiSignature.class);
        when(wxMpService.createJsapiSignature(eq(url))).thenReturn(signature);
 
        // 调用
        WxJsapiSignature result = socialClientService.createWxMpJsapiSignature(userType, url);
        // 断言
        assertSame(signature, result);
    }
 
    @Test
    public void testGetWxMpService_clientNull() {
        // 准备参数
        Integer userType = randomPojo(UserTypeEnum.class).getValue();
        // mock 方法
 
        // 调用
        WxMpService result = socialClientService.getWxMpService(userType);
        // 断言
        assertSame(wxMpService, result);
    }
 
    @Test
    public void testGetWxMpService_clientDisable() {
        // 准备参数
        Integer userType = randomPojo(UserTypeEnum.class).getValue();
        // mock 数据
        SocialClientDO client = randomPojo(SocialClientDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())
                .setUserType(userType).setSocialType(SocialTypeEnum.WECHAT_MP.getType()));
        socialClientMapper.insert(client);
 
        // 调用
        WxMpService result = socialClientService.getWxMpService(userType);
        // 断言
        assertSame(wxMpService, result);
    }
 
    @Test
    public void testGetWxMpService_clientEnable() {
        // 准备参数
        Integer userType = randomPojo(UserTypeEnum.class).getValue();
        // mock 数据
        SocialClientDO client = randomPojo(SocialClientDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())
                .setUserType(userType).setSocialType(SocialTypeEnum.WECHAT_MP.getType()));
        socialClientMapper.insert(client);
        // mock 方法
        WxMpProperties.ConfigStorage configStorage = mock(WxMpProperties.ConfigStorage.class);
        when(wxMpProperties.getConfigStorage()).thenReturn(configStorage);
 
        // 调用
        WxMpService result = socialClientService.getWxMpService(userType);
        // 断言
        assertNotSame(wxMpService, result);
        assertEquals(client.getClientId(), result.getWxMpConfigStorage().getAppId());
        assertEquals(client.getClientSecret(), result.getWxMpConfigStorage().getSecret());
    }
 
    // =================== 微信小程序独有 ===================
 
    @Test
    public void testGetWxMaPhoneNumberInfo_success() throws WxErrorException {
        // 准备参数
        Integer userType = randomPojo(UserTypeEnum.class).getValue();
        String phoneCode = randomString();
        // mock 方法
        WxMaUserService userService = mock(WxMaUserService.class);
        when(wxMaService.getUserService()).thenReturn(userService);
        WxMaPhoneNumberInfo phoneNumber = randomPojo(WxMaPhoneNumberInfo.class);
        when(userService.getPhoneNoInfo(eq(phoneCode))).thenReturn(phoneNumber);
 
        // 调用
        WxMaPhoneNumberInfo result = socialClientService.getWxMaPhoneNumberInfo(userType, phoneCode);
        // 断言
        assertSame(phoneNumber, result);
    }
 
    @Test
    public void testGetWxMaPhoneNumberInfo_exception() throws WxErrorException {
        // 准备参数
        Integer userType = randomPojo(UserTypeEnum.class).getValue();
        String phoneCode = randomString();
        // mock 方法
        WxMaUserService userService = mock(WxMaUserService.class);
        when(wxMaService.getUserService()).thenReturn(userService);
        WxErrorException wxErrorException = randomPojo(WxErrorException.class);
        when(userService.getPhoneNoInfo(eq(phoneCode))).thenThrow(wxErrorException);
 
        // 调用并断言异常
        assertServiceException(() -> socialClientService.getWxMaPhoneNumberInfo(userType, phoneCode),
                SOCIAL_CLIENT_WEIXIN_MINI_APP_PHONE_CODE_ERROR);
    }
 
    @Test
    public void testGetWxMaService_clientNull() {
        // 准备参数
        Integer userType = randomPojo(UserTypeEnum.class).getValue();
        // mock 方法
 
        // 调用
        WxMaService result = socialClientService.getWxMaService(userType);
        // 断言
        assertSame(wxMaService, result);
    }
 
    @Test
    public void testGetWxMaService_clientDisable() {
        // 准备参数
        Integer userType = randomPojo(UserTypeEnum.class).getValue();
        // mock 数据
        SocialClientDO client = randomPojo(SocialClientDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())
                .setUserType(userType).setSocialType(SocialTypeEnum.WECHAT_MINI_APP.getType()));
        socialClientMapper.insert(client);
 
        // 调用
        WxMaService result = socialClientService.getWxMaService(userType);
        // 断言
        assertSame(wxMaService, result);
    }
 
    @Test
    public void testGetWxMaService_clientEnable() {
        // 准备参数
        Integer userType = randomPojo(UserTypeEnum.class).getValue();
        // mock 数据
        SocialClientDO client = randomPojo(SocialClientDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())
                .setUserType(userType).setSocialType(SocialTypeEnum.WECHAT_MINI_APP.getType()));
        socialClientMapper.insert(client);
        // mock 方法
        WxMaProperties.ConfigStorage configStorage = mock(WxMaProperties.ConfigStorage.class);
        when(wxMaProperties.getConfigStorage()).thenReturn(configStorage);
 
        // 调用
        WxMaService result = socialClientService.getWxMaService(userType);
        // 断言
        assertNotSame(wxMaService, result);
        assertEquals(client.getClientId(), result.getWxMaConfig().getAppid());
        assertEquals(client.getClientSecret(), result.getWxMaConfig().getSecret());
    }
 
    // =================== 客户端管理 ===================
 
    @Test
    public void testCreateSocialClient_success() {
        // 准备参数
        SocialClientSaveReqVO reqVO = randomPojo(SocialClientSaveReqVO.class,
                o -> o.setSocialType(randomEle(SocialTypeEnum.values()).getType())
                        .setUserType(randomEle(UserTypeEnum.values()).getValue())
                        .setStatus(randomCommonStatus()))
                .setId(null); // 防止 id 被赋值
 
        // 调用
        Long socialClientId = socialClientService.createSocialClient(reqVO);
        // 断言
        assertNotNull(socialClientId);
        // 校验记录的属性是否正确
        SocialClientDO socialClient = socialClientMapper.selectById(socialClientId);
        assertPojoEquals(reqVO, socialClient, "id");
    }
 
    @Test
    public void testUpdateSocialClient_success() {
        // mock 数据
        SocialClientDO dbSocialClient = randomPojo(SocialClientDO.class);
        socialClientMapper.insert(dbSocialClient);// @Sql: 先插入出一条存在的数据
        // 准备参数
        SocialClientSaveReqVO reqVO = randomPojo(SocialClientSaveReqVO.class, o -> {
            o.setId(dbSocialClient.getId()); // 设置更新的 ID
            o.setSocialType(randomEle(SocialTypeEnum.values()).getType())
                    .setUserType(randomEle(UserTypeEnum.values()).getValue())
                    .setStatus(randomCommonStatus());
        });
 
        // 调用
        socialClientService.updateSocialClient(reqVO);
        // 校验是否更新正确
        SocialClientDO socialClient = socialClientMapper.selectById(reqVO.getId()); // 获取最新的
        assertPojoEquals(reqVO, socialClient);
    }
 
    @Test
    public void testUpdateSocialClient_notExists() {
        // 准备参数
        SocialClientSaveReqVO reqVO = randomPojo(SocialClientSaveReqVO.class);
 
        // 调用, 并断言异常
        assertServiceException(() -> socialClientService.updateSocialClient(reqVO), SOCIAL_CLIENT_NOT_EXISTS);
    }
 
    @Test
    public void testDeleteSocialClient_success() {
        // mock 数据
        SocialClientDO dbSocialClient = randomPojo(SocialClientDO.class);
        socialClientMapper.insert(dbSocialClient);// @Sql: 先插入出一条存在的数据
        // 准备参数
        Long id = dbSocialClient.getId();
 
        // 调用
        socialClientService.deleteSocialClient(id);
        // 校验数据不存在了
        assertNull(socialClientMapper.selectById(id));
    }
 
    @Test
    public void testDeleteSocialClient_notExists() {
        // 准备参数
        Long id = randomLongId();
 
        // 调用, 并断言异常
        assertServiceException(() -> socialClientService.deleteSocialClient(id), SOCIAL_CLIENT_NOT_EXISTS);
    }
 
    @Test
    public void testGetSocialClient() {
        // mock 数据
        SocialClientDO dbSocialClient = randomPojo(SocialClientDO.class);
        socialClientMapper.insert(dbSocialClient);// @Sql: 先插入出一条存在的数据
        // 准备参数
        Long id = dbSocialClient.getId();
 
        // 调用
        SocialClientDO socialClient = socialClientService.getSocialClient(id);
        // 校验数据正确
        assertPojoEquals(dbSocialClient, socialClient);
    }
 
    @Test
    public void testGetSocialClientPage() {
        // mock 数据
        SocialClientDO dbSocialClient = randomPojo(SocialClientDO.class, o -> { // 等会查询到
            o.setName("芋头");
            o.setSocialType(SocialTypeEnum.GITEE.getType());
            o.setUserType(UserTypeEnum.ADMIN.getValue());
            o.setClientId("iailab");
            o.setStatus(CommonStatusEnum.ENABLE.getStatus());
        });
        socialClientMapper.insert(dbSocialClient);
        // 测试 name 不匹配
        socialClientMapper.insert(cloneIgnoreId(dbSocialClient, o -> o.setName(randomString())));
        // 测试 socialType 不匹配
        socialClientMapper.insert(cloneIgnoreId(dbSocialClient, o -> o.setSocialType(SocialTypeEnum.DINGTALK.getType())));
        // 测试 userType 不匹配
        socialClientMapper.insert(cloneIgnoreId(dbSocialClient, o -> o.setUserType(UserTypeEnum.MEMBER.getValue())));
        // 测试 clientId 不匹配
        socialClientMapper.insert(cloneIgnoreId(dbSocialClient, o -> o.setClientId("dao")));
        // 测试 status 不匹配
        socialClientMapper.insert(cloneIgnoreId(dbSocialClient, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
        // 准备参数
        SocialClientPageReqVO reqVO = new SocialClientPageReqVO();
        reqVO.setName("芋");
        reqVO.setSocialType(SocialTypeEnum.GITEE.getType());
        reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
        reqVO.setClientId("yu");
        reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
 
        // 调用
        PageResult<SocialClientDO> pageResult = socialClientService.getSocialClientPage(reqVO);
        // 断言
        assertEquals(1, pageResult.getTotal());
        assertEquals(1, pageResult.getList().size());
        assertPojoEquals(dbSocialClient, pageResult.getList().get(0));
    }
 
}