1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.portal.web.system;
35
36 import java.io.File;
37 import java.io.FileOutputStream;
38 import java.io.InputStream;
39 import java.io.OutputStream;
40
41 import org.apache.commons.io.IOUtils;
42 import org.springframework.mock.web.MockHttpServletRequest;
43
44 import fr.paris.lutece.portal.business.user.AdminUser;
45 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
46 import fr.paris.lutece.portal.service.message.AdminMessage;
47 import fr.paris.lutece.portal.service.message.AdminMessageService;
48 import fr.paris.lutece.portal.service.plugin.PluginService;
49 import fr.paris.lutece.portal.service.security.SecurityTokenService;
50 import fr.paris.lutece.portal.service.util.AppPathService;
51 import fr.paris.lutece.test.LuteceTestCase;
52 import fr.paris.lutece.test.Utils;
53
54
55
56
57
58 public class PluginJspBeanTest extends LuteceTestCase
59 {
60 private static final String PLUGIN_NAME = "junitplugin";
61 private static final String PARAM_PLUGIN_TYPE = "plugin_type";
62 private static final String PARAM_PLUGIN_TYPE_ALL = "all";
63 private static final String PARAM_DB_POOL_NAME = "db_pool_name";
64 private static final String PATH_PLUGIN = "path.plugins";
65 private PluginJspBean instance;
66
67 @Override
68 protected void setUp( ) throws Exception
69 {
70 super.setUp( );
71 instance = new PluginJspBean( );
72 try ( InputStream in = this.getClass( ).getResourceAsStream( "junit_plugin.xml" ) )
73 {
74 try ( OutputStream out = new FileOutputStream( new File( AppPathService.getPath( PATH_PLUGIN ), "junit_plugin.xml" ) ) )
75 {
76 IOUtils.copy( in, out );
77 }
78 }
79 PluginService.init( );
80 }
81
82 @Override
83 protected void tearDown( ) throws Exception
84 {
85 PluginService.getPlugin( PLUGIN_NAME ).uninstall( );
86 new File( AppPathService.getPath( PATH_PLUGIN ), "junit_plugin.xml" ).delete( );
87 PluginService.init( );
88 super.tearDown( );
89 }
90
91
92
93
94 public void testGetManagePlugins( ) throws AccessDeniedException
95 {
96 MockHttpServletRequest request = new MockHttpServletRequest( );
97 Utils.registerAdminUserWithRigth( request, new AdminUser( ), PluginJspBean.RIGHT_MANAGE_PLUGINS );
98 request.addParameter( PARAM_PLUGIN_TYPE, PARAM_PLUGIN_TYPE_ALL );
99
100 instance.init( request, PluginJspBean.RIGHT_MANAGE_PLUGINS );
101 assertNotNull( instance.getManagePlugins( request ) );
102 }
103
104 public void testDoInstallPlugin( ) throws AccessDeniedException
105 {
106 assertFalse( PluginService.isPluginEnable( PLUGIN_NAME ) );
107 MockHttpServletRequest request = new MockHttpServletRequest( );
108 request.addParameter( "plugin_name", PLUGIN_NAME );
109 request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
110 SecurityTokenService.getInstance( ).getToken( request, "admin/system/manage_plugins.html" ) );
111 instance.doInstallPlugin( request, request.getServletContext( ) );
112 assertTrue( PluginService.isPluginEnable( PLUGIN_NAME ) );
113 }
114
115 public void testDoInstallPluginInvalidToken( ) throws AccessDeniedException
116 {
117 assertFalse( PluginService.isPluginEnable( PLUGIN_NAME ) );
118 MockHttpServletRequest request = new MockHttpServletRequest( );
119 request.addParameter( "plugin_name", PLUGIN_NAME );
120 request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
121 SecurityTokenService.getInstance( ).getToken( request, "admin/system/manage_plugins.html" ) + "b" );
122 try
123 {
124 instance.doInstallPlugin( request, request.getServletContext( ) );
125 fail( "Should have thrown" );
126 }
127 catch( AccessDeniedException e )
128 {
129 assertFalse( PluginService.isPluginEnable( PLUGIN_NAME ) );
130 }
131 }
132
133 public void testDoInstallPluginNoToken( ) throws AccessDeniedException
134 {
135 assertFalse( PluginService.isPluginEnable( PLUGIN_NAME ) );
136 MockHttpServletRequest request = new MockHttpServletRequest( );
137 request.addParameter( "plugin_name", PLUGIN_NAME );
138 try
139 {
140 instance.doInstallPlugin( request, request.getServletContext( ) );
141 fail( "Should have thrown" );
142 }
143 catch( AccessDeniedException e )
144 {
145 assertFalse( PluginService.isPluginEnable( PLUGIN_NAME ) );
146 }
147 }
148
149 public void testDoModifyPluginPool( ) throws AccessDeniedException
150 {
151 assertNull( PluginService.getPlugin( PLUGIN_NAME ).getDbPoolName( ) );
152 MockHttpServletRequest request = new MockHttpServletRequest( );
153 request.addParameter( "plugin_name", PLUGIN_NAME );
154 request.addParameter( PARAM_DB_POOL_NAME, "junit" );
155 request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
156 SecurityTokenService.getInstance( ).getToken( request, "admin/system/manage_plugins.html" ) );
157 instance.doModifyPluginPool( request );
158 assertEquals( "junit", PluginService.getPlugin( PLUGIN_NAME ).getDbPoolName( ) );
159 }
160
161 public void testDoModifyPluginPoolInvalidToken( ) throws AccessDeniedException
162 {
163 assertNull( PluginService.getPlugin( PLUGIN_NAME ).getDbPoolName( ) );
164 MockHttpServletRequest request = new MockHttpServletRequest( );
165 request.addParameter( "plugin_name", PLUGIN_NAME );
166 request.addParameter( PARAM_DB_POOL_NAME, "junit" );
167 request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
168 SecurityTokenService.getInstance( ).getToken( request, "admin/system/manage_plugins.html" ) + "b" );
169 try
170 {
171 instance.doModifyPluginPool( request );
172 fail( "Should have thrown" );
173 }
174 catch( AccessDeniedException e )
175 {
176 assertNull( PluginService.getPlugin( PLUGIN_NAME ).getDbPoolName( ) );
177 }
178 }
179
180 public void testDoModifyPluginPoolNoToken( ) throws AccessDeniedException
181 {
182 assertNull( PluginService.getPlugin( PLUGIN_NAME ).getDbPoolName( ) );
183 MockHttpServletRequest request = new MockHttpServletRequest( );
184 request.addParameter( "plugin_name", PLUGIN_NAME );
185 request.addParameter( PARAM_DB_POOL_NAME, "junit" );
186
187 try
188 {
189 instance.doModifyPluginPool( request );
190 fail( "Should have thrown" );
191 }
192 catch( AccessDeniedException e )
193 {
194 assertNull( PluginService.getPlugin( PLUGIN_NAME ).getDbPoolName( ) );
195 }
196 }
197
198 public void testGetConfirmUninstallPlugin( )
199 {
200 MockHttpServletRequest request = new MockHttpServletRequest( );
201 request.addParameter( "plugin_name", PLUGIN_NAME );
202 instance.getConfirmUninstallPlugin( request );
203 AdminMessage message = AdminMessageService.getMessage( request );
204 assertNotNull( message );
205 assertTrue( message.getRequestParameters( ).containsKey( SecurityTokenService.PARAMETER_TOKEN ) );
206 }
207
208 public void testDoUninstallPlugin( ) throws AccessDeniedException
209 {
210 PluginService.getPlugin( PLUGIN_NAME ).install( );
211 assertTrue( PluginService.isPluginEnable( PLUGIN_NAME ) );
212 MockHttpServletRequest request = new MockHttpServletRequest( );
213 request.addParameter( "plugin_name", PLUGIN_NAME );
214 request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
215 SecurityTokenService.getInstance( ).getToken( request, "jsp/admin/system/DoUninstallPlugin.jsp" ) );
216 instance.doUninstallPlugin( request, request.getServletContext( ) );
217 assertFalse( PluginService.isPluginEnable( PLUGIN_NAME ) );
218 }
219
220 public void testDoUninstallPluginInvalidToken( ) throws AccessDeniedException
221 {
222 PluginService.getPlugin( PLUGIN_NAME ).install( );
223 assertTrue( PluginService.isPluginEnable( PLUGIN_NAME ) );
224 MockHttpServletRequest request = new MockHttpServletRequest( );
225 request.addParameter( "plugin_name", PLUGIN_NAME );
226 request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
227 SecurityTokenService.getInstance( ).getToken( request, "jsp/admin/system/DoUninstallPlugin.jsp" ) + "b" );
228 try
229 {
230 instance.doUninstallPlugin( request, request.getServletContext( ) );
231 fail( "Should have thrown" );
232 }
233 catch( AccessDeniedException e )
234 {
235 assertTrue( PluginService.isPluginEnable( PLUGIN_NAME ) );
236 }
237 }
238
239 public void testDoUninstallPluginNoToken( ) throws AccessDeniedException
240 {
241 PluginService.getPlugin( PLUGIN_NAME ).install( );
242 assertTrue( PluginService.isPluginEnable( PLUGIN_NAME ) );
243 MockHttpServletRequest request = new MockHttpServletRequest( );
244 request.addParameter( "plugin_name", PLUGIN_NAME );
245
246 try
247 {
248 instance.doUninstallPlugin( request, request.getServletContext( ) );
249 fail( "Should have thrown" );
250 }
251 catch( AccessDeniedException e )
252 {
253 assertTrue( PluginService.isPluginEnable( PLUGIN_NAME ) );
254 }
255 }
256 }