-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from TAMULib/test-coverage-improvements
Test coverage improvements
- Loading branch information
Showing
20 changed files
with
650 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package edu.tamu.app.auth; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.when; | ||
import static org.springframework.test.util.ReflectionTestUtils.setField; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import edu.tamu.app.Whitelist; | ||
|
||
@RunWith(SpringRunner.class) | ||
public class WhitelistTest { | ||
|
||
private static final String[] testAuthorities = { "123456789", "987654321" }; | ||
|
||
@Mock | ||
private HttpServletRequest request; | ||
|
||
@InjectMocks | ||
private Whitelist whitelist; | ||
|
||
@Test | ||
public void testIsAllowed() { | ||
setField(whitelist, "whitelist", testAuthorities); | ||
when(request.getRemoteAddr()).thenReturn("123456789"); | ||
boolean result = whitelist.isAllowed(request); | ||
assertTrue("Result was not true", result); | ||
} | ||
|
||
@Test | ||
public void testIsAllowedWithNonwhitelistedRequest() { | ||
setField(whitelist, "whitelist", testAuthorities); | ||
when(request.getRemoteAddr()).thenReturn("111111111"); | ||
boolean result = whitelist.isAllowed(request); | ||
assertFalse("Result was not false", result); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/test/java/edu/tamu/app/auth/model/AppUserDetailsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package edu.tamu.app.auth.model; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import edu.tamu.app.enums.Role; | ||
import edu.tamu.app.model.User; | ||
import edu.tamu.weaver.auth.model.Credentials; | ||
|
||
@RunWith(SpringRunner.class) | ||
public class AppUserDetailsTest { | ||
|
||
private static final Credentials TEST_CREDENTIALS = new Credentials(); | ||
static { | ||
TEST_CREDENTIALS.setUin("123456789"); | ||
TEST_CREDENTIALS.setEmail("aggieJack@tamu.edu"); | ||
TEST_CREDENTIALS.setFirstName("Aggie"); | ||
TEST_CREDENTIALS.setLastName("Jack"); | ||
TEST_CREDENTIALS.setRole("ROLE_USER"); | ||
} | ||
|
||
private User testUser = new User(TEST_CREDENTIALS.getUin(), TEST_CREDENTIALS.getEmail(), TEST_CREDENTIALS.getFirstName(), TEST_CREDENTIALS.getLastName(), Role.valueOf(TEST_CREDENTIALS.getRole())); | ||
|
||
@Test | ||
public void testConstructor() { | ||
AppUserDetails appUser = new AppUserDetails(testUser); | ||
assertEquals("The parent constructor was not called correctly", testUser.getId(), appUser.getId()); | ||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
src/test/java/edu/tamu/app/auth/service/AppUserCredentialsServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package edu.tamu.app.auth.service; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Matchers.any; | ||
import static org.mockito.Mockito.when; | ||
import static org.springframework.test.util.ReflectionTestUtils.setField; | ||
|
||
import java.util.Optional; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import edu.tamu.app.enums.Role; | ||
import edu.tamu.app.model.User; | ||
import edu.tamu.app.model.repo.UserRepo; | ||
import edu.tamu.weaver.auth.model.Credentials; | ||
|
||
@RunWith(SpringRunner.class) | ||
public class AppUserCredentialsServiceTest { | ||
|
||
private static final Credentials TEST_CREDENTIALS_1 = new Credentials(); | ||
static { | ||
TEST_CREDENTIALS_1.setUin("123456789"); | ||
TEST_CREDENTIALS_1.setEmail("aggieJack@tamu.edu"); | ||
TEST_CREDENTIALS_1.setFirstName("Aggie"); | ||
TEST_CREDENTIALS_1.setLastName("Jack"); | ||
TEST_CREDENTIALS_1.setRole("ROLE_USER"); | ||
} | ||
|
||
private static final Credentials TEST_CREDENTIALS_2 = new Credentials(); | ||
static { | ||
TEST_CREDENTIALS_2.setUin("987654321"); | ||
TEST_CREDENTIALS_2.setEmail("aggieJack@tamu.edu"); | ||
TEST_CREDENTIALS_2.setFirstName("Aggie"); | ||
TEST_CREDENTIALS_2.setLastName("Jack"); | ||
TEST_CREDENTIALS_2.setRole("ROLE_USER"); | ||
} | ||
|
||
private static final Credentials TEST_NULL_CREDENTIALS = new Credentials(); | ||
static { | ||
TEST_NULL_CREDENTIALS.setUin("987654321"); | ||
TEST_NULL_CREDENTIALS.setEmail("aggieJack@tamu.edu"); | ||
TEST_NULL_CREDENTIALS.setFirstName("Aggie"); | ||
TEST_NULL_CREDENTIALS.setLastName("Jack"); | ||
} | ||
|
||
private static final Credentials TEST_CHANGED_CREDENTIALS = new Credentials(); | ||
static { | ||
TEST_CHANGED_CREDENTIALS.setUin("111111111"); | ||
TEST_CHANGED_CREDENTIALS.setEmail("jsmithk@tamu.edu"); | ||
TEST_CHANGED_CREDENTIALS.setFirstName("John"); | ||
TEST_CHANGED_CREDENTIALS.setLastName("Smith"); | ||
TEST_CHANGED_CREDENTIALS.setRole("ROLE_ADMIN"); | ||
} | ||
|
||
private User testUser1 = new User(TEST_CREDENTIALS_1.getUin(), TEST_CREDENTIALS_1.getEmail(), TEST_CREDENTIALS_1.getFirstName(), TEST_CREDENTIALS_1.getLastName(), Role.valueOf(TEST_CREDENTIALS_1.getRole())); | ||
private User testUser2 = new User(TEST_CREDENTIALS_2.getUin(), TEST_CREDENTIALS_2.getEmail(), TEST_CREDENTIALS_2.getFirstName(), TEST_CREDENTIALS_2.getLastName(), Role.valueOf(TEST_CREDENTIALS_2.getRole())); | ||
|
||
private static final String[] testAdmins = { "123456789", "987654321" }; | ||
|
||
private Optional<User> optionalUser1 = Optional.of(testUser1); | ||
|
||
@Mock | ||
private UserRepo userRepo; | ||
|
||
@InjectMocks | ||
private AppUserCredentialsService credentialsService; | ||
|
||
@Before | ||
public void setUp() { | ||
MockitoAnnotations.initMocks(this); | ||
when(userRepo.findByUsername(TEST_CREDENTIALS_1.getUin())).thenReturn(optionalUser1); | ||
when(userRepo.findByUsername(TEST_CREDENTIALS_2.getUin())).thenReturn(Optional.empty()); | ||
when(userRepo.findByUsername(TEST_CHANGED_CREDENTIALS.getUin())).thenReturn(optionalUser1); | ||
when(userRepo.create(any(String.class), any(String.class), any(String.class), any(String.class), any(Role.class))).thenReturn(testUser2); | ||
when(userRepo.save(any(User.class))).thenReturn(testUser1); | ||
} | ||
|
||
@Test | ||
public void testUpdateUserByCredentials() { | ||
setField(credentialsService, "admins", testAdmins); | ||
User foundUser = credentialsService.updateUserByCredentials(TEST_CREDENTIALS_1); | ||
assertEquals("Unable to find user", testUser1, foundUser); | ||
User unfoundUser = credentialsService.updateUserByCredentials(TEST_CREDENTIALS_2); | ||
assertEquals("Unable to find user", testUser2, unfoundUser); | ||
} | ||
|
||
@Test | ||
public void testGetAnonymousRole() { | ||
String anonRole = credentialsService.getAnonymousRole(); | ||
assertEquals("Anonymous Role not set correctly", Role.ROLE_ANONYMOUS.toString(), anonRole); | ||
} | ||
|
||
@Test | ||
public void testNullRole() { | ||
setField(credentialsService, "admins", testAdmins); | ||
User nullUser = credentialsService.updateUserByCredentials(TEST_NULL_CREDENTIALS); | ||
assertEquals("Null Role not updated", TEST_CREDENTIALS_1.getRole(), nullUser.getRole().toString()); | ||
} | ||
|
||
@Test | ||
public void testChangedUser() { | ||
User changedUser = credentialsService.updateUserByCredentials(TEST_CHANGED_CREDENTIALS); | ||
assertEquals("is present", changedUser, optionalUser1.get()); | ||
assertEquals("Username was not updated", TEST_CHANGED_CREDENTIALS.getUin(), changedUser.getUsername()); | ||
assertEquals("Email was not updated", TEST_CHANGED_CREDENTIALS.getEmail(), changedUser.getEmail()); | ||
assertEquals("First name was not updated", TEST_CHANGED_CREDENTIALS.getFirstName(), changedUser.getFirstName()); | ||
assertEquals("Last name was not updated", TEST_CHANGED_CREDENTIALS.getLastName(), changedUser.getLastName()); | ||
assertEquals("Role was not updated", TEST_CHANGED_CREDENTIALS.getRole(), changedUser.getRole().toString()); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/test/java/edu/tamu/app/auth/service/AppUserDetailsServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package edu.tamu.app.auth.service; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import edu.tamu.app.enums.Role; | ||
import edu.tamu.app.model.User; | ||
import edu.tamu.weaver.auth.model.Credentials; | ||
|
||
@RunWith(SpringRunner.class) | ||
public class AppUserDetailsServiceTest { | ||
|
||
private static final Credentials TEST_CREDENTIALS_1 = new Credentials(); | ||
static { | ||
TEST_CREDENTIALS_1.setUin("123456789"); | ||
TEST_CREDENTIALS_1.setEmail("aggieJack@tamu.edu"); | ||
TEST_CREDENTIALS_1.setFirstName("Aggie"); | ||
TEST_CREDENTIALS_1.setLastName("Jack"); | ||
TEST_CREDENTIALS_1.setRole("ROLE_USER"); | ||
} | ||
|
||
private User testUser1 = new User(TEST_CREDENTIALS_1.getUin(), TEST_CREDENTIALS_1.getEmail(), TEST_CREDENTIALS_1.getFirstName(), TEST_CREDENTIALS_1.getLastName(), Role.valueOf(TEST_CREDENTIALS_1.getRole())); | ||
|
||
@InjectMocks | ||
private AppUserDetailsService appUserDetailsService; | ||
|
||
@Test | ||
public void testBuildUserDetails() { | ||
UserDetails details = appUserDetailsService.buildUserDetails(testUser1); | ||
assertEquals("User details not built correctly", TEST_CREDENTIALS_1.getUin(), details.getUsername()); | ||
} | ||
|
||
} |
Oops, something went wrong.