Skip to content

Commit

Permalink
Testing: Clase AuthenticationService
Browse files Browse the repository at this point in the history
- Test básico para el método "register".
  • Loading branch information
nmarulo committed Nov 1, 2024
1 parent 0cfd14a commit aca383c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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.<User>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<LocalDateTime> localDateTimeMockedStatic = mockStatic(LocalDateTime.class)) {
localDateTimeMockedStatic.when(LocalDateTime::now)
.thenReturn(localDateTime);

final var actual = this.authenticationService.register(request);

assertEquals(expected, actual);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();

Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}

}

0 comments on commit aca383c

Please sign in to comment.