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 */
022
023
024import static org.junit.Assert.assertEquals;
025import static org.junit.Assert.assertNotNull;
026import static org.junit.Assert.assertTrue;
027
028import java.util.Vector;
029
030import javax.servlet.http.HttpServletResponse;
031
032import org.apache.fulcrum.security.model.turbine.entity.impl.TurbineUserImpl;
033import org.apache.turbine.modules.actions.VelocityActionDoesNothing;
034import org.apache.turbine.modules.actions.VelocitySecureActionDoesNothing;
035import org.apache.turbine.om.security.DefaultUserImpl;
036import org.apache.turbine.om.security.User;
037import org.apache.turbine.test.BaseTestCase;
038import org.apache.turbine.test.EnhancedMockHttpServletRequest;
039import org.apache.turbine.test.EnhancedMockHttpServletResponse;
040import org.apache.turbine.test.EnhancedMockHttpSession;
041import org.apache.turbine.util.RunData;
042import org.apache.turbine.util.TurbineConfig;
043import org.apache.turbine.util.uri.URIConstants;
044import org.junit.AfterClass;
045import org.junit.Before;
046import org.junit.BeforeClass;
047import org.junit.Test;
048
049import com.mockobjects.servlet.MockServletConfig;
050
051/**
052 * Tests ExecutePageValve.
053 *
054 * @author <a href="mailto:epugh@opensourceConnections.com">Eric Pugh</a>
055 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
056 * @version $Id: ExecutePageValveTest.java 1706425 2015-10-02 14:47:51Z tv $
057 */
058public class ExecutePageValveTest extends BaseTestCase
059{
060    private static TurbineConfig tc = null;
061    private MockServletConfig config = null;
062    private EnhancedMockHttpServletRequest request = null;
063    private EnhancedMockHttpSession session = null;
064    private HttpServletResponse response = null;
065
066
067    @BeforeClass
068    public static void init() {
069        tc = new TurbineConfig(
070                            ".",
071                            "/conf/test/CompleteTurbineResources.properties");
072        tc.initialize();
073    }
074
075    @Before
076    public void setUpBefore() throws Exception
077    {
078        config = new MockServletConfig();
079        config.setupNoParameters();
080        request = new EnhancedMockHttpServletRequest();
081        request.setupServerName("bob");
082        request.setupGetProtocol("http");
083        request.setupScheme("scheme");
084        request.setupPathInfo("damn");
085        request.setupGetServletPath("damn2");
086        request.setupGetContextPath("wow");
087        request.setupGetContentType("html/text");
088        request.setupAddHeader("Content-type", "html/text");
089        request.setupAddHeader("Accept-Language", "en-US");
090
091        session = new EnhancedMockHttpSession();
092        response = new EnhancedMockHttpServletResponse();
093
094        request.setSession(session);
095
096    }
097
098    @Test public void testValve() throws Exception
099    {
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}