Public Access
Finished registration Process and added tests
This commit is contained in:
1 parent
5dcdf7bd61
commit
afbe950ba4
14 files changed
+274
-42
No files matched your search
+93
@@ -0,0 +1,93 @@
|
||||
package auth_service.domain.registration.controller;
|
||||
|
||||
import auth_service.configuration.security.JwtAuthenticationFilter;
|
||||
import auth_service.configuration.security.JwtService;
|
||||
import auth_service.domain.registration.controller.dto.request.RegistrationRequest;
|
||||
import auth_service.domain.registration.exception.EmailAlreadyInUseException;
|
||||
import auth_service.domain.registration.model.Registration;
|
||||
import auth_service.domain.registration.service.RegistrationService;
|
||||
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.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
/**
|
||||
* Tests for RegistrationController.
|
||||
*/
|
||||
@WebMvcTest(RegistrationController.class)
|
||||
@AutoConfigureMockMvc(addFilters = false) // Skip security filters for simple controller test
|
||||
@AutoConfigureJsonTesters
|
||||
class RegistrationControllerTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@MockitoBean
|
||||
private RegistrationService registrationService;
|
||||
|
||||
// Needed because they are likely used in SecurityConfig and required by the ApplicationContext
|
||||
@MockitoBean
|
||||
private JwtService jwtService;
|
||||
|
||||
@MockitoBean
|
||||
private JwtAuthenticationFilter jwtAuthenticationFilter;
|
||||
|
||||
@Autowired
|
||||
private JacksonTester<RegistrationRequest> registrationRequestJson;
|
||||
|
||||
@Test
|
||||
void register_Success() throws Exception {
|
||||
// Arrange
|
||||
var request = RegistrationRequest.builder()
|
||||
.email("test@example.com")
|
||||
.password("SuperSecret123!")
|
||||
.inviteCode("test-invite-code")
|
||||
.steamId(314159L)
|
||||
.build();
|
||||
|
||||
long expectedUserId = 42L;
|
||||
|
||||
// Mock the service call, ignoring the exact static mapping output
|
||||
when(registrationService.register(any(Registration.class))).thenReturn(expectedUserId);
|
||||
|
||||
// Act & Assert
|
||||
mockMvc.perform(post("/api/registration/new")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(registrationRequestJson.write(request).getJson()))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.userId").value(expectedUserId));
|
||||
}
|
||||
|
||||
@Test
|
||||
void register_Failure_EmailAlreadyInUse() throws Exception {
|
||||
// Arrange
|
||||
var request = RegistrationRequest.builder()
|
||||
.email("test@example.com")
|
||||
.password("SuperSecret123!")
|
||||
.inviteCode("test-invite-code")
|
||||
.steamId(314159L)
|
||||
.build();
|
||||
|
||||
when(registrationService.register(any(Registration.class)))
|
||||
.thenThrow(new EmailAlreadyInUseException("The provided email address is already in use."));
|
||||
|
||||
// Act & Assert
|
||||
// Depending on your GlobalExceptionHandler, this could also be 400 or 409.
|
||||
// Assuming standard behavior without advice or mapping to 400 for now.
|
||||
mockMvc.perform(post("/api/registration/new")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(registrationRequestJson.write(request).getJson()))
|
||||
.andExpect(status().isBadRequest()); // Adjust status based on your @ExceptionHandler
|
||||
}
|
||||
}
|
||||
+30
-24
@@ -3,7 +3,6 @@ package auth_service.domain.registration.service;
|
||||
import auth_service.domain.registration.exception.EmailAlreadyInUseException;
|
||||
import auth_service.domain.registration.exception.InvalidInvitationCodeException;
|
||||
import auth_service.domain.registration.exception.SteamIdAlreadyRegisteredException;
|
||||
import auth_service.domain.registration.model.RegisteredUser;
|
||||
import auth_service.domain.registration.model.Registration;
|
||||
import auth_service.domain.registration.persistence.RegisteredUserRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -14,9 +13,14 @@ import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* Tests for RegistrationService.
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class RegistrationServiceTest {
|
||||
|
||||
@@ -39,60 +43,62 @@ class RegistrationServiceTest {
|
||||
validRegistration = Registration.builder()
|
||||
.email("test@example.com")
|
||||
.rawPassword("SuperSecret123!")
|
||||
.encryptedPassword("")
|
||||
.inviteCode("TBP-INVITE-2026")
|
||||
.inviteCode("test-invite-code")
|
||||
.steamId(314159L)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void register_withValidData_shouldReturnRegisteredUserAndMarkCodeAsUsed() {
|
||||
void register_Success() {
|
||||
// Arrange
|
||||
when(codeValidationService.isCodeValid("TBP-INVITE-2026")).thenReturn(true);
|
||||
when(registeredUserRepository.existsByEmail("test@example.com")).thenReturn(false);
|
||||
when(passwordEncoder.encode("SuperSecret123!")).thenReturn("hashedPasswordXYZ");
|
||||
|
||||
RegisteredUser expectedUser = new RegisteredUser("uuid-1234", "test@example.com");
|
||||
when(registeredUserRepository.save(validRegistration)).thenReturn(expectedUser);
|
||||
long expectedUserId = 42L;
|
||||
when(codeValidationService.isCodeValid(validRegistration.getInviteCode())).thenReturn(true);
|
||||
when(registeredUserRepository.existsByEmail(validRegistration.getEmail())).thenReturn(false);
|
||||
when(registeredUserRepository.existsBySteamId(validRegistration.getSteamId())).thenReturn(false);
|
||||
when(passwordEncoder.encode(validRegistration.getRawPassword())).thenReturn("hashedPasswordXYZ");
|
||||
when(registeredUserRepository.save(any(Registration.class))).thenReturn(expectedUserId);
|
||||
|
||||
// Act
|
||||
RegisteredUser result = registrationService.register(validRegistration);
|
||||
long result = registrationService.register(validRegistration);
|
||||
|
||||
// Assert
|
||||
assertNotNull(result);
|
||||
assertEquals("uuid-1234", result.id());
|
||||
assertEquals("test@example.com", result.email());
|
||||
assertEquals(expectedUserId, result);
|
||||
verify(passwordEncoder).encode("SuperSecret123!");
|
||||
verify(registeredUserRepository).save(validRegistration);
|
||||
assertEquals("hashedPasswordXYZ", validRegistration.getEncryptedPassword());
|
||||
}
|
||||
|
||||
@Test
|
||||
void register_withInvalidInviteCode_shouldThrowException() {
|
||||
void register_InvalidInviteCode() {
|
||||
// Arrange
|
||||
when(codeValidationService.isCodeValid("TBP-INVITE-2026")).thenReturn(false);
|
||||
when(codeValidationService.isCodeValid(validRegistration.getInviteCode())).thenReturn(false);
|
||||
|
||||
// Act & Assert
|
||||
assertThrows(InvalidInvitationCodeException.class, () -> registrationService.register(validRegistration));
|
||||
|
||||
verify(registeredUserRepository, never()).existsByEmail(anyString());
|
||||
verify(registeredUserRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void register_withExistingEmail_shouldThrowException() {
|
||||
void register_EmailAlreadyInUse() {
|
||||
// Arrange
|
||||
when(codeValidationService.isCodeValid("TBP-INVITE-2026")).thenReturn(true);
|
||||
when(registeredUserRepository.existsByEmail("test@example.com")).thenReturn(true);
|
||||
when(codeValidationService.isCodeValid(validRegistration.getInviteCode())).thenReturn(true);
|
||||
when(registeredUserRepository.existsByEmail(validRegistration.getEmail())).thenReturn(true);
|
||||
|
||||
// Act & Assert
|
||||
assertThrows(EmailAlreadyInUseException.class, () -> registrationService.register(validRegistration));
|
||||
|
||||
verify(registeredUserRepository, never()).existsBySteamId(anyLong());
|
||||
verify(registeredUserRepository, never()).save(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void register_withExistingSteamId_shouldThrowException() {
|
||||
void register_SteamIdAlreadyRegistered() {
|
||||
// Arrange
|
||||
when(codeValidationService.isCodeValid("TBP-INVITE-2026")).thenReturn(true);
|
||||
when(registeredUserRepository.existsByEmail("test@example.com")).thenReturn(false);
|
||||
when(registeredUserRepository.existsBySteamId(314159L)).thenReturn(true);
|
||||
when(codeValidationService.isCodeValid(validRegistration.getInviteCode())).thenReturn(true);
|
||||
when(registeredUserRepository.existsByEmail(validRegistration.getEmail())).thenReturn(false);
|
||||
when(registeredUserRepository.existsBySteamId(validRegistration.getSteamId())).thenReturn(true);
|
||||
|
||||
// Act & Assert
|
||||
assertThrows(SteamIdAlreadyRegisteredException.class, () -> registrationService.register(validRegistration));
|
||||
|
||||
Reference in new issue
Block a user