-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Added test cases change password web testing.
- Loading branch information
Showing
1 changed file
with
85 additions
and
3 deletions.
There are no files selected for viewing
88 changes: 85 additions & 3 deletions
88
pemits-web/src/test/java/com/pemits/webcare/web/WebCareApplicationTests.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 |
---|---|---|
@@ -1,11 +1,93 @@ | ||
package com.pemits.webcare.web; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
|
||
import java.util.Optional; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.beans.BeanUtils; | ||
import org.springframework.boot.test.json.JacksonTester; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.mock.web.MockHttpServletResponse; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
|
||
import com.pemits.webcare.api.user.entity.User; | ||
import com.pemits.webcare.api.user.service.UserServiceImpl; | ||
import com.pemits.webcare.web.user.UserController; | ||
import com.pemits.webcare.web.user.dto.ChangePasswordDto; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class WebCareApplicationTests { | ||
|
||
@Test | ||
void contextLoads() { | ||
} | ||
@Mock | ||
private PasswordEncoder passwordEncoder; | ||
private MockMvc mockMvc; | ||
@Mock | ||
private UserServiceImpl service; | ||
@InjectMocks | ||
private UserController userController; | ||
|
||
// initialized by `JacksonTester.initFields()` | ||
private JacksonTester<ChangePasswordDto> changePasswordDtoJacksonTester; | ||
|
||
@BeforeEach | ||
public void setup() { | ||
JacksonTester.initFields(this, new ObjectMapper()); | ||
mockMvc = MockMvcBuilders.standaloneSetup(userController).build(); | ||
} | ||
|
||
@Test | ||
void testChangePassword() throws Exception { | ||
final String oldPassword = "123456"; | ||
final String newPassword = "12345678"; | ||
final String oldPasswordHash = "abcdef"; | ||
final String newPasswordHash = "qwerty"; | ||
final long mockUserId = 1L; | ||
|
||
ChangePasswordDto dto = new ChangePasswordDto(); | ||
dto.setUserId(mockUserId); | ||
dto.setOldPassword(oldPassword); | ||
dto.setNewPassword(newPassword); | ||
|
||
// password encoder stubs | ||
when(passwordEncoder.encode(oldPassword)).thenReturn(oldPasswordHash); | ||
when(passwordEncoder.encode(newPassword)).thenReturn(newPasswordHash); | ||
|
||
// not updated user mock | ||
User user = new User(); | ||
user.setId(mockUserId); | ||
user.setPassword(passwordEncoder.encode(dto.getOldPassword())); | ||
|
||
// update user mock | ||
User saved = new User(); | ||
BeanUtils.copyProperties(user, saved); | ||
saved.setPassword(passwordEncoder.encode(dto.getNewPassword())); | ||
|
||
// stubs | ||
when(passwordEncoder.matches(dto.getOldPassword(), user.getPassword())).thenReturn(true); | ||
when(service.findOne(any(Long.class))).thenReturn(Optional.of(user)); | ||
when(service.save(any(User.class))).thenReturn(saved); | ||
|
||
MockHttpServletResponse response = mockMvc.perform( | ||
post("/v1/users/changePassword") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.content(changePasswordDtoJacksonTester.write(dto).getJson()) | ||
).andReturn().getResponse(); | ||
|
||
assertThat(response.getStatus(), equalTo(HttpStatus.OK.value())); | ||
} | ||
|
||
} |