diff --git a/src/test/java/dev/nmarulo/depensaapp/app/authentication/AuthenticationServiceTest.java b/src/test/java/dev/nmarulo/depensaapp/app/authentication/AuthenticationServiceTest.java index f4adad4..a4daa72 100644 --- a/src/test/java/dev/nmarulo/depensaapp/app/authentication/AuthenticationServiceTest.java +++ b/src/test/java/dev/nmarulo/depensaapp/app/authentication/AuthenticationServiceTest.java @@ -1,6 +1,7 @@ package dev.nmarulo.depensaapp.app.authentication; import dev.nmarulo.depensaapp.app.authentication.dtos.AuthenticationRes; +import dev.nmarulo.depensaapp.app.users.User; import dev.nmarulo.depensaapp.app.users.UserRepository; import dev.nmarulo.depensaapp.configuration.AppProperties; import org.junit.jupiter.api.BeforeAll; @@ -15,6 +16,7 @@ import org.springframework.security.oauth2.jwt.*; import java.time.Instant; +import java.time.LocalDateTime; import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -98,4 +100,26 @@ void testLogin() { } } + @Test + void testRegister() { + final var request = authenticationServiceTestUtil.getRegisterAuthenticationReq(); + final var expected = authenticationServiceTestUtil.getRegisterAuthenticationRes(); + final var newUser = authenticationServiceTestUtil.getNewUser(); + final var userOptional = Optional.empty(); + final var localDateTime = newUser.getCreatedAt(); + + when(this.userRepository.findByUsername(eq(request.getUsername()))).thenReturn(userOptional); + when(this.passwordEncoder.encode(eq(request.getPassword()))).thenReturn(newUser.getPassword()); + when(this.userRepository.save(eq(newUser))).thenReturn(newUser); + + try (MockedStatic localDateTimeMockedStatic = mockStatic(LocalDateTime.class)) { + localDateTimeMockedStatic.when(LocalDateTime::now) + .thenReturn(localDateTime); + + final var actual = this.authenticationService.register(request); + + assertEquals(expected, actual); + } + } + } diff --git a/src/test/java/dev/nmarulo/depensaapp/app/authentication/AuthenticationServiceTestUtil.java b/src/test/java/dev/nmarulo/depensaapp/app/authentication/AuthenticationServiceTestUtil.java index 81e8faa..ec63a07 100644 --- a/src/test/java/dev/nmarulo/depensaapp/app/authentication/AuthenticationServiceTestUtil.java +++ b/src/test/java/dev/nmarulo/depensaapp/app/authentication/AuthenticationServiceTestUtil.java @@ -3,6 +3,8 @@ import dev.nmarulo.depensaapp.FakeTestUtil; import dev.nmarulo.depensaapp.app.authentication.dtos.AuthenticationReq; import dev.nmarulo.depensaapp.app.authentication.dtos.AuthenticationRes; +import dev.nmarulo.depensaapp.app.authentication.dtos.RegisterAuthenticationReq; +import dev.nmarulo.depensaapp.app.authentication.dtos.RegisterAuthenticationRes; import dev.nmarulo.depensaapp.app.users.User; import lombok.Getter; @@ -21,6 +23,12 @@ public class AuthenticationServiceTestUtil { private final long plusSecondsJwtExpiresAt; + private final User newUser; + + private final RegisterAuthenticationReq registerAuthenticationReq; + + private final RegisterAuthenticationRes registerAuthenticationRes; + public AuthenticationServiceTestUtil() { this.user = initUser(); @@ -30,6 +38,18 @@ public AuthenticationServiceTestUtil() { this.authenticationRes = initAuthenticationRes(userRes); this.jwtIssuer = FakeTestUtil.randomWord(); this.plusSecondsJwtExpiresAt = FakeTestUtil.randomLong(); + this.newUser = initNewUser(); + this.registerAuthenticationReq = initRegisterAuthenticationReq(newUser); + this.registerAuthenticationRes = new RegisterAuthenticationRes(this.newUser.getUsername()); + } + + private RegisterAuthenticationReq initRegisterAuthenticationReq(User newUser) { + final var registerAuthenticationReq = new RegisterAuthenticationReq(); + + registerAuthenticationReq.setUsername(newUser.getUsername()); + registerAuthenticationReq.setPassword(FakeTestUtil.randomPassword()); + + return registerAuthenticationReq; } private AuthenticationRes.User initUserRes(User user) { @@ -73,4 +93,17 @@ private AuthenticationReq initAuthenticationReq(User user) { return authenticationReq; } + private User initNewUser() { + final var user = new User(); + final var createdAt = FakeTestUtil.randomPast(); + + user.setUsername(FakeTestUtil.randomUsername()); + user.setPassword(FakeTestUtil.randomPassword()); + user.setCreatedAt(createdAt); + user.setUpdatedAt(createdAt); + user.setShoppingLists(Collections.emptySet()); + + return user; + } + }