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.editor;
35  
36  import java.util.Locale;
37  
38  import org.springframework.mock.web.MockHttpServletRequest;
39  
40  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
41  import fr.paris.lutece.portal.service.editor.RichTextEditorService;
42  import fr.paris.lutece.portal.service.security.SecurityTokenService;
43  import fr.paris.lutece.test.LuteceTestCase;
44  
45  public class EditorChoiceLutecePanelJspBeanTest extends LuteceTestCase
46  {
47      private EditorChoiceLutecePanelJspBean _instance;
48      private String _boDefaultEditor;
49      private String _foDefaultEditor;
50  
51      @Override
52      protected void setUp( ) throws Exception
53      {
54          super.setUp( );
55          _instance = new EditorChoiceLutecePanelJspBean( );
56          _boDefaultEditor = RichTextEditorService.getBackOfficeDefaultEditor( );
57          _foDefaultEditor = RichTextEditorService.getFrontOfficeDefaultEditor( );
58      }
59  
60      @Override
61      protected void tearDown( ) throws Exception
62      {
63          RichTextEditorService.updateBackOfficeDefaultEditor( _boDefaultEditor );
64          RichTextEditorService.updateFrontOfficeDefaultEditor( _foDefaultEditor );
65          super.tearDown( );
66      }
67  
68      public void testDoUpdateBackOfficeEditor( ) throws AccessDeniedException
69      {
70          String strBOEditor = RichTextEditorService.getListEditorsForBackOffice( Locale.FRANCE ).stream( )
71                  .filter( ref -> !ref.getCode( ).equals( _boDefaultEditor ) ).findFirst( ).orElseThrow( IllegalStateException::new ).getCode( );
72          assertFalse( strBOEditor.equals( RichTextEditorService.getBackOfficeDefaultEditor( ) ) );
73  
74          MockHttpServletRequest request = new MockHttpServletRequest( );
75          request.setParameter( "editor_back_office", strBOEditor );
76          request.setParameter( SecurityTokenService.PARAMETER_TOKEN,
77                  SecurityTokenService.getInstance( ).getToken( request, "admin/dashboard/admin/editor_dashboard.html" ) );
78          _instance.doUpdateBackOfficeEditor( request );
79  
80          assertEquals( strBOEditor, RichTextEditorService.getBackOfficeDefaultEditor( ) );
81      }
82  
83      public void testDoUpdateBackOfficeEditorInvalidToken( ) throws AccessDeniedException
84      {
85          String strBOEditor = RichTextEditorService.getListEditorsForBackOffice( Locale.FRANCE ).stream( )
86                  .filter( ref -> !ref.getCode( ).equals( _boDefaultEditor ) ).findFirst( ).orElseThrow( IllegalStateException::new ).getCode( );
87          assertFalse( strBOEditor.equals( RichTextEditorService.getBackOfficeDefaultEditor( ) ) );
88  
89          MockHttpServletRequest request = new MockHttpServletRequest( );
90          request.setParameter( "editor_back_office", strBOEditor );
91          request.setParameter( SecurityTokenService.PARAMETER_TOKEN,
92                  SecurityTokenService.getInstance( ).getToken( request, "admin/dashboard/admin/editor_dashboard.html" ) + "b" );
93          try
94          {
95              _instance.doUpdateBackOfficeEditor( request );
96              fail( "Should have thrown" );
97          }
98          catch( AccessDeniedException e )
99          {
100             assertEquals( _boDefaultEditor, RichTextEditorService.getBackOfficeDefaultEditor( ) );
101         }
102     }
103 
104     public void testDoUpdateBackOfficeEditorNoToken( ) throws AccessDeniedException
105     {
106         String strBOEditor = RichTextEditorService.getListEditorsForBackOffice( Locale.FRANCE ).stream( )
107                 .filter( ref -> !ref.getCode( ).equals( _boDefaultEditor ) ).findFirst( ).orElseThrow( IllegalStateException::new ).getCode( );
108         assertFalse( strBOEditor.equals( RichTextEditorService.getBackOfficeDefaultEditor( ) ) );
109 
110         MockHttpServletRequest request = new MockHttpServletRequest( );
111         request.setParameter( "editor_back_office", strBOEditor );
112 
113         try
114         {
115             _instance.doUpdateBackOfficeEditor( request );
116             fail( "Should have thrown" );
117         }
118         catch( AccessDeniedException e )
119         {
120             assertEquals( _boDefaultEditor, RichTextEditorService.getBackOfficeDefaultEditor( ) );
121         }
122     }
123 
124     public void testDoUpdateFrontOfficeEditor( ) throws AccessDeniedException
125     {
126         String strFOEditor = RichTextEditorService.getListEditorsForFrontOffice( Locale.FRANCE ).stream( )
127                 .filter( ref -> !ref.getCode( ).equals( _foDefaultEditor ) ).findFirst( ).orElseThrow( IllegalStateException::new ).getCode( );
128         assertFalse( strFOEditor.equals( RichTextEditorService.getFrontOfficeDefaultEditor( ) ) );
129 
130         MockHttpServletRequest request = new MockHttpServletRequest( );
131         request.setParameter( "editor_front_office", strFOEditor );
132         request.setParameter( SecurityTokenService.PARAMETER_TOKEN,
133                 SecurityTokenService.getInstance( ).getToken( request, "admin/dashboard/admin/editor_dashboard.html" ) );
134         _instance.doUpdateFrontOfficeEditor( request );
135 
136         assertEquals( strFOEditor, RichTextEditorService.getFrontOfficeDefaultEditor( ) );
137     }
138 
139     public void testDoUpdateFrontOfficeEditorInvalidToken( ) throws AccessDeniedException
140     {
141         String strFOEditor = RichTextEditorService.getListEditorsForFrontOffice( Locale.FRANCE ).stream( )
142                 .filter( ref -> !ref.getCode( ).equals( _foDefaultEditor ) ).findFirst( ).orElseThrow( IllegalStateException::new ).getCode( );
143         assertFalse( strFOEditor.equals( RichTextEditorService.getFrontOfficeDefaultEditor( ) ) );
144 
145         MockHttpServletRequest request = new MockHttpServletRequest( );
146         request.setParameter( "editor_front_office", strFOEditor );
147         request.setParameter( SecurityTokenService.PARAMETER_TOKEN,
148                 SecurityTokenService.getInstance( ).getToken( request, "admin/dashboard/admin/editor_dashboard.html" ) + "b" );
149         try
150         {
151             _instance.doUpdateFrontOfficeEditor( request );
152             fail( "Should have thrown" );
153         }
154         catch( AccessDeniedException e )
155         {
156             assertEquals( _foDefaultEditor, RichTextEditorService.getFrontOfficeDefaultEditor( ) );
157         }
158     }
159 
160     public void testDoUpdateFrontOfficeEditorNoToken( ) throws AccessDeniedException
161     {
162         String strFOEditor = RichTextEditorService.getListEditorsForFrontOffice( Locale.FRANCE ).stream( )
163                 .filter( ref -> !ref.getCode( ).equals( _foDefaultEditor ) ).findFirst( ).orElseThrow( IllegalStateException::new ).getCode( );
164         assertFalse( strFOEditor.equals( RichTextEditorService.getFrontOfficeDefaultEditor( ) ) );
165 
166         MockHttpServletRequest request = new MockHttpServletRequest( );
167         request.setParameter( "editor_front_office", strFOEditor );
168 
169         try
170         {
171             _instance.doUpdateFrontOfficeEditor( request );
172             fail( "Should have thrown" );
173         }
174         catch( AccessDeniedException e )
175         {
176             assertEquals( _foDefaultEditor, RichTextEditorService.getFrontOfficeDefaultEditor( ) );
177         }
178     }
179 }