/*
 * Copyright 2002-2011 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.security.oauth2.provider.vote;

import static org.junit.Assert.assertEquals;

import java.util.Collections;

import org.junit.Test;
import org.springframework.security.access.AccessDecisionVoter;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.common.exceptions.InsufficientScopeException;
import org.springframework.security.oauth2.provider.AuthorizationRequest;
import org.springframework.security.oauth2.provider.OAuth2Authentication;

/**
 * @author Dave Syer
 * 
 */
public class TestScopeVoter {

	private ScopeVoter voter = new ScopeVoter();

	@Test
	public void testAbstainIfNotOAuth2() throws Exception {
		Authentication clientAuthentication = new UsernamePasswordAuthenticationToken("foo", "bar");
		assertEquals(
				AccessDecisionVoter.ACCESS_ABSTAIN,
				voter.vote(clientAuthentication, null,
						Collections.<ConfigAttribute> singleton(new SecurityConfig("SCOPE_READ"))));
	}

	@Test
	public void testDenyIfOAuth2AndExplictlyDenied() throws Exception {
		AuthorizationRequest clientAuthentication = new AuthorizationRequest("foo", Collections.singleton("read"),
				null, null);
		Authentication userAuthentication = null;
		OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(clientAuthentication, userAuthentication);
		assertEquals(
				AccessDecisionVoter.ACCESS_DENIED,
				voter.vote(oAuth2Authentication, null,
						Collections.<ConfigAttribute> singleton(new SecurityConfig("DENY_OAUTH"))));
	}

	@Test
	public void testAccessGrantedIfScopesPresent() throws Exception {
		AuthorizationRequest clientAuthentication = new AuthorizationRequest("foo", Collections.singleton("read"),
				null, null);
		Authentication userAuthentication = null;
		OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(clientAuthentication, userAuthentication);
		assertEquals(
				AccessDecisionVoter.ACCESS_GRANTED,
				voter.vote(oAuth2Authentication, null,
						Collections.<ConfigAttribute> singleton(new SecurityConfig("SCOPE_READ"))));
	}

	@Test
	public void testAccessGrantedIfScopesPresentWithPrefix() throws Exception {
		voter.setScopePrefix("scope=");
		AuthorizationRequest clientAuthentication = new AuthorizationRequest("foo", Collections.singleton("read"), null, null);
		Authentication userAuthentication = null;
		OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(clientAuthentication, userAuthentication);
		assertEquals(
				AccessDecisionVoter.ACCESS_GRANTED,
				voter.vote(oAuth2Authentication, null,
						Collections.<ConfigAttribute> singleton(new SecurityConfig("scope=read"))));
	}

	@Test
	public void testAccessDeniedIfWrongScopesPresent() throws Exception {
		AuthorizationRequest clientAuthentication = new AuthorizationRequest("foo", Collections.singleton("read"),
				null, null);
		Authentication userAuthentication = null;
		OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(clientAuthentication, userAuthentication);
		voter.setThrowException(false);
		assertEquals(
				AccessDecisionVoter.ACCESS_DENIED,
				voter.vote(oAuth2Authentication, null,
						Collections.<ConfigAttribute> singleton(new SecurityConfig("SCOPE_WRITE"))));
	}

	@Test(expected = InsufficientScopeException.class)
	public void testExceptionThrownIfWrongScopesPresent() throws Exception {
		AuthorizationRequest clientAuthentication = new AuthorizationRequest("foo", Collections.singleton("read"),
				null, null);
		Authentication userAuthentication = null;
		OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(clientAuthentication, userAuthentication);
		assertEquals(
				AccessDecisionVoter.ACCESS_DENIED,
				voter.vote(oAuth2Authentication, null,
						Collections.<ConfigAttribute> singleton(new SecurityConfig("SCOPE_WRITE"))));
	}
}

@@ -839,16 +839,82 @@
 nVoter;%0A
+import org.springframework.security.access.AccessDeniedException;%0A
 import o
@@ -1173,97 +1173,8 @@
 on;%0A
-import org.springframework.security.oauth2.common.exceptions.InsufficientScopeException;%0A
 impo
@@ -4051,25 +4051,20 @@
 d = 
-InsufficientScope
+AccessDenied
 Exce

fix bug in test case