View Javadoc

    1   package org.apache.turbine.pipeline;
    2
    3
    4   /*
    5    * Licensed to the Apache Software Foundation (ASF) under one
    6    * or more contributor license agreements.  See the NOTICE file
    7    * distributed with this work for additional information
    8    * regarding copyright ownership.  The ASF licenses this file
    9    * to you under the Apache License, Version 2.0 (the
    10   * "License"); you may not use this file except in compliance
    11   * with the License.  You may obtain a copy of the License at
    12   *
    13   *   http://www.apache.org/licenses/LICENSE-2.0
    14   *
    15   * Unless required by applicable law or agreed to in writing,
    16   * software distributed under the License is distributed on an
    17   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    18   * KIND, either express or implied.  See the License for the
    19   * specific language governing permissions and limitations
    20   * under the License.
    21   */
    22
    23
    24  import static org.junit.Assert.assertEquals;
    25  import static org.junit.Assert.assertFalse;
    26  import static org.junit.Assert.assertNotNull;
    27  import static org.junit.Assert.assertTrue;
    28  import static org.mockito.Mockito.mock;
    29  import static org.mockito.Mockito.when;
    30
    31  import java.util.Vector;
    32
    33  import javax.servlet.ServletConfig;
    34  import javax.servlet.http.HttpServletRequest;
    35  import javax.servlet.http.HttpServletResponse;
    36
    37  import org.apache.fulcrum.security.model.turbine.entity.impl.TurbineUserImpl;
    38  import org.apache.turbine.TurbineConstants;
    39  import org.apache.turbine.modules.actions.LoginUser;
    40  import org.apache.turbine.om.security.DefaultUserImpl;
    41  import org.apache.turbine.om.security.User;
    42  import org.apache.turbine.test.BaseTestCase;
    43  import org.apache.turbine.util.RunData;
    44  import org.apache.turbine.util.TurbineConfig;
    45  import org.junit.AfterClass;
    46  import org.junit.Before;
    47  import org.junit.BeforeClass;
    48  import org.junit.Test;
    49
    50  /**
    51   * Tests TurbinePipeline.
    52   *
    53   * @author <a href="mailto:epugh@opensourceConnections.com">Eric Pugh</a>
    54   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
    55   * @version $Id: DefaultSessionValidationValveTest.java 1754909 2016-08-02 12:55:35Z tv $
    56   */
    57  public class DefaultSessionValidationValveTest extends BaseTestCase
    58  {
    59      private static TurbineConfig tc = null;
    60      private ServletConfig config = null;
    61      private HttpServletRequest request = null;
    62      private HttpServletResponse response = null;
    63
    64      @BeforeClass
    65      public static void init()
    66      {
    67          tc = new TurbineConfig(
    68                              ".",
    69                              "/conf/test/CompleteTurbineResources.properties");
    70          tc.initialize();
    71      }
    72
    73      @Before
    74      public void setUpBefore() throws Exception
    75      {
    76          config = mock(ServletConfig.class);
    77          request = getMockRequest();
    78          response = mock(HttpServletResponse.class);
    79      }
    80
    81      /**
    82       * Tests the Valve.
    83       */
    84      @Test public void testAnonymousUser() throws Exception
    85      {
    86          Vector<String> v = new Vector<String>();
    87          v.add(LoginUser.CGI_USERNAME);
    88          v.add(LoginUser.CGI_PASSWORD);
    89          when(request.getParameterNames()).thenReturn(v.elements());
    90
    91          when(request.getParameterValues(LoginUser.CGI_USERNAME)).thenReturn(new String[] { "username" });
    92          when(request.getParameterValues(LoginUser.CGI_PASSWORD)).thenReturn(new String[] { "password" });
    93
    94          RunData runData = getRunData(request,response,config);
    95          runData.setAction(TurbineConstants.ACTION_LOGIN_DEFAULT);
    96
    97          Pipeline pipeline = new TurbinePipeline();
    98          PipelineData pipelineData = runData;
    99
    100         DefaultSessionValidationValve valve = new DefaultSessionValidationValve();
    101         pipeline.addValve(valve);
    102         pipeline.initialize();
    103
    104         pipeline.invoke(pipelineData);
    105         User user = runData.getUser();
    106         assertNotNull(user);
    107         assertEquals("",user.getName());
    108         assertFalse(user.hasLoggedIn());
    109     }
    110
    111     @Test public void testLoggedInUser() throws Exception
    112     {
    113         Vector<String> v = new Vector<String>();
    114         v.add(LoginUser.CGI_USERNAME);
    115         v.add(LoginUser.CGI_PASSWORD);
    116         when(request.getParameterNames()).thenReturn(v.elements());
    117
    118         when(request.getParameterValues(LoginUser.CGI_USERNAME)).thenReturn(new String[] { "username" });
    119         when(request.getParameterValues(LoginUser.CGI_PASSWORD)).thenReturn(new String[] { "password" });
    120
    121         RunData runData = getRunData(request,response,config);
    122         User tu = new DefaultUserImpl(new TurbineUserImpl());
    123         tu.setName("username");
    124         tu.setHasLoggedIn(Boolean.TRUE);
    125         runData.setAction("TestAction");
    126
    127         request.getSession().setAttribute(User.SESSION_KEY, tu);
    128
    129         Pipeline pipeline = new TurbinePipeline();
    130         PipelineData pipelineData = runData;
    131
    132         DefaultSessionValidationValve valve = new DefaultSessionValidationValve();
    133         pipeline.addValve(valve);
    134         pipeline.initialize();
    135
    136         pipeline.invoke(pipelineData);
    137         User user = runData.getUser();
    138         assertNotNull(user);
    139         assertEquals("username",user.getName());
    140         assertTrue(user.hasLoggedIn());
    141     }
    142
    143     @AfterClass
    144     public static void destroy()
    145     {
    146         tc.dispose();
    147     }
    148 }