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.admin;
35  
36  import java.util.HashMap;
37  import java.util.Locale;
38  import java.util.Map;
39  
40  import org.apache.commons.lang3.StringUtils;
41  import org.springframework.mock.web.MockHttpServletRequest;
42  import org.springframework.mock.web.MockHttpServletResponse;
43  import org.springframework.mock.web.MockServletConfig;
44  
45  import fr.paris.lutece.portal.business.right.Right;
46  import fr.paris.lutece.portal.business.user.AdminUser;
47  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
48  import fr.paris.lutece.portal.service.message.SiteMessageException;
49  import fr.paris.lutece.portal.service.security.UserNotSignedException;
50  import fr.paris.lutece.portal.web.LocalVariables;
51  import fr.paris.lutece.test.LuteceTestCase;
52  import fr.paris.lutece.test.Utils;
53  
54  /**
55   * AdminJspBeanTest Test Class
56   *
57   */
58  public class AdminJspBeanTest extends LuteceTestCase
59  {
60      private static final String PARAMETER_PAGE_ID = "page_id"; // home page
61      private static final String TEST_PAGE_ID = "1"; // home page
62      private static final String ATTRIBUTE_ADMIN_USER = "lutece_admin_user";
63  
64      /**
65       * Test of getAdminPage method, of class fr.paris.lutece.portal.web.admin.AdminJspBean.
66       */
67      public void testGetAdminPage( ) throws AccessDeniedException
68      {
69          MockHttpServletRequest request = new MockHttpServletRequest( );
70          request.addParameter( PARAMETER_PAGE_ID, TEST_PAGE_ID );
71          Utils.registerAdminUserWithRigth( request, new AdminUser( ), AdminPageJspBean.RIGHT_MANAGE_ADMIN_SITE );
72  
73          AdminPageJspBean instance = new AdminPageJspBean( );
74          instance.init( request, AdminPageJspBean.RIGHT_MANAGE_ADMIN_SITE );
75  
76          assertTrue( StringUtils.isNotEmpty( instance.getAdminPage( request ) ) );
77      }
78  
79      /**
80       * Test of getAdminPagePreview method, of class fr.paris.lutece.portal.web.admin.AdminJspBean.
81       * 
82       * @throws UserNotSignedException
83       */
84      public void testGetAdminPagePreview( ) throws AccessDeniedException, UserNotSignedException
85      {
86          MockHttpServletRequest request = new MockHttpServletRequest( );
87  
88          // FIXME : MokeHttpServletRequest should be fixed to support attributes
89          Map<String, Right> mapRights = new HashMap<>( );
90          Right right = new Right( );
91          right.setId( AdminPageJspBean.RIGHT_MANAGE_ADMIN_SITE );
92          mapRights.put( AdminPageJspBean.RIGHT_MANAGE_ADMIN_SITE, right );
93          AdminUser user = new AdminUser( );
94          user.setRights( mapRights );
95          user.setLocale( new Locale( "fr", "FR", "" ) );
96          request.getSession( true ).setAttribute( ATTRIBUTE_ADMIN_USER, user );
97          LocalVariables.setLocal( new MockServletConfig( ), request, new MockHttpServletResponse( ) );
98  
99          AdminPageJspBean instance = new AdminPageJspBean( );
100         instance.init( request, AdminPageJspBean.RIGHT_MANAGE_ADMIN_SITE );
101 
102         try
103         {
104             instance.getAdminPagePreview( request );
105         }
106         catch( SiteMessageException e )
107         {
108             fail( );
109         }
110     }
111 
112     @Override
113     public void tearDown( ) throws Exception
114     {
115         LocalVariables.setLocal( null, null, null );
116         super.tearDown( );
117     }
118 }