AuthControllerTest.java
4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package com.example.erp.module.usr;
import com.example.erp.common.exception.BizException;
import com.example.erp.module.usr.dto.LoginReqDTO;
import com.example.erp.module.usr.service.AuthService;
import com.example.erp.module.usr.vo.BrandVO;
import com.example.erp.module.usr.vo.LoginVO;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import java.util.List;
import java.util.Map;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import com.example.erp.config.SecurityConfig;
import com.example.erp.config.JwtAuthenticationFilter;
import com.example.erp.config.BeanConfig;
import com.example.erp.common.util.JwtUtil;
import com.example.erp.config.JwtProperties;
import com.example.erp.module.usr.controller.AuthController;
@WebMvcTest(controllers = AuthController.class)
@Import({SecurityConfig.class, JwtAuthenticationFilter.class, BeanConfig.class, JwtUtil.class, JwtProperties.class})
class AuthControllerTest {
@Autowired private MockMvc mockMvc;
@Autowired private ObjectMapper objectMapper;
@MockBean private AuthService authService;
@Test
void login_wrongPassword_returns40100() throws Exception {
when(authService.login(any())).thenThrow(new BizException(40100, "用户名或密码错误"));
Map<String, String> body = Map.of("brandNo", "STD", "username", "admin", "password", "wrong");
mockMvc.perform(post("/api/auth/login")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(body)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(40100))
.andExpect(jsonPath("$.message").value("用户名或密码错误"));
}
@Test
void login_validCredentials_returns200AndTokens() throws Exception {
LoginVO.UserInfoVO userInfo = new LoginVO.UserInfoVO();
userInfo.setUserId("u1");
userInfo.setUsername("admin");
userInfo.setUserType("普通用户");
userInfo.setLanguage("中文");
userInfo.setBrandId("b1");
LoginVO loginVO = new LoginVO();
loginVO.setAccessToken("access-token");
loginVO.setRefreshToken("refresh-token");
loginVO.setExpiresIn(86400L);
loginVO.setUserInfo(userInfo);
when(authService.login(any())).thenReturn(loginVO);
Map<String, String> body = Map.of("brandNo", "STD", "username", "admin", "password", "666666");
mockMvc.perform(post("/api/auth/login")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(body)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200))
.andExpect(jsonPath("$.data.accessToken").value("access-token"))
.andExpect(jsonPath("$.data.userInfo.userId").value("u1"));
}
@Test
void refresh_withInvalidToken_returns40103() throws Exception {
when(authService.refresh(anyString())).thenThrow(new BizException(40103, "Refresh Token 已失效,请重新登录"));
Map<String, String> body = Map.of("refreshToken", "invalid-token");
mockMvc.perform(post("/api/auth/refresh")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(body)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(40103));
}
@Test
void getBrands_returns200AndList() throws Exception {
BrandVO b = new BrandVO();
b.setSNo("STD");
b.setSName("标准版");
when(authService.getBrands()).thenReturn(List.of(b));
mockMvc.perform(get("/api/auth/brands"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value(200))
.andExpect(jsonPath("$.data[0].sNo").value("STD"))
.andExpect(jsonPath("$.data[0].sName").value("标准版"));
}
}