|  | 
|  | 1 | +/* | 
|  | 2 | + * Copyright 2011 the original author or authors. | 
|  | 3 | + * | 
|  | 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | 
|  | 5 | + * the License. You may obtain a copy of the License at | 
|  | 6 | + * | 
|  | 7 | + * http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | + * | 
|  | 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | 
|  | 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | 
|  | 11 | + * specific language governing permissions and limitations under the License. | 
|  | 12 | + */ | 
|  | 13 | +package org.springframework.security.oauth2.web.authentication; | 
|  | 14 | + | 
|  | 15 | +import static org.easymock.EasyMock.createMock; | 
|  | 16 | +import static org.junit.Assert.assertEquals; | 
|  | 17 | +import static org.junit.Assert.assertSame; | 
|  | 18 | +import static org.junit.Assert.assertTrue; | 
|  | 19 | + | 
|  | 20 | +import javax.servlet.http.HttpServletRequest; | 
|  | 21 | +import javax.servlet.http.HttpServletResponse; | 
|  | 22 | + | 
|  | 23 | +import org.junit.Before; | 
|  | 24 | +import org.junit.Rule; | 
|  | 25 | +import org.junit.Test; | 
|  | 26 | +import org.junit.rules.ExpectedException; | 
|  | 27 | +import org.springframework.http.HttpEntity; | 
|  | 28 | +import org.springframework.http.HttpStatus; | 
|  | 29 | +import org.springframework.http.ResponseEntity; | 
|  | 30 | +import org.springframework.security.core.AuthenticationException; | 
|  | 31 | +import org.springframework.security.core.userdetails.UsernameNotFoundException; | 
|  | 32 | +import org.springframework.security.oauth2.common.exceptions.InvalidClientException; | 
|  | 33 | +import org.springframework.security.oauth2.provider.web.OAuth2ExceptionRenderer; | 
|  | 34 | +import org.springframework.web.context.request.ServletWebRequest; | 
|  | 35 | + | 
|  | 36 | +/** | 
|  | 37 | + * | 
|  | 38 | + * @author Rob Winch | 
|  | 39 | + */ | 
|  | 40 | +public class TestOAuth2AuthenticationFailureHandler { | 
|  | 41 | +	@Rule | 
|  | 42 | +	public ExpectedException thrown = ExpectedException.none(); | 
|  | 43 | + | 
|  | 44 | +	private OAuth2ExceptionRendererStub renderer; | 
|  | 45 | +	private HttpServletRequest request; | 
|  | 46 | +	private HttpServletResponse response; | 
|  | 47 | +	private AuthenticationException originalException; | 
|  | 48 | + | 
|  | 49 | +	private OAuth2AuthenticationFailureHandler handler; | 
|  | 50 | + | 
|  | 51 | +	@Before | 
|  | 52 | +	public void setUp() { | 
|  | 53 | +		renderer = new OAuth2ExceptionRendererStub(); | 
|  | 54 | +		request = createMock(HttpServletRequest.class); | 
|  | 55 | +		response = createMock(HttpServletResponse.class); | 
|  | 56 | +		originalException = new UsernameNotFoundException("not found"); | 
|  | 57 | + | 
|  | 58 | +		handler = new OAuth2AuthenticationFailureHandler(); | 
|  | 59 | +		handler.setExceptionRenderer(renderer); | 
|  | 60 | +	} | 
|  | 61 | + | 
|  | 62 | +	@Test | 
|  | 63 | +	public void onAuthenticationFailure() throws Exception { | 
|  | 64 | +		handler.onAuthenticationFailure(request, response, originalException); | 
|  | 65 | +		Object body = renderer.entity.getBody(); | 
|  | 66 | +		assertTrue("The entity should be an InvalidClientException. Got "+body,body instanceof InvalidClientException); | 
|  | 67 | +		assertEquals(HttpStatus.UNAUTHORIZED,renderer.entity.getStatusCode()); | 
|  | 68 | +		assertSame(request,renderer.webRequest.getNativeRequest()); | 
|  | 69 | +		assertSame(response,renderer.webRequest.getNativeResponse()); | 
|  | 70 | +	} | 
|  | 71 | + | 
|  | 72 | +	@Test | 
|  | 73 | +	public void setExceptionRendererNullExceptionRenderer() { | 
|  | 74 | +		thrown.expect(IllegalArgumentException.class); | 
|  | 75 | +		thrown.expectMessage("exceptionRenderer cannot be null"); | 
|  | 76 | +		handler.setExceptionRenderer(null); | 
|  | 77 | +	} | 
|  | 78 | + | 
|  | 79 | +	/** | 
|  | 80 | +	 * Rather than deal with EasyMock class extension just capture the arguments using this stub | 
|  | 81 | +	 */ | 
|  | 82 | +	private static class OAuth2ExceptionRendererStub extends OAuth2ExceptionRenderer { | 
|  | 83 | +		private ResponseEntity<?> entity; | 
|  | 84 | +		private ServletWebRequest webRequest; | 
|  | 85 | +		@Override | 
|  | 86 | +		public void handleHttpEntityResponse(HttpEntity<?> responseEntity, ServletWebRequest webRequest) | 
|  | 87 | +				throws Exception { | 
|  | 88 | +			this.entity = (ResponseEntity<?>) responseEntity; | 
|  | 89 | +			this.webRequest = webRequest; | 
|  | 90 | +		} | 
|  | 91 | +	} | 
|  | 92 | +} | 
0 commit comments