    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.assertFalse;
    026import static org.junit.Assert.assertNotNull;
    027import static org.junit.Assert.assertTrue;
    028import static org.mockito.Mockito.mock;
    029import static org.mockito.Mockito.when;
    030
    031import java.util.Vector;
    032
    033import javax.servlet.ServletConfig;
    034import javax.servlet.http.HttpServletRequest;
    035import javax.servlet.http.HttpServletResponse;
    036import javax.servlet.http.HttpSession;
    037
    038import org.apache.turbine.TurbineConstants;
    039import org.apache.turbine.modules.actions.LoginUser;
    040import org.apache.turbine.om.security.User;
    041import org.apache.turbine.services.TurbineServices;
    042import org.apache.turbine.services.security.SecurityService;
    043import org.apache.turbine.test.BaseTestCase;
    044import org.apache.turbine.util.RunData;
    045import org.apache.turbine.util.TurbineConfig;
    046import org.junit.AfterClass;
    047import org.junit.Before;
    048import org.junit.BeforeClass;
    049import org.junit.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: DefaultLoginValveTest.java 1754909 2016-08-02 12:55:35Z tv $
    057 */
    058public class DefaultLoginValveTest 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    private HttpSession session = null;
    065    private SecurityService securityService = null;
    066
    067    @BeforeClass
    068    public static void init()
    069    {
    070        tc = new TurbineConfig(
    071                            ".",
    072                            "/conf/test/CompleteTurbineResources.properties");
    073        tc.initialize();
    074    }
    075
    076    @Before
    077    public void setUpBefore() throws Exception
    078    {
    079        config = mock(ServletConfig.class);
    080        request = getMockRequest();
    081        response = mock(HttpServletResponse.class);
    082        session = request.getSession();
    083
    084        // User must exist
    085        securityService = (SecurityService)TurbineServices.getInstance().getService(SecurityService.SERVICE_NAME);
    086        if (!securityService.accountExists("username"))
    087        {
    088            User user = securityService.getUserInstance();
    089            user.setName("username");
    090            securityService.addUser(user, "password");
    091        }
    092    }
    093
    094    /**
    095     * Tests the Valve.
    096     */
    097    @Test public void testDefaults() throws Exception
    098    {
    099        Vector<String> v = new Vector<String>();
    100        v.add(LoginUser.CGI_USERNAME);
    101        v.add(LoginUser.CGI_PASSWORD);
    102        when(request.getParameterNames()).thenReturn(v.elements());
    103
    104        when(request.getParameterValues(LoginUser.CGI_USERNAME)).thenReturn(new String[] { "username" });
    105        when(request.getParameterValues(LoginUser.CGI_PASSWORD)).thenReturn(new String[] { "password" });
    106
    107        RunData runData = getRunData(request,response,config);
    108        runData.setAction(TurbineConstants.ACTION_LOGIN_DEFAULT);
    109
    110        Pipeline pipeline = new TurbinePipeline();
    111        PipelineData pipelineData = runData;
    112
    113        DefaultLoginValve valve = new DefaultLoginValve();
    114        pipeline.addValve(valve);
    115        pipeline.initialize();
    116
    117        pipeline.invoke(pipelineData);
    118        User user = runData.getUser();
    119        assertNotNull(user);
    120        assertEquals("username",user.getName());
    121        assertTrue(user.hasLoggedIn());
    122    }
    123
    124    /**
    125     * Tests the LogoutAction.
    126     */
    127    @Test public void testLogout() throws Exception
    128    {
    129        User user = securityService.getUser("username");
    130        user.setHasLoggedIn(Boolean.TRUE);
    131        session.setAttribute(User.SESSION_KEY, user);
    132
    133        RunData runData = getRunData(request,response,config);
    134        runData.setAction(TurbineConstants.ACTION_LOGOUT_DEFAULT);
    135
    136        Pipeline pipeline = new TurbinePipeline();
    137        PipelineData pipelineData = runData;
    138
    139        DefaultLoginValve valve = new DefaultLoginValve();
    140        pipeline.addValve(valve);
    141        pipeline.initialize();
    142
    143        pipeline.invoke(pipelineData);
    144        user = runData.getUser();
    145        assertNotNull(user);
    146        assertTrue(securityService.isAnonymousUser(user));
    147        assertFalse(user.hasLoggedIn());
    148    }
    149
    150    @AfterClass
    151    public static void destroy()
    152    {
    153        tc.dispose();
    154    }
    155}