diff --git a/openid-connect-server/src/test/java/org/mitre/oauth2/service/impl/TestDefaultIntrospectionResultAssembler.java b/openid-connect-server/src/test/java/org/mitre/oauth2/service/impl/TestDefaultIntrospectionResultAssembler.java index 423cab6da..47aee4465 100644 --- a/openid-connect-server/src/test/java/org/mitre/oauth2/service/impl/TestDefaultIntrospectionResultAssembler.java +++ b/openid-connect-server/src/test/java/org/mitre/oauth2/service/impl/TestDefaultIntrospectionResultAssembler.java @@ -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 authScopes = scopes("foo", "bar", "baz"); + + // when + Map result = assembler.assembleFrom(accessToken, userInfo, authScopes); + + + // then + Map expected = new ImmutableMap.Builder() + .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 authScopes = scopes("foo", "bar", "baz"); + + // when + Map result = assembler.assembleFrom(refreshToken, userInfo, authScopes); + + + // then + Map expected = new ImmutableMap.Builder() + .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) { diff --git a/openid-connect-server/src/test/java/org/mitre/oauth2/service/impl/TestDefaultSystemScopeService.java b/openid-connect-server/src/test/java/org/mitre/oauth2/service/impl/TestDefaultSystemScopeService.java index 808d96b58..2346f6766 100644 --- a/openid-connect-server/src/test/java/org/mitre/oauth2/service/impl/TestDefaultSystemScopeService.java +++ b/openid-connect-server/src/test/java/org/mitre/oauth2/service/impl/TestDefaultSystemScopeService.java @@ -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 allScopes; private Set allScopeStrings; private Set allScopesWithValue; private Set 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 unRestrictedScopes = Sets.newHashSet(defaultDynScope1, defaultDynScope2, dynScope1); + assertThat(service.removeRestrictedAndReservedScopes(allScopes),equalTo(unRestrictedScopes)); + } + + @Test + public void removeReservedScopes() { + Set 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)); + + } }