pull/1367/merge
Aishwarya Gosavi 2022-05-02 14:09:10 +09:00 committed by GitHub
commit 4d33787ea9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 138 additions and 4 deletions

View File

@ -37,13 +37,16 @@ import org.springframework.security.core.Authentication;
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;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is; 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.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@ -176,6 +179,7 @@ public class TestDefaultIntrospectionResultAssembler {
assertThat(result, is(equalTo(expected))); assertThat(result, is(equalTo(expected)));
} }
@Test @Test
public void shouldAssembleExpectedResultForAccessTokenWithoutUserAuthentication() throws ParseException { public void shouldAssembleExpectedResultForAccessTokenWithoutUserAuthentication() throws ParseException {
// given // given
@ -256,7 +260,7 @@ public class TestDefaultIntrospectionResultAssembler {
} }
@Test @Test
public void shouldAssembleExpectedResultForRefreshTokenWithoutExpiry() { public void shouldAssembleExpectedResultForRefreshTokenWithoutExpiry(){
// given // given
OAuth2RefreshTokenEntity refreshToken = refreshToken(null, OAuth2RefreshTokenEntity refreshToken = refreshToken(null,
@ -304,6 +308,74 @@ public class TestDefaultIntrospectionResultAssembler {
} }
@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) { private UserInfo userInfo(String sub) {
UserInfo userInfo = mock(UserInfo.class); UserInfo userInfo = mock(UserInfo.class);

View File

@ -24,6 +24,7 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mitre.oauth2.model.SystemScope; import org.mitre.oauth2.model.SystemScope;
import org.mitre.oauth2.repository.SystemScopeRepository; import org.mitre.oauth2.repository.SystemScopeRepository;
import org.mitre.oauth2.service.SystemScopeService;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; 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.equalTo;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
@ -51,6 +54,8 @@ public class TestDefaultSystemScopeService {
private SystemScope defaultScope2; private SystemScope defaultScope2;
private SystemScope dynScope1; private SystemScope dynScope1;
private SystemScope restrictedScope1; private SystemScope restrictedScope1;
private SystemScope mySystemScope;
private SystemScope mySavedSystemScope;
private String defaultDynScope1String = "defaultDynScope1"; private String defaultDynScope1String = "defaultDynScope1";
private String defaultDynScope2String = "defaultDynScope2"; private String defaultDynScope2String = "defaultDynScope2";
@ -58,12 +63,14 @@ public class TestDefaultSystemScopeService {
private String defaultScope2String = "defaultScope2"; private String defaultScope2String = "defaultScope2";
private String dynScope1String = "dynScope1"; private String dynScope1String = "dynScope1";
private String restrictedScope1String = "restrictedScope1"; private String restrictedScope1String = "restrictedScope1";
private String mySavedSystemScopeString = "mySavedSystemScope";
private Set<SystemScope> allScopes; private Set<SystemScope> allScopes;
private Set<String> allScopeStrings; private Set<String> allScopeStrings;
private Set<SystemScope> allScopesWithValue; private Set<SystemScope> allScopesWithValue;
private Set<String> allScopeStringsWithValue; private Set<String> allScopeStringsWithValue;
@Mock @Mock
private SystemScopeRepository repository; private SystemScopeRepository repository;
@ -83,6 +90,9 @@ public class TestDefaultSystemScopeService {
defaultDynScope2 = new SystemScope(defaultDynScope2String); defaultDynScope2 = new SystemScope(defaultDynScope2String);
defaultDynScope1.setDefaultScope(true); defaultDynScope1.setDefaultScope(true);
defaultDynScope2.setDefaultScope(true); defaultDynScope2.setDefaultScope(true);
mySystemScope = new SystemScope();
mySavedSystemScope = new SystemScope(mySavedSystemScopeString);
// two strictly default scopes (restricted) // two strictly default scopes (restricted)
defaultScope1 = new SystemScope(defaultScope1String); defaultScope1 = new SystemScope(defaultScope1String);
@ -114,6 +124,8 @@ public class TestDefaultSystemScopeService {
Mockito.when(repository.getByValue(restrictedScope1String)).thenReturn(restrictedScope1); Mockito.when(repository.getByValue(restrictedScope1String)).thenReturn(restrictedScope1);
Mockito.when(repository.getAll()).thenReturn(allScopes); Mockito.when(repository.getAll()).thenReturn(allScopes);
Mockito.when(repository.save(mySystemScope)).thenReturn(mySavedSystemScope);
} }
@Test @Test
@ -186,4 +198,54 @@ public class TestDefaultSystemScopeService {
assertThat(service.scopesMatch(expected, actualBad), is(false)); 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));
}
} }