|
| 1 | +package me.chanjar.weixin.cp.tp.service.impl; |
| 2 | + |
| 3 | +import me.chanjar.weixin.common.error.WxErrorException; |
| 4 | +import me.chanjar.weixin.cp.bean.WxCpUser; |
| 5 | +import me.chanjar.weixin.cp.config.WxCpTpConfigStorage; |
| 6 | +import me.chanjar.weixin.cp.config.impl.WxCpTpDefaultConfigImpl; |
| 7 | +import me.chanjar.weixin.cp.tp.service.WxCpTpUserService; |
| 8 | +import org.mockito.Mock; |
| 9 | +import org.mockito.MockitoAnnotations; |
| 10 | +import org.testng.annotations.AfterClass; |
| 11 | +import org.testng.annotations.BeforeClass; |
| 12 | +import org.testng.annotations.Test; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | + |
| 16 | +import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.User.USER_SIMPLE_LIST; |
| 17 | +import static org.mockito.ArgumentMatchers.contains; |
| 18 | +import static org.mockito.ArgumentMatchers.eq; |
| 19 | +import static org.mockito.Mockito.verify; |
| 20 | +import static org.mockito.Mockito.when; |
| 21 | +import static org.testng.Assert.assertNotNull; |
| 22 | + |
| 23 | +/** |
| 24 | + * 企业微信-第三方开发-用户管理相关测试. |
| 25 | + * |
| 26 | + * @author GitHub Copilot |
| 27 | + */ |
| 28 | +public class WxCpTpUserServiceImplTest { |
| 29 | + |
| 30 | + @Mock |
| 31 | + private WxCpTpServiceApacheHttpClientImpl wxCpTpService; |
| 32 | + |
| 33 | + @Mock |
| 34 | + private WxCpTpConfigStorage configStorage; |
| 35 | + |
| 36 | + private WxCpTpUserService wxCpTpUserService; |
| 37 | + |
| 38 | + private AutoCloseable mockitoAnnotations; |
| 39 | + |
| 40 | + /** |
| 41 | + * Sets up. |
| 42 | + */ |
| 43 | + @BeforeClass |
| 44 | + public void setUp() { |
| 45 | + mockitoAnnotations = MockitoAnnotations.openMocks(this); |
| 46 | + when(wxCpTpService.getWxCpTpConfigStorage()).thenReturn(configStorage); |
| 47 | + WxCpTpDefaultConfigImpl defaultConfig = new WxCpTpDefaultConfigImpl(); |
| 48 | + when(configStorage.getApiUrl(contains(USER_SIMPLE_LIST))) |
| 49 | + .thenAnswer(invocation -> defaultConfig.getApiUrl(invocation.getArgument(0))); |
| 50 | + wxCpTpUserService = new WxCpTpUserServiceImpl(wxCpTpService); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Tear down. |
| 55 | + * |
| 56 | + * @throws Exception the exception |
| 57 | + */ |
| 58 | + @AfterClass |
| 59 | + public void tearDown() throws Exception { |
| 60 | + mockitoAnnotations.close(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * 测试使用 corpId 的 listSimpleByDepartment 方法,验证请求使用了 access_token 而非 suite_access_token. |
| 65 | + * |
| 66 | + * @throws WxErrorException the wx error exception |
| 67 | + */ |
| 68 | + @Test |
| 69 | + public void testListSimpleByDepartmentWithCorpId() throws WxErrorException { |
| 70 | + Long departId = 1L; |
| 71 | + String corpId = "test_corp_id"; |
| 72 | + String accessToken = "test_access_token"; |
| 73 | + String result = "{\"errcode\":0,\"errmsg\":\"ok\",\"userlist\":[{\"userid\":\"zhangsan\",\"name\":\"张三\"}]}"; |
| 74 | + |
| 75 | + when(configStorage.getAccessToken(corpId)).thenReturn(accessToken); |
| 76 | + String url = new WxCpTpDefaultConfigImpl().getApiUrl(USER_SIMPLE_LIST + departId); |
| 77 | + when(wxCpTpService.get(eq(url), contains("access_token=" + accessToken), eq(true))).thenReturn(result); |
| 78 | + |
| 79 | + List<WxCpUser> users = wxCpTpUserService.listSimpleByDepartment(departId, true, 0, corpId); |
| 80 | + assertNotNull(users); |
| 81 | + |
| 82 | + // 验证调用时传入了 withoutSuiteAccessToken=true,确保不会附加 suite_access_token |
| 83 | + verify(wxCpTpService).get(eq(url), contains("access_token=" + accessToken), eq(true)); |
| 84 | + } |
| 85 | +} |
0 commit comments