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.features;
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.right.FeatureGroup;
43 import fr.paris.lutece.portal.business.right.FeatureGroupHome;
44 import fr.paris.lutece.portal.business.right.IRightDAO;
45 import fr.paris.lutece.portal.business.right.Right;
46 import fr.paris.lutece.portal.business.right.RightHome;
47 import fr.paris.lutece.portal.business.user.AdminUser;
48 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
49 import fr.paris.lutece.portal.service.message.AdminMessage;
50 import fr.paris.lutece.portal.service.message.AdminMessageService;
51 import fr.paris.lutece.portal.service.security.SecurityTokenService;
52 import fr.paris.lutece.portal.service.spring.SpringContextService;
53 import fr.paris.lutece.portal.web.dashboard.AdminDashboardJspBean;
54 import fr.paris.lutece.test.LuteceTestCase;
55 import fr.paris.lutece.test.Utils;
56
57
58
59
60
61 public class FeaturesGroupJspBeanTest extends LuteceTestCase
62 {
63 private static final String PARAMETER_GROUP_ID = "group_id";
64 private static final String TEST_GROUP_ID = "CONTENT";
65 private FeaturesGroupJspBean instance;
66 private FeatureGroup featureGroup;
67 private Right right;
68
69 @Override
70 protected void setUp( ) throws Exception
71 {
72 super.setUp( );
73 instance = new FeaturesGroupJspBean( );
74 String strGroupName = getRandomName( );
75 featureGroup = new FeatureGroup( );
76 featureGroup.setId( strGroupName );
77 featureGroup.setLabelKey( strGroupName );
78 featureGroup.setDescriptionKey( strGroupName );
79
80 FeatureGroupHome.create( featureGroup );
81
82 right = new Right( );
83 String strRight = getRandomName( );
84 right.setDescriptionKey( strRight );
85 right.setId( strRight );
86 RightHome.create( right );
87 }
88
89 @Override
90 protected void tearDown( ) throws Exception
91 {
92 RightHome.remove( right.getId( ) );
93 FeatureGroupHome.remove( featureGroup.getId( ) );
94 super.tearDown( );
95 }
96
97
98
99
100
101
102 public void testDoDispatchFeature( ) throws AccessDeniedException
103 {
104 Right stored = RightHome.findByPrimaryKey( right.getId( ) );
105 assertNotNull( stored );
106 assertNull( stored.getFeatureGroup( ) );
107
108 MockHttpServletRequest request = new MockHttpServletRequest( );
109 request.addParameter( "right_id", right.getId( ) );
110 request.addParameter( "group_name", featureGroup.getId( ) );
111 request.addParameter( "order_id", Integer.toString( stored.getOrder( ) + 1 ) );
112 request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
113 SecurityTokenService.getInstance( ).getToken( request, AdminDashboardJspBean.TEMPLATE_MANAGE_DASHBOARDS ) );
114
115 instance.doDispatchFeature( request );
116 stored = RightHome.findByPrimaryKey( right.getId( ) );
117 assertNotNull( stored );
118 assertEquals( featureGroup.getId( ), stored.getFeatureGroup( ) );
119 assertEquals( stored.getOrder( ) + 1, right.getOrder( ) );
120 }
121
122 public void testDoDispatchFeatureInvalidToken( ) throws AccessDeniedException
123 {
124 Right stored = RightHome.findByPrimaryKey( right.getId( ) );
125 assertNotNull( stored );
126 assertNull( stored.getFeatureGroup( ) );
127
128 MockHttpServletRequest request = new MockHttpServletRequest( );
129 request.addParameter( "right_id", right.getId( ) );
130 request.addParameter( "group_name", featureGroup.getId( ) );
131 request.addParameter( "order_id", Integer.toString( stored.getOrder( ) + 1 ) );
132 request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
133 SecurityTokenService.getInstance( ).getToken( request, "admin/features/dispatch_features.html" ) + "b" );
134
135 try
136 {
137 instance.doDispatchFeature( request );
138 fail( "Should have thrown" );
139 }
140 catch( AccessDeniedException e )
141 {
142 stored = RightHome.findByPrimaryKey( right.getId( ) );
143 assertNotNull( stored );
144 assertNull( stored.getFeatureGroup( ) );
145 assertEquals( right.getOrder( ), stored.getOrder( ) );
146 }
147 }
148
149 public void testDoDispatchFeatureNoToken( ) throws AccessDeniedException
150 {
151 Right stored = RightHome.findByPrimaryKey( right.getId( ) );
152 assertNotNull( stored );
153 assertNull( stored.getFeatureGroup( ) );
154
155 MockHttpServletRequest request = new MockHttpServletRequest( );
156 request.addParameter( "right_id", right.getId( ) );
157 request.addParameter( "group_name", featureGroup.getId( ) );
158 request.addParameter( "order_id", Integer.toString( stored.getOrder( ) + 1 ) );
159
160 try
161 {
162 instance.doDispatchFeature( request );
163 fail( "Should have thrown" );
164 }
165 catch( AccessDeniedException e )
166 {
167 stored = RightHome.findByPrimaryKey( right.getId( ) );
168 assertNotNull( stored );
169 assertNull( stored.getFeatureGroup( ) );
170 assertEquals( right.getOrder( ), stored.getOrder( ) );
171 }
172 }
173
174
175
176
177 public void testGetCreateGroup( ) throws AccessDeniedException
178 {
179 MockHttpServletRequest request = new MockHttpServletRequest( );
180 Utils.registerAdminUserWithRigth( request, new AdminUser( ), FeaturesGroupJspBean.RIGHT_FEATURES_MANAGEMENT );
181
182 instance.init( request, FeaturesGroupJspBean.RIGHT_FEATURES_MANAGEMENT );
183 assertNotNull( instance.getCreateGroup( request ) );
184 }
185
186
187
188
189 public void testGetModifyGroup( ) throws AccessDeniedException
190 {
191 MockHttpServletRequest request = new MockHttpServletRequest( );
192 request.addParameter( PARAMETER_GROUP_ID, TEST_GROUP_ID );
193 Utils.registerAdminUserWithRigth( request, new AdminUser( ), FeaturesGroupJspBean.RIGHT_FEATURES_MANAGEMENT );
194
195 instance.init( request, FeaturesGroupJspBean.RIGHT_FEATURES_MANAGEMENT );
196 assertNotNull( instance.getModifyGroup( request ) );
197 }
198
199
200
201
202
203
204 public void testDoCreateGroup( ) throws AccessDeniedException
205 {
206 String strGroupName = getRandomName( );
207 MockHttpServletRequest request = new MockHttpServletRequest( );
208 request.addParameter( "group_id", strGroupName );
209 request.addParameter( "group_name", strGroupName );
210 request.addParameter( "group_description", strGroupName );
211 request.addParameter( "group_order", "1" );
212 request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
213 SecurityTokenService.getInstance( ).getToken( request, AdminDashboardJspBean.TEMPLATE_MANAGE_DASHBOARDS ) );
214
215 try
216 {
217 instance.doCreateGroup( request );
218 FeatureGroup group = FeatureGroupHome.findByPrimaryKey( strGroupName );
219 assertNotNull( group );
220 assertEquals( strGroupName, group.getId( ) );
221 assertEquals( strGroupName, group.getLabelKey( ) );
222 assertEquals( strGroupName, group.getDescriptionKey( ) );
223 assertEquals( 1, group.getOrder( ) );
224 }
225 finally
226 {
227 FeatureGroupHome.remove( strGroupName );
228 }
229 }
230
231 public void testDoCreateGroupInvalidToken( ) throws AccessDeniedException
232 {
233 String strGroupName = getRandomName( );
234 MockHttpServletRequest request = new MockHttpServletRequest( );
235 request.addParameter( "group_id", strGroupName );
236 request.addParameter( "group_name", strGroupName );
237 request.addParameter( "group_description", strGroupName );
238 request.addParameter( "group_order", "1" );
239 request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
240 SecurityTokenService.getInstance( ).getToken( request, "admin/features/create_group.html" ) + "b" );
241
242 try
243 {
244 instance.doCreateGroup( request );
245 fail( "Should have thrown" );
246 }
247 catch( AccessDeniedException e )
248 {
249 FeatureGroup group = FeatureGroupHome.findByPrimaryKey( strGroupName );
250 assertNull( group );
251 }
252 finally
253 {
254 FeatureGroupHome.remove( strGroupName );
255 }
256 }
257
258 public void testDoCreateGroupNoToken( ) throws AccessDeniedException
259 {
260 String strGroupName = getRandomName( );
261 MockHttpServletRequest request = new MockHttpServletRequest( );
262 request.addParameter( "group_id", strGroupName );
263 request.addParameter( "group_name", strGroupName );
264 request.addParameter( "group_description", strGroupName );
265 request.addParameter( "group_order", "1" );
266
267 try
268 {
269 instance.doCreateGroup( request );
270 fail( "Should have thrown" );
271 }
272 catch( AccessDeniedException e )
273 {
274 FeatureGroup group = FeatureGroupHome.findByPrimaryKey( strGroupName );
275 assertNull( group );
276 }
277 finally
278 {
279 FeatureGroupHome.remove( strGroupName );
280 }
281 }
282
283 private String getRandomName( )
284 {
285 Random rand = new SecureRandom( );
286 BigInteger bigInt = new BigInteger( 128, rand );
287 return "junit" + bigInt.toString( 36 );
288 }
289
290
291
292
293
294
295 public void testDoModifyGroup( ) throws AccessDeniedException
296 {
297 String strGroupName = getRandomName( );
298 MockHttpServletRequest request = new MockHttpServletRequest( );
299 request.addParameter( "group_id", featureGroup.getId( ) );
300 request.addParameter( "group_name", strGroupName );
301 request.addParameter( "group_description", strGroupName );
302 request.addParameter( "group_order", Integer.toString( featureGroup.getOrder( ) + 1 ) );
303 request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
304 SecurityTokenService.getInstance( ).getToken( request, AdminDashboardJspBean.TEMPLATE_MANAGE_DASHBOARDS ) );
305
306 instance.doModifyGroup( request );
307 FeatureGroup group = FeatureGroupHome.findByPrimaryKey( featureGroup.getId( ) );
308 assertNotNull( group );
309 assertEquals( featureGroup.getId( ), group.getId( ) );
310 assertEquals( strGroupName, group.getLabelKey( ) );
311 assertEquals( strGroupName, group.getDescriptionKey( ) );
312 assertEquals( featureGroup.getOrder( ) + 1, group.getOrder( ) );
313 }
314
315 public void testDoModifyGroupInvalidToken( ) throws AccessDeniedException
316 {
317 String strGroupName = getRandomName( );
318 MockHttpServletRequest request = new MockHttpServletRequest( );
319 request.addParameter( "group_id", featureGroup.getId( ) );
320 request.addParameter( "group_name", strGroupName );
321 request.addParameter( "group_description", strGroupName );
322 request.addParameter( "group_order", Integer.toString( featureGroup.getOrder( ) + 1 ) );
323 request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
324 SecurityTokenService.getInstance( ).getToken( request, "admin/features/modify_group.html" ) + "b" );
325
326 try
327 {
328 instance.doModifyGroup( request );
329 fail( "Should have thrown" );
330 }
331 catch( AccessDeniedException e )
332 {
333 FeatureGroup group = FeatureGroupHome.findByPrimaryKey( featureGroup.getId( ) );
334 assertNotNull( group );
335 assertEquals( featureGroup.getId( ), group.getId( ) );
336 assertEquals( featureGroup.getLabelKey( ), group.getLabelKey( ) );
337 assertEquals( featureGroup.getDescriptionKey( ), group.getDescriptionKey( ) );
338 assertEquals( featureGroup.getOrder( ), group.getOrder( ) );
339 }
340 }
341
342 public void testDoModifyGroupNoToken( ) throws AccessDeniedException
343 {
344 String strGroupName = getRandomName( );
345 MockHttpServletRequest request = new MockHttpServletRequest( );
346 request.addParameter( "group_id", featureGroup.getId( ) );
347 request.addParameter( "group_name", strGroupName );
348 request.addParameter( "group_description", strGroupName );
349 request.addParameter( "group_order", Integer.toString( featureGroup.getOrder( ) + 1 ) );
350
351 try
352 {
353 instance.doModifyGroup( request );
354 fail( "Should have thrown" );
355 }
356 catch( AccessDeniedException e )
357 {
358 FeatureGroup group = FeatureGroupHome.findByPrimaryKey( featureGroup.getId( ) );
359 assertNotNull( group );
360 assertEquals( featureGroup.getId( ), group.getId( ) );
361 assertEquals( featureGroup.getLabelKey( ), group.getLabelKey( ) );
362 assertEquals( featureGroup.getDescriptionKey( ), group.getDescriptionKey( ) );
363 assertEquals( featureGroup.getOrder( ), group.getOrder( ) );
364 }
365 }
366
367
368
369
370 public void testGetRemoveGroup( ) throws AccessDeniedException
371 {
372 MockHttpServletRequest request = new MockHttpServletRequest( );
373 request.addParameter( PARAMETER_GROUP_ID, TEST_GROUP_ID );
374 Utils.registerAdminUserWithRigth( request, new AdminUser( ), FeaturesGroupJspBean.RIGHT_FEATURES_MANAGEMENT );
375
376 instance.init( request, FeaturesGroupJspBean.RIGHT_FEATURES_MANAGEMENT );
377 instance.getRemoveGroup( request );
378 AdminMessage message = AdminMessageService.getMessage( request );
379 assertNotNull( message );
380 assertTrue( message.getRequestParameters( ).containsKey( SecurityTokenService.PARAMETER_TOKEN ) );
381 }
382
383
384
385
386
387
388 public void testDoRemoveGroup( ) throws AccessDeniedException
389 {
390 assertNotNull( FeatureGroupHome.findByPrimaryKey( featureGroup.getId( ) ) );
391 MockHttpServletRequest request = new MockHttpServletRequest( );
392 request.addParameter( PARAMETER_GROUP_ID, featureGroup.getId( ) );
393 request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
394 SecurityTokenService.getInstance( ).getToken( request, AdminDashboardJspBean.TEMPLATE_MANAGE_DASHBOARDS ) );
395
396 instance.doRemoveGroup( request );
397 assertNull( FeatureGroupHome.findByPrimaryKey( featureGroup.getId( ) ) );
398 }
399
400 public void testDoRemoveGroupInvalidToken( ) throws AccessDeniedException
401 {
402 assertNotNull( FeatureGroupHome.findByPrimaryKey( featureGroup.getId( ) ) );
403 MockHttpServletRequest request = new MockHttpServletRequest( );
404 request.addParameter( PARAMETER_GROUP_ID, featureGroup.getId( ) );
405 request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
406 SecurityTokenService.getInstance( ).getToken( request, "jsp/admin/features/DoRemoveGroup.jsp" ) + "b" );
407
408 try
409 {
410 instance.doRemoveGroup( request );
411 fail( "Should have thrown" );
412 }
413 catch( AccessDeniedException e )
414 {
415 assertNotNull( FeatureGroupHome.findByPrimaryKey( featureGroup.getId( ) ) );
416 }
417 }
418
419 public void testDoRemoveGroupNoToken( ) throws AccessDeniedException
420 {
421 assertNotNull( FeatureGroupHome.findByPrimaryKey( featureGroup.getId( ) ) );
422 MockHttpServletRequest request = new MockHttpServletRequest( );
423 request.addParameter( PARAMETER_GROUP_ID, featureGroup.getId( ) );
424
425 try
426 {
427 instance.doRemoveGroup( request );
428 fail( "Should have thrown" );
429 }
430 catch( AccessDeniedException e )
431 {
432 assertNotNull( FeatureGroupHome.findByPrimaryKey( featureGroup.getId( ) ) );
433 }
434 }
435
436 public void testDoDispatchFeatureGroup( ) throws AccessDeniedException
437 {
438 MockHttpServletRequest request = new MockHttpServletRequest( );
439 request.addParameter( "group_id", featureGroup.getId( ) );
440 request.addParameter( "order_id", Integer.toString( featureGroup.getOrder( ) + 1 ) );
441 request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
442 SecurityTokenService.getInstance( ).getToken( request, AdminDashboardJspBean.TEMPLATE_MANAGE_DASHBOARDS ) );
443
444 instance.doDispatchFeatureGroup( request );
445 FeatureGroup stored = FeatureGroupHome.findByPrimaryKey( featureGroup.getId( ) );
446 assertNotNull( stored );
447 assertEquals( featureGroup.getOrder( ) + 1, stored.getOrder( ) );
448
449 }
450
451 public void testDoDispatchFeatureGroupInvalidToken( ) throws AccessDeniedException
452 {
453 MockHttpServletRequest request = new MockHttpServletRequest( );
454 request.addParameter( "group_id", featureGroup.getId( ) );
455 request.addParameter( "order_id", Integer.toString( featureGroup.getOrder( ) + 1 ) );
456 request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
457 SecurityTokenService.getInstance( ).getToken( request, "admin/features/manage_groups.html" ) + "b" );
458
459 try
460 {
461 instance.doDispatchFeatureGroup( request );
462 fail( "Should have thrown" );
463 }
464 catch( AccessDeniedException e )
465 {
466 FeatureGroup stored = FeatureGroupHome.findByPrimaryKey( featureGroup.getId( ) );
467 assertNotNull( stored );
468 assertEquals( featureGroup.getOrder( ), stored.getOrder( ) );
469 }
470 }
471
472 public void testDoDispatchFeatureGroupNoToken( ) throws AccessDeniedException
473 {
474 MockHttpServletRequest request = new MockHttpServletRequest( );
475 request.addParameter( "group_id", featureGroup.getId( ) );
476 request.addParameter( "order_id", Integer.toString( featureGroup.getOrder( ) + 1 ) );
477
478 try
479 {
480 instance.doDispatchFeatureGroup( request );
481 fail( "Should have thrown" );
482 }
483 catch( AccessDeniedException e )
484 {
485 FeatureGroup stored = FeatureGroupHome.findByPrimaryKey( featureGroup.getId( ) );
486 assertNotNull( stored );
487 assertEquals( featureGroup.getOrder( ), stored.getOrder( ) );
488 }
489 }
490
491 public void testDoReinitFeatures( ) throws AccessDeniedException
492 {
493 right.setFeatureGroup( featureGroup.getId( ) );
494 RightHome.update( right );
495 right.setOrder( 100 );
496 ( (IRightDAO) SpringContextService.getBean( "rightDAO" ) ).store( right );
497
498 Right stored = RightHome.findByPrimaryKey( right.getId( ) );
499 assertNotNull( stored );
500 assertEquals( 100, stored.getOrder( ) );
501
502 MockHttpServletRequest request = new MockHttpServletRequest( );
503 request.addParameter( "group_id", featureGroup.getId( ) );
504 request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
505 SecurityTokenService.getInstance( ).getToken( request, AdminDashboardJspBean.TEMPLATE_MANAGE_DASHBOARDS ) );
506
507 instance.doReinitFeatures( request );
508 stored = RightHome.findByPrimaryKey( right.getId( ) );
509 assertNotNull( stored );
510 assertEquals( 1, stored.getOrder( ) );
511 }
512
513 public void testDoReinitFeaturesInvalidToken( ) throws AccessDeniedException
514 {
515 right.setFeatureGroup( featureGroup.getId( ) );
516 RightHome.update( right );
517 right.setOrder( 100 );
518 ( (IRightDAO) SpringContextService.getBean( "rightDAO" ) ).store( right );
519
520 Right stored = RightHome.findByPrimaryKey( right.getId( ) );
521 assertNotNull( stored );
522 assertEquals( 100, stored.getOrder( ) );
523
524 MockHttpServletRequest request = new MockHttpServletRequest( );
525 request.addParameter( "group_id", featureGroup.getId( ) );
526 request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
527 SecurityTokenService.getInstance( ).getToken( request, "admin/features/dispatch_features.html" ) + "b" );
528
529 try
530 {
531 instance.doReinitFeatures( request );
532 fail( "Should have thrown" );
533 }
534 catch( AccessDeniedException e )
535 {
536 stored = RightHome.findByPrimaryKey( right.getId( ) );
537 assertNotNull( stored );
538 assertEquals( 100, stored.getOrder( ) );
539 }
540 }
541
542 public void testDoReinitFeaturesNoToken( ) throws AccessDeniedException
543 {
544 right.setFeatureGroup( featureGroup.getId( ) );
545 RightHome.update( right );
546 right.setOrder( 100 );
547 ( (IRightDAO) SpringContextService.getBean( "rightDAO" ) ).store( right );
548
549 Right stored = RightHome.findByPrimaryKey( right.getId( ) );
550 assertNotNull( stored );
551 assertEquals( 100, stored.getOrder( ) );
552
553 MockHttpServletRequest request = new MockHttpServletRequest( );
554 request.addParameter( "group_id", featureGroup.getId( ) );
555
556 try
557 {
558 instance.doReinitFeatures( request );
559 fail( "Should have thrown" );
560 }
561 catch( AccessDeniedException e )
562 {
563 stored = RightHome.findByPrimaryKey( right.getId( ) );
564 assertNotNull( stored );
565 assertEquals( 100, stored.getOrder( ) );
566 }
567 }
568 }