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.assertNotNull;
26  import static org.junit.Assert.assertTrue;
27  
28  import java.util.Vector;
29  
30  import javax.servlet.http.HttpServletResponse;
31  
32  import org.apache.fulcrum.security.model.turbine.entity.impl.TurbineUserImpl;
33  import org.apache.turbine.modules.actions.VelocityActionDoesNothing;
34  import org.apache.turbine.modules.actions.VelocitySecureActionDoesNothing;
35  import org.apache.turbine.om.security.DefaultUserImpl;
36  import org.apache.turbine.om.security.User;
37  import org.apache.turbine.test.BaseTestCase;
38  import org.apache.turbine.test.EnhancedMockHttpServletRequest;
39  import org.apache.turbine.test.EnhancedMockHttpServletResponse;
40  import org.apache.turbine.test.EnhancedMockHttpSession;
41  import org.apache.turbine.util.RunData;
42  import org.apache.turbine.util.TurbineConfig;
43  import org.apache.turbine.util.uri.URIConstants;
44  import org.junit.AfterClass;
45  import org.junit.Before;
46  import org.junit.BeforeClass;
47  import org.junit.Test;
48  
49  import com.mockobjects.servlet.MockServletConfig;
50  
51  /**
52   * Tests ExecutePageValve.
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: ExecutePageValveTest.java 1706425 2015-10-02 14:47:51Z tv $
57   */
58  public class ExecutePageValveTest extends BaseTestCase
59  {
60      private static TurbineConfig tc = null;
61      private MockServletConfig config = null;
62      private EnhancedMockHttpServletRequest request = null;
63      private EnhancedMockHttpSession session = null;
64      private HttpServletResponse response = null;
65  
66  
67      @BeforeClass
68      public static void init() {
69          tc = new TurbineConfig(
70                              ".",
71                              "/conf/test/CompleteTurbineResources.properties");
72          tc.initialize();
73      }
74  
75      @Before
76      public void setUpBefore() throws Exception
77      {
78          config = new MockServletConfig();
79          config.setupNoParameters();
80          request = new EnhancedMockHttpServletRequest();
81          request.setupServerName("bob");
82          request.setupGetProtocol("http");
83          request.setupScheme("scheme");
84          request.setupPathInfo("damn");
85          request.setupGetServletPath("damn2");
86          request.setupGetContextPath("wow");
87          request.setupGetContentType("html/text");
88          request.setupAddHeader("Content-type", "html/text");
89          request.setupAddHeader("Accept-Language", "en-US");
90  
91          session = new EnhancedMockHttpSession();
92          response = new EnhancedMockHttpServletResponse();
93  
94          request.setSession(session);
95  
96      }
97  
98      @Test public void testValve() throws Exception
99      {
100         Vector<String> v = new Vector<String>();
101         v.add(URIConstants.CGI_TEMPLATE_PARAM);
102         request.setupGetParameterNames(v.elements());
103         String nulls[] = new String[1];
104         nulls[0]="Index.vm";
105         request.setupAddParameter(URIConstants.CGI_TEMPLATE_PARAM, nulls);
106 
107         RunData runData = getRunData(request, response, config);
108         runData.setScreenTemplate("ExistPageWithLayout.vm");
109         User tu = new DefaultUserImpl(new TurbineUserImpl());
110         tu.setName("username");
111         tu.setHasLoggedIn(Boolean.TRUE);
112         String actionName = VelocityActionDoesNothing.class.getName();
113         actionName = actionName.substring(actionName.lastIndexOf(".")+1);
114         runData.setAction(actionName);
115         runData.setUser(tu);
116 
117         Pipeline pipeline = new TurbinePipeline();
118 
119         PipelineData pipelineData = runData;
120         ExecutePageValve valve = new ExecutePageValve();
121         pipeline.addValve(valve);
122         pipeline.initialize();
123 
124         int numberOfCalls = VelocityActionDoesNothing.numberOfCalls;
125         pipeline.invoke(pipelineData);
126         assertEquals("Assert action was called",numberOfCalls +1,VelocityActionDoesNothing.numberOfCalls);
127         User user = runData.getUser();
128         assertNotNull(user);
129         assertEquals("username", user.getName());
130         assertTrue(user.hasLoggedIn());
131     }
132 
133     @Test public void testValveWithSecureAction() throws Exception
134     {
135         Vector<String> v = new Vector<String>();
136         v.add(URIConstants.CGI_TEMPLATE_PARAM);
137         request.setupGetParameterNames(v.elements());
138         String nulls[] = new String[1];
139         nulls[0]="Index.vm";
140         request.setupAddParameter(URIConstants.CGI_TEMPLATE_PARAM, nulls);
141 
142         RunData runData = getRunData(request, response, config);
143         runData.setScreenTemplate("ExistPageWithLayout.vm");
144         User tu = new DefaultUserImpl(new TurbineUserImpl());
145         tu.setName("username");
146         tu.setHasLoggedIn(Boolean.TRUE);
147         String actionName = VelocitySecureActionDoesNothing.class.getName();
148         actionName = actionName.substring(actionName.lastIndexOf(".")+1);
149         runData.setAction(actionName);
150         runData.setUser(tu);
151 
152         Pipeline pipeline = new TurbinePipeline();
153 
154         PipelineData pipelineData = runData;
155         ExecutePageValve valve = new ExecutePageValve();
156         pipeline.addValve(valve);
157         pipeline.initialize();
158 
159         int numberOfCalls = VelocitySecureActionDoesNothing.numberOfCalls;
160         int isAuthorizedCalls = VelocitySecureActionDoesNothing.isAuthorizedCalls;
161         pipeline.invoke(pipelineData);
162         assertEquals("Assert action was called",numberOfCalls +1,VelocitySecureActionDoesNothing.numberOfCalls);
163         assertEquals("Assert authorization was called",isAuthorizedCalls +1,VelocitySecureActionDoesNothing.isAuthorizedCalls);
164         User user = runData.getUser();
165         assertNotNull(user);
166         assertEquals("username", user.getName());
167         assertTrue(user.hasLoggedIn());
168     }
169 
170     @AfterClass
171     public static void destroy()
172     {
173         tc.dispose();
174     }
175 }