AuthControllerTest.java 4.54 KB
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("标准版"));
    }
}