CsrfProtectionSystemListener implements IPentahoSystemListener { @Override public boolean startup( IPentahoSession session ) { if ( PentahoSystem.isCsrfProtectionEnabled() ) { List csrProtectionNodes = PentahoSystem.getSystemSettings().getSystemSettings( "csrf-protection" ); if ( csrProtectionNodes.size() > 0 ) { Element csrfProtectionElem = (Element) csrProtectionNodes.get( 0 ); try { CsrfProtectionDefinition csrfProtectionDefinition = WebUtil.parseXmlCsrfProtectionDefinition( csrfProtectionElem ); PentahoSystem.registerReference( new SingletonPentahoObjectReference.Builder<CsrfProtectionDefinition>( CsrfProtectionDefinition.class ) .object( csrfProtectionDefinition ) .build(), CsrfProtectionDefinition.class ); } catch ( IllegalArgumentException parseError ) { Logger.warn( CsrfProtectionSystemListener.class.getName(), Messages.getInstance().getString( "CsrfProtectionSystemListener.WARN_CSRF_SYSTEM_NOT_REGISTERED", parseError.getMessage() ) ); } } } return true; }  @Override boolean startup( IPentahoSession session ); @Override void shutdown();  }
@Test public void testStartupCsrfProtectionDisabled() throws Exception { PowerMockito.mockStatic( PentahoSystem.class ); when( PentahoSystem.registerReference( any(), eq( CsrfProtectionDefinition.class ) ) ).thenReturn( null ); when( PentahoSystem.isCsrfProtectionEnabled() ).thenReturn( false ); boolean result = csrfProtectionSystemListener.startup( session ); assertTrue( result ); PowerMockito.verifyStatic( times( 0 ) ); PentahoSystem.registerReference( any() ); }
@Test public void testStartupCsrfProtectionEnabledButNoCsrfProtectionElement() throws Exception { PowerMockito.mockStatic( PentahoSystem.class ); when( PentahoSystem.registerReference( any(), eq( CsrfProtectionDefinition.class ) ) ).thenReturn( null ); when( PentahoSystem.isCsrfProtectionEnabled() ).thenReturn( false ); when( PentahoSystem.getSystemSettings() ).thenReturn( systemSettings ); when( systemSettings.getSystemSettings( eq( "csrf-protection" ) ) ).thenReturn( new ArrayList() ); boolean result = csrfProtectionSystemListener.startup( session ); assertTrue( result ); PowerMockito.verifyStatic( times( 0 ) ); PentahoSystem.registerReference( any(), eq( CsrfProtectionDefinition.class ) ); }
@Test public void testStartupCsrfProtectionEnabledAndOneCsrfProtectionElement() throws Exception { PowerMockito.mockStatic( PentahoSystem.class ); PowerMockito.mockStatic( WebUtil.class ); when( PentahoSystem.registerReference( any(), eq( CsrfProtectionDefinition.class ) ) ).thenReturn( null ); when( PentahoSystem.isCsrfProtectionEnabled() ).thenReturn( true ); when( PentahoSystem.getSystemSettings() ).thenReturn( systemSettings ); Element protectionElem = Mockito.mock( Element.class ); List csrfProtectionElems = new ArrayList(); csrfProtectionElems.add( protectionElem ); when( systemSettings.getSystemSettings( eq( "csrf-protection" ) ) ).thenReturn( csrfProtectionElems ); CsrfProtectionDefinition csrfProtectionDefinition = Mockito.mock( CsrfProtectionDefinition.class ); when( WebUtil.parseXmlCsrfProtectionDefinition( eq( protectionElem ) ) ).thenReturn( csrfProtectionDefinition ); boolean result = csrfProtectionSystemListener.startup( session ); assertTrue( result ); PowerMockito.verifyStatic( times( 1 ) ); WebUtil.parseXmlCsrfProtectionDefinition( eq( protectionElem ) ); PowerMockito.verifyStatic( times( 1 ) ); PentahoSystem.registerReference( any(), eq( CsrfProtectionDefinition.class ) ); }
@Test public void testStartupCsrfProtectionEnabledAndOneInvalidCsrfProtectionElement() throws Exception { PowerMockito.mockStatic( PentahoSystem.class ); PowerMockito.mockStatic( WebUtil.class ); when( PentahoSystem.registerReference( any(), eq( CsrfProtectionDefinition.class ) ) ).thenReturn( null ); when( PentahoSystem.isCsrfProtectionEnabled() ).thenReturn( true ); when( PentahoSystem.getSystemSettings() ).thenReturn( systemSettings ); Element protectionElem = Mockito.mock( Element.class ); List csrfProtectionElems = new ArrayList(); csrfProtectionElems.add( protectionElem ); when( systemSettings.getSystemSettings( eq( "csrf-protection" ) ) ).thenReturn( csrfProtectionElems ); when( WebUtil.parseXmlCsrfProtectionDefinition( eq( protectionElem ) ) ) .thenThrow( new IllegalArgumentException( "TEST" ) ); boolean result = csrfProtectionSystemListener.startup( session ); assertTrue( result ); PowerMockito.verifyStatic( times( 1 ) ); WebUtil.parseXmlCsrfProtectionDefinition( eq( protectionElem ) ); PowerMockito.verifyStatic( times( 0 ) ); PentahoSystem.registerReference( any(), eq( CsrfProtectionDefinition.class ) ); }