Fixed tests
Build, Test & Push Auth Service / quality-check (push) Successful in 21s
Build, Test & Push Auth Service / docker (push) Successful in 1m19s

This commit is contained in:
lobo committed 2026-04-11 12:32:32 +02:00
1 parent e21dea1e18
commit 3f80777e37
3 files changed
+70 -6

No files matched your search

@@ -49,9 +49,9 @@ class LoginControllerTest {
void login_Success() throws Exception { void login_Success() throws Exception {
// Arrange // Arrange
var request = new LoginRequest("test@example.com", "password"); var request = new LoginRequest("test@example.com", "password");
var authenticatedUser = User.builder().email("test@example.com").build(); var authenticatedUser = User.builder().id(815L).email("test@example.com").build();
when(loginService.login(any(User.class))).thenReturn(authenticatedUser); when(loginService.login(any(User.class))).thenReturn(authenticatedUser);
when(jwtService.generateToken("0815", "test@example.com")).thenReturn("mock-token"); when(jwtService.generateToken("815", "test@example.com")).thenReturn("mock-token");
// Act & Assert // Act & Assert
mockMvc.perform(post("/api/auth/login") mockMvc.perform(post("/api/auth/login")
@@ -2,24 +2,38 @@ package auth_service.domain.user.controller;
import auth_service.configuration.security.JwtAuthenticationFilter; import auth_service.configuration.security.JwtAuthenticationFilter;
import auth_service.configuration.security.JwtService; import auth_service.configuration.security.JwtService;
import auth_service.configuration.security.UserPrincipal;
import auth_service.domain.user.controller.dto.request.UserSettingsRequest;
import auth_service.domain.user.exception.UserNotFoundException; import auth_service.domain.user.exception.UserNotFoundException;
import auth_service.domain.user.model.User; import auth_service.domain.user.model.User;
import auth_service.domain.user.model.UserSettings;
import auth_service.domain.user.service.UserService; import auth_service.domain.user.service.UserService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters;
import org.springframework.boot.test.json.JacksonTester;
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc; import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest; import org.springframework.boot.webmvc.test.autoconfigure.WebMvcTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.test.context.bean.override.mockito.MockitoBean; import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import java.util.Collections;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(UserController.class) @WebMvcTest(UserController.class)
@AutoConfigureMockMvc(addFilters = false) @AutoConfigureMockMvc(addFilters = false)
@AutoConfigureJsonTesters
class UserControllerTest { class UserControllerTest {
@Autowired @Autowired
@@ -34,16 +48,28 @@ class UserControllerTest {
@MockitoBean @MockitoBean
private JwtAuthenticationFilter jwtAuthenticationFilter; private JwtAuthenticationFilter jwtAuthenticationFilter;
@Autowired
private JacksonTester<UserSettingsRequest> userSettingsRequestJson;
@AfterEach
void tearDown() {
SecurityContextHolder.clearContext();
}
@Test @Test
void getCurrentUserV1_Success() throws Exception { void getCurrentUserV1_Success() throws Exception {
// Arrange // Arrange
var email = "test@example.com"; var email = "test@example.com";
var user = User.builder().email(email).build(); var settings = UserSettings.builder().language("en").theme("dark").build();
var user = User.builder().email(email).settings(settings).build();
var principal = new UserPrincipal("1", email);
SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(principal, null, Collections.emptyList())
);
when(userService.getUserByEmail(email)).thenReturn(user); when(userService.getUserByEmail(email)).thenReturn(user);
// Act & Assert // Act & Assert
mockMvc.perform(get("/api/users/me") mockMvc.perform(get("/api/users/me")
.principal(() -> email)
.accept(MediaType.APPLICATION_JSON)) .accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$.email").value(email)); .andExpect(jsonPath("$.email").value(email));
@@ -53,12 +79,34 @@ class UserControllerTest {
void getCurrentUserV1_NotFound() throws Exception { void getCurrentUserV1_NotFound() throws Exception {
// Arrange // Arrange
var email = "notfound@example.com"; var email = "notfound@example.com";
var principal = new UserPrincipal("1", email);
SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(principal, null, Collections.emptyList())
);
when(userService.getUserByEmail(email)).thenThrow(new UserNotFoundException("User not found")); when(userService.getUserByEmail(email)).thenThrow(new UserNotFoundException("User not found"));
// Act & Assert // Act & Assert
mockMvc.perform(get("/api/users/me") mockMvc.perform(get("/api/users/me")
.principal(() -> email)
.accept(MediaType.APPLICATION_JSON)) .accept(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound()); .andExpect(status().isNotFound());
} }
@Test
void updateUserSettings_Success() throws Exception {
// Arrange
var email = "test@example.com";
var principal = new UserPrincipal("1", email);
SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken(principal, null, Collections.emptyList())
);
var request = new UserSettingsRequest(true, "en", 123456789L);
// Act & Assert
mockMvc.perform(put("/api/users/settings")
.contentType(MediaType.APPLICATION_JSON)
.content(userSettingsRequestJson.write(request).getJson()))
.andExpect(status().isOk());
verify(userService).updateUserSettings(eq(1L), eq(123456789L), any());
}
} }
@@ -2,6 +2,7 @@ package auth_service.domain.user.service;
import auth_service.domain.user.exception.UserNotFoundException; import auth_service.domain.user.exception.UserNotFoundException;
import auth_service.domain.user.model.User; import auth_service.domain.user.model.User;
import auth_service.domain.user.model.UserSettings;
import auth_service.domain.user.persistence.UserRepository; import auth_service.domain.user.persistence.UserRepository;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
@@ -13,7 +14,7 @@ import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.*;
/** /**
* Tests for UserService. * Tests for UserService.
@@ -50,4 +51,19 @@ class UserServiceTest {
// Act & Assert // Act & Assert
assertThrows(UserNotFoundException.class, () -> userService.getUserByEmail(email)); assertThrows(UserNotFoundException.class, () -> userService.getUserByEmail(email));
} }
@Test
void updateUserSettings_Success() {
// Arrange
long userId = 1L;
long steamId = 123456789L;
var userSettings = UserSettings.builder().language("en").build();
when(userRepository.updateSettings(userId, steamId, userSettings)).thenReturn(true);
// Act
userService.updateUserSettings(userId, steamId, userSettings);
// Assert
verify(userRepository, times(1)).updateSettings(userId, steamId, userSettings);
}
} }