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  import java.util.function.Function;
40  import java.util.stream.Collectors;
41  
42  import org.springframework.mock.web.MockHttpServletRequest;
43  
44  import fr.paris.lutece.portal.business.dashboard.DashboardFactory;
45  import fr.paris.lutece.portal.business.dashboard.DashboardHome;
46  import fr.paris.lutece.portal.business.right.Right;
47  import fr.paris.lutece.portal.business.right.RightHome;
48  import fr.paris.lutece.portal.business.user.AdminUser;
49  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
50  import fr.paris.lutece.portal.service.admin.PasswordResetException;
51  import fr.paris.lutece.portal.service.dashboard.DashboardService;
52  import fr.paris.lutece.portal.service.dashboard.IDashboardComponent;
53  import fr.paris.lutece.portal.service.security.SecurityTokenService;
54  import fr.paris.lutece.test.LuteceTestCase;
55  import fr.paris.lutece.test.Utils;
56  
57  public class DashboardJspBeanTest extends LuteceTestCase
58  {
59      private DashboardJspBean _instance;
60      private IDashboardComponent _dashboard;
61      private int _nZone;
62  
63      @Override
64      protected void setUp( ) throws Exception
65      {
66          super.setUp( );
67          _instance = new DashboardJspBean( );
68          _dashboard = new TestDashboardComponent( );
69          _dashboard.setName( getRandomName( ) );
70          _dashboard.setRight( "ALL" );
71          _nZone = DashboardService.getInstance( ).getColumnCount( );
72          _dashboard.setZone( _nZone );
73          DashboardFactory.registerDashboardComponent( _dashboard );
74          DashboardHome.create( _dashboard );
75      }
76  
77      private String getRandomName( )
78      {
79          Random rand = new SecureRandom( );
80          BigInteger bigInt = new BigInteger( 128, rand );
81          return "junit" + bigInt.toString( 36 );
82      }
83  
84      @Override
85      protected void tearDown( ) throws Exception
86      {
87          DashboardHome.remove( _dashboard.getName( ) );
88          // TODO : dashboard should be unregistered
89          super.tearDown( );
90      }
91  
92      public void testGetManageDashboards( ) throws PasswordResetException, AccessDeniedException
93      {
94          MockHttpServletRequest request = new MockHttpServletRequest( );
95          AdminUser user = new AdminUser( );
96          // set all rights to have all dashboards
97          user.setRights( RightHome.getRightsList( ).stream( ).collect( Collectors.toMap( Right::getId, Function.identity( ) ) ) );
98          Utils.registerAdminUser( request, user );
99          _instance.init( request, DashboardJspBean.RIGHT_MANAGE_DASHBOARD );
100 
101     }
102 
103     public void testdoMoveDashboard( ) throws AccessDeniedException
104     {
105         IDashboardComponent stored = DashboardHome.findByPrimaryKey( _dashboard.getName( ) );
106         assertNotNull( stored );
107         assertEquals( 0, stored.getOrder( ) );
108         assertEquals( _nZone, stored.getZone( ) );
109 
110         MockHttpServletRequest request = new MockHttpServletRequest( );
111         request.setParameter( "dashboard_name", _dashboard.getName( ) );
112         request.setParameter( "dashboard_order", "-1" );
113         request.setParameter( "dashboard_column", "-1" );
114         request.setParameter( SecurityTokenService.PARAMETER_TOKEN,
115                 SecurityTokenService.getInstance( ).getToken( request, "/admin/dashboard/manage_dashboards.html" ) );
116 
117         _instance.doMoveDashboard( request );
118 
119         stored = DashboardHome.findByPrimaryKey( _dashboard.getName( ) );
120         assertNotNull( stored );
121         assertEquals( 1, stored.getOrder( ) );
122         assertEquals( -1, stored.getZone( ) );
123     }
124 
125     public void testdoMoveDashboardInvalidToken( ) throws AccessDeniedException
126     {
127         IDashboardComponent stored = DashboardHome.findByPrimaryKey( _dashboard.getName( ) );
128         assertNotNull( stored );
129         assertEquals( 0, stored.getOrder( ) );
130         assertEquals( _nZone, stored.getZone( ) );
131 
132         MockHttpServletRequest request = new MockHttpServletRequest( );
133         request.setParameter( "dashboard_name", _dashboard.getName( ) );
134         request.setParameter( "dashboard_order", "-1" );
135         request.setParameter( "dashboard_column", "-1" );
136         request.setParameter( SecurityTokenService.PARAMETER_TOKEN,
137                 SecurityTokenService.getInstance( ).getToken( request, "/admin/dashboard/manage_dashboards.html" ) + "b" );
138 
139         try
140         {
141             _instance.doMoveDashboard( request );
142             fail( "Should have thrown" );
143         }
144         catch( AccessDeniedException e )
145         {
146             stored = DashboardHome.findByPrimaryKey( _dashboard.getName( ) );
147             assertNotNull( stored );
148             assertEquals( 0, stored.getOrder( ) );
149             assertEquals( _nZone, stored.getZone( ) );
150         }
151     }
152 
153     public void testdoMoveDashboardNoToken( ) throws AccessDeniedException
154     {
155         IDashboardComponent stored = DashboardHome.findByPrimaryKey( _dashboard.getName( ) );
156         assertNotNull( stored );
157         assertEquals( 0, stored.getOrder( ) );
158         assertEquals( _nZone, stored.getZone( ) );
159 
160         MockHttpServletRequest request = new MockHttpServletRequest( );
161         request.setParameter( "dashboard_name", _dashboard.getName( ) );
162         request.setParameter( "dashboard_order", "-1" );
163         request.setParameter( "dashboard_column", "-1" );
164 
165         try
166         {
167             _instance.doMoveDashboard( request );
168             fail( "Should have thrown" );
169         }
170         catch( AccessDeniedException e )
171         {
172             stored = DashboardHome.findByPrimaryKey( _dashboard.getName( ) );
173             assertNotNull( stored );
174             assertEquals( 0, stored.getOrder( ) );
175             assertEquals( _nZone, stored.getZone( ) );
176         }
177     }
178 
179     public void testDoReorderColumn( ) throws AccessDeniedException
180     {
181         IDashboardComponent stored = DashboardHome.findByPrimaryKey( _dashboard.getName( ) );
182         assertNotNull( stored );
183         assertEquals( 0, stored.getOrder( ) );
184         assertEquals( _nZone, stored.getZone( ) );
185         int nZone = DashboardHome.findColumns( ).stream( ).max( Integer::compare ).orElse( 1 ) + 1;
186         stored.setZone( nZone );
187         stored.setOrder( -1 );
188         DashboardHome.update( stored );
189         stored = DashboardHome.findByPrimaryKey( _dashboard.getName( ) );
190         assertEquals( -1, stored.getOrder( ) );
191         assertEquals( nZone, stored.getZone( ) );
192 
193         MockHttpServletRequest request = new MockHttpServletRequest( );
194         request.setParameter( "column", Integer.toString( nZone ) );
195         request.setParameter( SecurityTokenService.PARAMETER_TOKEN,
196                 SecurityTokenService.getInstance( ).getToken( request, "/admin/dashboard/manage_dashboards.html" ) );
197 
198         _instance.doReorderColumn( request );
199 
200         stored = DashboardHome.findByPrimaryKey( _dashboard.getName( ) );
201         assertEquals( 1, stored.getOrder( ) );
202         assertEquals( nZone, stored.getZone( ) );
203     }
204 
205     public void testDoReorderColumnInvalidToken( ) throws AccessDeniedException
206     {
207         IDashboardComponent stored = DashboardHome.findByPrimaryKey( _dashboard.getName( ) );
208         assertNotNull( stored );
209         assertEquals( 0, stored.getOrder( ) );
210         assertEquals( _nZone, stored.getZone( ) );
211         int nZone = DashboardHome.findColumns( ).stream( ).max( Integer::compare ).orElse( 0 ) + 1;
212         stored.setZone( nZone );
213         stored.setOrder( -1 );
214         DashboardHome.update( stored );
215         stored = DashboardHome.findByPrimaryKey( _dashboard.getName( ) );
216         assertEquals( -1, stored.getOrder( ) );
217         assertEquals( nZone, stored.getZone( ) );
218 
219         MockHttpServletRequest request = new MockHttpServletRequest( );
220         request.setParameter( "column", Integer.toString( nZone ) );
221         request.setParameter( SecurityTokenService.PARAMETER_TOKEN,
222                 SecurityTokenService.getInstance( ).getToken( request, "/admin/dashboard/manage_dashboards.html" ) + "b" );
223 
224         try
225         {
226             _instance.doReorderColumn( request );
227             fail( "Should have thrown" );
228         }
229         catch( AccessDeniedException e )
230         {
231             stored = DashboardHome.findByPrimaryKey( _dashboard.getName( ) );
232             assertEquals( -1, stored.getOrder( ) );
233             assertEquals( nZone, stored.getZone( ) );
234         }
235     }
236 
237     public void testDoReorderColumnNoToken( ) throws AccessDeniedException
238     {
239         IDashboardComponent stored = DashboardHome.findByPrimaryKey( _dashboard.getName( ) );
240         assertNotNull( stored );
241         assertEquals( 0, stored.getOrder( ) );
242         assertEquals( _nZone, stored.getZone( ) );
243         int nZone = DashboardHome.findColumns( ).stream( ).max( Integer::compare ).orElse( 0 ) + 1;
244         stored.setZone( nZone );
245         stored.setOrder( -1 );
246         DashboardHome.update( stored );
247         stored = DashboardHome.findByPrimaryKey( _dashboard.getName( ) );
248         assertEquals( -1, stored.getOrder( ) );
249         assertEquals( nZone, stored.getZone( ) );
250 
251         MockHttpServletRequest request = new MockHttpServletRequest( );
252         request.setParameter( "column", Integer.toString( nZone ) );
253 
254         try
255         {
256             _instance.doReorderColumn( request );
257             fail( "Should have thrown" );
258         }
259         catch( AccessDeniedException e )
260         {
261             stored = DashboardHome.findByPrimaryKey( _dashboard.getName( ) );
262             assertEquals( -1, stored.getOrder( ) );
263             assertEquals( nZone, stored.getZone( ) );
264         }
265     }
266 }