Merge dbba32fda6
into d074573de0
commit
4d33787ea9
|
@ -37,13 +37,16 @@ import org.springframework.security.core.Authentication;
|
|||
import org.springframework.security.oauth2.provider.OAuth2Authentication;
|
||||
import org.springframework.security.oauth2.provider.OAuth2Request;
|
||||
|
||||
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
@ -175,7 +178,8 @@ public class TestDefaultIntrospectionResultAssembler {
|
|||
.build();
|
||||
assertThat(result, is(equalTo(expected)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void shouldAssembleExpectedResultForAccessTokenWithoutUserAuthentication() throws ParseException {
|
||||
// given
|
||||
|
@ -256,7 +260,7 @@ public class TestDefaultIntrospectionResultAssembler {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void shouldAssembleExpectedResultForRefreshTokenWithoutExpiry() {
|
||||
public void shouldAssembleExpectedResultForRefreshTokenWithoutExpiry(){
|
||||
|
||||
// given
|
||||
OAuth2RefreshTokenEntity refreshToken = refreshToken(null,
|
||||
|
@ -302,7 +306,75 @@ public class TestDefaultIntrospectionResultAssembler {
|
|||
.build();
|
||||
assertThat(result, is(equalTo(expected)));
|
||||
}
|
||||
|
||||
|
||||
@Test(expected= ParseException.class)
|
||||
public void testAssembleFrom_OAuth2AccessTokenEntityExpiration() throws ParseException{
|
||||
|
||||
// given
|
||||
OAuth2AccessTokenEntity accessToken = accessToken(null, scopes("foo", "bar"), null, "Bearer",
|
||||
oauth2AuthenticationWithUser(oauth2Request("clientId"), "name"));
|
||||
|
||||
UserInfo userInfo = userInfo("sub");
|
||||
|
||||
Set<String> authScopes = scopes("foo", "bar", "baz");
|
||||
|
||||
// when
|
||||
Map<String, Object> result = assembler.assembleFrom(accessToken, userInfo, authScopes);
|
||||
|
||||
|
||||
// then
|
||||
Map<String, Object> expected = new ImmutableMap.Builder<String, Object>()
|
||||
.put("sub", "sub")
|
||||
.put("exp",123L)
|
||||
.put("scope", "bar foo")
|
||||
.put("active", Boolean.TRUE)
|
||||
.put("user_id", "name")
|
||||
.put("client_id", "clientId")
|
||||
.put("token_type", "Bearer")
|
||||
.build();
|
||||
|
||||
|
||||
doThrow(ParseException.class).when(accessToken).getExpiration();
|
||||
accessToken.getExpiration();
|
||||
|
||||
//not reachable
|
||||
assertThat(result, is(not(equalTo(expected))));
|
||||
}
|
||||
|
||||
@Test(expected=ParseException.class)
|
||||
public void testAssembleFrom_OAuth2RefreshTokenEntityExpiration() throws ParseException{
|
||||
|
||||
// given
|
||||
OAuth2RefreshTokenEntity refreshToken = refreshToken(null,
|
||||
oauth2AuthenticationWithUser(oauth2Request("clientId", scopes("foo", "bar")), "name"));
|
||||
|
||||
UserInfo userInfo = userInfo("sub");
|
||||
|
||||
Set<String> authScopes = scopes("foo", "bar", "baz");
|
||||
|
||||
// when
|
||||
Map<String, Object> result = assembler.assembleFrom(refreshToken, userInfo, authScopes);
|
||||
|
||||
|
||||
// then
|
||||
Map<String, Object> expected = new ImmutableMap.Builder<String, Object>()
|
||||
.put("sub", "sub")
|
||||
.put("exp", 123L)
|
||||
.put("scope", "bar foo")
|
||||
.put("active", Boolean.TRUE)
|
||||
.put("user_id", "name")
|
||||
.put("client_id", "clientId")
|
||||
.build();
|
||||
|
||||
doThrow(ParseException.class).when(refreshToken).getExpiration();
|
||||
refreshToken.getExpiration();
|
||||
|
||||
//not reachable
|
||||
assertThat(result, is(not(equalTo(expected))));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private UserInfo userInfo(String sub) {
|
||||
|
|
|
@ -24,6 +24,7 @@ import org.junit.Test;
|
|||
import org.junit.runner.RunWith;
|
||||
import org.mitre.oauth2.model.SystemScope;
|
||||
import org.mitre.oauth2.repository.SystemScopeRepository;
|
||||
import org.mitre.oauth2.service.SystemScopeService;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
|
@ -34,6 +35,8 @@ import com.google.common.collect.Sets;
|
|||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.hamcrest.CoreMatchers.notNullValue;
|
||||
|
||||
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
|
@ -51,19 +54,23 @@ public class TestDefaultSystemScopeService {
|
|||
private SystemScope defaultScope2;
|
||||
private SystemScope dynScope1;
|
||||
private SystemScope restrictedScope1;
|
||||
|
||||
private SystemScope mySystemScope;
|
||||
private SystemScope mySavedSystemScope;
|
||||
|
||||
private String defaultDynScope1String = "defaultDynScope1";
|
||||
private String defaultDynScope2String = "defaultDynScope2";
|
||||
private String defaultScope1String = "defaultScope1";
|
||||
private String defaultScope2String = "defaultScope2";
|
||||
private String dynScope1String = "dynScope1";
|
||||
private String restrictedScope1String = "restrictedScope1";
|
||||
private String mySavedSystemScopeString = "mySavedSystemScope";
|
||||
|
||||
private Set<SystemScope> allScopes;
|
||||
private Set<String> allScopeStrings;
|
||||
private Set<SystemScope> allScopesWithValue;
|
||||
private Set<String> allScopeStringsWithValue;
|
||||
|
||||
|
||||
@Mock
|
||||
private SystemScopeRepository repository;
|
||||
|
||||
|
@ -83,7 +90,10 @@ public class TestDefaultSystemScopeService {
|
|||
defaultDynScope2 = new SystemScope(defaultDynScope2String);
|
||||
defaultDynScope1.setDefaultScope(true);
|
||||
defaultDynScope2.setDefaultScope(true);
|
||||
mySystemScope = new SystemScope();
|
||||
mySavedSystemScope = new SystemScope(mySavedSystemScopeString);
|
||||
|
||||
|
||||
// two strictly default scopes (restricted)
|
||||
defaultScope1 = new SystemScope(defaultScope1String);
|
||||
defaultScope2 = new SystemScope(defaultScope2String);
|
||||
|
@ -114,6 +124,8 @@ public class TestDefaultSystemScopeService {
|
|||
Mockito.when(repository.getByValue(restrictedScope1String)).thenReturn(restrictedScope1);
|
||||
|
||||
Mockito.when(repository.getAll()).thenReturn(allScopes);
|
||||
Mockito.when(repository.save(mySystemScope)).thenReturn(mySavedSystemScope);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -185,5 +197,55 @@ public class TestDefaultSystemScopeService {
|
|||
// extra scope (fail)
|
||||
assertThat(service.scopesMatch(expected, actualBad), is(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeRestrictedAndReservedScopes() {
|
||||
|
||||
Set<SystemScope> unRestrictedScopes = Sets.newHashSet(defaultDynScope1, defaultDynScope2, dynScope1);
|
||||
assertThat(service.removeRestrictedAndReservedScopes(allScopes),equalTo(unRestrictedScopes));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void removeReservedScopes() {
|
||||
Set<SystemScope> unReservedScopes = Sets.newHashSet(defaultDynScope2, defaultDynScope1, defaultScope2,restrictedScope1,defaultScope1,dynScope1);
|
||||
assertThat(service.removeReservedScopes(allScopes), equalTo(unReservedScopes));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getById_notfound() {
|
||||
|
||||
// check null condition
|
||||
|
||||
assertThat(service.getById((long) 60), is(nullValue()));
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getByValue_notfound() {
|
||||
|
||||
// check null condition
|
||||
|
||||
assertThat(service.getByValue("defaultDynScope1String"), is(nullValue()));
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void save_null() {
|
||||
// check null condition
|
||||
assertThat(service.save(defaultScope1), is(nullValue()));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void save_value() {
|
||||
assertThat(service.save(mySystemScope), equalTo(mySavedSystemScope));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getReserveScopes() {
|
||||
assertThat(service.getReserved(), equalTo(SystemScopeService.reservedScopes));
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue