View Javadoc
1   /*
2    * Copyright (c) 2002-2025, City of 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.style;
35  
36  import java.math.BigInteger;
37  import java.security.SecureRandom;
38  import java.util.Random;
39  
40  import org.apache.commons.lang3.StringUtils;
41  import org.springframework.mock.web.MockHttpServletRequest;
42  
43  import fr.paris.lutece.portal.business.style.Style;
44  import fr.paris.lutece.portal.business.style.StyleHome;
45  import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
46  import fr.paris.lutece.portal.business.stylesheet.StyleSheetHome;
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.message.AdminMessage;
50  import fr.paris.lutece.portal.service.message.AdminMessageService;
51  import fr.paris.lutece.portal.service.security.SecurityTokenService;
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  /**
57   * StylesJspBeanTest Test Class
58   *
59   */
60  public class StylesJspBeanTest extends LuteceTestCase
61  {
62      private StylesJspBean instance;
63      private Style style;
64  
65      @Override
66      protected void setUp( ) throws Exception
67      {
68          super.setUp( );
69          instance = new StylesJspBean( );
70          style = new Style( );
71          int nId = StyleHome.getStylesList( ).stream( ).map( Style::getId ).max( Integer::compare ).get( ) + 1;
72          style.setId( nId );
73          style.setDescription( getRandomName( ) );
74          style.setPortalComponentId( 2 );
75          StyleHome.create( style );
76      }
77  
78      @Override
79      protected void tearDown( ) throws Exception
80      {
81          StyleHome.remove( style.getId( ) );
82          super.tearDown( );
83      }
84  
85      private String getRandomName( )
86      {
87          Random rand = new SecureRandom( );
88          BigInteger bigInt = new BigInteger( 128, rand );
89          return "junit" + bigInt.toString( 36 );
90      }
91  
92      /**
93       * Test of getStylesManagement method, of class fr.paris.lutece.portal.web.style.StylesJspBean.
94       */
95      public void testGetStylesManagement( ) throws AccessDeniedException
96      {
97          System.out.println( "getStylesManagement" );
98  
99          MockHttpServletRequest request = new MockHttpServletRequest( );
100         Utils.registerAdminUserWithRigth( request, new AdminUser( ), StylesJspBean.RIGHT_MANAGE_STYLE );
101 
102         instance.init( request, StylesJspBean.RIGHT_MANAGE_STYLE );
103         assertTrue( StringUtils.isNotEmpty( instance.getStylesManagement( request ) ) );
104     }
105 
106     /**
107      * Test of getCreateStyle method, of class fr.paris.lutece.portal.web.style.StylesJspBean.
108      */
109     public void testGetCreateStyle( ) throws AccessDeniedException
110     {
111         MockHttpServletRequest request = new MockHttpServletRequest( );
112         Utils.registerAdminUserWithRigth( request, new AdminUser( ), StylesJspBean.RIGHT_MANAGE_STYLE );
113 
114         instance.init( request, StylesJspBean.RIGHT_MANAGE_STYLE );
115         String html = instance.getCreateStyle( request );
116         assertNotNull( html );
117     }
118 
119     /**
120      * Test of doCreateStyle method, of fr.paris.lutece.portal.web.style.StylesJspBean.
121      * 
122      * @throws AccessDeniedException
123      */
124     public void testDoCreateStyle( ) throws AccessDeniedException
125     {
126         MockHttpServletRequest request = new MockHttpServletRequest( );
127         int nId = StyleHome.getStylesList( ).stream( ).map( Style::getId ).max( Integer::compare ).get( ) + 1;
128         request.addParameter( Parameters.STYLE_ID, Integer.toString( nId ) );
129         String name = getRandomName( );
130         request.addParameter( Parameters.STYLE_NAME, name );
131         String portalComponantId = "1";
132         request.addParameter( Parameters.PORTAL_COMPONENT, portalComponantId );
133         request.addParameter( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, "admin/style/create_style.html" ) );
134         try
135         {
136             instance.doCreateStyle( request );
137             AdminMessage message = AdminMessageService.getMessage( request );
138             assertNull( message );
139             Style stored = StyleHome.findByPrimaryKey( nId );
140             assertNotNull( stored );
141             assertEquals( nId, stored.getId( ) );
142         }
143         finally
144         {
145             StyleHome.remove( nId );
146         }
147     }
148 
149     public void testDoCreateStyleInvalidToken( ) throws AccessDeniedException
150     {
151         MockHttpServletRequest request = new MockHttpServletRequest( );
152         int nId = StyleHome.getStylesList( ).stream( ).map( Style::getId ).max( Integer::compare ).get( ) + 1;
153         request.addParameter( Parameters.STYLE_ID, Integer.toString( nId ) );
154         String name = getRandomName( );
155         request.addParameter( Parameters.STYLE_NAME, name );
156         String portalComponantId = "1";
157         request.addParameter( Parameters.PORTAL_COMPONENT, portalComponantId );
158         request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
159                 SecurityTokenService.getInstance( ).getToken( request, "admin/style/create_style.html" ) + "b" );
160         try
161         {
162             instance.doCreateStyle( request );
163             fail( "Should have thrown" );
164         }
165         catch( AccessDeniedException e )
166         {
167             Style stored = StyleHome.findByPrimaryKey( nId );
168             assertNull( stored );
169         }
170         finally
171         {
172             StyleHome.remove( nId );
173         }
174     }
175 
176     public void testDoCreateStyleNoToken( ) throws AccessDeniedException
177     {
178         MockHttpServletRequest request = new MockHttpServletRequest( );
179         int nId = StyleHome.getStylesList( ).stream( ).map( Style::getId ).max( Integer::compare ).get( ) + 1;
180         request.addParameter( Parameters.STYLE_ID, Integer.toString( nId ) );
181         String name = getRandomName( );
182         request.addParameter( Parameters.STYLE_NAME, name );
183         String portalComponantId = "1";
184         request.addParameter( Parameters.PORTAL_COMPONENT, portalComponantId );
185         try
186         {
187             instance.doCreateStyle( request );
188             fail( "Should have thrown" );
189         }
190         catch( AccessDeniedException e )
191         {
192             Style stored = StyleHome.findByPrimaryKey( nId );
193             assertNull( stored );
194         }
195         finally
196         {
197             StyleHome.remove( nId );
198         }
199     }
200 
201     /**
202      * Test of getModifyStyle method, of class fr.paris.lutece.portal.web.style.StylesJspBean.
203      */
204     public void testGetModifyStyle( ) throws AccessDeniedException
205     {
206         int nStyleId = style.getId( );
207         MockHttpServletRequest request = new MockHttpServletRequest( );
208         request.addParameter( Parameters.STYLE_ID, Integer.toString( nStyleId ) );
209         Utils.registerAdminUserWithRigth( request, new AdminUser( ), StylesJspBean.RIGHT_MANAGE_STYLE );
210 
211         instance.init( request, StylesJspBean.RIGHT_MANAGE_STYLE );
212         String html = instance.getModifyStyle( request );
213         assertNotNull( html );
214     }
215 
216     /**
217      * Test of doModifyStyle method, of fr.paris.lutece.portal.web.style.StylesJspBean.
218      * 
219      * @throws AccessDeniedException
220      */
221     public void testDoModifyStyle( ) throws AccessDeniedException
222     {
223         int nStyleId = style.getId( );
224         MockHttpServletRequest request = new MockHttpServletRequest( );
225         request.addParameter( Parameters.STYLE_ID, Integer.toString( nStyleId ) );
226         request.addParameter( Parameters.PORTAL_COMPONENT, Integer.toString( style.getPortalComponentId( ) ) );
227         request.addParameter( Parameters.STYLE_NAME, style.getDescription( ) + "_mod" );
228         request.addParameter( SecurityTokenService.PARAMETER_TOKEN, SecurityTokenService.getInstance( ).getToken( request, "admin/style/modify_style.html" ) );
229         instance.doModifyStyle( request );
230         AdminMessage message = AdminMessageService.getMessage( request );
231         assertNull( message );
232         Style stored = StyleHome.findByPrimaryKey( style.getId( ) );
233         assertNotNull( stored );
234         assertEquals( style.getId( ), stored.getId( ) );
235         assertEquals( style.getDescription( ) + "_mod", stored.getDescription( ) );
236     }
237 
238     public void testDoModifyStyleInvalidToken( ) throws AccessDeniedException
239     {
240         int nStyleId = style.getId( );
241         MockHttpServletRequest request = new MockHttpServletRequest( );
242         request.addParameter( Parameters.STYLE_ID, Integer.toString( nStyleId ) );
243         request.addParameter( Parameters.PORTAL_COMPONENT, Integer.toString( style.getPortalComponentId( ) ) );
244         request.addParameter( Parameters.STYLE_NAME, style.getDescription( ) + "_mod" );
245         request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
246                 SecurityTokenService.getInstance( ).getToken( request, "admin/style/modify_style.html" ) + "b" );
247         try
248         {
249             instance.doModifyStyle( request );
250             fail( "Should have thrown" );
251         }
252         catch( AccessDeniedException e )
253         {
254             Style stored = StyleHome.findByPrimaryKey( style.getId( ) );
255             assertNotNull( stored );
256             assertEquals( style.getId( ), stored.getId( ) );
257             assertEquals( style.getDescription( ), stored.getDescription( ) );
258         }
259     }
260 
261     public void testDoModifyStyleNoToken( ) throws AccessDeniedException
262     {
263         int nStyleId = style.getId( );
264         MockHttpServletRequest request = new MockHttpServletRequest( );
265         request.addParameter( Parameters.STYLE_ID, Integer.toString( nStyleId ) );
266         request.addParameter( Parameters.PORTAL_COMPONENT, Integer.toString( style.getPortalComponentId( ) ) );
267         request.addParameter( Parameters.STYLE_NAME, style.getDescription( ) + "_mod" );
268         try
269         {
270             instance.doModifyStyle( request );
271             fail( "Should have thrown" );
272         }
273         catch( AccessDeniedException e )
274         {
275             Style stored = StyleHome.findByPrimaryKey( style.getId( ) );
276             assertNotNull( stored );
277             assertEquals( style.getId( ), stored.getId( ) );
278             assertEquals( style.getDescription( ), stored.getDescription( ) );
279         }
280     }
281 
282     /**
283      * Test of getConfirmRemoveStyle method, of class fr.paris.lutece.portal.web.style.StylesJspBean.
284      */
285     public void testGetConfirmRemoveStyle( ) throws AccessDeniedException
286     {
287         MockHttpServletRequest request = new MockHttpServletRequest( );
288         request.addParameter( Parameters.STYLE_ID, Integer.toString( style.getId( ) ) );
289         Utils.registerAdminUserWithRigth( request, new AdminUser( ), StylesJspBean.RIGHT_MANAGE_STYLE );
290 
291         instance.init( request, StylesJspBean.RIGHT_MANAGE_STYLE );
292         instance.getConfirmRemoveStyle( request );
293         AdminMessage message = AdminMessageService.getMessage( request );
294         assertNotNull( message );
295         assertTrue( message.getRequestParameters( ).containsKey( SecurityTokenService.PARAMETER_TOKEN ) );
296     }
297 
298     public void testGetConfirmRemoveStyleWithStyleSheet( ) throws AccessDeniedException
299     {
300         StyleSheet stylesheet = new StyleSheet( );
301         String randomName = getRandomName( );
302         stylesheet.setDescription( randomName );
303         stylesheet.setModeId( 1 );
304         stylesheet.setStyleId( style.getId( ) );
305         stylesheet.setFile( "file" );
306         stylesheet.setSource( "<a/>".getBytes( ) );
307         StyleSheetHome.create( stylesheet );
308         try
309         {
310             MockHttpServletRequest request = new MockHttpServletRequest( );
311             request.addParameter( Parameters.STYLE_ID, Integer.toString( style.getId( ) ) );
312             request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
313                     SecurityTokenService.getInstance( ).getToken( request, "jsp/admin/style/DoRemoveStyle.jsp" ) );
314             instance.getConfirmRemoveStyle( request );
315             AdminMessage message = AdminMessageService.getMessage( request );
316             assertNotNull( message );
317             assertTrue( message.getRequestParameters( ).containsKey( SecurityTokenService.PARAMETER_TOKEN ) );
318             assertTrue( message.getRequestParameters( ).containsKey( Parameters.STYLESHEET_ID ) );
319             assertEquals( Integer.toString( stylesheet.getId( ) ), message.getRequestParameters( ).get( Parameters.STYLESHEET_ID ) );
320         }
321         finally
322         {
323             StyleSheetHome.remove( stylesheet.getId( ) );
324         }
325     }
326 
327     /**
328      * Test of doRemoveStyle method, of fr.paris.lutece.portal.web.style.StylesJspBean.
329      * 
330      * @throws AccessDeniedException
331      */
332     public void testDoRemoveStyle( ) throws AccessDeniedException
333     {
334         MockHttpServletRequest request = new MockHttpServletRequest( );
335         request.addParameter( Parameters.STYLE_ID, Integer.toString( style.getId( ) ) );
336         request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
337                 SecurityTokenService.getInstance( ).getToken( request, "jsp/admin/style/DoRemoveStyle.jsp" ) );
338         instance.doRemoveStyle( request );
339         assertNull( StyleHome.findByPrimaryKey( style.getId( ) ) );
340     }
341 
342     public void testDoRemoveStyleInvalidToken( ) throws AccessDeniedException
343     {
344         MockHttpServletRequest request = new MockHttpServletRequest( );
345         request.addParameter( Parameters.STYLE_ID, Integer.toString( style.getId( ) ) );
346         request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
347                 SecurityTokenService.getInstance( ).getToken( request, "jsp/admin/style/DoRemoveStyle.jsp" ) + "b" );
348         try
349         {
350             instance.doRemoveStyle( request );
351             fail( "Should have thrown" );
352         }
353         catch( AccessDeniedException e )
354         {
355             assertNotNull( StyleHome.findByPrimaryKey( style.getId( ) ) );
356         }
357     }
358 
359     public void testDoRemoveStyleNoToken( ) throws AccessDeniedException
360     {
361         MockHttpServletRequest request = new MockHttpServletRequest( );
362         request.addParameter( Parameters.STYLE_ID, Integer.toString( style.getId( ) ) );
363         try
364         {
365             instance.doRemoveStyle( request );
366             fail( "Should have thrown" );
367         }
368         catch( AccessDeniedException e )
369         {
370             assertNotNull( StyleHome.findByPrimaryKey( style.getId( ) ) );
371         }
372     }
373 }