Skip to content

Commit

Permalink
fix: update unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Sotatek-PhucNguyen5 committed Aug 18, 2023
1 parent f60e60f commit 3e4331b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
Expand Up @@ -79,9 +79,10 @@ void whenCallSignUp() throws Exception {
SignUpRequest request = new SignUpRequest();
request.setEmail("test@gmail.com");
request.setPassword("@nhPhuc96");
HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
MessageResponse res = MessageResponse.builder().code(CommonConstant.CODE_SUCCESS)
.message(CommonConstant.RESPONSE_SUCCESS).build();
given(authenticationService.signUp(request)).willReturn(res);
given(authenticationService.signUp(request, httpServletRequest)).willReturn(res);
mockMvc.perform(post("/api/v1/auth/sign-up")
.content(asJsonString(request))
.contentType(MediaType.APPLICATION_JSON))
Expand Down
@@ -1,12 +1,14 @@
package org.cardanofoundation.authentication.controller;

import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.google.gson.Gson;
import jakarta.servlet.http.HttpServletRequest;
import org.cardanofoundation.authentication.constant.CommonConstant;
import org.cardanofoundation.authentication.model.request.auth.ResetPasswordRequest;
import org.cardanofoundation.authentication.model.response.MessageResponse;
Expand Down Expand Up @@ -61,7 +63,8 @@ void whenCallActive() throws Exception {
void whenCallForgotPassword() throws Exception {
MessageResponse res = MessageResponse.builder().code(CommonConstant.CODE_SUCCESS)
.message(CommonConstant.RESPONSE_SUCCESS).build();
given(verifyService.forgotPassword("Test@gmail.com")).willReturn(res);
HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
given(verifyService.forgotPassword("Test@gmail.com", httpServletRequest)).willReturn(res);
mockMvc.perform(get("/api/v1/verify/forgot-password")
.param("email", "Test@gmail.com")
.accept(MediaType.APPLICATION_JSON))
Expand Down
Expand Up @@ -2,8 +2,10 @@

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import jakarta.servlet.http.HttpServletRequest;
import java.time.Instant;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -49,6 +51,7 @@
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.servlet.LocaleResolver;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
Expand Down Expand Up @@ -96,6 +99,9 @@ class AuthenticationServiceTest {
@Mock
private RefreshTokenRepository refreshTokenRepository;

@Mock
private LocaleResolver localeResolver;

private final String SIGNATURE_TEST = "84582aa201276761646472657373581de18a18031ff10e307f9ceff8929608c5f58bdba08304e380c034f85909a166686173686564f453393534353735313438313233323636333232355840850ff657e23963414e7c1bf708928dc994ecafea29790089c810af1ac7486aae12a4ed736d16528051aeff1991ee8d2aef19fe3d375f3ad019925ff1530ed608";

private final String ADDRESS_WALLET = "stake1u80n7nvm3qlss9ls0krp5xh7sqxlazp8kz6n3fp5sgnul5cnxyg4p";
Expand Down Expand Up @@ -254,10 +260,11 @@ void whenSignUp_EmailIsExist_ThrowException() {
SignUpRequest signUpRequest = new SignUpRequest();
signUpRequest.setEmail("test5.6@gmail.com");
signUpRequest.setPassword(PASSWORD);
HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
when(userService.checkExistEmailAndStatus("test5.6@gmail.com", EStatus.ACTIVE))
.thenReturn(true);
BusinessException exception = Assertions.assertThrows(BusinessException.class, () -> {
authenticationService.signUp(signUpRequest);
authenticationService.signUp(signUpRequest, httpServletRequest);
});
String expectedCode = CommonErrorCode.EMAIL_IS_ALREADY_EXIST.getServiceErrorCode();
String actualCode = exception.getErrorCode();
Expand All @@ -269,6 +276,7 @@ void whenSignUp_EmailIsNotExist_returnResponseSuccess() {
SignUpRequest signUpRequest = new SignUpRequest();
signUpRequest.setEmail("test5.6@gmail.com");
signUpRequest.setPassword(PASSWORD);
HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
UserEntity user = UserEntity.builder().email("test5.6@gmail.com").build();
when(userService.checkExistEmailAndStatus("test5.6@gmail.com", EStatus.ACTIVE))
.thenReturn(false);
Expand All @@ -279,8 +287,8 @@ void whenSignUp_EmailIsNotExist_returnResponseSuccess() {
when(userService.saveUser(signUpRequest)).thenReturn(user);
when(jwtProvider.generateCodeForVerify("test5.6@gmail.com")).thenReturn(JWT);
doNothing().when(sendMailExecutor)
.execute(new MailHandler(mailProvider, user, EUserAction.CREATED, JWT));
MessageResponse response = authenticationService.signUp(signUpRequest);
.execute(new MailHandler(mailProvider, user, EUserAction.CREATED, any(), JWT));
MessageResponse response = authenticationService.signUp(signUpRequest, httpServletRequest);
String expectedCode = CommonConstant.CODE_SUCCESS;
Assertions.assertEquals(expectedCode, response.getCode());
}
Expand All @@ -290,6 +298,7 @@ void whenSignUp_EmailIsNotExistWithStatusPending_returnResponseSuccess() {
SignUpRequest signUpRequest = new SignUpRequest();
signUpRequest.setEmail("test5.6@gmail.com");
signUpRequest.setPassword(PASSWORD);
HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
UserEntity user = UserEntity.builder().email("test5.6@gmail.com").status(EStatus.PENDING)
.password(PASSWORD).build();
when(userService.checkExistEmailAndStatus("test5.6@gmail.com", EStatus.ACTIVE))
Expand All @@ -301,8 +310,8 @@ void whenSignUp_EmailIsNotExistWithStatusPending_returnResponseSuccess() {
when(userRepository.save(user)).thenReturn(user);
when(jwtProvider.generateCodeForVerify("test5.6@gmail.com")).thenReturn(JWT);
doNothing().when(sendMailExecutor)
.execute(new MailHandler(mailProvider, user, EUserAction.CREATED, JWT));
MessageResponse response = authenticationService.signUp(signUpRequest);
.execute(new MailHandler(mailProvider, user, EUserAction.CREATED, any(), JWT));
MessageResponse response = authenticationService.signUp(signUpRequest, httpServletRequest);
String expectedCode = CommonConstant.CODE_SUCCESS;
Assertions.assertEquals(expectedCode, response.getCode());
}
Expand Down
Expand Up @@ -2,8 +2,10 @@

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import jakarta.servlet.http.HttpServletRequest;
import java.util.Optional;
import java.util.concurrent.ThreadPoolExecutor;
import org.cardanofoundation.authentication.constant.CommonConstant;
Expand All @@ -28,6 +30,7 @@
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.servlet.LocaleResolver;

@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
Expand Down Expand Up @@ -57,6 +60,9 @@ class VerifyServiceTest {
@Mock
private ThreadPoolExecutor sendMailExecutor;

@Mock
private LocaleResolver localeResolver;

private final String CODE = "eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJzb3RhdGVrIiwianRpIjoiMiIsImlhdCI6MTY3Mjk5Nzc0MSwiZXhwIjoxNjczMDg0MTQxfQ.B62gXo6iqQfHMT62q17zdhwMF8I77-P6xblKcx7ZI3-gij6YckvFYVVuoIa_qXgTTFnEeRDBQEVo3o20D1w6pffBrgbvxvMbjhOG0ONS9Xs1UQChwQs7v3lxkqoKZ8dNf0Eib43HxLZhBEBIeXa1kln4sS8osWf5iEgno0od7z9KwWK1N2Coj0o-1HE453fFyRveDJgd0DvXohbHADMmjH9t0WkXJwUK26Lv1tkqPlkIzGBPgYnYEIygdayqqt4EtP6CtgI9QOzCYSZUUFzxo-VVDzA0J7DpQbYn8G2PAuAbCXCO6lTkvmXMiyZAoZshqRhBNb7uDI66dwOJLV3NzuunSa8QOO8eNUaDoHHvR_9_J-yHTFBicoM69JHQ7UzJVyFHGmh1M8lHsJ9y6DdAobtBSyJFBhFeDj7S8bgpIvIyNoHDsf24xdlqCngE1qBsxjfp0L_yMPBxsIhW3Juopwe1c6btWTEaRaVaxhKE5yKbRsTtAzDDkdEyg_--9eXH";

private final String EMAIL = "test@gmail.com";
Expand Down Expand Up @@ -172,21 +178,23 @@ void whenResetPassword_returnResponse() {

@Test
void whenForgotPassword_userInValid_throwException() {
HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
when(userRepository.findByEmailAndStatus(EMAIL, EStatus.ACTIVE)).thenReturn(Optional.empty());
MessageResponse response = verifyService.forgotPassword(EMAIL);
MessageResponse response = verifyService.forgotPassword(EMAIL, httpServletRequest);
String expectedCode = CommonConstant.CODE_FAILURE;
String actualCode = response.getCode();
Assertions.assertEquals(expectedCode, actualCode);
}

@Test
void whenForgotPassword_returnResponse() {
HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
UserEntity user = UserEntity.builder().build();
when(userRepository.findByEmailAndStatus(EMAIL, EStatus.ACTIVE)).thenReturn(Optional.of(user));
when(jwtProvider.generateCodeForVerify(EMAIL)).thenReturn(CODE);
doNothing().when(sendMailExecutor)
.execute(new MailHandler(mailProvider, user, EUserAction.RESET_PASSWORD, CODE));
MessageResponse response = verifyService.forgotPassword(EMAIL);
.execute(new MailHandler(mailProvider, user, EUserAction.RESET_PASSWORD, any(), CODE));
MessageResponse response = verifyService.forgotPassword(EMAIL, httpServletRequest);
String expectedCode = CommonConstant.CODE_SUCCESS;
String actualCode = response.getCode();
Assertions.assertEquals(expectedCode, actualCode);
Expand Down

0 comments on commit 3e4331b

Please sign in to comment.