View Javadoc
1   /*
2    * Copyright (c) 2002-2024, 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.plugins.identitystore.modules.quality.web;
35  
36  import fr.paris.lutece.plugins.identitystore.business.duplicates.suspicions.SuspiciousIdentityHome;
37  import org.springframework.mock.web.MockHttpServletRequest;
38  import org.springframework.mock.web.MockHttpServletResponse;
39  import org.springframework.mock.web.MockServletConfig;
40  import fr.paris.lutece.portal.business.user.AdminUser;
41  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
42  import fr.paris.lutece.portal.service.admin.AdminAuthenticationService;
43  import fr.paris.lutece.portal.service.security.UserNotSignedException;
44  import java.util.List;
45  import java.io.IOException;
46  import fr.paris.lutece.test.LuteceTestCase;
47  import fr.paris.lutece.portal.service.security.SecurityTokenService;
48  
49  /**
50   * This is the business class test for the object SuspiciousIdentity
51   */
52  public class ManageSuspiciousIdentitysTest extends LuteceTestCase
53  {
54      private static final String CUSTOMERID1 = "CustomerId1";
55      private static final String CUSTOMERID2 = "CustomerId2";
56  
57      public void testJspBeans( )
58      {
59          MockHttpServletRequest request = new MockHttpServletRequest( );
60          MockServletConfig config = new MockServletConfig( );
61  
62          // display admin SuspiciousIdentity management JSP
63          ManageSuspiciousIdentitys jspbean = new ManageSuspiciousIdentitys( );
64          String html = jspbean.getManageSuspiciousIdentitys( request );
65          assertNotNull( html );
66  
67          // display admin SuspiciousIdentity creation JSP
68          html = jspbean.getCreateSuspiciousIdentity( request );
69          assertNotNull( html );
70  
71          // action create SuspiciousIdentity
72          request = new MockHttpServletRequest( );
73  
74          MockHttpServletResponse response = new MockHttpServletResponse( );
75          AdminUser adminUser = new AdminUser( );
76          adminUser.setAccessCode( "admin" );
77  
78          request.addParameter( "customer_id", CUSTOMERID1 );
79          request.addParameter( "action", "createSuspiciousIdentity" );
80          request.addParameter( "token", SecurityTokenService.getInstance( ).getToken( request, "createSuspiciousIdentity" ) );
81          request.setMethod( "POST" );
82  
83          try
84          {
85              AdminAuthenticationService.getInstance( ).registerUser( request, adminUser );
86              html = jspbean.processController( request, response );
87  
88              // MockResponse object does not redirect, result is always null
89              assertNull( html );
90          }
91          catch( AccessDeniedException e )
92          {
93              fail( "access denied" );
94          }
95          catch( UserNotSignedException e )
96          {
97              fail( "user not signed in" );
98          }
99  
100         // display modify SuspiciousIdentity JSP
101         request = new MockHttpServletRequest( );
102         request.addParameter( "customer_id", CUSTOMERID1 );
103         List<Integer> listIds = SuspiciousIdentityHome.getIdSuspiciousIdentitysList( );
104         assertFalse( listIds.isEmpty( ) );
105         request.addParameter( "id", String.valueOf( listIds.get( 0 ) ) );
106         jspbean = new ManageSuspiciousIdentitys( );
107 
108         assertNotNull( jspbean.getModifySuspiciousIdentity( request ) );
109 
110         // action modify SuspiciousIdentity
111         request = new MockHttpServletRequest( );
112         response = new MockHttpServletResponse( );
113 
114         adminUser = new AdminUser( );
115         adminUser.setAccessCode( "admin" );
116 
117         request.addParameter( "customer_id", CUSTOMERID2 );
118         request.setRequestURI( "jsp/admin/plugins/example/ManageSuspiciousIdentitys.jsp" );
119         // important pour que MVCController sache quelle action effectuer, sinon, il redirigera vers createSuspiciousIdentity, qui est l'action par défaut
120         request.addParameter( "action", "modifySuspiciousIdentity" );
121         request.addParameter( "token", SecurityTokenService.getInstance( ).getToken( request, "modifySuspiciousIdentity" ) );
122 
123         try
124         {
125             AdminAuthenticationService.getInstance( ).registerUser( request, adminUser );
126             html = jspbean.processController( request, response );
127 
128             // MockResponse object does not redirect, result is always null
129             assertNull( html );
130         }
131         catch( AccessDeniedException e )
132         {
133             fail( "access denied" );
134         }
135         catch( UserNotSignedException e )
136         {
137             fail( "user not signed in" );
138         }
139 
140         // get remove SuspiciousIdentity
141         request = new MockHttpServletRequest( );
142         // request.setRequestURI("jsp/admin/plugins/example/ManageSuspiciousIdentitys.jsp");
143         request.addParameter( "id", String.valueOf( listIds.get( 0 ) ) );
144         jspbean = new ManageSuspiciousIdentitys( );
145         request.addParameter( "action", "confirmRemoveSuspiciousIdentity" );
146         assertNotNull( jspbean.getModifySuspiciousIdentity( request ) );
147 
148         // do remove SuspiciousIdentity
149         request = new MockHttpServletRequest( );
150         response = new MockHttpServletResponse( );
151         request.setRequestURI( "jsp/admin/plugins/example/ManageSuspiciousIdentitys.jsp" );
152         // important pour que MVCController sache quelle action effectuer, sinon, il redirigera vers createSuspiciousIdentity, qui est l'action par défaut
153         request.addParameter( "action", "removeSuspiciousIdentity" );
154         request.addParameter( "token", SecurityTokenService.getInstance( ).getToken( request, "removeSuspiciousIdentity" ) );
155         request.addParameter( "id", String.valueOf( listIds.get( 0 ) ) );
156         request.setMethod( "POST" );
157         adminUser = new AdminUser( );
158         adminUser.setAccessCode( "admin" );
159 
160         try
161         {
162             AdminAuthenticationService.getInstance( ).registerUser( request, adminUser );
163             html = jspbean.processController( request, response );
164 
165             // MockResponse object does not redirect, result is always null
166             assertNull( html );
167         }
168         catch( AccessDeniedException e )
169         {
170             fail( "access denied" );
171         }
172         catch( UserNotSignedException e )
173         {
174             fail( "user not signed in" );
175         }
176 
177     }
178 }