blob: 7dbf524ef86e48f521cf4ff99054946924da3bf7 [file] [log] [blame]
22301071fa22ac62025-05-24 21:38:17 +08001package com.example.g8backend.service;
2
3import com.example.g8backend.controller.AuthController;
4import com.example.g8backend.util.JwtUtil;
5import org.junit.jupiter.api.BeforeEach;
6import org.junit.jupiter.api.DisplayName;
7import org.junit.jupiter.api.Test;
8import org.springframework.beans.factory.annotation.Autowired;
9import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
10import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
11import org.springframework.boot.test.mock.mockito.MockBean;
12import org.springframework.security.core.Authentication;
13import org.springframework.security.core.context.SecurityContext;
14import org.springframework.security.core.context.SecurityContextHolder;
15import org.springframework.test.web.servlet.MockMvc;
16
17import static org.mockito.Mockito.*;
18import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
19import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
20import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
21
22@WebMvcTest(AuthController.class)
23@AutoConfigureMockMvc(addFilters = false) // 关键:禁用Security过滤器
24class 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}