-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mnhock
authored and
mnhock
committed
Sep 28, 2023
1 parent
3b0be2d
commit 9538b2f
Showing
6 changed files
with
155 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
.../java/com/enofex/naikan/security/HttpStatusReturningAuthenticationFailureHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.enofex.naikan.security; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.io.PrintWriter; | ||
import java.nio.charset.StandardCharsets; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.springframework.security.core.AuthenticationException; | ||
|
||
class HttpStatusReturningAuthenticationFailureHandlerTest { | ||
|
||
private HttpStatusReturningAuthenticationFailureHandler failureHandler; | ||
private ObjectMapper objectMapper; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
this.failureHandler = new HttpStatusReturningAuthenticationFailureHandler(); | ||
this.objectMapper = new ObjectMapper(); | ||
} | ||
|
||
@Test | ||
void shouldReturnCorrectValuesAuthenticationFailure() throws Exception { | ||
HttpServletRequest request = mock(HttpServletRequest.class); | ||
HttpServletResponse response = mock(HttpServletResponse.class); | ||
AuthenticationException exception = mock(AuthenticationException.class); | ||
PrintWriter writer = mock(PrintWriter.class); | ||
|
||
when(response.getWriter()).thenReturn(writer); | ||
|
||
this.failureHandler.onAuthenticationFailure(request, response, exception); | ||
|
||
verify(response).setCharacterEncoding(StandardCharsets.UTF_8.name()); | ||
verify(response).setStatus(HttpServletResponse.SC_UNAUTHORIZED); | ||
verify(writer).write(objectMapper.writeValueAsString(Mockito.any())); | ||
verify(writer).flush(); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
.../java/com/enofex/naikan/security/HttpStatusReturningAuthenticationSuccessHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.enofex.naikan.security; | ||
|
||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.enofex.naikan.administration.user.AdministrationUserService; | ||
import com.enofex.naikan.administration.user.User; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.util.List; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.springframework.security.core.Authentication; | ||
|
||
class HttpStatusReturningAuthenticationSuccessHandlerTest { | ||
|
||
private HttpStatusReturningAuthenticationSuccessHandler successHandler; | ||
private AdministrationUserService userService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
this.userService = mock(AdministrationUserService.class); | ||
this.successHandler = new HttpStatusReturningAuthenticationSuccessHandler(this.userService); | ||
} | ||
|
||
@Test | ||
void shouldAuthenticateFirstUserAndShouldBeAdmin() { | ||
HttpServletRequest request = mock(HttpServletRequest.class); | ||
HttpServletResponse response = mock(HttpServletResponse.class); | ||
Authentication authentication = mock(Authentication.class); | ||
|
||
when(this.userService.count()).thenReturn(0L); | ||
when(authentication.getName()).thenReturn("firstUser"); | ||
|
||
this.successHandler.onAuthenticationSuccess(request, response, authentication); | ||
|
||
verify(this.userService).save(new User("firstUser", List.of("ROLE_ADMIN"))); | ||
verify(response).setStatus(HttpServletResponse.SC_OK); | ||
} | ||
|
||
@Test | ||
void shouldnAuthenticateIfUserNotFound() { | ||
HttpServletRequest request = mock(HttpServletRequest.class); | ||
HttpServletResponse response = mock(HttpServletResponse.class); | ||
Authentication authentication = mock(Authentication.class); | ||
|
||
when(this.userService.count()).thenReturn(1L); | ||
when(authentication.getName()).thenReturn("newUser"); | ||
when(this.userService.findByName("newUser")).thenReturn(null); | ||
|
||
this.successHandler.onAuthenticationSuccess(request, response, authentication); | ||
|
||
verify(this.userService).save(new User("newUser")); | ||
verify(response).setStatus(HttpServletResponse.SC_OK); | ||
} | ||
|
||
@Test | ||
void shouldnAuthenticateIfUserAlreadyExists() { | ||
HttpServletRequest request = mock(HttpServletRequest.class); | ||
HttpServletResponse response = mock(HttpServletResponse.class); | ||
Authentication authentication = mock(Authentication.class); | ||
|
||
when(this.userService.count()).thenReturn(1L); | ||
when(authentication.getName()).thenReturn("existingUser"); | ||
when(this.userService.findByName("existingUser")).thenReturn(new User("existingUser")); | ||
|
||
this.successHandler.onAuthenticationSuccess(request, response, authentication); | ||
|
||
verify(this.userService, never()).save(Mockito.any(User.class)); | ||
verify(response).setStatus(HttpServletResponse.SC_OK); | ||
} | ||
} |