View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.web.filter.authz;
20  
21  import org.apache.shiro.SecurityUtils;
22  import org.apache.shiro.authc.UsernamePasswordToken;
23  import org.apache.shiro.test.SecurityManagerTestSupport;
24  import org.junit.Test;
25  
26  import javax.servlet.ServletRequest;
27  import javax.servlet.ServletResponse;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  import java.io.IOException;
31  
32  import static org.easymock.EasyMock.*;
33  
34  /**
35   * Test cases for the {@link AuthorizationFilter} class.
36   */
37  public class AuthorizationFilterTest extends SecurityManagerTestSupport {
38  
39      @Test
40      public void testUserOnAccessDeniedWithResponseError() throws IOException {
41          // Tests when a user (known identity) is denied access and no unauthorizedUrl has been configured.
42          // This should trigger an HTTP response error code.
43  
44          //log in the user using the account provided by the superclass for tests:
45          SecurityUtils.getSubject().login(new UsernamePasswordToken("test", "test"));
46          
47          AuthorizationFilter filter = new AuthorizationFilter() {
48              @Override
49              protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue)
50                      throws Exception {
51                  return false; //for this test case
52              }
53          };
54  
55          HttpServletRequest request = createNiceMock(HttpServletRequest.class);
56          HttpServletResponse response = createNiceMock(HttpServletResponse.class);
57  
58          response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
59          replay(response);
60          filter.onAccessDenied(request, response);
61          verify(response);
62      }
63  
64      @Test
65      public void testUserOnAccessDeniedWithRedirect() throws IOException {
66          // Tests when a user (known identity) is denied access and an unauthorizedUrl *has* been configured.
67          // This should trigger an HTTP redirect
68  
69          //log in the user using the account provided by the superclass for tests:
70          SecurityUtils.getSubject().login(new UsernamePasswordToken("test", "test"));
71  
72          String unauthorizedUrl = "unauthorized.jsp";
73  
74          AuthorizationFilter filter = new AuthorizationFilter() {
75              @Override
76              protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue)
77                      throws Exception {
78                  return false; //for this test case
79              }
80          };
81          filter.setUnauthorizedUrl(unauthorizedUrl);
82  
83          HttpServletRequest request = createNiceMock(HttpServletRequest.class);
84          HttpServletResponse response = createNiceMock(HttpServletResponse.class);
85  
86          expect(request.getContextPath()).andReturn("/").anyTimes();
87  
88          String encoded = "/" + unauthorizedUrl;
89          expect(response.encodeRedirectURL(unauthorizedUrl)).andReturn(encoded);
90          response.sendRedirect(encoded);
91          replay(request);
92          replay(response);
93  
94          filter.onAccessDenied(request, response);
95  
96          verify(request);
97          verify(response);
98      }
99  }