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.stylesheet;
35  
36  import java.io.IOException;
37  import java.math.BigInteger;
38  import java.security.SecureRandom;
39  import java.util.ArrayList;
40  import java.util.HashMap;
41  import java.util.List;
42  import java.util.Map;
43  import java.util.Random;
44  
45  import org.apache.commons.fileupload.FileItem;
46  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
47  import org.apache.commons.lang3.StringUtils;
48  import org.springframework.mock.web.MockHttpServletRequest;
49  
50  import fr.paris.lutece.portal.business.style.Style;
51  import fr.paris.lutece.portal.business.style.StyleHome;
52  import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
53  import fr.paris.lutece.portal.business.stylesheet.StyleSheetHome;
54  import fr.paris.lutece.portal.business.user.AdminUser;
55  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
56  import fr.paris.lutece.portal.service.message.AdminMessage;
57  import fr.paris.lutece.portal.service.message.AdminMessageService;
58  import fr.paris.lutece.portal.service.security.SecurityTokenService;
59  import fr.paris.lutece.portal.web.constants.Parameters;
60  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
61  import fr.paris.lutece.test.LuteceTestCase;
62  import fr.paris.lutece.test.Utils;
63  
64  /**
65   * StyleSheetJspBean Test Class
66   *
67   */
68  public class StyleSheetJspBeanTest extends LuteceTestCase
69  {
70  
71      private StyleSheetJspBean instance;
72      private Style style;
73      private StyleSheet stylesheet;
74  
75      @Override
76      protected void setUp( ) throws Exception
77      {
78          super.setUp( );
79          instance = new StyleSheetJspBean( );
80          style = new Style( );
81          int nId = StyleHome.getStylesList( ).stream( ).map( Style::getId ).max( Integer::compare ).get( ) + 1;
82          style.setId( nId );
83          style.setDescription( getRandomName( ) );
84          style.setPortalComponentId( 2 );
85          StyleHome.create( style );
86          stylesheet = new StyleSheet( );
87          stylesheet.setDescription( getRandomName( ) );
88          stylesheet.setModeId( 1 );
89          stylesheet.setStyleId( style.getId( ) );
90          stylesheet.setFile( "file" );
91          stylesheet.setSource( "<a/>".getBytes( ) );
92          StyleSheetHome.create( stylesheet );
93      }
94  
95      @Override
96      protected void tearDown( ) throws Exception
97      {
98          StyleSheetHome.remove( stylesheet.getId( ) );
99          StyleHome.remove( style.getId( ) );
100         super.tearDown( );
101     }
102 
103     private String getRandomName( )
104     {
105         Random rand = new SecureRandom( );
106         BigInteger bigInt = new BigInteger( 128, rand );
107         return "junit" + bigInt.toString( 36 );
108     }
109 
110     /**
111      * Test of getManageStyleSheet method, of class fr.paris.lutece.portal.web.stylesheet.StyleSheetJspBean.
112      */
113     public void testGetStyleSheetManagement( ) throws AccessDeniedException
114     {
115         MockHttpServletRequest request = new MockHttpServletRequest( );
116         Utils.registerAdminUserWithRigth( request, new AdminUser( ), StyleSheetJspBean.RIGHT_MANAGE_STYLESHEET );
117 
118         instance.init( request, StyleSheetJspBean.RIGHT_MANAGE_STYLESHEET );
119         assertTrue( StringUtils.isNotEmpty( instance.getManageStyleSheet( request ) ) );
120     }
121 
122     /**
123      * Test of getCreateStyleSheet method, of class fr.paris.lutece.portal.web.stylesheet.StyleSheetJspBean.
124      */
125     public void testGetCreateStyleSheet( ) throws AccessDeniedException
126     {
127         MockHttpServletRequest request = new MockHttpServletRequest( );
128         request.addParameter( Parameters.MODE_ID, "0" );
129         Utils.registerAdminUserWithRigth( request, new AdminUser( ), StyleSheetJspBean.RIGHT_MANAGE_STYLESHEET );
130 
131         instance.init( request, StyleSheetJspBean.RIGHT_MANAGE_STYLESHEET );
132         String html = instance.getCreateStyleSheet( request );
133         assertNotNull( html );
134     }
135 
136     /**
137      * Test of doCreateStyleSheet method, of class fr.paris.lutece.portal.web.stylesheet.StyleSheetJspBean.
138      * 
139      * @throws IOException
140      * @throws AccessDeniedException
141      */
142     public void testDoCreateStyleSheet( ) throws IOException, AccessDeniedException
143     {
144         MockHttpServletRequest request = new MockHttpServletRequest( );
145         Map<String, String [ ]> parameters = new HashMap<>( );
146         final String randomName = getRandomName( );
147         parameters.put( Parameters.STYLESHEET_NAME, new String [ ] {
148                 randomName
149         } );
150         parameters.put( Parameters.STYLES, new String [ ] {
151                 Integer.toString( style.getId( ) )
152         } );
153         parameters.put( Parameters.MODE_STYLESHEET, new String [ ] {
154                 "0"
155         } );
156         parameters.put( SecurityTokenService.PARAMETER_TOKEN, new String [ ] {
157                 SecurityTokenService.getInstance( ).getToken( request, "admin/stylesheet/create_stylesheet.html" )
158         } );
159         Map<String, List<FileItem>> multipartFiles = new HashMap<>( );
160         List<FileItem> items = new ArrayList<>( );
161         FileItem source = new DiskFileItemFactory( ).createItem( Parameters.STYLESHEET_SOURCE, "application/xml", true, randomName );
162         source.getOutputStream( ).write( "<a/>".getBytes( ) );
163         items.add( source );
164         multipartFiles.put( Parameters.STYLESHEET_SOURCE, items );
165         MultipartHttpServletRequest multipart = new MultipartHttpServletRequest( request, multipartFiles, parameters );
166         try
167         {
168             instance.doCreateStyleSheet( multipart );
169             assertTrue( StyleSheetHome.getStyleSheetList( 0 ).stream( ).anyMatch( stylesheet -> stylesheet.getDescription( ).equals( randomName ) ) );
170         }
171         finally
172         {
173             StyleSheetHome.getStyleSheetList( 0 ).stream( ).filter( stylesheet -> stylesheet.getDescription( ).equals( randomName ) )
174                     .forEach( stylesheet -> StyleSheetHome.remove( stylesheet.getId( ) ) );
175         }
176     }
177 
178     public void testDoCreateStyleSheetInvalidToken( ) throws IOException, AccessDeniedException
179     {
180         MockHttpServletRequest request = new MockHttpServletRequest( );
181         Map<String, String [ ]> parameters = new HashMap<>( );
182         final String randomName = getRandomName( );
183         parameters.put( Parameters.STYLESHEET_NAME, new String [ ] {
184                 randomName
185         } );
186         parameters.put( Parameters.STYLES, new String [ ] {
187                 Integer.toString( style.getId( ) )
188         } );
189         parameters.put( Parameters.MODE_STYLESHEET, new String [ ] {
190                 "0"
191         } );
192         parameters.put( SecurityTokenService.PARAMETER_TOKEN, new String [ ] {
193                 SecurityTokenService.getInstance( ).getToken( request, "admin/stylesheet/create_stylesheet.html" ) + "b"
194         } );
195         Map<String, List<FileItem>> multipartFiles = new HashMap<>( );
196         List<FileItem> items = new ArrayList<>( );
197         FileItem source = new DiskFileItemFactory( ).createItem( Parameters.STYLESHEET_SOURCE, "application/xml", true, randomName );
198         source.getOutputStream( ).write( "<a/>".getBytes( ) );
199         items.add( source );
200         multipartFiles.put( Parameters.STYLESHEET_SOURCE, items );
201         MultipartHttpServletRequest multipart = new MultipartHttpServletRequest( request, multipartFiles, parameters );
202         try
203         {
204             instance.doCreateStyleSheet( multipart );
205             fail( "Should have thrown" );
206         }
207         catch( AccessDeniedException e )
208         {
209             assertTrue( StyleSheetHome.getStyleSheetList( 0 ).stream( ).noneMatch( stylesheet -> stylesheet.getDescription( ).equals( randomName ) ) );
210         }
211         finally
212         {
213             StyleSheetHome.getStyleSheetList( 0 ).stream( ).filter( stylesheet -> stylesheet.getDescription( ).equals( randomName ) )
214                     .forEach( stylesheet -> StyleSheetHome.remove( stylesheet.getId( ) ) );
215         }
216     }
217 
218     public void testDoCreateStyleSheetNoToken( ) throws IOException, AccessDeniedException
219     {
220         MockHttpServletRequest request = new MockHttpServletRequest( );
221         Map<String, String [ ]> parameters = new HashMap<>( );
222         final String randomName = getRandomName( );
223         parameters.put( Parameters.STYLESHEET_NAME, new String [ ] {
224                 randomName
225         } );
226         parameters.put( Parameters.STYLES, new String [ ] {
227                 Integer.toString( style.getId( ) )
228         } );
229         parameters.put( Parameters.MODE_STYLESHEET, new String [ ] {
230                 "0"
231         } );
232         Map<String, List<FileItem>> multipartFiles = new HashMap<>( );
233         List<FileItem> items = new ArrayList<>( );
234         FileItem source = new DiskFileItemFactory( ).createItem( Parameters.STYLESHEET_SOURCE, "application/xml", true, randomName );
235         source.getOutputStream( ).write( "<a/>".getBytes( ) );
236         items.add( source );
237         multipartFiles.put( Parameters.STYLESHEET_SOURCE, items );
238         MultipartHttpServletRequest multipart = new MultipartHttpServletRequest( request, multipartFiles, parameters );
239         try
240         {
241             instance.doCreateStyleSheet( multipart );
242             fail( "Should have thrown" );
243         }
244         catch( AccessDeniedException e )
245         {
246             assertTrue( StyleSheetHome.getStyleSheetList( 0 ).stream( ).noneMatch( stylesheet -> stylesheet.getDescription( ).equals( randomName ) ) );
247         }
248         finally
249         {
250             StyleSheetHome.getStyleSheetList( 0 ).stream( ).filter( stylesheet -> stylesheet.getDescription( ).equals( randomName ) )
251                     .forEach( stylesheet -> StyleSheetHome.remove( stylesheet.getId( ) ) );
252         }
253     }
254 
255     /**
256      * Test of getModifyStyleSheet method, of class fr.paris.lutece.portal.web.stylesheet.StyleSheetJspBean.
257      */
258     public void testGetModifyStyleSheet( ) throws AccessDeniedException
259     {
260         MockHttpServletRequest request = new MockHttpServletRequest( );
261         request.addParameter( Parameters.STYLESHEET_ID, Integer.toString( stylesheet.getId( ) ) );
262         Utils.registerAdminUserWithRigth( request, new AdminUser( ), StyleSheetJspBean.RIGHT_MANAGE_STYLESHEET );
263 
264         instance.init( request, StyleSheetJspBean.RIGHT_MANAGE_STYLESHEET );
265         assertNotNull( instance.getModifyStyleSheet( request ) );
266     }
267 
268     /**
269      * Test of doModifyStyleSheet method, of class fr.paris.lutece.portal.web.stylesheet.StyleSheetJspBean.
270      * 
271      * @throws AccessDeniedException
272      * @throws IOException
273      */
274     public void testDoModifyStyleSheet( ) throws AccessDeniedException, IOException
275     {
276         MockHttpServletRequest request = new MockHttpServletRequest( );
277         Map<String, String [ ]> parameters = new HashMap<>( );
278         parameters.put( Parameters.STYLESHEET_ID, new String [ ] {
279                 Integer.toString( stylesheet.getId( ) )
280         } );
281         parameters.put( Parameters.STYLESHEET_NAME, new String [ ] {
282                 stylesheet.getDescription( ) + "_mod"
283         } );
284         parameters.put( Parameters.STYLES, new String [ ] {
285                 Integer.toString( stylesheet.getStyleId( ) )
286         } );
287         parameters.put( Parameters.MODE_STYLESHEET, new String [ ] {
288                 Integer.toString( stylesheet.getModeId( ) )
289         } );
290         parameters.put( SecurityTokenService.PARAMETER_TOKEN, new String [ ] {
291                 SecurityTokenService.getInstance( ).getToken( request, "admin/stylesheet/modify_stylesheet.html" )
292         } );
293         Map<String, List<FileItem>> multipartFiles = new HashMap<>( );
294         List<FileItem> items = new ArrayList<>( );
295         FileItem source = new DiskFileItemFactory( ).createItem( Parameters.STYLESHEET_SOURCE, "application/xml", true, stylesheet.getDescription( ) );
296         source.getOutputStream( ).write( "<a/>".getBytes( ) );
297         items.add( source );
298         multipartFiles.put( Parameters.STYLESHEET_SOURCE, items );
299         MultipartHttpServletRequest multipart = new MultipartHttpServletRequest( request, multipartFiles, parameters );
300 
301         instance.doModifyStyleSheet( multipart );
302         AdminMessage message = AdminMessageService.getMessage( request );
303         assertNull( message );
304         StyleSheet stored = StyleSheetHome.findByPrimaryKey( stylesheet.getId( ) );
305         assertNotNull( stored );
306         assertEquals( stylesheet.getDescription( ) + "_mod", stored.getDescription( ) );
307     }
308 
309     public void testDoModifyStyleSheetInvalidToken( ) throws AccessDeniedException, IOException
310     {
311         MockHttpServletRequest request = new MockHttpServletRequest( );
312         Map<String, String [ ]> parameters = new HashMap<>( );
313         parameters.put( Parameters.STYLESHEET_ID, new String [ ] {
314                 Integer.toString( stylesheet.getId( ) )
315         } );
316         parameters.put( Parameters.STYLESHEET_NAME, new String [ ] {
317                 stylesheet.getDescription( ) + "_mod"
318         } );
319         parameters.put( Parameters.STYLES, new String [ ] {
320                 Integer.toString( stylesheet.getStyleId( ) )
321         } );
322         parameters.put( Parameters.MODE_STYLESHEET, new String [ ] {
323                 Integer.toString( stylesheet.getModeId( ) )
324         } );
325         parameters.put( SecurityTokenService.PARAMETER_TOKEN, new String [ ] {
326                 SecurityTokenService.getInstance( ).getToken( request, "admin/stylesheet/modify_stylesheet.html" ) + "b"
327         } );
328         Map<String, List<FileItem>> multipartFiles = new HashMap<>( );
329         List<FileItem> items = new ArrayList<>( );
330         FileItem source = new DiskFileItemFactory( ).createItem( Parameters.STYLESHEET_SOURCE, "application/xml", true, stylesheet.getDescription( ) );
331         source.getOutputStream( ).write( "<a/>".getBytes( ) );
332         items.add( source );
333         multipartFiles.put( Parameters.STYLESHEET_SOURCE, items );
334         MultipartHttpServletRequest multipart = new MultipartHttpServletRequest( request, multipartFiles, parameters );
335         try
336         {
337             instance.doModifyStyleSheet( multipart );
338             fail( "Should have thrown" );
339         }
340         catch( AccessDeniedException e )
341         {
342             StyleSheet stored = StyleSheetHome.findByPrimaryKey( stylesheet.getId( ) );
343             assertNotNull( stored );
344             assertEquals( stylesheet.getDescription( ), stored.getDescription( ) );
345         }
346     }
347 
348     public void testDoModifyStyleSheetNoToken( ) throws AccessDeniedException, IOException
349     {
350         MockHttpServletRequest request = new MockHttpServletRequest( );
351         Map<String, String [ ]> parameters = new HashMap<>( );
352         parameters.put( Parameters.STYLESHEET_ID, new String [ ] {
353                 Integer.toString( stylesheet.getId( ) )
354         } );
355         parameters.put( Parameters.STYLESHEET_NAME, new String [ ] {
356                 stylesheet.getDescription( ) + "_mod"
357         } );
358         parameters.put( Parameters.STYLES, new String [ ] {
359                 Integer.toString( stylesheet.getStyleId( ) )
360         } );
361         parameters.put( Parameters.MODE_STYLESHEET, new String [ ] {
362                 Integer.toString( stylesheet.getModeId( ) )
363         } );
364         Map<String, List<FileItem>> multipartFiles = new HashMap<>( );
365         List<FileItem> items = new ArrayList<>( );
366         FileItem source = new DiskFileItemFactory( ).createItem( Parameters.STYLESHEET_SOURCE, "application/xml", true, stylesheet.getDescription( ) );
367         source.getOutputStream( ).write( "<a/>".getBytes( ) );
368         items.add( source );
369         multipartFiles.put( Parameters.STYLESHEET_SOURCE, items );
370         MultipartHttpServletRequest multipart = new MultipartHttpServletRequest( request, multipartFiles, parameters );
371         try
372         {
373             instance.doModifyStyleSheet( multipart );
374             fail( "Should have thrown" );
375         }
376         catch( AccessDeniedException e )
377         {
378             StyleSheet stored = StyleSheetHome.findByPrimaryKey( stylesheet.getId( ) );
379             assertNotNull( stored );
380             assertEquals( stylesheet.getDescription( ), stored.getDescription( ) );
381         }
382     }
383 
384     /**
385      * Test of getConfirmRemoveStyleSheet method, of class fr.paris.lutece.portal.web.stylesheet.StyleSheetJspBean.
386      */
387     public void testGetConfirmRemoveStyleSheet( ) throws AccessDeniedException
388     {
389         MockHttpServletRequest request = new MockHttpServletRequest( );
390         request.addParameter( Parameters.STYLESHEET_ID, Integer.toString( stylesheet.getId( ) ) );
391         request.addParameter( Parameters.STYLE_ID, Integer.toString( style.getId( ) ) );
392         Utils.registerAdminUserWithRigth( request, new AdminUser( ), StyleSheetJspBean.RIGHT_MANAGE_STYLESHEET );
393 
394         instance.init( request, StyleSheetJspBean.RIGHT_MANAGE_STYLESHEET );
395         instance.getRemoveStyleSheet( request );
396         AdminMessage message = AdminMessageService.getMessage( request );
397         assertNotNull( message );
398         assertTrue( message.getRequestParameters( ).containsKey( SecurityTokenService.PARAMETER_TOKEN ) );
399     }
400 
401     /**
402      * Test of doRemoveStyleSheet method, of class fr.paris.lutece.portal.web.stylesheet.StyleSheetJspBean.
403      * 
404      * @throws AccessDeniedException
405      */
406     public void testDoRemoveStyleSheet( ) throws AccessDeniedException
407     {
408         MockHttpServletRequest request = new MockHttpServletRequest( );
409         request.addParameter( Parameters.STYLESHEET_ID, Integer.toString( stylesheet.getId( ) ) );
410         request.addParameter( Parameters.STYLE_ID, Integer.toString( style.getId( ) ) );
411         request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
412                 SecurityTokenService.getInstance( ).getToken( request, "jsp/admin/style/DoRemoveStyleSheet.jsp" ) );
413 
414         instance.doRemoveStyleSheet( request );
415         assertNull( StyleSheetHome.findByPrimaryKey( stylesheet.getId( ) ) );
416     }
417 
418     public void testDoRemoveStyleSheetInvalidToken( ) throws AccessDeniedException
419     {
420         MockHttpServletRequest request = new MockHttpServletRequest( );
421         request.addParameter( Parameters.STYLESHEET_ID, Integer.toString( stylesheet.getId( ) ) );
422         request.addParameter( Parameters.STYLE_ID, Integer.toString( style.getId( ) ) );
423         request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
424                 SecurityTokenService.getInstance( ).getToken( request, "jsp/admin/style/DoRemoveStyleSheet.jsp" ) + "b" );
425 
426         try
427         {
428             instance.doRemoveStyleSheet( request );
429             fail( "Should have thrown" );
430         }
431         catch( AccessDeniedException e )
432         {
433             StyleSheet stored = StyleSheetHome.findByPrimaryKey( stylesheet.getId( ) );
434             assertNotNull( stored );
435             assertEquals( stylesheet.getId( ), stored.getId( ) );
436         }
437     }
438 
439     public void testDoRemoveStyleSheetNoToken( ) throws AccessDeniedException
440     {
441         MockHttpServletRequest request = new MockHttpServletRequest( );
442         request.addParameter( Parameters.STYLESHEET_ID, Integer.toString( stylesheet.getId( ) ) );
443         request.addParameter( Parameters.STYLE_ID, Integer.toString( style.getId( ) ) );
444 
445         try
446         {
447             instance.doRemoveStyleSheet( request );
448             fail( "Should have thrown" );
449         }
450         catch( AccessDeniedException e )
451         {
452             StyleSheet stored = StyleSheetHome.findByPrimaryKey( stylesheet.getId( ) );
453             assertNotNull( stored );
454             assertEquals( stylesheet.getId( ), stored.getId( ) );
455         }
456     }
457 }