AdminLicenseTest.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. package com.xuekairui.gateway;
  2. import org.junit.jupiter.api.*;
  3. import org.springframework.http.MediaType;
  4. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
  5. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
  6. /**
  7. * 入驻信息审核管理接口测试
  8. * 覆盖:审核列表查询、审核通过/拒绝、异常场景
  9. */
  10. @DisplayName("入驻信息审核管理接口测试")
  11. @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
  12. class AdminLicenseTest extends GatewayBaseTest {
  13. private String adminToken() {
  14. return generateAccessToken(100L, "13900000000", "ADMIN");
  15. }
  16. // ==================== 前置:上传入驻信息 ====================
  17. @Test
  18. @Order(1)
  19. @DisplayName("前置:上传入驻信息用于审核测试")
  20. void setup_uploadLicense() throws Exception {
  21. String body = """
  22. {
  23. "storeName": "审核测试大药房",
  24. "terminalType": "SINGLE"
  25. "province": "广东省",
  26. "city": "深圳市",
  27. "district": "南山区",
  28. "storeAddress": "科技园路1号",
  29. "contactPerson": "李四",
  30. "contactPhone": "13900139001",
  31. "businessLicenseUrl": "https://oss.example.com/license/review-test.jpg",
  32. "drugLicenseUrl": "https://oss.example.com/drug/review-test.jpg",
  33. "creditCode": "91110000MA12345678"
  34. }
  35. """;
  36. mockMvc.perform(post("/api/business-license/upload")
  37. .header("Authorization", "Bearer " + adminToken())
  38. .contentType(MediaType.APPLICATION_JSON)
  39. .content(body))
  40. .andExpect(status().isOk())
  41. .andExpect(jsonPath("$.code").value(200));
  42. }
  43. // ==================== 审核列表查询 ====================
  44. @Test
  45. @Order(2)
  46. @DisplayName("GET /api/admin/license/review - 查询待审核列表(默认PENDING)")
  47. void listPendingReviews_defaultStatus_shouldReturnList() throws Exception {
  48. mockMvc.perform(get("/api/admin/license/review")
  49. .header("Authorization", "Bearer " + adminToken()))
  50. .andExpect(status().isOk())
  51. .andExpect(jsonPath("$.code").value(200))
  52. .andExpect(jsonPath("$.data.records").isArray())
  53. .andExpect(jsonPath("$.data.total").isNumber());
  54. }
  55. @Test
  56. @Order(3)
  57. @DisplayName("GET /api/admin/license/review?reviewStatus=PENDING - 显式查询待审核")
  58. void listPendingReviews_explicitStatus_shouldReturnList() throws Exception {
  59. mockMvc.perform(get("/api/admin/license/review?reviewStatus=PENDING")
  60. .header("Authorization", "Bearer " + adminToken()))
  61. .andExpect(status().isOk())
  62. .andExpect(jsonPath("$.code").value(200))
  63. .andExpect(jsonPath("$.data.records").isArray())
  64. .andExpect(jsonPath("$.data.total").isNumber());
  65. }
  66. @Test
  67. @Order(4)
  68. @DisplayName("GET /api/admin/license/review?reviewStatus=APPROVED - 查询已通过")
  69. void listApprovedReviews_shouldReturnList() throws Exception {
  70. mockMvc.perform(get("/api/admin/license/review?reviewStatus=APPROVED")
  71. .header("Authorization", "Bearer " + adminToken()))
  72. .andExpect(status().isOk())
  73. .andExpect(jsonPath("$.code").value(200))
  74. .andExpect(jsonPath("$.data.records").isArray())
  75. .andExpect(jsonPath("$.data.total").isNumber());
  76. }
  77. @Test
  78. @Order(5)
  79. @DisplayName("GET /api/admin/license/review?reviewStatus=REJECTED - 查询已拒绝")
  80. void listRejectedReviews_shouldReturnList() throws Exception {
  81. mockMvc.perform(get("/api/admin/license/review?reviewStatus=REJECTED")
  82. .header("Authorization", "Bearer " + adminToken()))
  83. .andExpect(status().isOk())
  84. .andExpect(jsonPath("$.code").value(200))
  85. .andExpect(jsonPath("$.data.records").isArray())
  86. .andExpect(jsonPath("$.data.total").isNumber());
  87. }
  88. @Test
  89. @Order(6)
  90. @DisplayName("GET /api/admin/license/review - 无Token应返回401")
  91. void listPendingReviews_withoutToken_shouldReturn401() throws Exception {
  92. mockMvc.perform(get("/api/admin/license/review"))
  93. .andExpect(status().isUnauthorized());
  94. }
  95. // ==================== 管理员新增 ====================
  96. @Test
  97. @Order(7)
  98. @DisplayName("POST /api/admin/license - 管理员新增入驻信息(创建后状态为PENDING)")
  99. void createByAdmin_shouldSucceedAndReturnPending() throws Exception {
  100. String body = """
  101. {
  102. "userId": 3,
  103. "storeName": "管理员新增药房",
  104. "terminalType": "SINGLE"
  105. "province": "广东省",
  106. "city": "深圳市",
  107. "district": "福田区",
  108. "storeAddress": "福华路100号",
  109. "contactPerson": "赵六",
  110. "contactPhone": "13900139003",
  111. "businessLicenseUrl": "https://oss.example.com/license/admin-create.jpg",
  112. "drugLicenseUrl": "https://oss.example.com/drug/admin-create.jpg",
  113. "creditCode": "91110000MA11111111",
  114. "medicalDeviceClass3Url": "https://oss.example.com/device/class3.jpg"
  115. }
  116. """;
  117. mockMvc.perform(post("/api/admin/license")
  118. .header("Authorization", "Bearer " + adminToken())
  119. .contentType(MediaType.APPLICATION_JSON)
  120. .content(body))
  121. .andExpect(status().isOk())
  122. .andExpect(jsonPath("$.code").value(200));
  123. // 验证创建后状态为 PENDING(待审核确认)
  124. mockMvc.perform(get("/api/admin/license/review?reviewStatus=PENDING")
  125. .header("Authorization", "Bearer " + adminToken()))
  126. .andExpect(status().isOk())
  127. .andExpect(jsonPath("$.code").value(200))
  128. .andExpect(jsonPath("$.data").isArray());
  129. }
  130. @Test
  131. @Order(8)
  132. @DisplayName("POST /api/admin/license - 缺少必填字段应返回400(缺少店铺名称)")
  133. void createByAdmin_missingStoreName_shouldReturn400() throws Exception {
  134. String body = """
  135. {
  136. "userId": 4,
  137. "terminalType": "SINGLE"
  138. "province": "广东省",
  139. "city": "深圳市",
  140. "district": "福田区",
  141. "storeAddress": "福华路100号",
  142. "contactPerson": "赵六",
  143. "contactPhone": "13900139004",
  144. "businessLicenseUrl": "https://oss.example.com/license/missing.jpg",
  145. "drugLicenseUrl": "https://oss.example.com/drug/missing.jpg"
  146. }
  147. """;
  148. mockMvc.perform(post("/api/admin/license")
  149. .header("Authorization", "Bearer " + adminToken())
  150. .contentType(MediaType.APPLICATION_JSON)
  151. .content(body))
  152. .andExpect(status().isBadRequest());
  153. }
  154. @Test
  155. @Order(9)
  156. @DisplayName("POST /api/admin/license - 缺少userId应返回400")
  157. void createByAdmin_missingUserId_shouldReturn400() throws Exception {
  158. String body = """
  159. {
  160. "storeName": "无客户药房",
  161. "terminalType": "SINGLE"
  162. "province": "广东省",
  163. "city": "深圳市",
  164. "district": "福田区",
  165. "storeAddress": "福华路100号",
  166. "contactPerson": "赵六",
  167. "contactPhone": "13900139005",
  168. "businessLicenseUrl": "https://oss.example.com/license/no-user.jpg",
  169. "drugLicenseUrl": "https://oss.example.com/drug/no-user.jpg"
  170. }
  171. """;
  172. mockMvc.perform(post("/api/admin/license")
  173. .header("Authorization", "Bearer " + adminToken())
  174. .contentType(MediaType.APPLICATION_JSON)
  175. .content(body))
  176. .andExpect(status().isBadRequest());
  177. }
  178. @Test
  179. @Order(10)
  180. @DisplayName("POST /api/admin/license - 无Token应返回401")
  181. void createByAdmin_withoutToken_shouldReturn401() throws Exception {
  182. mockMvc.perform(post("/api/admin/license")
  183. .contentType(MediaType.APPLICATION_JSON)
  184. .content("{\"userId\":5,\"storeName\":\"test\"}"))
  185. .andExpect(status().isUnauthorized());
  186. }
  187. // ==================== 管理员编辑 ====================
  188. @Test
  189. @Order(20)
  190. @DisplayName("PUT /api/admin/license/{licenseId} - 管理员编辑入驻信息")
  191. void editByAdmin_shouldSucceed() throws Exception {
  192. String body = """
  193. {
  194. "storeName": "编辑后药房名称",
  195. "terminalType": "CHAIN"
  196. "province": "北京市",
  197. "city": "北京市",
  198. "district": "海淀区",
  199. "storeAddress": "中关村大街1号",
  200. "contactPerson": "李四更新",
  201. "contactPhone": "13900139001",
  202. "businessLicenseUrl": "https://oss.example.com/license/edited.jpg",
  203. "drugLicenseUrl": "https://oss.example.com/drug/edited.jpg",
  204. "creditCode": "91110000MA12345678"
  205. }
  206. """;
  207. mockMvc.perform(put("/api/admin/license/1")
  208. .header("Authorization", "Bearer " + adminToken())
  209. .contentType(MediaType.APPLICATION_JSON)
  210. .content(body))
  211. .andExpect(status().isOk())
  212. .andExpect(jsonPath("$.code").value(200));
  213. }
  214. @Test
  215. @Order(21)
  216. @DisplayName("PUT /api/admin/license/{licenseId} - 缺少必填字段应返回400(缺少营业执照)")
  217. void editByAdmin_missingBusinessLicense_shouldReturn400() throws Exception {
  218. String body = """
  219. {
  220. "storeName": "编辑测试药房",
  221. "terminalType": "SINGLE"
  222. "province": "广东省",
  223. "city": "深圳市",
  224. "district": "南山区",
  225. "storeAddress": "科技园路1号",
  226. "contactPerson": "李四",
  227. "contactPhone": "13900139001",
  228. "drugLicenseUrl": "https://oss.example.com/drug/edited.jpg"
  229. }
  230. """;
  231. mockMvc.perform(put("/api/admin/license/1")
  232. .header("Authorization", "Bearer " + adminToken())
  233. .contentType(MediaType.APPLICATION_JSON)
  234. .content(body))
  235. .andExpect(status().isBadRequest());
  236. }
  237. @Test
  238. @Order(22)
  239. @DisplayName("PUT /api/admin/license/{licenseId} - 不存在的licenseId应返回404")
  240. void editByAdmin_nonExistent_shouldReturn404() throws Exception {
  241. String body = """
  242. {
  243. "storeName": "不存在的药房",
  244. "terminalType": "SINGLE"
  245. "province": "广东省",
  246. "city": "深圳市",
  247. "district": "南山区",
  248. "storeAddress": "科技园路1号",
  249. "contactPerson": "李四",
  250. "contactPhone": "13900139001",
  251. "businessLicenseUrl": "https://oss.example.com/license/not-found.jpg",
  252. "drugLicenseUrl": "https://oss.example.com/drug/not-found.jpg"
  253. }
  254. """;
  255. mockMvc.perform(put("/api/admin/license/9999")
  256. .header("Authorization", "Bearer " + adminToken())
  257. .contentType(MediaType.APPLICATION_JSON)
  258. .content(body))
  259. .andExpect(status().isOk())
  260. .andExpect(jsonPath("$.code").value(404));
  261. }
  262. @Test
  263. @Order(23)
  264. @DisplayName("PUT /api/admin/license/{licenseId} - 无Token应返回401")
  265. void editByAdmin_withoutToken_shouldReturn401() throws Exception {
  266. mockMvc.perform(put("/api/admin/license/1")
  267. .contentType(MediaType.APPLICATION_JSON)
  268. .content("{\"storeName\":\"test\"}"))
  269. .andExpect(status().isUnauthorized());
  270. }
  271. // ==================== 审核操作 ====================
  272. @Test
  273. @Order(30)
  274. @DisplayName("POST /api/admin/license/review - 审核通过")
  275. void reviewLicense_approve_shouldSucceed() throws Exception {
  276. String body = """
  277. {
  278. "licenseId": 1,
  279. "action": "APPROVED"
  280. }
  281. """;
  282. mockMvc.perform(post("/api/admin/license/review")
  283. .header("Authorization", "Bearer " + adminToken())
  284. .contentType(MediaType.APPLICATION_JSON)
  285. .content(body))
  286. .andExpect(status().isOk())
  287. .andExpect(jsonPath("$.code").value(200));
  288. }
  289. @Test
  290. @Order(31)
  291. @DisplayName("POST /api/admin/license/review - 重复审核应返回错误")
  292. void reviewLicense_duplicate_shouldReturnError() throws Exception {
  293. String body = """
  294. {
  295. "licenseId": 1,
  296. "action": "APPROVED"
  297. }
  298. """;
  299. mockMvc.perform(post("/api/admin/license/review")
  300. .header("Authorization", "Bearer " + adminToken())
  301. .contentType(MediaType.APPLICATION_JSON)
  302. .content(body))
  303. .andExpect(status().isOk())
  304. .andExpect(jsonPath("$.code").value(400));
  305. }
  306. @Test
  307. @Order(32)
  308. @DisplayName("POST /api/admin/license/review - 不存在的licenseId应返回404")
  309. void reviewLicense_nonExistent_shouldReturn404() throws Exception {
  310. String body = """
  311. {
  312. "licenseId": 9999,
  313. "action": "APPROVED"
  314. }
  315. """;
  316. mockMvc.perform(post("/api/admin/license/review")
  317. .header("Authorization", "Bearer " + adminToken())
  318. .contentType(MediaType.APPLICATION_JSON)
  319. .content(body))
  320. .andExpect(status().isOk())
  321. .andExpect(jsonPath("$.code").value(404));
  322. }
  323. @Test
  324. @Order(33)
  325. @DisplayName("POST /api/admin/license/review - 参数缺失应返回400(缺少action)")
  326. void reviewLicense_missingAction_shouldReturn400() throws Exception {
  327. String body = """
  328. {
  329. "licenseId": 1
  330. }
  331. """;
  332. mockMvc.perform(post("/api/admin/license/review")
  333. .header("Authorization", "Bearer " + adminToken())
  334. .contentType(MediaType.APPLICATION_JSON)
  335. .content(body))
  336. .andExpect(status().isBadRequest());
  337. }
  338. @Test
  339. @Order(34)
  340. @DisplayName("POST /api/admin/license/review - 参数缺失应返回400(缺少licenseId)")
  341. void reviewLicense_missingLicenseId_shouldReturn400() throws Exception {
  342. String body = """
  343. {
  344. "action": "APPROVED"
  345. }
  346. """;
  347. mockMvc.perform(post("/api/admin/license/review")
  348. .header("Authorization", "Bearer " + adminToken())
  349. .contentType(MediaType.APPLICATION_JSON)
  350. .content(body))
  351. .andExpect(status().isBadRequest());
  352. }
  353. @Test
  354. @Order(35)
  355. @DisplayName("POST /api/admin/license/review - 无效审核动作应返回400")
  356. void reviewLicense_invalidAction_shouldReturn400() throws Exception {
  357. String body = """
  358. {
  359. "licenseId": 1,
  360. "action": "INVALID_ACTION"
  361. }
  362. """;
  363. mockMvc.perform(post("/api/admin/license/review")
  364. .header("Authorization", "Bearer " + adminToken())
  365. .contentType(MediaType.APPLICATION_JSON)
  366. .content(body))
  367. .andExpect(status().isOk())
  368. .andExpect(jsonPath("$.code").value(400));
  369. }
  370. @Test
  371. @Order(36)
  372. @DisplayName("POST /api/admin/license/review - 拒绝并填写原因")
  373. void reviewLicense_rejectWithReason_shouldSucceed() throws Exception {
  374. // 需要一条新的PENDING记录来测试拒绝
  375. // 先用另一个用户上传
  376. String uploadBody = """
  377. {
  378. "storeName": "拒绝测试药房",
  379. "terminalType": "CHAIN"
  380. "province": "北京市",
  381. "city": "北京市",
  382. "district": "朝阳区",
  383. "storeAddress": "望京路88号",
  384. "contactPerson": "王五",
  385. "contactPhone": "13900139002",
  386. "businessLicenseUrl": "https://oss.example.com/license/reject-test.jpg",
  387. "drugLicenseUrl": "https://oss.example.com/drug/reject-test.jpg",
  388. "creditCode": "91110000MA87654321"
  389. }
  390. """;
  391. mockMvc.perform(post("/api/business-license/upload")
  392. .header("Authorization", "Bearer " + generateAccessToken(2L, "13900139000"))
  393. .contentType(MediaType.APPLICATION_JSON)
  394. .content(uploadBody))
  395. .andExpect(status().isOk());
  396. // 查询待审核列表找到新上传的执照ID并拒绝
  397. String rejectBody = """
  398. {
  399. "licenseId": 2,
  400. "action": "REJECTED",
  401. "rejectReason": "资质图片不清晰,请重新上传"
  402. }
  403. """;
  404. mockMvc.perform(post("/api/admin/license/review")
  405. .header("Authorization", "Bearer " + adminToken())
  406. .contentType(MediaType.APPLICATION_JSON)
  407. .content(rejectBody))
  408. .andExpect(status().isOk())
  409. .andExpect(jsonPath("$.code").value(200));
  410. }
  411. @Test
  412. @Order(37)
  413. @DisplayName("POST /api/admin/license/review - 驳回原因必填,缺失应返回400")
  414. void reviewLicense_rejectWithoutReason_shouldReturn400() throws Exception {
  415. // 先创建一条新的PENDING记录用于测试驳回原因必填
  416. String uploadBody = """
  417. {
  418. "storeName": "驳回原因测试药房",
  419. "terminalType": "CLINIC"
  420. "province": "上海市",
  421. "city": "上海市",
  422. "district": "浦东新区",
  423. "storeAddress": "张江路66号",
  424. "contactPerson": "钱七",
  425. "contactPhone": "13900139006",
  426. "businessLicenseUrl": "https://oss.example.com/license/reject-reason.jpg",
  427. "drugLicenseUrl": "https://oss.example.com/drug/reject-reason.jpg"
  428. }
  429. """;
  430. mockMvc.perform(post("/api/business-license/upload")
  431. .header("Authorization", "Bearer " + generateAccessToken(5L, "13900139006"))
  432. .contentType(MediaType.APPLICATION_JSON)
  433. .content(uploadBody))
  434. .andExpect(status().isOk());
  435. // 驳回但不填原因,应返回400
  436. String rejectBody = """
  437. {
  438. "licenseId": 3,
  439. "action": "REJECTED"
  440. }
  441. """;
  442. mockMvc.perform(post("/api/admin/license/review")
  443. .header("Authorization", "Bearer " + adminToken())
  444. .contentType(MediaType.APPLICATION_JSON)
  445. .content(rejectBody))
  446. .andExpect(status().isOk())
  447. .andExpect(jsonPath("$.code").value(400));
  448. }
  449. @Test
  450. @Order(38)
  451. @DisplayName("POST /api/admin/license/review - 无Token应返回401")
  452. void reviewLicense_withoutToken_shouldReturn401() throws Exception {
  453. mockMvc.perform(post("/api/admin/license/review")
  454. .contentType(MediaType.APPLICATION_JSON)
  455. .content("{\"licenseId\":1,\"action\":\"APPROVED\"}"))
  456. .andExpect(status().isUnauthorized());
  457. }
  458. }