updated and expanded unit tests to account for new data layer

pull/819/merge
Justin Richer 2015-05-26 21:54:27 -04:00
parent d5a08d4996
commit fe6d2f8a6e
1 changed files with 40 additions and 26 deletions

View File

@ -58,10 +58,14 @@ import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.format.annotation.DateTimeFormat.ISO; import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.format.datetime.DateFormatter; import org.springframework.format.datetime.DateFormatter;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.provider.OAuth2Authentication; import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.OAuth2Request; import org.springframework.security.oauth2.provider.OAuth2Request;
@ -98,6 +102,8 @@ import static org.junit.Assert.fail;
@SuppressWarnings(value = {"rawtypes", "unchecked"}) @SuppressWarnings(value = {"rawtypes", "unchecked"})
public class TestMITREidDataService_1_2 { public class TestMITREidDataService_1_2 {
private static Logger logger = LoggerFactory.getLogger(TestMITREidDataService_1_2.class);
@Mock @Mock
private OAuth2ClientRepository clientRepository; private OAuth2ClientRepository clientRepository;
@Mock @Mock
@ -317,7 +323,7 @@ public class TestMITREidDataService_1_2 {
" ]" + " ]" +
"}"; "}";
System.err.println(configJson); logger.debug(configJson);
JsonReader reader = new JsonReader(new StringReader(configJson)); JsonReader reader = new JsonReader(new StringReader(configJson));
final Map<Long, OAuth2RefreshTokenEntity> fakeDb = new HashMap<Long, OAuth2RefreshTokenEntity>(); final Map<Long, OAuth2RefreshTokenEntity> fakeDb = new HashMap<Long, OAuth2RefreshTokenEntity>();
@ -585,7 +591,7 @@ public class TestMITREidDataService_1_2 {
"}"; "}";
System.err.println(configJson); logger.debug(configJson);
JsonReader reader = new JsonReader(new StringReader(configJson)); JsonReader reader = new JsonReader(new StringReader(configJson));
@ -797,7 +803,7 @@ public class TestMITREidDataService_1_2 {
" ]" + " ]" +
"}"; "}";
System.err.println(configJson); logger.debug(configJson);
JsonReader reader = new JsonReader(new StringReader(configJson)); JsonReader reader = new JsonReader(new StringReader(configJson));
@ -950,7 +956,7 @@ public class TestMITREidDataService_1_2 {
"}"; "}";
System.err.println(configJson); logger.debug(configJson);
JsonReader reader = new JsonReader(new StringReader(configJson)); JsonReader reader = new JsonReader(new StringReader(configJson));
@ -1091,7 +1097,7 @@ public class TestMITREidDataService_1_2 {
" ]" + " ]" +
"}"; "}";
System.err.println(configJson); logger.debug(configJson);
JsonReader reader = new JsonReader(new StringReader(configJson)); JsonReader reader = new JsonReader(new StringReader(configJson));
@ -1316,7 +1322,7 @@ public class TestMITREidDataService_1_2 {
" ]" + " ]" +
"}"; "}";
System.err.println(configJson); logger.debug(configJson);
JsonReader reader = new JsonReader(new StringReader(configJson)); JsonReader reader = new JsonReader(new StringReader(configJson));
@ -1389,7 +1395,7 @@ public class TestMITREidDataService_1_2 {
OAuth2Request req1 = new OAuth2Request(new HashMap<String, String>(), "client1", new ArrayList<GrantedAuthority>(), OAuth2Request req1 = new OAuth2Request(new HashMap<String, String>(), "client1", new ArrayList<GrantedAuthority>(),
true, new HashSet<String>(), new HashSet<String>(), "http://foo.com", true, new HashSet<String>(), new HashSet<String>(), "http://foo.com",
new HashSet<String>(), null); new HashSet<String>(), null);
Authentication mockAuth1 = mock(Authentication.class, withSettings().serializable()); Authentication mockAuth1 = new UsernamePasswordAuthenticationToken("user1", "pass1", AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
OAuth2Authentication auth1 = new OAuth2Authentication(req1, mockAuth1); OAuth2Authentication auth1 = new OAuth2Authentication(req1, mockAuth1);
AuthenticationHolderEntity holder1 = new AuthenticationHolderEntity(); AuthenticationHolderEntity holder1 = new AuthenticationHolderEntity();
@ -1399,8 +1405,7 @@ public class TestMITREidDataService_1_2 {
OAuth2Request req2 = new OAuth2Request(new HashMap<String, String>(), "client2", new ArrayList<GrantedAuthority>(), OAuth2Request req2 = new OAuth2Request(new HashMap<String, String>(), "client2", new ArrayList<GrantedAuthority>(),
true, new HashSet<String>(), new HashSet<String>(), "http://bar.com", true, new HashSet<String>(), new HashSet<String>(), "http://bar.com",
new HashSet<String>(), null); new HashSet<String>(), null);
Authentication mockAuth2 = mock(Authentication.class, withSettings().serializable()); OAuth2Authentication auth2 = new OAuth2Authentication(req2, null);
OAuth2Authentication auth2 = new OAuth2Authentication(req2, mockAuth2);
AuthenticationHolderEntity holder2 = new AuthenticationHolderEntity(); AuthenticationHolderEntity holder2 = new AuthenticationHolderEntity();
holder2.setId(2L); holder2.setId(2L);
@ -1408,14 +1413,14 @@ public class TestMITREidDataService_1_2 {
List<AuthenticationHolderEntity> allAuthHolders = ImmutableList.of(holder1, holder2); List<AuthenticationHolderEntity> allAuthHolders = ImmutableList.of(holder1, holder2);
Mockito.when(clientRepository.getAllClients()).thenReturn(new HashSet<ClientDetailsEntity>()); when(clientRepository.getAllClients()).thenReturn(new HashSet<ClientDetailsEntity>());
Mockito.when(approvedSiteRepository.getAll()).thenReturn(new HashSet<ApprovedSite>()); when(approvedSiteRepository.getAll()).thenReturn(new HashSet<ApprovedSite>());
Mockito.when(wlSiteRepository.getAll()).thenReturn(new HashSet<WhitelistedSite>()); when(wlSiteRepository.getAll()).thenReturn(new HashSet<WhitelistedSite>());
Mockito.when(blSiteRepository.getAll()).thenReturn(new HashSet<BlacklistedSite>()); when(blSiteRepository.getAll()).thenReturn(new HashSet<BlacklistedSite>());
Mockito.when(authHolderRepository.getAll()).thenReturn(allAuthHolders); when(authHolderRepository.getAll()).thenReturn(allAuthHolders);
Mockito.when(tokenRepository.getAllAccessTokens()).thenReturn(new HashSet<OAuth2AccessTokenEntity>()); when(tokenRepository.getAllAccessTokens()).thenReturn(new HashSet<OAuth2AccessTokenEntity>());
Mockito.when(tokenRepository.getAllRefreshTokens()).thenReturn(new HashSet<OAuth2RefreshTokenEntity>()); when(tokenRepository.getAllRefreshTokens()).thenReturn(new HashSet<OAuth2RefreshTokenEntity>());
Mockito.when(sysScopeRepository.getAll()).thenReturn(new HashSet<SystemScope>()); when(sysScopeRepository.getAll()).thenReturn(new HashSet<SystemScope>());
// do the data export // do the data export
StringWriter stringWriter = new StringWriter(); StringWriter stringWriter = new StringWriter();
@ -1475,7 +1480,16 @@ public class TestMITREidDataService_1_2 {
if (compare == null) { if (compare == null) {
fail("Could not find matching authentication holder id: " + holder.get("id").getAsString()); fail("Could not find matching authentication holder id: " + holder.get("id").getAsString());
} else { } else {
assertTrue(holder.get("authentication").isJsonObject()); assertTrue(holder.get("clientId").getAsString().equals(compare.getClientId()));
assertTrue(holder.get("approved").getAsBoolean() == compare.isApproved());
assertTrue(holder.get("redirectUri").getAsString().equals(compare.getRedirectUri()));
if (compare.getUserAuth() != null) {
assertTrue(holder.get("savedUserAuthentication").isJsonObject());
JsonObject savedAuth = holder.get("savedUserAuthentication").getAsJsonObject();
assertTrue(savedAuth.get("name").getAsString().equals(compare.getUserAuth().getName()));
assertTrue(savedAuth.get("authenticated").getAsBoolean() == compare.getUserAuth().isAuthenticated());
assertTrue(savedAuth.get("sourceClass").getAsString().equals(compare.getUserAuth().getSourceClass()));
}
checked.add(compare); checked.add(compare);
} }
} }
@ -1515,14 +1529,14 @@ public class TestMITREidDataService_1_2 {
"\"" + MITREidDataService.SYSTEMSCOPES + "\": [], " + "\"" + MITREidDataService.SYSTEMSCOPES + "\": [], " +
"\"" + MITREidDataService.AUTHENTICATIONHOLDERS + "\": [" + "\"" + MITREidDataService.AUTHENTICATIONHOLDERS + "\": [" +
"{\"id\":1,\"authentication\":{\"authorizationRequest\":{\"clientId\":\"client1\",\"redirectUri\":\"http://foo.com\"}," "{\"id\":1,\"clientId\":\"client1\",\"redirectUri\":\"http://foo.com\","
+ "\"userAuthentication\":null}}," + + "\"savedUserAuthentication\":null}," +
"{\"id\":2,\"authentication\":{\"authorizationRequest\":{\"clientId\":\"client2\",\"redirectUri\":\"http://bar.com\"}," "{\"id\":2,\"clientId\":\"client2\",\"redirectUri\":\"http://bar.com\","
+ "\"userAuthentication\":null}}" + + "\"savedUserAuthentication\":null}" +
" ]" + " ]" +
"}"; "}";
System.err.println(configJson); logger.debug(configJson);
JsonReader reader = new JsonReader(new StringReader(configJson)); JsonReader reader = new JsonReader(new StringReader(configJson));
@ -1705,7 +1719,7 @@ public class TestMITREidDataService_1_2 {
" ]" + " ]" +
"}"; "}";
System.err.println(configJson); logger.debug(configJson);
JsonReader reader = new JsonReader(new StringReader(configJson)); JsonReader reader = new JsonReader(new StringReader(configJson));
@ -1812,7 +1826,7 @@ public class TestMITREidDataService_1_2 {
" ]" + " ]" +
"}"; "}";
System.err.println(configJson); logger.debug(configJson);
JsonReader reader = new JsonReader(new StringReader(configJson)); JsonReader reader = new JsonReader(new StringReader(configJson));
final Map<Long, OAuth2RefreshTokenEntity> fakeRefreshTokenTable = new HashMap<Long, OAuth2RefreshTokenEntity>(); final Map<Long, OAuth2RefreshTokenEntity> fakeRefreshTokenTable = new HashMap<Long, OAuth2RefreshTokenEntity>();