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  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import static org.mockito.Mockito.mock;
27  import static org.mockito.Mockito.when;
28  
29  import java.util.Vector;
30  
31  import javax.servlet.ServletConfig;
32  import javax.servlet.ServletOutputStream;
33  import javax.servlet.http.HttpServletRequest;
34  import javax.servlet.http.HttpServletResponse;
35  
36  import org.apache.fulcrum.security.model.turbine.entity.impl.TurbineUserImpl;
37  import org.apache.turbine.modules.actions.VelocityActionDoesNothing;
38  import org.apache.turbine.modules.actions.VelocitySecureActionDoesNothing;
39  import org.apache.turbine.om.security.DefaultUserImpl;
40  import org.apache.turbine.om.security.User;
41  import org.apache.turbine.test.BaseTestCase;
42  import org.apache.turbine.util.RunData;
43  import org.apache.turbine.util.TurbineConfig;
44  import org.apache.turbine.util.uri.URIConstants;
45  import org.junit.jupiter.api.AfterAll;
46  import org.junit.jupiter.api.AfterEach;
47  import org.junit.jupiter.api.BeforeAll;
48  import org.junit.jupiter.api.BeforeEach;
49  import org.junit.jupiter.api.Test;
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 1853146 2019-02-07 16:14:46Z gk $
57   */
58  public class ExecutePageValveTest 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  
65      @BeforeAll
66      public static void init()
67      {
68          tc = new TurbineConfig(
69                              ".",
70                              "/conf/test/CompleteTurbineResources.properties");
71          tc.initialize();
72      }
73  
74      @BeforeEach
75      public void setUpBefore() throws Exception
76      {
77          config = mock(ServletConfig.class);
78          request = getMockRequest();
79          response = mock(HttpServletResponse.class);
80          ServletOutputStream sos = mock(ServletOutputStream.class);
81  
82          when(response.getOutputStream()).thenReturn(sos);
83      }
84  
85      @Test public void testValve() throws Exception
86      {
87          Vector<String> v = new Vector<String>();
88          v.add(URIConstants.CGI_TEMPLATE_PARAM);
89          when(request.getParameterNames()).thenReturn(v.elements());
90  
91          when(request.getParameterValues(URIConstants.CGI_TEMPLATE_PARAM)).thenReturn(new String[] { "Index.vm" });
92  
93          RunData runData = getRunData(request, response, config);
94          runData.setScreenTemplate("ExistPageWithLayout.vm");
95          User tu = new DefaultUserImpl(new TurbineUserImpl());
96          tu.setName("username");
97          tu.setHasLoggedIn(Boolean.TRUE);
98          String actionName = VelocityActionDoesNothing.class.getName();
99          actionName = actionName.substring(actionName.lastIndexOf(".")+1);
100         runData.setAction(actionName);
101         runData.setUser(tu);
102 
103         Pipeline pipeline = new TurbinePipeline();
104 
105         PipelineData pipelineData = runData;
106         ExecutePageValve valve = new ExecutePageValve();
107         pipeline.addValve(valve);
108         pipeline.initialize();
109 
110         int numberOfCalls = VelocityActionDoesNothing.numberOfCalls;
111         pipeline.invoke(pipelineData);
112         assertEquals(numberOfCalls +1,VelocityActionDoesNothing.numberOfCalls, "Assert action was called");
113         User user = runData.getUser();
114         assertNotNull(user);
115         assertEquals("username", user.getName());
116         assertTrue(user.hasLoggedIn());
117     }
118 
119     @Test public void testValveWithSecureAction() throws Exception
120     {
121         Vector<String> v = new Vector<String>();
122         v.add(URIConstants.CGI_TEMPLATE_PARAM);
123         when(request.getParameterNames()).thenReturn(v.elements());
124 
125         when(request.getParameterValues(URIConstants.CGI_TEMPLATE_PARAM)).thenReturn(new String[] { "Index.vm" });
126 
127         RunData runData = getRunData(request, response, config);
128         runData.setScreenTemplate("ExistPageWithLayout.vm");
129         User tu = new DefaultUserImpl(new TurbineUserImpl());
130         tu.setName("username");
131         tu.setHasLoggedIn(Boolean.TRUE);
132         String actionName = VelocitySecureActionDoesNothing.class.getName();
133         actionName = actionName.substring(actionName.lastIndexOf(".")+1);
134         runData.setAction(actionName);
135         runData.setUser(tu);
136 
137         Pipeline pipeline = new TurbinePipeline();
138 
139         PipelineData pipelineData = runData;
140         ExecutePageValve valve = new ExecutePageValve();
141         pipeline.addValve(valve);
142         pipeline.initialize();
143 
144         int numberOfCalls = VelocitySecureActionDoesNothing.numberOfCalls;
145         int isAuthorizedCalls = VelocitySecureActionDoesNothing.isAuthorizedCalls;
146         pipeline.invoke(pipelineData);
147         assertEquals(numberOfCalls +1,VelocitySecureActionDoesNothing.numberOfCalls, "Assert action was called");
148         assertEquals(isAuthorizedCalls +1,VelocitySecureActionDoesNothing.isAuthorizedCalls, "Assert authorization was called");
149         User user = runData.getUser();
150         assertNotNull(user);
151         assertEquals("username", user.getName());
152         assertTrue(user.hasLoggedIn());
153     }
154 
155     @AfterAll
156     public static void destroy()
157     {
158         tc.dispose();
159     }
160 }