-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #227 from bounswe/issue#226-unit-test-for-authenti…
…cation-service Issue#226 unit test for authentication service
- Loading branch information
Showing
3 changed files
with
220 additions
and
13 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
186 changes: 186 additions & 0 deletions
186
backend/demo-group7/src/test/java/com/group7/demo/AuthenticationServiceTest.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,186 @@ | ||
package com.group7.demo; | ||
|
||
import com.group7.demo.dtos.LoginRequest; | ||
import com.group7.demo.dtos.LoginResponse; | ||
import com.group7.demo.dtos.RegisterRequest; | ||
import com.group7.demo.models.Role; | ||
import com.group7.demo.models.User; | ||
import com.group7.demo.repository.UserRepository; | ||
import com.group7.demo.services.AuthenticationService; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
class AuthenticationServiceTest { | ||
|
||
@Mock | ||
private UserRepository userRepository; | ||
|
||
@Mock | ||
private BCryptPasswordEncoder passwordEncoder; | ||
|
||
@InjectMocks | ||
private AuthenticationService authenticationService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@Test | ||
void testRegister_Success() throws Exception { | ||
RegisterRequest request = new RegisterRequest("John Doe", "[email protected]", "john_doe", "123456", Role.TRAINER); | ||
when(userRepository.findByUsername(request.getUsername())).thenReturn(Optional.empty()); | ||
when(userRepository.findByEmail(request.getEmail())).thenReturn(Optional.empty()); | ||
when(passwordEncoder.encode(request.getPassword())).thenReturn("encodedPassword"); | ||
|
||
User savedUser = User.builder() | ||
.id(1L) | ||
.fullName(request.getFullName()) | ||
.username(request.getUsername()) | ||
.email(request.getEmail()) | ||
.password("encodedPassword") | ||
.role(request.getRole()) | ||
.sessionToken("mockedSessionToken") | ||
.build(); | ||
when(userRepository.save(any(User.class))).thenReturn(savedUser); | ||
|
||
User result = authenticationService.register(request); | ||
|
||
assertNotNull(result); | ||
assertEquals("john_doe", result.getUsername()); | ||
verify(userRepository, times(1)).save(any(User.class)); | ||
} | ||
|
||
@Test | ||
void testRegister_UsernameExists() { | ||
RegisterRequest request = new RegisterRequest("John Doe", "[email protected]", "john_doe", "123456", Role.TRAINER); | ||
when(userRepository.findByUsername(request.getUsername())).thenReturn(Optional.of(new User())); | ||
|
||
Exception exception = assertThrows(Exception.class, () -> authenticationService.register(request)); | ||
|
||
assertEquals("Username already exists", exception.getMessage()); | ||
verify(userRepository, never()).save(any(User.class)); | ||
} | ||
|
||
@Test | ||
void testLogin_Success() throws Exception { | ||
LoginRequest request = new LoginRequest("john_doe", "123456"); | ||
User mockUser = User.builder() | ||
.id(1L) | ||
.username(request.getUsername()) | ||
.password("encodedPassword") | ||
.sessionToken("mockedSessionToken") | ||
.build(); | ||
|
||
when(userRepository.findByUsername(request.getUsername())).thenReturn(Optional.of(mockUser)); | ||
when(passwordEncoder.matches(request.getPassword(), mockUser.getPassword())).thenReturn(true); | ||
|
||
LoginResponse response = authenticationService.login(request); | ||
|
||
assertNotNull(response); | ||
assertEquals("mockedSessionToken", response.getSessionToken()); | ||
assertEquals("john_doe", response.getUsername()); | ||
} | ||
|
||
@Test | ||
void testLogin_InvalidCredentials() { | ||
LoginRequest request = new LoginRequest("john_doe", "wrongPassword"); | ||
User mockUser = User.builder() | ||
.username(request.getUsername()) | ||
.password("encodedPassword") | ||
.build(); | ||
|
||
when(userRepository.findByUsername(request.getUsername())).thenReturn(Optional.of(mockUser)); | ||
when(passwordEncoder.matches(request.getPassword(), mockUser.getPassword())).thenReturn(false); | ||
|
||
Exception exception = assertThrows(Exception.class, () -> authenticationService.login(request)); | ||
|
||
assertEquals("Invalid credentials", exception.getMessage()); | ||
} | ||
|
||
@Test | ||
void testLogout_Success() { | ||
String sessionToken = "existingToken"; | ||
User mockUser = User.builder() | ||
.username("john_doe") | ||
.sessionToken(sessionToken) | ||
.build(); | ||
|
||
when(userRepository.findBySessionToken(sessionToken)).thenReturn(Optional.of(mockUser)); | ||
|
||
authenticationService.logout(sessionToken); | ||
|
||
assertNull(mockUser.getSessionToken()); | ||
verify(userRepository, times(1)).save(mockUser); | ||
} | ||
|
||
@Test | ||
void testGetAuthenticatedUser_LoggedInUser() { | ||
String sessionToken = "validToken"; | ||
User mockUser = User.builder() | ||
.username("john_doe") | ||
.email("[email protected]") | ||
.role(Role.TRAINER) | ||
.sessionToken(sessionToken) | ||
.build(); | ||
|
||
when(userRepository.findBySessionToken(sessionToken)).thenReturn(Optional.of(mockUser)); | ||
|
||
HttpServletRequest request = mock(HttpServletRequest.class); | ||
when(request.getHeader("x-session-token")).thenReturn(sessionToken); | ||
|
||
LoginResponse response = authenticationService.getAuthenticatedUser(request); | ||
|
||
assertNotNull(response); | ||
assertEquals("john_doe", response.getUsername()); | ||
} | ||
|
||
@Test | ||
void testGetAuthenticatedUser_GuestUser() { | ||
HttpServletRequest request = mock(HttpServletRequest.class); | ||
when(request.getHeader("x-session-token")).thenReturn(null); | ||
LoginResponse response = authenticationService.getAuthenticatedUser(request); | ||
assertNull(response); | ||
} | ||
|
||
@Test | ||
void testGetAuthenticatedUserInternal_LoggedInUser() { | ||
String sessionToken = "validToken"; | ||
User mockUser = User.builder() | ||
.username("john_doe") | ||
.email("[email protected]") | ||
.role(Role.TRAINER) | ||
.sessionToken(sessionToken) | ||
.build(); | ||
|
||
when(userRepository.findBySessionToken(sessionToken)).thenReturn(Optional.of(mockUser)); | ||
|
||
HttpServletRequest request = mock(HttpServletRequest.class); | ||
when(request.getHeader("x-session-token")).thenReturn(sessionToken); | ||
|
||
User user = authenticationService.getAuthenticatedUserInternal(request); | ||
|
||
assertNotNull(user); | ||
assertEquals(mockUser.getUsername(), user.getUsername()); | ||
} | ||
|
||
@Test | ||
void testGetAuthenticatedUserInternal_GuestUser() { | ||
HttpServletRequest request = mock(HttpServletRequest.class); | ||
when(request.getHeader("x-session-token")).thenReturn(null); | ||
User user = authenticationService.getAuthenticatedUserInternal(request); | ||
assertNull(user); | ||
} | ||
|
||
} |
13 changes: 0 additions & 13 deletions
13
backend/demo-group7/src/test/java/com/group7/demo/DemoApplicationTests.java
This file was deleted.
Oops, something went wrong.