Public Access
Fixed tests
This commit is contained in:
1 parent
03d5a011ae
commit
14a4ccfb87
3 files changed
+70
-6
No files matched your search
@@ -49,9 +49,9 @@ class LoginControllerTest {
|
||||
void login_Success() throws Exception {
|
||||
// Arrange
|
||||
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(jwtService.generateToken("0815", "test@example.com")).thenReturn("mock-token");
|
||||
when(jwtService.generateToken("815", "test@example.com")).thenReturn("mock-token");
|
||||
|
||||
// Act & Assert
|
||||
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.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.model.User;
|
||||
import auth_service.domain.user.model.UserSettings;
|
||||
import auth_service.domain.user.service.UserService;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
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.WebMvcTest;
|
||||
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.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.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.status;
|
||||
|
||||
@WebMvcTest(UserController.class)
|
||||
@AutoConfigureMockMvc(addFilters = false)
|
||||
@AutoConfigureJsonTesters
|
||||
class UserControllerTest {
|
||||
|
||||
@Autowired
|
||||
@@ -34,16 +48,28 @@ class UserControllerTest {
|
||||
@MockitoBean
|
||||
private JwtAuthenticationFilter jwtAuthenticationFilter;
|
||||
|
||||
@Autowired
|
||||
private JacksonTester<UserSettingsRequest> userSettingsRequestJson;
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
SecurityContextHolder.clearContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getCurrentUserV1_Success() throws Exception {
|
||||
// Arrange
|
||||
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);
|
||||
|
||||
// Act & Assert
|
||||
mockMvc.perform(get("/api/users/me")
|
||||
.principal(() -> email)
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.email").value(email));
|
||||
@@ -53,12 +79,34 @@ class UserControllerTest {
|
||||
void getCurrentUserV1_NotFound() throws Exception {
|
||||
// Arrange
|
||||
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"));
|
||||
|
||||
// Act & Assert
|
||||
mockMvc.perform(get("/api/users/me")
|
||||
.principal(() -> email)
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
.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.model.User;
|
||||
import auth_service.domain.user.model.UserSettings;
|
||||
import auth_service.domain.user.persistence.UserRepository;
|
||||
import org.junit.jupiter.api.Test;
|
||||
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.assertThrows;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* Tests for UserService.
|
||||
@@ -50,4 +51,19 @@ class UserServiceTest {
|
||||
// Act & Assert
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user