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