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 java.util.Vector;
    25
    26  import javax.servlet.http.HttpServletResponse;
    27
    28  import org.apache.fulcrum.security.model.turbine.entity.impl.TurbineUserImpl;
    29  import org.apache.turbine.TurbineConstants;
    30  import org.apache.turbine.modules.actions.LoginUser;
    31  import org.apache.turbine.om.security.DefaultUserImpl;
    32  import org.apache.turbine.om.security.User;
    33  import org.apache.turbine.test.BaseTestCase;
    34  import org.apache.turbine.test.EnhancedMockHttpServletRequest;
    35  import org.apache.turbine.test.EnhancedMockHttpSession;
    36  import org.apache.turbine.util.RunData;
    37  import org.apache.turbine.util.TurbineConfig;
    38  import org.junit.AfterClass;
    39  import org.junit.Before;
    40  import org.junit.BeforeClass;
    41  import org.junit.Test;
    42
    43  import com.mockobjects.servlet.MockHttpServletResponse;
    44  import com.mockobjects.servlet.MockServletConfig;
    45
    46  import static org.junit.Assert.*;
    47
    48  /**
    49   * Tests TurbinePipeline.
    50   *
    51   * @author <a href="mailto:epugh@opensourceConnections.com">Eric Pugh</a>
    52   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
    53   * @version $Id: DefaultSessionValidationValveTest.java 1606111 2014-06-27 14:46:47Z gk $
    54   */
    55  public class DefaultSessionValidationValveTest extends BaseTestCase
    56  {
    57      private static TurbineConfig tc = null;
    58      private MockServletConfig config = null;
    59      private EnhancedMockHttpServletRequest request = null;
    60      private EnhancedMockHttpSession session = null;
    61      private HttpServletResponse response = null;
    62
    63
    64
    65      @BeforeClass
    66      public static void init() {
    67          tc = new TurbineConfig(
    68                              ".",
    69                              "/conf/test/CompleteTurbineResources.properties");
    70          tc.initialize();
    71      }
    72
    73      @Before
    74      public void setUpBefore() throws Exception {
    75          config = new MockServletConfig();
    76          config.setupNoParameters();
    77          request = new EnhancedMockHttpServletRequest();
    78          request.setupServerName("bob");
    79          request.setupGetProtocol("http");
    80          request.setupScheme("scheme");
    81          request.setupPathInfo("damn");
    82          request.setupGetServletPath("damn2");
    83          request.setupGetContextPath("wow");
    84          request.setupGetContentType("html/text");
    85          request.setupAddHeader("Content-type", "html/text");
    86          request.setupAddHeader("Accept-Language", "en-US");
    87
    88          session = new EnhancedMockHttpSession();
    89          response = new MockHttpServletResponse();
    90
    91          request.setSession(session);
    92
    93      }
    94
    95      /**
    96       * Tests the Valve.
    97       */
    98      @Test public void testAnonymousUser() throws Exception
    99      {
    100         Vector<String> v = new Vector<String>();
    101         v.add(LoginUser.CGI_USERNAME);
    102         v.add(LoginUser.CGI_PASSWORD);
    103         request.setupGetParameterNames(v.elements());
    104
    105         request.setupAddParameter(LoginUser.CGI_USERNAME,"username");
    106         request.setupAddParameter(LoginUser.CGI_PASSWORD,"password");
    107
    108         RunData runData = getRunData(request,response,config);
    109         runData.setAction(TurbineConstants.ACTION_LOGIN_DEFAULT);
    110
    111         Pipeline pipeline = new TurbinePipeline();
    112         PipelineData pipelineData = runData;
    113
    114         DefaultSessionValidationValve valve = new DefaultSessionValidationValve();
    115         pipeline.addValve(valve);
    116         pipeline.initialize();
    117
    118         pipeline.invoke(pipelineData);
    119         User user = runData.getUser();
    120         assertNotNull(user);
    121         assertEquals("",user.getName());
    122         assertFalse(user.hasLoggedIn());
    123     }
    124
    125     @Test public void testLoggedInUser() throws Exception
    126     {
    127         Vector<String> v = new Vector<String>();
    128         v.add(LoginUser.CGI_USERNAME);
    129         v.add(LoginUser.CGI_PASSWORD);
    130         request.setupGetParameterNames(v.elements());
    131
    132         request.setupAddParameter(LoginUser.CGI_USERNAME,"username");
    133         request.setupAddParameter(LoginUser.CGI_PASSWORD,"password");
    134
    135         RunData runData = getRunData(request,response,config);
    136         User tu = new DefaultUserImpl(new TurbineUserImpl());
    137         tu.setName("username");
    138         tu.setHasLoggedIn(Boolean.TRUE);
    139         runData.setAction("TestAction");
    140
    141         session.setupGetAttribute(User.SESSION_KEY, tu);
    142
    143         Pipeline pipeline = new TurbinePipeline();
    144         PipelineData pipelineData = runData;
    145
    146         DefaultSessionValidationValve valve = new DefaultSessionValidationValve();
    147         pipeline.addValve(valve);
    148         pipeline.initialize();
    149
    150         pipeline.invoke(pipelineData);
    151         User user = runData.getUser();
    152         assertNotNull(user);
    153         assertEquals("username",user.getName());
    154         assertTrue(user.hasLoggedIn());
    155     }
    156
    157     @AfterClass
    158     public static void destroy() {
    159         tc.dispose();
    160     }
    161 }