Skip to content

Commit

Permalink
Modified PR to reflect coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
nbaars committed Sep 10, 2019
1 parent 0ac8b7c commit 43c25dc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
39 changes: 12 additions & 27 deletions webwolf/src/test/java/org/owasp/webwolf/user/UserServiceTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,56 +10,43 @@

import static org.mockito.Mockito.*;

/**
* @author rjclancy
* @since 3/26/19.
*/
@RunWith(SpringJUnit4ClassRunner.class)
public class UserServiceTest {

@Mock
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));
}
}
42 changes: 17 additions & 25 deletions webwolf/src/test/java/org/owasp/webwolf/user/UserValidatorTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {

Expand All @@ -26,60 +23,55 @@ 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");
BindException errors = new BindException(validUserForm, "validUserForm");

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");
BindException errors = new BindException(validUserForm, "validUserForm");

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();
}
}

0 comments on commit 43c25dc

Please sign in to comment.