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