001package org.apache.turbine.pipeline; 002 003 004/* 005 * Licensed to the Apache Software Foundation (ASF) under one 006 * or more contributor license agreements. See the NOTICE file 007 * distributed with this work for additional information 008 * regarding copyright ownership. The ASF licenses this file 009 * to you under the Apache License, Version 2.0 (the 010 * "License"); you may not use this file except in compliance 011 * with the License. You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, 016 * software distributed under the License is distributed on an 017 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 018 * KIND, either express or implied. See the License for the 019 * specific language governing permissions and limitations 020 * under the License. 021 */ 022import static org.junit.jupiter.api.Assertions.assertEquals; 023import static org.junit.jupiter.api.Assertions.assertNotNull; 024import static org.junit.jupiter.api.Assertions.assertTrue; 025 026import static org.mockito.Mockito.mock; 027import static org.mockito.Mockito.when; 028 029import java.util.Vector; 030 031import javax.servlet.ServletConfig; 032import javax.servlet.ServletOutputStream; 033import javax.servlet.http.HttpServletRequest; 034import javax.servlet.http.HttpServletResponse; 035 036import org.apache.fulcrum.security.model.turbine.entity.impl.TurbineUserImpl; 037import org.apache.turbine.modules.actions.VelocityActionDoesNothing; 038import org.apache.turbine.modules.actions.VelocitySecureActionDoesNothing; 039import org.apache.turbine.om.security.DefaultUserImpl; 040import org.apache.turbine.om.security.User; 041import org.apache.turbine.test.BaseTestCase; 042import org.apache.turbine.util.RunData; 043import org.apache.turbine.util.TurbineConfig; 044import org.apache.turbine.util.uri.URIConstants; 045import org.junit.jupiter.api.AfterAll; 046import org.junit.jupiter.api.BeforeAll; 047import org.junit.jupiter.api.BeforeEach; 048import org.junit.jupiter.api.Test; 049 050/** 051 * Tests ExecutePageValve. 052 * 053 * @author <a href="mailto:epugh@opensourceConnections.com">Eric Pugh</a> 054 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a> 055 * @version $Id$ 056 */ 057public class ExecutePageValveTest extends BaseTestCase 058{ 059 private static TurbineConfig tc = null; 060 private ServletConfig config = null; 061 private HttpServletRequest request = null; 062 private HttpServletResponse response = null; 063 064 @BeforeAll 065 public static void init() 066 { 067 tc = new TurbineConfig( 068 ".", 069 "/conf/test/CompleteTurbineResources.properties"); 070 tc.initialize(); 071 } 072 073 @BeforeEach 074 public void setUpBefore() throws Exception 075 { 076 config = mock(ServletConfig.class); 077 request = getMockRequest(); 078 response = mock(HttpServletResponse.class); 079 ServletOutputStream sos = mock(ServletOutputStream.class); 080 081 when(response.getOutputStream()).thenReturn(sos); 082 } 083 084 @Test public void testValve() throws Exception 085 { 086 Vector<String> v = new Vector<>(); 087 v.add(URIConstants.CGI_TEMPLATE_PARAM); 088 when(request.getParameterNames()).thenReturn(v.elements()); 089 090 when(request.getParameterValues(URIConstants.CGI_TEMPLATE_PARAM)).thenReturn(new String[] { "Index.vm" }); 091 092 RunData runData = getRunData(request, response, config); 093 runData.setScreenTemplate("ExistPageWithLayout.vm"); 094 User tu = new DefaultUserImpl(new TurbineUserImpl()); 095 tu.setName("username"); 096 tu.setHasLoggedIn(Boolean.TRUE); 097 String actionName = VelocityActionDoesNothing.class.getName(); 098 actionName = actionName.substring(actionName.lastIndexOf(".")+1); 099 runData.setAction(actionName); 100 runData.setUser(tu); 101 102 Pipeline pipeline = new TurbinePipeline(); 103 104 PipelineData pipelineData = runData; 105 ExecutePageValve valve = new ExecutePageValve(); 106 pipeline.addValve(valve); 107 pipeline.initialize(); 108 109 int numberOfCalls = VelocityActionDoesNothing.numberOfCalls; 110 pipeline.invoke(pipelineData); 111 assertEquals(numberOfCalls +1,VelocityActionDoesNothing.numberOfCalls, "Assert action was called"); 112 User user = runData.getUser(); 113 assertNotNull(user); 114 assertEquals("username", user.getName()); 115 assertTrue(user.hasLoggedIn()); 116 } 117 118 @Test public void testValveWithSecureAction() throws Exception 119 { 120 Vector<String> v = new Vector<>(); 121 v.add(URIConstants.CGI_TEMPLATE_PARAM); 122 when(request.getParameterNames()).thenReturn(v.elements()); 123 124 when(request.getParameterValues(URIConstants.CGI_TEMPLATE_PARAM)).thenReturn(new String[] { "Index.vm" }); 125 126 RunData runData = getRunData(request, response, config); 127 runData.setScreenTemplate("ExistPageWithLayout.vm"); 128 User tu = new DefaultUserImpl(new TurbineUserImpl()); 129 tu.setName("username"); 130 tu.setHasLoggedIn(Boolean.TRUE); 131 String actionName = VelocitySecureActionDoesNothing.class.getName(); 132 actionName = actionName.substring(actionName.lastIndexOf(".")+1); 133 runData.setAction(actionName); 134 runData.setUser(tu); 135 136 Pipeline pipeline = new TurbinePipeline(); 137 138 PipelineData pipelineData = runData; 139 ExecutePageValve valve = new ExecutePageValve(); 140 pipeline.addValve(valve); 141 pipeline.initialize(); 142 143 int numberOfCalls = VelocitySecureActionDoesNothing.numberOfCalls; 144 int isAuthorizedCalls = VelocitySecureActionDoesNothing.isAuthorizedCalls; 145 pipeline.invoke(pipelineData); 146 assertEquals(numberOfCalls +1,VelocitySecureActionDoesNothing.numberOfCalls, "Assert action was called"); 147 assertEquals(isAuthorizedCalls +1,VelocitySecureActionDoesNothing.isAuthorizedCalls, "Assert authorization was called"); 148 User user = runData.getUser(); 149 assertNotNull(user); 150 assertEquals("username", user.getName()); 151 assertTrue(user.hasLoggedIn()); 152 } 153 154 @AfterAll 155 public static void destroy() 156 { 157 tc.dispose(); 158 } 159}