    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 java.util.Vector;
    025
    026import javax.servlet.http.HttpServletResponse;
    027
    028import org.apache.fulcrum.security.model.turbine.entity.impl.TurbineUserImpl;
    029import org.apache.turbine.TurbineConstants;
    030import org.apache.turbine.modules.actions.LoginUser;
    031import org.apache.turbine.om.security.DefaultUserImpl;
    032import org.apache.turbine.om.security.User;
    033import org.apache.turbine.test.BaseTestCase;
    034import org.apache.turbine.test.EnhancedMockHttpServletRequest;
    035import org.apache.turbine.test.EnhancedMockHttpSession;
    036import org.apache.turbine.util.RunData;
    037import org.apache.turbine.util.TurbineConfig;
    038import org.junit.AfterClass;
    039import org.junit.Before;
    040import org.junit.BeforeClass;
    041import org.junit.Test;
    042
    043import com.mockobjects.servlet.MockHttpServletResponse;
    044import com.mockobjects.servlet.MockServletConfig;
    045
    046import static org.junit.Assert.*;
    047
    048/**
    049 * Tests TurbinePipeline.
    050 *
    051 * @author <a href="mailto:epugh@opensourceConnections.com">Eric Pugh</a>
    052 * @author <a href="mailto:peter@courcoux.biz">Peter Courcoux</a>
    053 * @version $Id: DefaultSessionValidationValveTest.java 1606111 2014-06-27 14:46:47Z gk $
    054 */
    055public class DefaultSessionValidationValveTest extends BaseTestCase
    056{
    057    private static TurbineConfig tc = null;
    058    private MockServletConfig config = null;
    059    private EnhancedMockHttpServletRequest request = null;
    060    private EnhancedMockHttpSession session = null;
    061    private HttpServletResponse response = null;
    062
    063
    064
    065    @BeforeClass
    066    public static void init() {
    067        tc = new TurbineConfig(
    068                            ".",
    069                            "/conf/test/CompleteTurbineResources.properties");
    070        tc.initialize();
    071    }
    072
    073    @Before
    074    public void setUpBefore() throws Exception {
    075        config = new MockServletConfig();
    076        config.setupNoParameters();
    077        request = new EnhancedMockHttpServletRequest();
    078        request.setupServerName("bob");
    079        request.setupGetProtocol("http");
    080        request.setupScheme("scheme");
    081        request.setupPathInfo("damn");
    082        request.setupGetServletPath("damn2");
    083        request.setupGetContextPath("wow");
    084        request.setupGetContentType("html/text");
    085        request.setupAddHeader("Content-type", "html/text");
    086        request.setupAddHeader("Accept-Language", "en-US");
    087
    088        session = new EnhancedMockHttpSession();
    089        response = new MockHttpServletResponse();
    090
    091        request.setSession(session);
    092
    093    }
    094
    095    /**
    096     * Tests the Valve.
    097     */
    098    @Test public void testAnonymousUser() throws Exception
    099    {
    100        Vector<String> v = new Vector<String>();
    101        v.add(LoginUser.CGI_USERNAME);
    102        v.add(LoginUser.CGI_PASSWORD);
    103        request.setupGetParameterNames(v.elements());
    104
    105        request.setupAddParameter(LoginUser.CGI_USERNAME,"username");
    106        request.setupAddParameter(LoginUser.CGI_PASSWORD,"password");
    107
    108        RunData runData = getRunData(request,response,config);
    109        runData.setAction(TurbineConstants.ACTION_LOGIN_DEFAULT);
    110
    111        Pipeline pipeline = new TurbinePipeline();
    112        PipelineData pipelineData = runData;
    113
    114        DefaultSessionValidationValve valve = new DefaultSessionValidationValve();
    115        pipeline.addValve(valve);
    116        pipeline.initialize();
    117
    118        pipeline.invoke(pipelineData);
    119        User user = runData.getUser();
    120        assertNotNull(user);
    121        assertEquals("",user.getName());
    122        assertFalse(user.hasLoggedIn());
    123    }
    124
    125    @Test public void testLoggedInUser() throws Exception
    126    {
    127        Vector<String> v = new Vector<String>();
    128        v.add(LoginUser.CGI_USERNAME);
    129        v.add(LoginUser.CGI_PASSWORD);
    130        request.setupGetParameterNames(v.elements());
    131
    132        request.setupAddParameter(LoginUser.CGI_USERNAME,"username");
    133        request.setupAddParameter(LoginUser.CGI_PASSWORD,"password");
    134
    135        RunData runData = getRunData(request,response,config);
    136        User tu = new DefaultUserImpl(new TurbineUserImpl());
    137        tu.setName("username");
    138        tu.setHasLoggedIn(Boolean.TRUE);
    139        runData.setAction("TestAction");
    140
    141        session.setupGetAttribute(User.SESSION_KEY, tu);
    142
    143        Pipeline pipeline = new TurbinePipeline();
    144        PipelineData pipelineData = runData;
    145
    146        DefaultSessionValidationValve valve = new DefaultSessionValidationValve();
    147        pipeline.addValve(valve);
    148        pipeline.initialize();
    149
    150        pipeline.invoke(pipelineData);
    151        User user = runData.getUser();
    152        assertNotNull(user);
    153        assertEquals("username",user.getName());
    154        assertTrue(user.hasLoggedIn());
    155    }
    156
    157    @AfterClass
    158    public static void destroy() {
    159        tc.dispose();
    160    }
    161}