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