|
| 1 | +/* |
| 2 | + * Copyright (c) 2002-2017, Mairie de Paris |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions |
| 7 | + * are met: |
| 8 | + * |
| 9 | + * 1. Redistributions of source code must retain the above copyright notice |
| 10 | + * and the following disclaimer. |
| 11 | + * |
| 12 | + * 2. Redistributions in binary form must reproduce the above copyright notice |
| 13 | + * and the following disclaimer in the documentation and/or other materials |
| 14 | + * provided with the distribution. |
| 15 | + * |
| 16 | + * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its |
| 17 | + * contributors may be used to endorse or promote products derived from |
| 18 | + * this software without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 24 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | + * POSSIBILITY OF SUCH DAMAGE. |
| 31 | + * |
| 32 | + * License 1.0 |
| 33 | + */ |
| 34 | +package fr.paris.lutece.portal.web.portlet; |
| 35 | + |
| 36 | +import java.math.BigInteger; |
| 37 | +import java.security.SecureRandom; |
| 38 | +import java.util.HashMap; |
| 39 | +import java.util.Map; |
| 40 | +import java.util.Random; |
| 41 | + |
| 42 | +import org.springframework.mock.web.MockHttpServletRequest; |
| 43 | +import org.springframework.web.context.request.RequestContextHolder; |
| 44 | +import org.springframework.web.context.request.ServletRequestAttributes; |
| 45 | + |
| 46 | +import fr.paris.lutece.portal.business.portlet.Portlet; |
| 47 | +import fr.paris.lutece.portal.business.user.AdminUser; |
| 48 | +import fr.paris.lutece.portal.service.admin.AccessDeniedException; |
| 49 | +import fr.paris.lutece.portal.service.admin.PasswordResetException; |
| 50 | +import fr.paris.lutece.portal.service.security.SecurityTokenService; |
| 51 | +import fr.paris.lutece.portal.service.util.AppException; |
| 52 | +import fr.paris.lutece.portal.web.constants.Parameters; |
| 53 | +import fr.paris.lutece.test.LuteceTestCase; |
| 54 | +import fr.paris.lutece.test.Utils; |
| 55 | + |
| 56 | +public class PortletJspBeanTest extends LuteceTestCase |
| 57 | +{ |
| 58 | + public void testGetCreateTemplate( ) throws PasswordResetException, AccessDeniedException |
| 59 | + { |
| 60 | + PortletJspBean instance = new TestPortletJspBean( ); |
| 61 | + MockHttpServletRequest request = new MockHttpServletRequest( ); |
| 62 | + Utils.registerAdminUserWithRigth( request, new AdminUser( ), PortletJspBean.RIGHT_MANAGE_ADMIN_SITE ); |
| 63 | + instance.init( request, PortletJspBean.RIGHT_MANAGE_ADMIN_SITE ); |
| 64 | + RequestContextHolder.setRequestAttributes( new ServletRequestAttributes( request ) ); |
| 65 | + |
| 66 | + Map<String, Object> model = new HashMap<>( ); |
| 67 | + assertNotNull( instance.getCreateTemplate( "1", "ALIAS_PORTLET", model ) ); |
| 68 | + assertTrue( model.containsKey( SecurityTokenService.MARK_TOKEN ) ); |
| 69 | + } |
| 70 | + |
| 71 | + public void testGetModifyTemplateTemplate( ) throws PasswordResetException, AccessDeniedException |
| 72 | + { |
| 73 | + PortletJspBean instance = new TestPortletJspBean( ); |
| 74 | + MockHttpServletRequest request = new MockHttpServletRequest( ); |
| 75 | + Utils.registerAdminUserWithRigth( request, new AdminUser( ), PortletJspBean.RIGHT_MANAGE_ADMIN_SITE ); |
| 76 | + instance.init( request, PortletJspBean.RIGHT_MANAGE_ADMIN_SITE ); |
| 77 | + RequestContextHolder.setRequestAttributes( new ServletRequestAttributes( request ) ); |
| 78 | + |
| 79 | + Map<String, Object> model = new HashMap<>( ); |
| 80 | + model.put( "alias_portlet", "1" ); |
| 81 | + Portlet portlet = new TestPortlet( "ALIAS_PORTLET" ); |
| 82 | + assertNotNull( instance.getModifyTemplate( portlet, model ) ); |
| 83 | + assertTrue( model.containsKey( SecurityTokenService.MARK_TOKEN ) ); |
| 84 | + } |
| 85 | + |
| 86 | + public void testSetPortletCommonData( ) |
| 87 | + { |
| 88 | + PortletJspBean instance = new TestPortletJspBean( ); |
| 89 | + MockHttpServletRequest request = new MockHttpServletRequest( ); |
| 90 | + String strName = getRandomName( ); |
| 91 | + request.setParameter( Parameters.PORTLET_NAME, strName ); |
| 92 | + request.setParameter( Parameters.ORDER, "1" ); |
| 93 | + request.setParameter( Parameters.COLUMN, "1" ); |
| 94 | + request.setParameter( Parameters.ACCEPT_ALIAS, "1" ); |
| 95 | + request.setParameter( Parameters.DISPLAY_PORTLET_TITLE, "1" ); |
| 96 | + request.setParameter( Parameters.STYLE, "1" ); |
| 97 | + request.setParameter( "page_id", "1" ); |
| 98 | + request.setParameter( SecurityTokenService.PARAMETER_TOKEN, |
| 99 | + SecurityTokenService.getInstance( ).getToken( request, "admin/portlet/create_portlet.html" ) ); |
| 100 | + Portlet portlet = new TestPortlet( "ALIAS_PORTLET" ); |
| 101 | + instance.setPortletCommonData( request, portlet ); |
| 102 | + |
| 103 | + assertEquals( strName, portlet.getName( ) ); |
| 104 | + assertEquals( 1, portlet.getOrder( ) ); |
| 105 | + assertEquals( 1, portlet.getColumn( ) ); |
| 106 | + assertEquals( 1, portlet.getAcceptAlias( ) ); |
| 107 | + assertEquals( 1, portlet.getDisplayPortletTitle( ) ); |
| 108 | + assertEquals( 1, portlet.getStyleId( ) ); |
| 109 | + assertEquals( 1, portlet.getPageId( ) ); |
| 110 | + } |
| 111 | + |
| 112 | + public void testSetPortletCommonDataInvalidToken( ) |
| 113 | + { |
| 114 | + PortletJspBean instance = new TestPortletJspBean( ); |
| 115 | + MockHttpServletRequest request = new MockHttpServletRequest( ); |
| 116 | + String strName = getRandomName( ); |
| 117 | + request.setParameter( Parameters.PORTLET_NAME, strName ); |
| 118 | + request.setParameter( Parameters.ORDER, "1" ); |
| 119 | + request.setParameter( Parameters.COLUMN, "1" ); |
| 120 | + request.setParameter( Parameters.ACCEPT_ALIAS, "1" ); |
| 121 | + request.setParameter( Parameters.DISPLAY_PORTLET_TITLE, "1" ); |
| 122 | + request.setParameter( Parameters.STYLE, "1" ); |
| 123 | + request.setParameter( "page_id", "1" ); |
| 124 | + request.setParameter( SecurityTokenService.PARAMETER_TOKEN, |
| 125 | + SecurityTokenService.getInstance( ).getToken( request, "admin/portlet/create_portlet.html" ) + "b" ); |
| 126 | + Portlet portlet = new TestPortlet( "ALIAS_PORTLET" ); |
| 127 | + try |
| 128 | + { |
| 129 | + instance.setPortletCommonData( request, portlet ); |
| 130 | + fail( "Should have thrown" ); |
| 131 | + } |
| 132 | + catch ( AppException e ) |
| 133 | + { |
| 134 | + assertNotNull( e.getCause( ) ); |
| 135 | + assertTrue( e.getCause( ) instanceof AccessDeniedException ); |
| 136 | + assertEquals( "ALIAS_PORTLET", portlet.getName( ) ); |
| 137 | + assertEquals( 0, portlet.getOrder( ) ); |
| 138 | + assertEquals( 0, portlet.getColumn( ) ); |
| 139 | + assertEquals( 0, portlet.getAcceptAlias( ) ); |
| 140 | + assertEquals( 0, portlet.getDisplayPortletTitle( ) ); |
| 141 | + assertEquals( 0, portlet.getStyleId( ) ); |
| 142 | + assertEquals( 0, portlet.getPageId( ) ); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + public void testSetPortletCommonDataNoToken( ) |
| 147 | + { |
| 148 | + PortletJspBean instance = new TestPortletJspBean( ); |
| 149 | + MockHttpServletRequest request = new MockHttpServletRequest( ); |
| 150 | + String strName = getRandomName( ); |
| 151 | + request.setParameter( Parameters.PORTLET_NAME, strName ); |
| 152 | + request.setParameter( Parameters.ORDER, "1" ); |
| 153 | + request.setParameter( Parameters.COLUMN, "1" ); |
| 154 | + request.setParameter( Parameters.ACCEPT_ALIAS, "1" ); |
| 155 | + request.setParameter( Parameters.DISPLAY_PORTLET_TITLE, "1" ); |
| 156 | + request.setParameter( Parameters.STYLE, "1" ); |
| 157 | + request.setParameter( "page_id", "1" ); |
| 158 | + |
| 159 | + Portlet portlet = new TestPortlet( "ALIAS_PORTLET" ); |
| 160 | + try |
| 161 | + { |
| 162 | + instance.setPortletCommonData( request, portlet ); |
| 163 | + fail( "Should have thrown" ); |
| 164 | + } |
| 165 | + catch ( AppException e ) |
| 166 | + { |
| 167 | + assertNotNull( e.getCause( ) ); |
| 168 | + assertTrue( e.getCause( ) instanceof AccessDeniedException ); |
| 169 | + assertEquals( "ALIAS_PORTLET", portlet.getName( ) ); |
| 170 | + assertEquals( 0, portlet.getOrder( ) ); |
| 171 | + assertEquals( 0, portlet.getColumn( ) ); |
| 172 | + assertEquals( 0, portlet.getAcceptAlias( ) ); |
| 173 | + assertEquals( 0, portlet.getDisplayPortletTitle( ) ); |
| 174 | + assertEquals( 0, portlet.getStyleId( ) ); |
| 175 | + assertEquals( 0, portlet.getPageId( ) ); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private String getRandomName( ) |
| 180 | + { |
| 181 | + Random rand = new SecureRandom( ); |
| 182 | + BigInteger bigInt = new BigInteger( 128, rand ); |
| 183 | + return "junit" + bigInt.toString( 36 ); |
| 184 | + } |
| 185 | +} |
0 commit comments