|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright 2015 The MITRE Corporation |
| 3 | + * and the MIT Kerberos and Internet Trust Consortium |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + *******************************************************************************/ |
| 17 | + |
| 18 | +package org.mitre.oauth2.introspectingfilter.service.impl; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.Test; |
| 25 | +import org.springframework.security.core.GrantedAuthority; |
| 26 | +import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| 27 | + |
| 28 | +import com.google.gson.JsonObject; |
| 29 | + |
| 30 | +import static org.junit.Assert.assertTrue; |
| 31 | + |
| 32 | +/** |
| 33 | + * @author jricher |
| 34 | + * |
| 35 | + */ |
| 36 | +public class TestScopeBasedIntrospectionAuthoritiesGranter { |
| 37 | + |
| 38 | +private JsonObject introspectionResponse; |
| 39 | + |
| 40 | +private ScopeBasedIntrospectionAuthoritiesGranter granter = new ScopeBasedIntrospectionAuthoritiesGranter(); |
| 41 | + |
| 42 | +/** |
| 43 | + * @throws java.lang.Exception |
| 44 | + */ |
| 45 | +@Before |
| 46 | +public void setUp() throws Exception { |
| 47 | +introspectionResponse = new JsonObject(); |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * Test method for {@link org.mitre.oauth2.introspectingfilter.service.impl.ScopeBasedIntrospectionAuthoritiesGranter#getAuthorities(com.google.gson.JsonObject)}. |
| 52 | + */ |
| 53 | +@Test |
| 54 | +public void testGetAuthoritiesJsonObject_withScopes() { |
| 55 | +introspectionResponse.addProperty("scope", "foo bar baz batman"); |
| 56 | + |
| 57 | +List<GrantedAuthority> expected = new ArrayList<>(); |
| 58 | +expected.add(new SimpleGrantedAuthority("ROLE_API")); |
| 59 | +expected.add(new SimpleGrantedAuthority("OAUTH_SCOPE_foo")); |
| 60 | +expected.add(new SimpleGrantedAuthority("OAUTH_SCOPE_bar")); |
| 61 | +expected.add(new SimpleGrantedAuthority("OAUTH_SCOPE_baz")); |
| 62 | +expected.add(new SimpleGrantedAuthority("OAUTH_SCOPE_batman")); |
| 63 | + |
| 64 | +List<GrantedAuthority> authorities = granter.getAuthorities(introspectionResponse); |
| 65 | + |
| 66 | +assertTrue(authorities.containsAll(expected)); |
| 67 | +assertTrue(expected.containsAll(authorities)); |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Test method for {@link org.mitre.oauth2.introspectingfilter.service.impl.ScopeBasedIntrospectionAuthoritiesGranter#getAuthorities(com.google.gson.JsonObject)}. |
| 72 | + */ |
| 73 | +@Test |
| 74 | +public void testGetAuthoritiesJsonObject_withoutScopes() { |
| 75 | + |
| 76 | +List<GrantedAuthority> expected = new ArrayList<>(); |
| 77 | +expected.add(new SimpleGrantedAuthority("ROLE_API")); |
| 78 | + |
| 79 | +List<GrantedAuthority> authorities = granter.getAuthorities(introspectionResponse); |
| 80 | + |
| 81 | +assertTrue(authorities.containsAll(expected)); |
| 82 | +assertTrue(expected.containsAll(authorities)); |
| 83 | +} |
| 84 | + |
| 85 | +} |
0 commit comments