From e194461520f104ffb5163b9d4af1a8510078858f Mon Sep 17 00:00:00 2001 From: Sergei Prokofev Date: Wed, 12 Jun 2024 22:13:54 +0400 Subject: [PATCH] add api test for updatePassword endpoint --- .../spring/user/service/UserService.java | 1 + .../spring/user/api/UserApiTest.java | 29 ++++++++++++++++++- .../spring/user/api/data/ApiTestData.java | 29 ++++++++++++++++++- .../spring/user/api/data/DataStatus.java | 1 + .../user/api/helper/AssertionsHelper.java | 1 - ...ApiTesUpdatePasswordArgumentsProvider.java | 29 +++++++++++++++++++ .../ApiTestUpdateUserArgumentsProvider.java | 3 +- .../holder/ApiTestArgumentsHolder.java | 18 ++++++++++-- .../spring/user/jdbc/Jdbc.java | 21 ++++++++++---- 9 files changed, 119 insertions(+), 13 deletions(-) create mode 100644 src/test/java/com/digitalsanctuary/spring/user/api/provider/ApiTesUpdatePasswordArgumentsProvider.java diff --git a/src/main/java/com/digitalsanctuary/spring/user/service/UserService.java b/src/main/java/com/digitalsanctuary/spring/user/service/UserService.java index 9b41ee1..501048e 100644 --- a/src/main/java/com/digitalsanctuary/spring/user/service/UserService.java +++ b/src/main/java/com/digitalsanctuary/spring/user/service/UserService.java @@ -221,6 +221,7 @@ public void changeUserPassword(final User user, final String password) { * @return true, if successful */ public boolean checkIfValidOldPassword(final User user, final String oldPassword) { + System.out.println(user.getPassword() + " " + oldPassword); return passwordEncoder.matches(oldPassword, user.getPassword()); } diff --git a/src/test/java/com/digitalsanctuary/spring/user/api/UserApiTest.java b/src/test/java/com/digitalsanctuary/spring/user/api/UserApiTest.java index 0c19e9e..2f5cf28 100644 --- a/src/test/java/com/digitalsanctuary/spring/user/api/UserApiTest.java +++ b/src/test/java/com/digitalsanctuary/spring/user/api/UserApiTest.java @@ -4,6 +4,7 @@ import com.digitalsanctuary.spring.user.api.data.DataStatus; import com.digitalsanctuary.spring.user.api.data.Response; import com.digitalsanctuary.spring.user.api.helper.AssertionsHelper; +import com.digitalsanctuary.spring.user.api.provider.ApiTesUpdatePasswordArgumentsProvider; import com.digitalsanctuary.spring.user.api.provider.ApiTestUpdateUserArgumentsProvider; import com.digitalsanctuary.spring.user.api.provider.holder.ApiTestArgumentsHolder; import com.digitalsanctuary.spring.user.api.provider.ApiTestRegistrationArgumentsProvider; @@ -31,7 +32,7 @@ public class UserApiTest extends BaseApiTest { @Autowired private UserService userService; - private static final UserDto baseTestUser = ApiTestData.getUserDto(); + private static final UserDto baseTestUser = ApiTestData.BASE_TEST_USER; @AfterAll public static void deleteTestUser() { @@ -99,4 +100,30 @@ public void updateUser(ApiTestArgumentsHolder argumentsHolder) throws Exception AssertionsHelper.compareResponses(actual, expected); } + @ParameterizedTest + @ArgumentsSource(ApiTesUpdatePasswordArgumentsProvider.class) + @Order(4) + public void updatePassword(ApiTestArgumentsHolder argumentsHolder) throws Exception { + // test precondition + User user; + if ((user = userService.findUserByEmail(baseTestUser.getEmail())) == null) { + user = userService.registerNewUserAccount(baseTestUser); + } + userService.authWithoutPassword(user); + + ResultActions action = perform(MockMvcRequestBuilders.post(URL + "/updatePassword") + .contentType(MediaType.APPLICATION_FORM_URLENCODED) + .content(buildUrlEncodedFormEntity(argumentsHolder.getPasswordDto()))); + if (argumentsHolder.getStatus() == DataStatus.VALID) { + action.andExpect(status().isOk()); + } else { + action.andExpect(status().is4xxClientError()); + } + + MockHttpServletResponse actual = action.andReturn().getResponse(); + Response expected = argumentsHolder.getResponse(); + AssertionsHelper.compareResponses(actual, expected); + } + + } diff --git a/src/test/java/com/digitalsanctuary/spring/user/api/data/ApiTestData.java b/src/test/java/com/digitalsanctuary/spring/user/api/data/ApiTestData.java index da82c1f..f2e2256 100644 --- a/src/test/java/com/digitalsanctuary/spring/user/api/data/ApiTestData.java +++ b/src/test/java/com/digitalsanctuary/spring/user/api/data/ApiTestData.java @@ -1,9 +1,25 @@ package com.digitalsanctuary.spring.user.api.data; +import com.digitalsanctuary.spring.user.dto.PasswordDto; import com.digitalsanctuary.spring.user.dto.UserDto; - public class ApiTestData { + public static final UserDto BASE_TEST_USER = getUserDto(); + + public static PasswordDto getPasswordDto() { + PasswordDto dto = new PasswordDto(); + dto.setNewPassword("newTestApiUserPassword"); + dto.setOldPassword("testApiUserPassword"); + return dto; + } + + public static PasswordDto getInvalidPasswordDto() { + PasswordDto dto = new PasswordDto(); + dto.setNewPassword("newTestApiUserPassword"); + dto.setOldPassword("invalidPassword"); + return dto; + } + public static UserDto getUserDto() { UserDto userDto = new UserDto(); userDto.setFirstName("testApiUser"); @@ -57,6 +73,17 @@ public static Response userUpdateSuccess() { return new Response(true, null, null, new String[]{"Your Profile Was Successfully Updated.

"}, null ); + } + public static Response passwordUpdateSuccess() { + return new Response(true, 0, null, + new String[]{"Password updated successfully"}, null + ); + } + + public static Response passwordUpdateFailry() { + return new Response(false, 1, null, + new String[]{"Invalid Old Password"}, null + ); } } diff --git a/src/test/java/com/digitalsanctuary/spring/user/api/data/DataStatus.java b/src/test/java/com/digitalsanctuary/spring/user/api/data/DataStatus.java index e131783..f1731b3 100644 --- a/src/test/java/com/digitalsanctuary/spring/user/api/data/DataStatus.java +++ b/src/test/java/com/digitalsanctuary/spring/user/api/data/DataStatus.java @@ -4,6 +4,7 @@ public enum DataStatus { NEW, EXIST, INVALID, + VALID, LOGGED, NOT_LOGGED } diff --git a/src/test/java/com/digitalsanctuary/spring/user/api/helper/AssertionsHelper.java b/src/test/java/com/digitalsanctuary/spring/user/api/helper/AssertionsHelper.java index d3270f4..4a88e6a 100644 --- a/src/test/java/com/digitalsanctuary/spring/user/api/helper/AssertionsHelper.java +++ b/src/test/java/com/digitalsanctuary/spring/user/api/helper/AssertionsHelper.java @@ -7,7 +7,6 @@ public class AssertionsHelper { - public static void compareResponses(MockHttpServletResponse servletResponse, Response excepted) throws Exception{ Response actual = JsonUtil.readValue(servletResponse.getContentAsString(), Response.class); Assertions.assertEquals(actual, excepted); diff --git a/src/test/java/com/digitalsanctuary/spring/user/api/provider/ApiTesUpdatePasswordArgumentsProvider.java b/src/test/java/com/digitalsanctuary/spring/user/api/provider/ApiTesUpdatePasswordArgumentsProvider.java new file mode 100644 index 0000000..b9a9dc5 --- /dev/null +++ b/src/test/java/com/digitalsanctuary/spring/user/api/provider/ApiTesUpdatePasswordArgumentsProvider.java @@ -0,0 +1,29 @@ +package com.digitalsanctuary.spring.user.api.provider; + +import com.digitalsanctuary.spring.user.api.data.ApiTestData; +import com.digitalsanctuary.spring.user.api.data.DataStatus; +import com.digitalsanctuary.spring.user.api.provider.holder.ApiTestArgumentsHolder; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; + +import java.util.stream.Stream; + +public class ApiTesUpdatePasswordArgumentsProvider implements ArgumentsProvider { + + @Override + public Stream provideArguments(ExtensionContext extensionContext) throws Exception { + return Stream.of( + new ApiTestArgumentsHolder( + ApiTestData.getPasswordDto(), + DataStatus.VALID, + ApiTestData.passwordUpdateSuccess() + ), + new ApiTestArgumentsHolder( + ApiTestData.getInvalidPasswordDto(), + DataStatus.INVALID, + ApiTestData.passwordUpdateFailry() + ) + ).map(Arguments::of); + } +} diff --git a/src/test/java/com/digitalsanctuary/spring/user/api/provider/ApiTestUpdateUserArgumentsProvider.java b/src/test/java/com/digitalsanctuary/spring/user/api/provider/ApiTestUpdateUserArgumentsProvider.java index db73bb0..abf1748 100644 --- a/src/test/java/com/digitalsanctuary/spring/user/api/provider/ApiTestUpdateUserArgumentsProvider.java +++ b/src/test/java/com/digitalsanctuary/spring/user/api/provider/ApiTestUpdateUserArgumentsProvider.java @@ -9,8 +9,7 @@ import java.util.stream.Stream; -public class ApiTestUpdateUserArgumentsProvider implements ArgumentsProvider -{ +public class ApiTestUpdateUserArgumentsProvider implements ArgumentsProvider { @Override public Stream provideArguments(ExtensionContext extensionContext) throws Exception { return Stream.of( diff --git a/src/test/java/com/digitalsanctuary/spring/user/api/provider/holder/ApiTestArgumentsHolder.java b/src/test/java/com/digitalsanctuary/spring/user/api/provider/holder/ApiTestArgumentsHolder.java index c26a462..58e249a 100644 --- a/src/test/java/com/digitalsanctuary/spring/user/api/provider/holder/ApiTestArgumentsHolder.java +++ b/src/test/java/com/digitalsanctuary/spring/user/api/provider/holder/ApiTestArgumentsHolder.java @@ -2,13 +2,15 @@ import com.digitalsanctuary.spring.user.api.data.DataStatus; import com.digitalsanctuary.spring.user.api.data.Response; +import com.digitalsanctuary.spring.user.dto.PasswordDto; import com.digitalsanctuary.spring.user.dto.UserDto; public class ApiTestArgumentsHolder { - private final UserDto userDto; - private final DataStatus status; - private final Response response; + private UserDto userDto; + private PasswordDto passwordDto; + private DataStatus status; + private Response response; public ApiTestArgumentsHolder(UserDto userDto, DataStatus status, Response response) { @@ -17,6 +19,12 @@ public ApiTestArgumentsHolder(UserDto userDto, DataStatus status, Response respo this.response = response; } + public ApiTestArgumentsHolder(PasswordDto passwordDto, DataStatus status, Response response) { + this.passwordDto = passwordDto; + this.status = status; + this.response = response; + } + public UserDto getUserDto() { return userDto; } @@ -28,4 +36,8 @@ public DataStatus getStatus() { public Response getResponse() { return response; } + + public PasswordDto getPasswordDto() { + return passwordDto; + } } diff --git a/src/test/java/com/digitalsanctuary/spring/user/jdbc/Jdbc.java b/src/test/java/com/digitalsanctuary/spring/user/jdbc/Jdbc.java index a7c9881..1b8c6f8 100644 --- a/src/test/java/com/digitalsanctuary/spring/user/jdbc/Jdbc.java +++ b/src/test/java/com/digitalsanctuary/spring/user/jdbc/Jdbc.java @@ -12,16 +12,12 @@ * Using for delete/save user test data */ public class Jdbc { - private static final String DELETE_VERIFICATION_TOKEN_QUERY = "DELETE FROM verification_token WHERE user_id = (SELECT id FROM user_account WHERE email = ?)"; - private static final String DELETE_TEST_USER_ROLE = "DELETE FROM users_roles WHERE user_id = (SELECT id FROM user_account WHERE email = ?)"; - private static final String DELETE_TEST_PASSWORD_RESET_TOKEN = "DELETE FROM password_reset_token WHERE user_id = (SELECT id FROM user_account WHERE email = ?)"; - private static final String DELETE_TEST_USER_QUERY = "DELETE FROM user_account WHERE email = ?"; - private static final String GET_LAST_USER_ID_QUERY = "SELECT max(id) FROM user_account"; + private static final String GET_PASSWORD_RESET_TOKEN_BY_USER_EMAIL_QUERY = "SELECT token FROM password_reset_token WHERE user_id = (SELECT id FROM user_account WHERE email = ?)"; private static final String SAVE_TEST_USER_QUERY = "INSERT INTO user_account (id, first_name, last_name, email, " + "password, enabled, failed_login_attempts, locked) VALUES (?,?,?,?,?,?,?,?)"; @@ -70,4 +66,19 @@ private static void execute(Connection connection, String query, Object[] params } statement.executeUpdate(); } + + public static String getPasswordRestTokenByUserEmail(String email) { + String token = ""; + try (Connection connection = ConnectionManager.open()) { + PreparedStatement statement = connection.prepareStatement(GET_PASSWORD_RESET_TOKEN_BY_USER_EMAIL_QUERY); + statement.setString(1, email); + ResultSet set = statement.executeQuery(); + if (set.next()) { + token = set.getString(1); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + return token; + } }