/
Chebarik
/
PackageManagement
Обзор
Документация
Войти
/
Chebarik
/
PackageManagement
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/test/java/com/packagemanagement/presentation/controller/AuthControllerTest.java
160 строк
7 KB
Николай Мехоношин
Implemented tests for non functional requirements
14 апр 2026, 18:50
14 апр 2026, 18:50
9179c8b
Код
Авторство
О чём код?
package com.packagemanagement.presentation.controller; import com.fasterxml.jackson.databind.ObjectMapper; import com.packagemanagement.application.service.AuthenticationService; import com.packagemanagement.presentation.exception.GlobalExceptionHandler; import com.packagemanagement.presentation.request.LoginRequest; import com.packagemanagement.presentation.request.RegisterRequest; import com.packagemanagement.presentation.response.TokenResponse; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.http.MediaType; import org.springframework.security.authentication.BadCredentialsException; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; @ExtendWith(MockitoExtension.class) @DisplayName("AuthController") class AuthControllerTest { private MockMvc mockMvc; private ObjectMapper objectMapper; @Mock private AuthenticationService authenticationService; @InjectMocks private AuthController authController; @BeforeEach void setUp() { objectMapper = new ObjectMapper(); mockMvc = MockMvcBuilders.standaloneSetup(authController) .setControllerAdvice(new GlobalExceptionHandler()) .build(); } @Nested @DisplayName("POST /api/auth/login") class Login { @Test @DisplayName("should return 200 with token for valid credentials") void shouldReturnTokenForValidCredentials() throws Exception { LoginRequest request = new LoginRequest("john", "password123"); TokenResponse tokenResponse = new TokenResponse("jwt-token", "Bearer", 3600); when(authenticationService.authenticate(eq("john"), eq("password123"), anyString())) .thenReturn(tokenResponse); mockMvc.perform(post("/api/auth/login") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(request))) .andExpect(status().isOk()) .andExpect(jsonPath("$.success").value(true)) .andExpect(jsonPath("$.data.accessToken").value("jwt-token")) .andExpect(jsonPath("$.data.tokenType").value("Bearer")) .andExpect(jsonPath("$.data.expiresIn").value(3600)); } @Test @DisplayName("should return 401 for invalid credentials") void shouldReturn401ForInvalidCredentials() throws Exception { LoginRequest request = new LoginRequest("john", "wrong"); when(authenticationService.authenticate(eq("john"), eq("wrong"), anyString())) .thenThrow(new BadCredentialsException("Invalid username or password")); mockMvc.perform(post("/api/auth/login") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(request))) .andExpect(status().isUnauthorized()) .andExpect(jsonPath("$.success").value(false)); } @Test @DisplayName("should return 400 for blank username") void shouldReturn400ForBlankUsername() throws Exception { mockMvc.perform(post("/api/auth/login") .contentType(MediaType.APPLICATION_JSON) .content("{\"username\":\"\",\"password\":\"password123\"}")) .andExpect(status().isBadRequest()); } @Test @DisplayName("should return 400 for blank password") void shouldReturn400ForBlankPassword() throws Exception { mockMvc.perform(post("/api/auth/login") .contentType(MediaType.APPLICATION_JSON) .content("{\"username\":\"john\",\"password\":\"\"}")) .andExpect(status().isBadRequest()); } } @Nested @DisplayName("POST /api/auth/register") class Register { @Test @DisplayName("should return 201 with token for valid registration") void shouldReturnTokenForValidRegistration() throws Exception { RegisterRequest request = new RegisterRequest("newuser", "Password1!", "new@test.com"); TokenResponse tokenResponse = new TokenResponse("new-jwt", "Bearer", 3600); when(authenticationService.register(eq("newuser"), eq("Password1!"), eq("new@test.com"), anyString())) .thenReturn(tokenResponse); mockMvc.perform(post("/api/auth/register") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(request))) .andExpect(status().isCreated()) .andExpect(jsonPath("$.success").value(true)) .andExpect(jsonPath("$.data.accessToken").value("new-jwt")) .andExpect(jsonPath("$.data.tokenType").value("Bearer")); } @Test @DisplayName("should return 400 for duplicate username") void shouldReturn400ForDuplicateUsername() throws Exception { RegisterRequest request = new RegisterRequest("taken", "Password1!", "t@t.com"); when(authenticationService.register(eq("taken"), anyString(), anyString(), anyString())) .thenThrow(new IllegalArgumentException("Username 'taken' is already taken")); mockMvc.perform(post("/api/auth/register") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(request))) .andExpect(status().isBadRequest()) .andExpect(jsonPath("$.success").value(false)); } @Test @DisplayName("should return 400 for short password") void shouldReturn400ForShortPassword() throws Exception { mockMvc.perform(post("/api/auth/register") .contentType(MediaType.APPLICATION_JSON) .content("{\"username\":\"validuser\",\"password\":\"short\",\"email\":\"e@e.com\"}")) .andExpect(status().isBadRequest()); } @Test @DisplayName("should return 400 for short username") void shouldReturn400ForShortUsername() throws Exception { mockMvc.perform(post("/api/auth/register") .contentType(MediaType.APPLICATION_JSON) .content("{\"username\":\"ab\",\"password\":\"Password1!\",\"email\":\"e@e.com\"}")) .andExpect(status().isBadRequest()); } } }