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  import javax.servlet.http.HttpSession;
    37
    38  import org.apache.turbine.TurbineConstants;
    39  import org.apache.turbine.modules.actions.LoginUser;
    40  import org.apache.turbine.om.security.User;
    41  import org.apache.turbine.services.TurbineServices;
    42  import org.apache.turbine.services.security.SecurityService;
    43  import org.apache.turbine.test.BaseTestCase;
    44  import org.apache.turbine.util.RunData;
    45  import org.apache.turbine.util.TurbineConfig;
    46  import org.junit.AfterClass;
    47  import org.junit.Before;
    48  import org.junit.BeforeClass;
    49  import org.junit.Test;
    50
    51  /**
    52   * Tests TurbinePipeline.
    53   *
    54   * @author <a href="mailto:epugh@opensourceConnections.com">Eric Pugh</a>
    55   * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
    56   * @version $Id: DefaultLoginValveTest.java 1754909 2016-08-02 12:55:35Z tv $
    57   */
    58  public class DefaultLoginValveTest extends BaseTestCase
    59  {
    60      private static TurbineConfig tc = null;
    61      private ServletConfig config = null;
    62      private HttpServletRequest request = null;
    63      private HttpServletResponse response = null;
    64      private HttpSession session = null;
    65      private SecurityService securityService = null;
    66
    67      @BeforeClass
    68      public static void init()
    69      {
    70          tc = new TurbineConfig(
    71                              ".",
    72                              "/conf/test/CompleteTurbineResources.properties");
    73          tc.initialize();
    74      }
    75
    76      @Before
    77      public void setUpBefore() throws Exception
    78      {
    79          config = mock(ServletConfig.class);
    80          request = getMockRequest();
    81          response = mock(HttpServletResponse.class);
    82          session = request.getSession();
    83
    84          // User must exist
    85          securityService = (SecurityService)TurbineServices.getInstance().getService(SecurityService.SERVICE_NAME);
    86          if (!securityService.accountExists("username"))
    87          {
    88              User user = securityService.getUserInstance();
    89              user.setName("username");
    90              securityService.addUser(user, "password");
    91          }
    92      }
    93
    94      /**
    95       * Tests the Valve.
    96       */
    97      @Test public void testDefaults() throws Exception
    98      {
    99          Vector<String> v = new Vector<String>();
    100         v.add(LoginUser.CGI_USERNAME);
    101         v.add(LoginUser.CGI_PASSWORD);
    102         when(request.getParameterNames()).thenReturn(v.elements());
    103
    104         when(request.getParameterValues(LoginUser.CGI_USERNAME)).thenReturn(new String[] { "username" });
    105         when(request.getParameterValues(LoginUser.CGI_PASSWORD)).thenReturn(new String[] { "password" });
    106
    107         RunData runData = getRunData(request,response,config);
    108         runData.setAction(TurbineConstants.ACTION_LOGIN_DEFAULT);
    109
    110         Pipeline pipeline = new TurbinePipeline();
    111         PipelineData pipelineData = runData;
    112
    113         DefaultLoginValve valve = new DefaultLoginValve();
    114         pipeline.addValve(valve);
    115         pipeline.initialize();
    116
    117         pipeline.invoke(pipelineData);
    118         User user = runData.getUser();
    119         assertNotNull(user);
    120         assertEquals("username",user.getName());
    121         assertTrue(user.hasLoggedIn());
    122     }
    123
    124     /**
    125      * Tests the LogoutAction.
    126      */
    127     @Test public void testLogout() throws Exception
    128     {
    129         User user = securityService.getUser("username");
    130         user.setHasLoggedIn(Boolean.TRUE);
    131         session.setAttribute(User.SESSION_KEY, user);
    132
    133         RunData runData = getRunData(request,response,config);
    134         runData.setAction(TurbineConstants.ACTION_LOGOUT_DEFAULT);
    135
    136         Pipeline pipeline = new TurbinePipeline();
    137         PipelineData pipelineData = runData;
    138
    139         DefaultLoginValve valve = new DefaultLoginValve();
    140         pipeline.addValve(valve);
    141         pipeline.initialize();
    142
    143         pipeline.invoke(pipelineData);
    144         user = runData.getUser();
    145         assertNotNull(user);
    146         assertTrue(securityService.isAnonymousUser(user));
    147         assertFalse(user.hasLoggedIn());
    148     }
    149
    150     @AfterClass
    151     public static void destroy()
    152     {
    153         tc.dispose();
    154     }
    155 }