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.jupiter.api.Assertions.assertEquals;
025import static org.junit.jupiter.api.Assertions.assertNotNull;
026import static org.junit.jupiter.api.Assertions.assertTrue;
027import static org.junit.jupiter.api.Assertions.assertFalse;
028import static org.mockito.Mockito.mock;
029
030import static org.mockito.Mockito.when;
031
032import java.util.Vector;
033
034import javax.servlet.ServletConfig;
035import javax.servlet.http.HttpServletRequest;
036import javax.servlet.http.HttpServletResponse;
037
038import org.apache.fulcrum.security.model.turbine.entity.impl.TurbineUserImpl;
039import org.apache.turbine.TurbineConstants;
040import org.apache.turbine.modules.actions.LoginUser;
041import org.apache.turbine.om.security.DefaultUserImpl;
042import org.apache.turbine.om.security.User;
043import org.apache.turbine.test.BaseTestCase;
044import org.apache.turbine.util.RunData;
045import org.apache.turbine.util.TurbineConfig;
046import org.junit.jupiter.api.AfterAll;
047import org.junit.jupiter.api.BeforeAll;
048import org.junit.jupiter.api.BeforeEach;
049import org.junit.jupiter.api.Test;
050
051/**
052 * Tests TurbinePipeline.
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$
057 */
058public class DefaultSessionValidationValveTest extends BaseTestCase
059{
060    private static TurbineConfig tc = null;
061    private ServletConfig config = null;
062    private HttpServletRequest request = null;
063    private HttpServletResponse response = null;
064
065    @BeforeAll
066    public static void init()
067    {
068        tc = new TurbineConfig(
069                            ".",
070                            "/conf/test/CompleteTurbineResources.properties");
071        tc.initialize();
072    }
073
074    @BeforeEach
075    public void setUpBefore() throws Exception
076    {
077        config = mock(ServletConfig.class);
078        request = getMockRequest();
079        response = mock(HttpServletResponse.class);
080    }
081
082    /**
083     * Tests the Valve.
084     */
085    @Test public void testAnonymousUser() throws Exception
086    {
087        Vector<String> v = new Vector<>();
088        v.add(LoginUser.CGI_USERNAME);
089        v.add(LoginUser.CGI_PASSWORD);
090        when(request.getParameterNames()).thenReturn(v.elements());
091
092        when(request.getParameterValues(LoginUser.CGI_USERNAME)).thenReturn(new String[] { "username" });
093        when(request.getParameterValues(LoginUser.CGI_PASSWORD)).thenReturn(new String[] { "password" });
094
095        RunData runData = getRunData(request,response,config);
096        runData.setAction(TurbineConstants.ACTION_LOGIN_DEFAULT);
097
098        Pipeline pipeline = new TurbinePipeline();
099        PipelineData pipelineData = runData;
100
101        DefaultSessionValidationValve valve = new DefaultSessionValidationValve();
102        pipeline.addValve(valve);
103        pipeline.initialize();
104
105        pipeline.invoke(pipelineData);
106        User user = runData.getUser();
107        assertNotNull(user);
108        assertEquals("",user.getName());
109        assertFalse(user.hasLoggedIn());
110    }
111
112    @Test public void testLoggedInUser() throws Exception
113    {
114        Vector<String> v = new Vector<>();
115        v.add(LoginUser.CGI_USERNAME);
116        v.add(LoginUser.CGI_PASSWORD);
117        when(request.getParameterNames()).thenReturn(v.elements());
118
119        when(request.getParameterValues(LoginUser.CGI_USERNAME)).thenReturn(new String[] { "username" });
120        when(request.getParameterValues(LoginUser.CGI_PASSWORD)).thenReturn(new String[] { "password" });
121
122        RunData runData = getRunData(request,response,config);
123        User tu = new DefaultUserImpl(new TurbineUserImpl());
124        tu.setName("username");
125        tu.setHasLoggedIn(Boolean.TRUE);
126        runData.setAction("TestAction");
127
128        request.getSession().setAttribute(User.SESSION_KEY, tu);
129
130        Pipeline pipeline = new TurbinePipeline();
131        PipelineData pipelineData = runData;
132
133        DefaultSessionValidationValve valve = new DefaultSessionValidationValve();
134        pipeline.addValve(valve);
135        pipeline.initialize();
136
137        pipeline.invoke(pipelineData);
138        User user = runData.getUser();
139        assertNotNull(user);
140        assertEquals("username",user.getName());
141        assertTrue(user.hasLoggedIn());
142    }
143
144    @AfterAll
145    public static void destroy()
146    {
147        tc.dispose();
148    }
149}