22301071 | fa22ac6 | 2025-05-24 21:38:17 +0800 | [diff] [blame] | 1 | package com.example.g8backend.service; |
| 2 | |
| 3 | import com.example.g8backend.controller.AuthController; |
| 4 | import com.example.g8backend.util.JwtUtil; |
| 5 | import org.junit.jupiter.api.BeforeEach; |
| 6 | import org.junit.jupiter.api.DisplayName; |
| 7 | import org.junit.jupiter.api.Test; |
| 8 | import org.springframework.beans.factory.annotation.Autowired; |
| 9 | import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; |
| 10 | import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 11 | import org.springframework.boot.test.mock.mockito.MockBean; |
| 12 | import org.springframework.security.core.Authentication; |
| 13 | import org.springframework.security.core.context.SecurityContext; |
| 14 | import org.springframework.security.core.context.SecurityContextHolder; |
| 15 | import org.springframework.test.web.servlet.MockMvc; |
| 16 | |
| 17 | import static org.mockito.Mockito.*; |
| 18 | import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; |
| 19 | import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; |
| 20 | import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; |
| 21 | |
| 22 | @WebMvcTest(AuthController.class) |
| 23 | @AutoConfigureMockMvc(addFilters = false) // 关键:禁用Security过滤器 |
| 24 | class AuthControllerTest { |
| 25 | |
| 26 | @Autowired |
| 27 | private MockMvc mockMvc; |
| 28 | |
| 29 | // 为所有依赖的 Bean 添加 Mock |
| 30 | @MockBean |
| 31 | private com.example.g8backend.service.IUserService userService; |
| 32 | @MockBean |
| 33 | private com.example.g8backend.service.IUserStatsService userStatsService; |
| 34 | @MockBean |
| 35 | private com.example.g8backend.util.mailUtil mailUtil; |
| 36 | @MockBean |
| 37 | private org.springframework.security.crypto.password.PasswordEncoder passwordEncoder; |
| 38 | @MockBean |
| 39 | private JwtUtil jwtUtil; |
| 40 | @MockBean |
| 41 | private org.springframework.data.redis.core.RedisTemplate<String, Object> redisTemplate; |
| 42 | |
| 43 | @BeforeEach |
| 44 | void setUp() { |
| 45 | // 默认设置为已认证用户 |
| 46 | Authentication authentication = mock(Authentication.class); |
| 47 | when(authentication.getPrincipal()).thenReturn(123L); |
| 48 | SecurityContext securityContext = mock(SecurityContext.class); |
| 49 | when(securityContext.getAuthentication()).thenReturn(authentication); |
| 50 | SecurityContextHolder.setContext(securityContext); |
| 51 | } |
| 52 | |
| 53 | @Test |
| 54 | @DisplayName("刷新token-认证通过返回新token") |
| 55 | void refreshToken_ShouldReturnNewToken() throws Exception { |
| 56 | String newToken = "new.jwt.token"; |
| 57 | when(jwtUtil.generateToken(123L)).thenReturn(newToken); |
| 58 | |
| 59 | mockMvc.perform(post("/auth/refresh-token") |
| 60 | .header("Authorization", "Bearer old.jwt.token") |
| 61 | .with(csrf()) |
| 62 | .accept("application/json")) // 加上这一行 |
| 63 | .andExpect(status().isOk()) |
| 64 | .andExpect(jsonPath("$.code").value(200)) |
| 65 | .andExpect(jsonPath("$.message").value("Token刷新成功")) |
| 66 | .andExpect(jsonPath("$.data").value(newToken)); |
| 67 | } |
| 68 | |
| 69 | @Test |
| 70 | @DisplayName("刷新token-未认证返回401") |
| 71 | void refreshToken_Unauthenticated_ShouldReturn401() throws Exception { |
| 72 | // 设置为未认证 |
| 73 | Authentication authentication = mock(Authentication.class); |
| 74 | when(authentication.getPrincipal()).thenReturn("anonymousUser"); |
| 75 | SecurityContext securityContext = mock(SecurityContext.class); |
| 76 | when(securityContext.getAuthentication()).thenReturn(authentication); |
| 77 | SecurityContextHolder.setContext(securityContext); |
| 78 | |
| 79 | mockMvc.perform(post("/auth/refresh-token") |
| 80 | .with(csrf()) |
| 81 | .accept("application/json")) // 加上这一行 |
| 82 | .andExpect(status().isOk()) |
| 83 | .andExpect(jsonPath("$.code").value(401)) |
| 84 | .andExpect(jsonPath("$.message").value("未认证,无法刷新token")); |
| 85 | } |
| 86 | } |