|
| 1 | +package io.aiven.klaw.controller; |
| 2 | + |
| 3 | +import static org.mockito.ArgumentMatchers.anyString; |
| 4 | +import static org.mockito.Mockito.when; |
| 5 | +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; |
| 6 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 7 | + |
| 8 | +import io.aiven.klaw.model.response.ResetPasswordInfo; |
| 9 | +import io.aiven.klaw.service.UsersTeamsControllerService; |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.MethodOrderer; |
| 13 | +import org.junit.jupiter.api.Order; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.api.TestMethodOrder; |
| 16 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 17 | +import org.junit.jupiter.params.ParameterizedTest; |
| 18 | +import org.junit.jupiter.params.provider.CsvSource; |
| 19 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 20 | +import org.springframework.http.MediaType; |
| 21 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 22 | +import org.springframework.test.util.ReflectionTestUtils; |
| 23 | +import org.springframework.test.web.servlet.MockMvc; |
| 24 | +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; |
| 25 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 26 | + |
| 27 | +@ExtendWith(SpringExtension.class) |
| 28 | +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) |
| 29 | +public class UserTeamsControllerTest { |
| 30 | + |
| 31 | + @MockBean private UsersTeamsControllerService usersTeamsControllerService; |
| 32 | + |
| 33 | + private MockMvc mvc; |
| 34 | + |
| 35 | + @BeforeEach |
| 36 | + public void setUp() { |
| 37 | + UsersTeamsController usersTeamsController = new UsersTeamsController(); |
| 38 | + mvc = MockMvcBuilders.standaloneSetup(usersTeamsController).dispatchOptions(true).build(); |
| 39 | + ReflectionTestUtils.setField( |
| 40 | + usersTeamsController, "usersTeamsControllerService", usersTeamsControllerService); |
| 41 | + } |
| 42 | + |
| 43 | + @ParameterizedTest |
| 44 | + @Order(1) |
| 45 | + @CsvSource({ |
| 46 | + "'invalidpwd', 400", // Invalid password -> Expect 4xx Client Error |
| 47 | + "'Invalidpwd321@', 200" // Valid password -> Expect 200 OK |
| 48 | + }) |
| 49 | + public void resetPasswordWithTokenTest(String password, int expectedStatus) throws Exception { |
| 50 | + ResetPasswordInfo passwordReset = new ResetPasswordInfo(); |
| 51 | + when(usersTeamsControllerService.resetPassword(anyString(), anyString(), anyString())) |
| 52 | + .thenReturn(passwordReset); |
| 53 | + |
| 54 | + mvc.perform( |
| 55 | + MockMvcRequestBuilders.post("/reset/password") |
| 56 | + .param("token", "token") |
| 57 | + .param("password", password) |
| 58 | + .param("username", "username") |
| 59 | + .contentType(MediaType.APPLICATION_JSON) |
| 60 | + .characterEncoding(StandardCharsets.UTF_8) |
| 61 | + .accept(MediaType.APPLICATION_JSON)) |
| 62 | + .andDo(print()) |
| 63 | + .andExpect(status().is(expectedStatus)); |
| 64 | + } |
| 65 | + |
| 66 | +} |
0 commit comments