PlatformAccountBindingTest.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package com.xuekairui.gateway;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.junit.jupiter.api.*;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.http.MediaType;
  9. import org.springframework.test.web.servlet.MockMvc;
  10. import org.springframework.test.web.servlet.MvcResult;
  11. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
  12. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
  13. /**
  14. * B2B平台账号绑定全流程测试
  15. *
  16. * 测试场景:
  17. * 1. 绑定药师帮账号
  18. * 2. 查询已绑定列表
  19. * 3. 验证账号
  20. * 4. 解绑账号
  21. * 5. 异常场景测试
  22. */
  23. @SpringBootTest
  24. @AutoConfigureMockMvc
  25. @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
  26. @Slf4j
  27. public class PlatformAccountBindingTest extends GatewayBaseTest {
  28. @Autowired
  29. private MockMvc mockMvc;
  30. @Autowired
  31. private ObjectMapper objectMapper;
  32. private String accessToken;
  33. private Long userId;
  34. /**
  35. * 前置条件:先登录获取Token
  36. */
  37. @BeforeEach
  38. public void setUp() throws Exception {
  39. // 登录获取Token
  40. MvcResult result = mockMvc.perform(post("/api/auth/sms/login")
  41. .contentType(MediaType.APPLICATION_JSON)
  42. .content("{\"phone\":\"13800138000\",\"code\":\"123456\"}"))
  43. .andExpect(status().isOk())
  44. .andReturn();
  45. String response = result.getResponse().getContentAsString();
  46. accessToken = objectMapper.readTree(response).get("data").get("accessToken").asText();
  47. userId = objectMapper.readTree(response).get("data").get("userId").asLong();
  48. log.info("🔑 登录成功: userId={}", userId);
  49. }
  50. /**
  51. * 测试1:绑定药师帮账号
  52. */
  53. @Test
  54. @Order(1)
  55. @DisplayName("1. 绑定药师帮账号")
  56. public void testBindYaoshibangAccount() throws Exception {
  57. mockMvc.perform(post("/api/platform-account/bind")
  58. .header("Authorization", "Bearer " + accessToken)
  59. .contentType(MediaType.APPLICATION_JSON)
  60. .content(objectMapper.writeValueAsString(new Object() {
  61. public String platformCode = "yaoshibang";
  62. public String platformName = "药师帮";
  63. public String account = "13800138000";
  64. public String password = "test_password_123";
  65. })))
  66. .andExpect(status().isOk())
  67. .andExpect(jsonPath("$.code").value(200))
  68. .andExpect(jsonPath("$.message").value("操作成功"));
  69. log.info("✅ 绑定药师帮账号成功");
  70. }
  71. /**
  72. * 测试2:绑定药帮忙账号
  73. */
  74. @Test
  75. @Order(2)
  76. @DisplayName("2. 绑定药帮忙账号")
  77. public void testBindYaobangmangAccount() throws Exception {
  78. mockMvc.perform(post("/api/platform-account/bind")
  79. .header("Authorization", "Bearer " + accessToken)
  80. .contentType(MediaType.APPLICATION_JSON)
  81. .content(objectMapper.writeValueAsString(new Object() {
  82. public String platformCode = "yaobangmang";
  83. public String platformName = "药帮忙";
  84. public String account = "13900139000";
  85. public String password = "test_password_456";
  86. })))
  87. .andExpect(status().isOk())
  88. .andExpect(jsonPath("$.code").value(200));
  89. log.info("✅ 绑定药帮忙账号成功");
  90. }
  91. /**
  92. * 测试3:查询已绑定平台列表
  93. */
  94. @Test
  95. @Order(3)
  96. @DisplayName("3. 查询已绑定平台列表")
  97. public void testGetBoundAccounts() throws Exception {
  98. mockMvc.perform(get("/api/platform-account/list")
  99. .header("Authorization", "Bearer " + accessToken))
  100. .andExpect(status().isOk())
  101. .andExpect(jsonPath("$.code").value(200))
  102. .andExpect(jsonPath("$.data").isArray())
  103. .andExpect(jsonPath("$.data.length()").value(2)) // 已绑定2个平台
  104. .andExpect(jsonPath("$.data[0].platformCode").exists())
  105. .andExpect(jsonPath("$.data[0].account").exists())
  106. .andExpect(jsonPath("$.data[0].verifyStatus").exists());
  107. log.info("✅ 查询已绑定平台列表成功");
  108. }
  109. /**
  110. * 测试4:验证平台账号
  111. */
  112. @Test
  113. @Order(4)
  114. @DisplayName("4. 验证药师帮账号")
  115. public void testVerifyAccount() throws Exception {
  116. mockMvc.perform(post("/api/platform-account/verify/yaoshibang")
  117. .header("Authorization", "Bearer " + accessToken))
  118. .andExpect(status().isOk())
  119. .andExpect(jsonPath("$.code").value(200));
  120. log.info("✅ 验证药师帮账号成功");
  121. }
  122. /**
  123. * 测试5:更新已绑定的账号
  124. */
  125. @Test
  126. @Order(5)
  127. @DisplayName("5. 更新药师帮账号(重新绑定)")
  128. public void testUpdateBoundAccount() throws Exception {
  129. mockMvc.perform(post("/api/platform-account/bind")
  130. .header("Authorization", "Bearer " + accessToken)
  131. .contentType(MediaType.APPLICATION_JSON)
  132. .content(objectMapper.writeValueAsString(new Object() {
  133. public String platformCode = "yaoshibang";
  134. public String platformName = "药师帮";
  135. public String account = "13800138000";
  136. public String password = "new_password_789"; // 新密码
  137. })))
  138. .andExpect(status().isOk())
  139. .andExpect(jsonPath("$.code").value(200));
  140. log.info("✅ 更新药师帮账号成功");
  141. }
  142. /**
  143. * 测试6:解绑平台账号
  144. */
  145. @Test
  146. @Order(6)
  147. @DisplayName("6. 解绑药帮忙账号")
  148. public void testUnbindAccount() throws Exception {
  149. mockMvc.perform(delete("/api/platform-account/unbind/yaobangmang")
  150. .header("Authorization", "Bearer " + accessToken))
  151. .andExpect(status().isOk())
  152. .andExpect(jsonPath("$.code").value(200));
  153. // 验证解绑后只剩1个平台
  154. mockMvc.perform(get("/api/platform-account/list")
  155. .header("Authorization", "Bearer " + accessToken))
  156. .andExpect(jsonPath("$.data.length()").value(1));
  157. log.info("✅ 解绑药帮忙账号成功");
  158. }
  159. /**
  160. * 测试7:异常场景 - 绑定不存在的平台
  161. */
  162. @Test
  163. @Order(7)
  164. @DisplayName("7. 异常场景:绑定不支持的平台")
  165. public void testBindUnsupportedPlatform() throws Exception {
  166. mockMvc.perform(post("/api/platform-account/bind")
  167. .header("Authorization", "Bearer " + accessToken)
  168. .contentType(MediaType.APPLICATION_JSON)
  169. .content(objectMapper.writeValueAsString(new Object() {
  170. public String platformCode = "unsupported_platform";
  171. public String platformName = "不支持的平台";
  172. public String account = "13800138000";
  173. public String password = "password";
  174. })))
  175. .andExpect(status().isOk())
  176. .andExpect(jsonPath("$.code").isNumber()); // 业务错误码
  177. log.info("✅ 不支持的平台拦截正确");
  178. }
  179. /**
  180. * 测试8:异常场景 - 验证未绑定的平台
  181. */
  182. @Test
  183. @Order(8)
  184. @DisplayName("8. 异常场景:验证未绑定的平台")
  185. public void testVerifyUnboundPlatform() throws Exception {
  186. mockMvc.perform(post("/api/platform-account/verify/yiyaocheng")
  187. .header("Authorization", "Bearer " + accessToken))
  188. .andExpect(status().isOk())
  189. .andExpect(jsonPath("$.code").value(400));
  190. log.info("✅ 未绑定平台验证返回业务错误");
  191. }
  192. /**
  193. * 测试9:异常场景 - 解绑未绑定的平台
  194. */
  195. @Test
  196. @Order(9)
  197. @DisplayName("9. 异常场景:解绑未绑定的平台")
  198. public void testUnbindUnboundPlatform() throws Exception {
  199. mockMvc.perform(delete("/api/platform-account/unbind/yiyaocheng")
  200. .header("Authorization", "Bearer " + accessToken))
  201. .andExpect(status().isOk())
  202. .andExpect(jsonPath("$.code").value(400));
  203. log.info("✅ 未绑定平台解绑拦截正确");
  204. }
  205. /**
  206. * 测试10:账号脱敏验证
  207. */
  208. @Test
  209. @Order(10)
  210. @DisplayName("10. 验证账号脱敏显示")
  211. public void testAccountMasking() throws Exception {
  212. MvcResult result = mockMvc.perform(get("/api/platform-account/list")
  213. .header("Authorization", "Bearer " + accessToken))
  214. .andExpect(status().isOk())
  215. .andReturn();
  216. String response = result.getResponse().getContentAsString();
  217. String account = objectMapper.readTree(response)
  218. .get("data").get(0).get("account").asText();
  219. // 验证账号已脱敏:138****8000
  220. Assertions.assertTrue(account.contains("****"), "账号应该脱敏显示");
  221. Assertions.assertFalse(account.equals("13800138000"), "账号不应明文显示");
  222. log.info("✅ 账号脱敏验证通过: {}", account);
  223. }
  224. }