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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.http.lib;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.mockito.Mockito.mock;
22  
23  import javax.servlet.FilterChain;
24  import javax.servlet.FilterConfig;
25  import javax.servlet.ServletResponse;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletRequestWrapper;
28  
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.fs.CommonConfigurationKeys;
31  import org.apache.hadoop.hbase.testclassification.SmallTests;
32  import org.apache.hadoop.hbase.http.ServerConfigurationKeys;
33  import org.apache.hadoop.hbase.http.lib.StaticUserWebFilter.StaticUserFilter;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  import org.mockito.ArgumentCaptor;
37  import org.mockito.Mockito;
38  
39  @Category(SmallTests.class)
40  public class TestStaticUserWebFilter {
41    private FilterConfig mockConfig(String username) {
42      FilterConfig mock = Mockito.mock(FilterConfig.class);
43      Mockito.doReturn(username).when(mock).getInitParameter(
44              ServerConfigurationKeys.HBASE_HTTP_STATIC_USER);
45      return mock;
46    }
47    
48    @Test
49    public void testFilter() throws Exception {
50      FilterConfig config = mockConfig("myuser");
51      StaticUserFilter suf = new StaticUserFilter();
52      suf.init(config);
53      
54      ArgumentCaptor<HttpServletRequestWrapper> wrapperArg =
55        ArgumentCaptor.forClass(HttpServletRequestWrapper.class);
56  
57      FilterChain chain = mock(FilterChain.class);
58      
59      suf.doFilter(mock(HttpServletRequest.class), mock(ServletResponse.class),
60          chain);
61          
62      Mockito.verify(chain).doFilter(wrapperArg.capture(), Mockito.<ServletResponse>anyObject());
63      
64      HttpServletRequestWrapper wrapper = wrapperArg.getValue();
65      assertEquals("myuser", wrapper.getUserPrincipal().getName());
66      assertEquals("myuser", wrapper.getRemoteUser());
67      
68      suf.destroy();
69    }
70    
71    @Test
72    public void testOldStyleConfiguration() {
73      Configuration conf = new Configuration();
74      conf.set("dfs.web.ugi", "joe,group1,group2");
75      assertEquals("joe", StaticUserWebFilter.getUsernameFromConf(conf));
76    }
77  
78    @Test
79    public void testConfiguration() {
80      Configuration conf = new Configuration();
81      conf.set(CommonConfigurationKeys.HADOOP_HTTP_STATIC_USER, "dr.stack");
82      assertEquals("dr.stack", StaticUserWebFilter.getUsernameFromConf(conf));
83    }
84  
85  }