Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit d892312

Browse files
committed
FF-193 Refactoring.
1 parent 29d8fcd commit d892312

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/main/java/de/filefighter/rest/domain/user/business/UserBusinessService.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,14 @@ public void registerNewUser(UserRegisterForm newUser) {
104104

105105
// check pws.
106106
String password = newUser.getPassword();
107+
String confirmationPassword = newUser.getConfirmationPassword();
108+
109+
if (!stringIsValid(password) || !stringIsValid(confirmationPassword))
110+
throw new UserNotRegisteredException("Wanted to change password, but password was not valid.");
111+
107112
if (!passwordIsValid(password))
108113
throw new UserNotRegisteredException("Password needs to be at least 8 characters long and, contains at least one uppercase and lowercase letter and a number.");
109114

110-
String confirmationPassword = newUser.getConfirmationPassword();
111-
112115
if (!password.contentEquals(confirmationPassword))
113116
throw new UserNotRegisteredException("Passwords do not match.");
114117

@@ -139,10 +142,8 @@ public void registerNewUser(UserRegisterForm newUser) {
139142
}
140143

141144
public boolean passwordIsValid(String password) {
142-
if (!stringIsValid(password))
143-
return false;
144-
145-
if (this.passwordCheckDisabled) return true;
145+
if (this.passwordCheckDisabled)
146+
return true;
146147

147148
Pattern pattern = Pattern.compile("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=\\S+).{8,20}$");
148149
return pattern.matcher(password).matches();

src/test/java/de/filefighter/rest/domain/user/business/UserBusinessServiceUnitTest.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ void passwordIsValidReturnsFalse() {
159159
void passwordIsValidWorks() {
160160
String works = "Password1234?!";
161161
assertDoesNotThrow(() -> userBusinessService.passwordIsValid(works));
162+
163+
userBusinessService.passwordCheckDisabled = true; //remember to reset it if beforeEach is removed.
164+
String doesWork = "pw";
165+
assertDoesNotThrow(() -> userBusinessService.passwordIsValid(doesWork));
162166
}
163167

164168
@Test
@@ -184,11 +188,23 @@ void registerNewUserThrows() {
184188
userBusinessService.registerNewUser(userRegisterForm));
185189
assertEquals("User could not be registered. Username already taken.", ex.getMessage());
186190

187-
//Passwords not valid
191+
//passwords empty
188192
when(userRepositoryMock.findByLowercaseUsername(username.toLowerCase())).thenReturn(null);
189193
when(userDtoServiceMock.createDto(any())).thenReturn(null);
190-
userRegisterForm.setPassword(notValidPassword);
194+
userRegisterForm.setPassword("");
195+
ex = assertThrows(UserNotRegisteredException.class, () ->
196+
userBusinessService.registerNewUser(userRegisterForm));
197+
assertEquals("User could not be registered. Wanted to change password, but password was not valid.", ex.getMessage());
198+
199+
userRegisterForm.setPassword("somepassword");
200+
userRegisterForm.setConfirmationPassword("");
201+
ex = assertThrows(UserNotRegisteredException.class, () ->
202+
userBusinessService.registerNewUser(userRegisterForm));
203+
assertEquals("User could not be registered. Wanted to change password, but password was not valid.", ex.getMessage());
191204

205+
//Passwords not valid
206+
userRegisterForm.setConfirmationPassword(notValidPassword);
207+
userRegisterForm.setPassword(notValidPassword);
192208
ex = assertThrows(UserNotRegisteredException.class, () ->
193209
userBusinessService.registerNewUser(userRegisterForm));
194210
assertEquals("User could not be registered. Password needs to be at least 8 characters long and, contains at least one uppercase and lowercase letter and a number.", ex.getMessage());
@@ -233,6 +249,9 @@ void registerNewUserWorks() {
233249
.build();
234250

235251
assertDoesNotThrow(() -> userBusinessService.registerNewUser(userRegisterForm));
252+
253+
userRegisterForm.setGroupIds(null);
254+
assertDoesNotThrow(() -> userBusinessService.registerNewUser(userRegisterForm));
236255
}
237256

238257
@Test
@@ -303,6 +322,10 @@ void updatePasswordThrows() {
303322
userBusinessService.updateUser(userId, userRegisterForm, authenticatedUser));
304323
assertEquals("User could not get updated. Wanted to change password, but password was not valid.", ex.getMessage());
305324

325+
userRegisterForm.setConfirmationPassword("");
326+
ex = assertThrows(UserNotUpdatedException.class, () ->
327+
userBusinessService.updateUser(userId, userRegisterForm, authenticatedUser));
328+
assertEquals("User could not get updated. Wanted to change password, but password was not valid.", ex.getMessage());
306329

307330
userRegisterForm.setPassword("somepw");
308331
userRegisterForm.setConfirmationPassword("somepw");

0 commit comments

Comments
 (0)