fixed unit tests

pull/743/head
Justin Richer 2014-11-22 23:45:22 -05:00
parent 1a2ca25359
commit 3e7ade9a67
2 changed files with 58 additions and 51 deletions

View File

@ -149,7 +149,8 @@ public class DefaultOAuth2ClientDetailsEntityService implements ClientDetailsEnt
} }
private void ensureRefreshTokenConsistency(ClientDetailsEntity client) { private void ensureRefreshTokenConsistency(ClientDetailsEntity client) {
if (client.getAuthorizedGrantTypes().contains("refresh_token") || client.getScope().contains(SystemScopeService.OFFLINE_ACCESS)) { if (client.getAuthorizedGrantTypes().contains("refresh_token")
|| client.getScope().contains(SystemScopeService.OFFLINE_ACCESS)) {
client.getScope().add(SystemScopeService.OFFLINE_ACCESS); client.getScope().add(SystemScopeService.OFFLINE_ACCESS);
client.getAuthorizedGrantTypes().add("refresh_token"); client.getAuthorizedGrantTypes().add("refresh_token");
} }

View File

@ -16,11 +16,13 @@
******************************************************************************/ ******************************************************************************/
package org.mitre.oauth2.service.impl; package org.mitre.oauth2.service.impl;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail; import static org.junit.Assert.fail;
import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.junit.Before; import org.junit.Before;
@ -39,7 +41,9 @@ import org.mockito.InjectMocks;
import org.mockito.Matchers; import org.mockito.Matchers;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import org.springframework.security.oauth2.common.exceptions.InvalidClientException; import org.springframework.security.oauth2.common.exceptions.InvalidClientException;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
@ -78,6 +82,31 @@ public class TestDefaultOAuth2ClientDetailsEntityService {
@Before @Before
public void prepare() { public void prepare() {
Mockito.reset(clientRepository, tokenRepository, approvedSiteService, whitelistedSiteService, blacklistedSiteService, scopeService, statsService); Mockito.reset(clientRepository, tokenRepository, approvedSiteService, whitelistedSiteService, blacklistedSiteService, scopeService, statsService);
Mockito.when(clientRepository.saveClient(Mockito.any(ClientDetailsEntity.class))).thenAnswer(new Answer<ClientDetailsEntity>() {
@Override
public ClientDetailsEntity answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
return (ClientDetailsEntity) args[0];
}
});
Mockito.when(clientRepository.updateClient(Mockito.anyLong(), Mockito.any(ClientDetailsEntity.class))).thenAnswer(new Answer<ClientDetailsEntity>() {
@Override
public ClientDetailsEntity answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
return (ClientDetailsEntity) args[1];
}
});
Mockito.when(scopeService.removeRestrictedScopes(Mockito.anySet())).thenAnswer(new Answer<Set<String>>() {
@Override
public Set<String> answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
return (Set<String>) args[0];
}
});
} }
/** /**
@ -128,20 +157,15 @@ public class TestDefaultOAuth2ClientDetailsEntityService {
@Test @Test
public void saveNewClient_yesOfflineAccess() { public void saveNewClient_yesOfflineAccess() {
ClientDetailsEntity client = Mockito.mock(ClientDetailsEntity.class); ClientDetailsEntity client = new ClientDetailsEntity();
Mockito.when(client.getId()).thenReturn(null);
Mockito.when(client.isAllowRefresh()).thenReturn(true); Set<String> grantTypes = new HashSet<String>();
grantTypes.add("refresh_token");
client.setGrantTypes(grantTypes);
// scopes returned by client entities are Strings client = service.saveNewClient(client);
@SuppressWarnings("unchecked")
Set<String> scopes = Mockito.mock(Set.class);
Mockito.when(client.getScope()).thenReturn(scopes); assertThat(client.getScope().contains(SystemScopeService.OFFLINE_ACCESS), is(equalTo(true)));
service.saveNewClient(client);
Mockito.verify(scopes).add(SystemScopeService.OFFLINE_ACCESS);
} }
/** /**
@ -150,20 +174,11 @@ public class TestDefaultOAuth2ClientDetailsEntityService {
@Test @Test
public void saveNewClient_noOfflineAccess() { public void saveNewClient_noOfflineAccess() {
ClientDetailsEntity client = Mockito.mock(ClientDetailsEntity.class); ClientDetailsEntity client = new ClientDetailsEntity();
Mockito.when(client.getId()).thenReturn(null);
Mockito.when(client.isAllowRefresh()).thenReturn(false); client = service.saveNewClient(client);
// scopes returned by client entities are Strings assertThat(client.getScope().contains(SystemScopeService.OFFLINE_ACCESS), is(equalTo(false)));
@SuppressWarnings("unchecked")
Set<String> scopes = Mockito.mock(Set.class);
Mockito.when(client.getScope()).thenReturn(scopes);
service.saveNewClient(client);
Mockito.verify(scopes).remove(SystemScopeService.OFFLINE_ACCESS);
} }
@Test @Test
@ -276,38 +291,29 @@ public class TestDefaultOAuth2ClientDetailsEntityService {
@Test @Test
public void updateClient_yesOfflineAccess() { public void updateClient_yesOfflineAccess() {
ClientDetailsEntity oldClient = Mockito.mock(ClientDetailsEntity.class); ClientDetailsEntity oldClient = new ClientDetailsEntity();
ClientDetailsEntity newClient = Mockito.mock(ClientDetailsEntity.class); ClientDetailsEntity client = new ClientDetailsEntity();
Mockito.when(newClient.isAllowRefresh()).thenReturn(true); Set<String> grantTypes = new HashSet<String>();
grantTypes.add("refresh_token");
client.setGrantTypes(grantTypes);
// scopes returned by client entities are Strings client = service.updateClient(oldClient, client);
@SuppressWarnings("unchecked")
Set<String> scopes = Mockito.mock(Set.class);
Mockito.when(newClient.getScope()).thenReturn(scopes); assertThat(client.getScope().contains(SystemScopeService.OFFLINE_ACCESS), is(equalTo(true)));
service.updateClient(oldClient, newClient);
Mockito.verify(scopes).add(SystemScopeService.OFFLINE_ACCESS);
} }
@Test @Test
public void updateClient_noOfflineAccess() { public void updateClient_noOfflineAccess() {
ClientDetailsEntity oldClient = Mockito.mock(ClientDetailsEntity.class); ClientDetailsEntity oldClient = new ClientDetailsEntity();
ClientDetailsEntity newClient = Mockito.mock(ClientDetailsEntity.class);
Mockito.when(newClient.isAllowRefresh()).thenReturn(false); oldClient.getScope().add(SystemScopeService.OFFLINE_ACCESS);
// scopes returned by client entities are Strings ClientDetailsEntity client = new ClientDetailsEntity();
@SuppressWarnings("unchecked")
Set<String> scopes = Mockito.mock(Set.class);
Mockito.when(newClient.getScope()).thenReturn(scopes); client = service.updateClient(oldClient, client);
service.updateClient(oldClient, newClient); assertThat(client.getScope().contains(SystemScopeService.OFFLINE_ACCESS), is(equalTo(false)));
Mockito.verify(scopes).remove(SystemScopeService.OFFLINE_ACCESS);
} }
} }