View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.dashboard;
35  
36  import java.math.BigInteger;
37  import java.security.SecureRandom;
38  import java.util.Random;
39  
40  import org.springframework.mock.web.MockHttpServletRequest;
41  
42  import fr.paris.lutece.portal.business.dashboard.AdminDashboardFactory;
43  import fr.paris.lutece.portal.business.dashboard.AdminDashboardHome;
44  import fr.paris.lutece.portal.business.user.AdminUser;
45  import fr.paris.lutece.portal.business.user.AdminUserHome;
46  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
47  import fr.paris.lutece.portal.service.admin.AdminAuthenticationService;
48  import fr.paris.lutece.portal.service.admin.PasswordResetException;
49  import fr.paris.lutece.portal.service.dashboard.admin.IAdminDashboardComponent;
50  import fr.paris.lutece.portal.service.security.SecurityTokenService;
51  import fr.paris.lutece.test.LuteceTestCase;
52  import fr.paris.lutece.test.Utils;
53  
54  /**
55   * AdminDashboardJspBean Test Class
56   *
57   */
58  public class AdminDashboardJspBeanTest extends LuteceTestCase
59  {
60  
61      private AdminDashboardJspBean instance;
62      private IAdminDashboardComponent _dashboard;
63  
64      @Override
65      protected void setUp( ) throws Exception
66      {
67          super.setUp( );
68          instance = new AdminDashboardJspBean( );
69          _dashboard = new TestAdminDashboardCompoent( );
70          _dashboard.setName( getRandomName( ) );
71          AdminDashboardFactory.registerDashboardComponent( _dashboard );
72          AdminDashboardHome.create( _dashboard );
73      }
74  
75      @Override
76      protected void tearDown( ) throws Exception
77      {
78          AdminDashboardHome.remove( _dashboard.getName( ) );
79          // TODO : dashboard should be unregistered
80          super.tearDown( );
81      }
82  
83      private String getRandomName( )
84      {
85          Random rand = new SecureRandom( );
86          BigInteger bigInt = new BigInteger( 128, rand );
87          return "junit" + bigInt.toString( 36 );
88      }
89  
90      /**
91       * Test of getAdminDashboards method, of class fr.paris.lutece.portal.web.dashboard.AdminDashboardJspBean.
92       * 
93       * @throws Exception
94       *             e
95       */
96      public void testGetAdminDashboards( ) throws Exception
97      {
98          MockHttpServletRequest request = new MockHttpServletRequest( );
99  
100         AdminUser user = AdminUserHome.findUserByLogin( "admin" );
101         AdminAuthenticationService.getInstance( ).registerUser( request, user );
102 
103         instance = new AdminDashboardJspBean( );
104 
105         // should not throw on freemarker templating
106         String adminDashboards = instance.getAdminDashboards( request );
107         assertNotNull( adminDashboards );
108         assertFalse( "".equals( adminDashboards ) );
109     }
110 
111     public void testGetManageDashboards( ) throws PasswordResetException, AccessDeniedException
112     {
113         MockHttpServletRequest request = new MockHttpServletRequest( );
114         Utils.registerAdminUserWithRigth( request, new AdminUser( ), AdminDashboardJspBean.RIGHT_MANAGE_ADMINDASHBOARD );
115 
116         instance.init( request, AdminDashboardJspBean.RIGHT_MANAGE_ADMINDASHBOARD );
117 
118         assertNotNull( instance.getManageDashboards( request ) );
119     }
120 
121     public void testDoMoveAdminDashboard( ) throws AccessDeniedException
122     {
123         IAdminDashboardComponent stored = AdminDashboardHome.findByPrimaryKey( _dashboard.getName( ) );
124         assertNotNull( stored );
125         assertEquals( 0, stored.getOrder( ) );
126         assertEquals( 0, stored.getZone( ) );
127 
128         MockHttpServletRequest request = new MockHttpServletRequest( );
129         request.setParameter( "dashboard_name", _dashboard.getName( ) );
130         request.setParameter( "dashboard_order", "-1" );
131         request.setParameter( "dashboard_column", "-1" );
132         request.setParameter( SecurityTokenService.PARAMETER_TOKEN,
133                 SecurityTokenService.getInstance( ).getToken( request, "/admin/dashboard/admin/manage_dashboards.html" ) );
134 
135         instance.doMoveAdminDashboard( request );
136 
137         stored = AdminDashboardHome.findByPrimaryKey( _dashboard.getName( ) );
138         assertNotNull( stored );
139         assertEquals( 1, stored.getOrder( ) );
140         assertEquals( -1, stored.getZone( ) );
141     }
142 
143     public void testDoMoveAdminDashboardInvalidToken( ) throws AccessDeniedException
144     {
145         IAdminDashboardComponent stored = AdminDashboardHome.findByPrimaryKey( _dashboard.getName( ) );
146         assertNotNull( stored );
147         assertEquals( 0, stored.getOrder( ) );
148         assertEquals( 0, stored.getZone( ) );
149 
150         MockHttpServletRequest request = new MockHttpServletRequest( );
151         request.setParameter( "dashboard_name", _dashboard.getName( ) );
152         request.setParameter( "dashboard_order", "-1" );
153         request.setParameter( "dashboard_column", "-1" );
154         request.setParameter( SecurityTokenService.PARAMETER_TOKEN,
155                 SecurityTokenService.getInstance( ).getToken( request, "/admin/dashboard/admin/manage_dashboards.html" ) + "b" );
156 
157         try
158         {
159             instance.doMoveAdminDashboard( request );
160             fail( "Should have thrown" );
161         }
162         catch( AccessDeniedException e )
163         {
164             stored = AdminDashboardHome.findByPrimaryKey( _dashboard.getName( ) );
165             assertNotNull( stored );
166             assertEquals( 0, stored.getOrder( ) );
167             assertEquals( 0, stored.getZone( ) );
168         }
169     }
170 
171     public void testDoMoveAdminDashboardNoToken( ) throws AccessDeniedException
172     {
173         IAdminDashboardComponent stored = AdminDashboardHome.findByPrimaryKey( _dashboard.getName( ) );
174         assertNotNull( stored );
175         assertEquals( 0, stored.getOrder( ) );
176         assertEquals( 0, stored.getZone( ) );
177 
178         MockHttpServletRequest request = new MockHttpServletRequest( );
179         request.setParameter( "dashboard_name", _dashboard.getName( ) );
180         request.setParameter( "dashboard_order", "-1" );
181         request.setParameter( "dashboard_column", "-1" );
182 
183         try
184         {
185             instance.doMoveAdminDashboard( request );
186             fail( "Should have thrown" );
187         }
188         catch( AccessDeniedException e )
189         {
190             stored = AdminDashboardHome.findByPrimaryKey( _dashboard.getName( ) );
191             assertNotNull( stored );
192             assertEquals( 0, stored.getOrder( ) );
193             assertEquals( 0, stored.getZone( ) );
194         }
195     }
196 
197     public void testDoReorderColumn( ) throws AccessDeniedException
198     {
199         IAdminDashboardComponent stored = AdminDashboardHome.findByPrimaryKey( _dashboard.getName( ) );
200         assertNotNull( stored );
201         assertEquals( 0, stored.getOrder( ) );
202         assertEquals( 0, stored.getZone( ) );
203         int nZone = AdminDashboardHome.findColumns( ).stream( ).max( Integer::compare ).orElse( 1 );
204         stored.setZone( nZone );
205         stored.setOrder( -1 );
206         AdminDashboardHome.update( stored );
207         stored = AdminDashboardHome.findByPrimaryKey( _dashboard.getName( ) );
208         assertEquals( -1, stored.getOrder( ) );
209         assertEquals( nZone, stored.getZone( ) );
210 
211         MockHttpServletRequest request = new MockHttpServletRequest( );
212         request.setParameter( "column", Integer.toString( nZone ) );
213         request.setParameter( SecurityTokenService.PARAMETER_TOKEN,
214                 SecurityTokenService.getInstance( ).getToken( request, "/admin/dashboard/admin/manage_dashboards.html" ) );
215 
216         instance.doReorderColumn( request );
217 
218         stored = AdminDashboardHome.findByPrimaryKey( _dashboard.getName( ) );
219         assertEquals( 1, stored.getOrder( ) );
220         assertEquals( nZone, stored.getZone( ) );
221     }
222 
223     public void testDoReorderColumnInvalidToken( ) throws AccessDeniedException
224     {
225         IAdminDashboardComponent stored = AdminDashboardHome.findByPrimaryKey( _dashboard.getName( ) );
226         assertNotNull( stored );
227         assertEquals( 0, stored.getOrder( ) );
228         assertEquals( 0, stored.getZone( ) );
229         int nZone = AdminDashboardHome.findColumns( ).stream( ).max( Integer::compare ).orElse( 0 ) + 1;
230         stored.setZone( nZone );
231         stored.setOrder( -1 );
232         AdminDashboardHome.update( stored );
233         stored = AdminDashboardHome.findByPrimaryKey( _dashboard.getName( ) );
234         assertEquals( -1, stored.getOrder( ) );
235         assertEquals( nZone, stored.getZone( ) );
236 
237         MockHttpServletRequest request = new MockHttpServletRequest( );
238         request.setParameter( "column", Integer.toString( nZone ) );
239         request.setParameter( SecurityTokenService.PARAMETER_TOKEN,
240                 SecurityTokenService.getInstance( ).getToken( request, "/admin/dashboard/admin/manage_dashboards.html" ) + "b" );
241 
242         try
243         {
244             instance.doReorderColumn( request );
245             fail( "Should have thrown" );
246         }
247         catch( AccessDeniedException e )
248         {
249             stored = AdminDashboardHome.findByPrimaryKey( _dashboard.getName( ) );
250             assertEquals( -1, stored.getOrder( ) );
251             assertEquals( nZone, stored.getZone( ) );
252         }
253     }
254 
255     public void testDoReorderColumnNoToken( ) throws AccessDeniedException
256     {
257         IAdminDashboardComponent stored = AdminDashboardHome.findByPrimaryKey( _dashboard.getName( ) );
258         assertNotNull( stored );
259         assertEquals( 0, stored.getOrder( ) );
260         assertEquals( 0, stored.getZone( ) );
261         int nZone = AdminDashboardHome.findColumns( ).stream( ).max( Integer::compare ).orElse( 0 ) + 1;
262         stored.setZone( nZone );
263         stored.setOrder( -1 );
264         AdminDashboardHome.update( stored );
265         stored = AdminDashboardHome.findByPrimaryKey( _dashboard.getName( ) );
266         assertEquals( -1, stored.getOrder( ) );
267         assertEquals( nZone, stored.getZone( ) );
268 
269         MockHttpServletRequest request = new MockHttpServletRequest( );
270         request.setParameter( "column", Integer.toString( nZone ) );
271 
272         try
273         {
274             instance.doReorderColumn( request );
275             fail( "Should have thrown" );
276         }
277         catch( AccessDeniedException e )
278         {
279             stored = AdminDashboardHome.findByPrimaryKey( _dashboard.getName( ) );
280             assertEquals( -1, stored.getOrder( ) );
281             assertEquals( nZone, stored.getZone( ) );
282         }
283     }
284 }