diff --git a/webwolf/src/main/java/org/owasp/webwolf/user/UserService.java b/webwolf/src/main/java/org/owasp/webwolf/user/UserService.java index 5acc52f920..b8cd0c9c36 100644 --- a/webwolf/src/main/java/org/owasp/webwolf/user/UserService.java +++ b/webwolf/src/main/java/org/owasp/webwolf/user/UserService.java @@ -14,9 +14,12 @@ @Service public class UserService implements UserDetailsService { - @Autowired private UserRepository userRepository; + public UserService(UserRepository userRepository) { + this.userRepository = userRepository; + } + @Override public WebGoatUser loadUserByUsername(final String username) throws UsernameNotFoundException { WebGoatUser webGoatUser = userRepository.findByUsername(username); diff --git a/webwolf/src/test/java/org/owasp/webwolf/user/UserServiceTest.java b/webwolf/src/test/java/org/owasp/webwolf/user/UserServiceTest.java index 3d611c30a6..e050488417 100644 --- a/webwolf/src/test/java/org/owasp/webwolf/user/UserServiceTest.java +++ b/webwolf/src/test/java/org/owasp/webwolf/user/UserServiceTest.java @@ -1,8 +1,6 @@ package org.owasp.webwolf.user; - -import org.junit.Assert; -import org.junit.Before; +import org.assertj.core.api.Assertions; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; @@ -12,10 +10,6 @@ import static org.mockito.Mockito.*; -/** - * @author rjclancy - * @since 3/26/19. - */ @RunWith(SpringJUnit4ClassRunner.class) public class UserServiceTest { @@ -23,45 +17,36 @@ public class UserServiceTest { private UserRepository mockUserRepository; @InjectMocks - private UserService cut; + private UserService sut; @Test public void testLoadUserByUsername(){ - // setup - final String username = "guest"; - final String password = "123"; - + var username = "guest"; + var password = "123"; WebGoatUser user = new WebGoatUser(username, password); when(mockUserRepository.findByUsername(username)).thenReturn(user); - // execute - final WebGoatUser webGoatUser = cut.loadUserByUsername(username); + var webGoatUser = sut.loadUserByUsername(username); - // verify - Assert.assertEquals(username, webGoatUser.getUsername()); - Assert.assertEquals(password, webGoatUser.getPassword()); + Assertions.assertThat(username).isEqualTo(webGoatUser.getUsername()); + Assertions.assertThat(password).isEqualTo(webGoatUser.getPassword()); } @Test(expected = UsernameNotFoundException.class) public void testLoadUserByUsername_NULL(){ - // setup - final String username = "guest"; + var username = "guest"; when(mockUserRepository.findByUsername(username)).thenReturn(null); - // execute - cut.loadUserByUsername(username); + sut.loadUserByUsername(username); } @Test public void testAddUser(){ - // setup - final String username = "guest"; - final String password = "guest"; + var username = "guest"; + var password = "guest"; - // execute - cut.addUser(username, password); + sut.addUser(username, password); - // verify verify(mockUserRepository, times(1)).save(any(WebGoatUser.class)); } } diff --git a/webwolf/src/test/java/org/owasp/webwolf/user/UserValidatorTest.java b/webwolf/src/test/java/org/owasp/webwolf/user/UserValidatorTest.java index 60e614a589..5c160f5e15 100644 --- a/webwolf/src/test/java/org/owasp/webwolf/user/UserValidatorTest.java +++ b/webwolf/src/test/java/org/owasp/webwolf/user/UserValidatorTest.java @@ -1,5 +1,6 @@ package org.owasp.webwolf.user; +import org.assertj.core.api.Assertions; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -12,10 +13,6 @@ import static org.junit.Assert.assertFalse; import static org.mockito.Mockito.when; -/** - * @author rjclancy - * @since 3/26/19. - */ @RunWith(SpringJUnit4ClassRunner.class) public class UserValidatorTest { @@ -26,8 +23,8 @@ public class UserValidatorTest { private UserValidator userValidator; @Test - public void testValidUserForm() { - UserForm validUserForm = new UserForm(); + public void validUserFormShouldNotHaveErrors() { + var validUserForm = new UserForm(); validUserForm.setUsername("guest"); validUserForm.setMatchingPassword("123"); validUserForm.setPassword("123"); @@ -35,12 +32,12 @@ public void testValidUserForm() { userValidator.validate(validUserForm, errors); - assertFalse(errors.hasErrors()); + Assertions.assertThat(errors.hasErrors()).isFalse(); } @Test - public void testValidUserForm_INVALID_PASSWORD() { - UserForm validUserForm = new UserForm(); + public void whenPasswordDoNotMatchShouldFail() { + var validUserForm = new UserForm(); validUserForm.setUsername("guest"); validUserForm.setMatchingPassword("123"); validUserForm.setPassword("124"); @@ -48,38 +45,33 @@ public void testValidUserForm_INVALID_PASSWORD() { userValidator.validate(validUserForm, errors); - assertTrue(errors.hasErrors()); + Assertions.assertThat(errors.hasErrors()).isTrue(); } @Test - public void testValidUserForm_DUPLICATE_USER() { - // setup - final String username = "guest"; - final String password = "123"; - - UserForm validUserForm = new UserForm(); + public void registerExistingUserAgainShouldFail() { + var username = "guest"; + var password = "123"; + var validUserForm = new UserForm(); validUserForm.setUsername(username); validUserForm.setMatchingPassword(password); validUserForm.setPassword("124"); BindException errors = new BindException(validUserForm, "validUserForm"); - - WebGoatUser webGoatUser = new WebGoatUser(username, password); + var webGoatUser = new WebGoatUser(username, password); when(mockUserRepository.findByUsername(validUserForm.getUsername())).thenReturn(webGoatUser); - // execute userValidator.validate(validUserForm, errors); - // verify - assertTrue(errors.hasErrors()); + Assertions.assertThat(errors.hasErrors()).isTrue(); } @Test - public void testSupports(){ - Assert.assertTrue(userValidator.supports(UserForm.class)); + public void testSupports() { + Assertions.assertThat(userValidator.supports(UserForm.class)).isTrue(); } @Test - public void testSupports_false(){ - Assert.assertFalse(userValidator.supports(UserService.class)); + public void testSupports_false() { + Assertions.assertThat(userValidator.supports(UserService.class)).isFalse(); } } \ No newline at end of file