From 9a644b56406b2f42e0012863420928edebc68d68 Mon Sep 17 00:00:00 2001 From: Marcel Raddatz Date: Fri, 10 Apr 2026 22:29:50 +0200 Subject: [PATCH] fix(test): update AuthControllerTest to verify authenticateInSession delegation After extracting authenticateInSession to AuthService, the mock doesn't populate the session. Replace session-attribute assertions with verify() calls that confirm the controller correctly delegates to authService. Co-Authored-By: Claude Sonnet 4.6 --- .../recipeapp/auth/AuthControllerTest.java | 22 ++++++++----------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/backend/src/test/java/com/recipeapp/auth/AuthControllerTest.java b/backend/src/test/java/com/recipeapp/auth/AuthControllerTest.java index f82a582..882bafc 100644 --- a/backend/src/test/java/com/recipeapp/auth/AuthControllerTest.java +++ b/backend/src/test/java/com/recipeapp/auth/AuthControllerTest.java @@ -10,19 +10,17 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.http.MediaType; -import org.springframework.security.web.context.HttpSessionSecurityContextRepository; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import java.util.UUID; -import static org.hamcrest.Matchers.notNullValue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @ExtendWith(MockitoExtension.class) @@ -100,7 +98,7 @@ class AuthControllerTest { } @Test - void signupShouldStoreSecurityContextInSession() throws Exception { + void signupShouldDelegateSessionCreationToAuthService() throws Exception { var request = new SignupRequest("sarah@example.com", "s3cure!Pass", "Sarah"); var response = UserResponse.basic(UUID.randomUUID(), "sarah@example.com", "Sarah"); @@ -109,14 +107,13 @@ class AuthControllerTest { mockMvc.perform(post("/v1/auth/signup") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(request))) - .andExpect(status().isCreated()) - .andExpect(request().sessionAttribute( - HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, - notNullValue())); + .andExpect(status().isCreated()); + + verify(authService).authenticateInSession(eq("sarah@example.com"), eq("user"), any()); } @Test - void loginShouldStoreSecurityContextInSession() throws Exception { + void loginShouldDelegateSessionCreationToAuthService() throws Exception { var request = new LoginRequest("sarah@example.com", "s3cure!Pass"); var response = UserResponse.withHousehold( UUID.randomUUID(), "sarah@example.com", "Sarah", @@ -127,10 +124,9 @@ class AuthControllerTest { mockMvc.perform(post("/v1/auth/login") .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(request))) - .andExpect(status().isOk()) - .andExpect(request().sessionAttribute( - HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, - notNullValue())); + .andExpect(status().isOk()); + + verify(authService).authenticateInSession(eq("sarah@example.com"), eq("user"), any()); } @Test