You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by as...@apache.org on 2017/05/09 10:16:44 UTC

svn commit: r1794522 - /sling/trunk/bundles/auth/core/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java

Author: asanso
Date: Tue May  9 10:16:44 2017
New Revision: 1794522

URL: http://svn.apache.org/viewvc?rev=1794522&view=rev
Log:
SLING-6053 - SlingAuthenticator identifies wrong sibling node with AuthenticationInfo 

* applied patch from Miklos Csere . Thanks!!

Modified:
    sling/trunk/bundles/auth/core/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java

Modified: sling/trunk/bundles/auth/core/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/auth/core/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java?rev=1794522&r1=1794521&r2=1794522&view=diff
==============================================================================
--- sling/trunk/bundles/auth/core/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java (original)
+++ sling/trunk/bundles/auth/core/src/test/java/org/apache/sling/auth/core/impl/SlingAuthenticatorTest.java Tue May  9 10:16:44 2017
@@ -28,7 +28,6 @@ import org.jmock.Expectations;
 import org.jmock.Mockery;
 import org.jmock.integration.junit4.JUnit4Mockery;
 import org.junit.Assert;
-import org.junit.Ignore;
 import org.junit.Test;
 import junitx.util.PrivateAccessor;
 
@@ -138,7 +137,111 @@ public class SlingAuthenticatorTest {
          */
         Assert.assertTrue(AUTH_TYPE.equals(authInfo.getAuthType()));
     }
+    
+    /**
+     * Test is OK for same node;
+     * @throws Throwable
+     */
+    @Test
+    public void test_childNodeShouldHaveAuthenticationInfo2() throws Throwable {
+        final String AUTH_TYPE = "AUTH_TYPE_TEST";
+        final String PROTECTED_PATH = "/content/en/test";
+        final String REQUEST_CHILD_NODE = "/content/en/test";
+
+        SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
+
+        PathBasedHolderCache<AbstractAuthenticationHandlerHolder> authRequiredCache = new PathBasedHolderCache<AbstractAuthenticationHandlerHolder>();
+        authRequiredCache.addHolder(buildAuthHolderForAuthTypeAndPath(AUTH_TYPE, PROTECTED_PATH));
+
+        PrivateAccessor.setField(slingAuthenticator, "authHandlerCache", authRequiredCache);
+        final HttpServletRequest request = context.mock(HttpServletRequest.class);
+        buildExpectationsForRequestPathAndAuthPath(request, REQUEST_CHILD_NODE, PROTECTED_PATH);
+
+        AuthenticationInfo authInfo = (AuthenticationInfo) PrivateAccessor.invoke(slingAuthenticator, "getAuthenticationInfo",
+                new Class[]{HttpServletRequest.class, HttpServletResponse.class}, new Object[]{request, context.mock(HttpServletResponse.class)});
+        /**
+         * The AUTH TYPE defined aboved should  be used for the path /test and his children: eg /test/childnode.
+         */
+        Assert.assertTrue(AUTH_TYPE.equals(authInfo.getAuthType()));
+    }
+    
+    /**
+     * Test is OK for same node with ending slash;
+     * @throws Throwable
+     */
+    @Test
+    public void test_childNodeShouldHaveAuthenticationInfo3() throws Throwable {
+        final String AUTH_TYPE = "AUTH_TYPE_TEST";
+        final String PROTECTED_PATH = "/content/en/test";
+        final String REQUEST_CHILD_NODE = "/content/en/test/";
+
+        SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
+
+        PathBasedHolderCache<AbstractAuthenticationHandlerHolder> authRequiredCache = new PathBasedHolderCache<AbstractAuthenticationHandlerHolder>();
+        authRequiredCache.addHolder(buildAuthHolderForAuthTypeAndPath(AUTH_TYPE, PROTECTED_PATH));
+
+        PrivateAccessor.setField(slingAuthenticator, "authHandlerCache", authRequiredCache);
+        final HttpServletRequest request = context.mock(HttpServletRequest.class);
+        buildExpectationsForRequestPathAndAuthPath(request, REQUEST_CHILD_NODE, PROTECTED_PATH);
+
+        AuthenticationInfo authInfo = (AuthenticationInfo) PrivateAccessor.invoke(slingAuthenticator, "getAuthenticationInfo",
+                new Class[]{HttpServletRequest.class, HttpServletResponse.class}, new Object[]{request, context.mock(HttpServletResponse.class)});
+        /**
+         * The AUTH TYPE defined aboved should  be used for the path /test and his children: eg /test/childnode.
+         */
+        Assert.assertTrue(AUTH_TYPE.equals(authInfo.getAuthType()));
+    }
+    
+    /**
+     * Test is OK for same node with extension
+     * @throws Throwable
+     */
+    @Test
+    public void test_childNodeShouldHaveAuthenticationInfo4() throws Throwable {
+        final String AUTH_TYPE = "AUTH_TYPE_TEST";
+        final String PROTECTED_PATH = "/content/en/test";
+        final String REQUEST_CHILD_NODE = "/content/en/test.html";
+
+        SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
+
+        PathBasedHolderCache<AbstractAuthenticationHandlerHolder> authRequiredCache = new PathBasedHolderCache<AbstractAuthenticationHandlerHolder>();
+        authRequiredCache.addHolder(buildAuthHolderForAuthTypeAndPath(AUTH_TYPE, PROTECTED_PATH));
+
+        PrivateAccessor.setField(slingAuthenticator, "authHandlerCache", authRequiredCache);
+        final HttpServletRequest request = context.mock(HttpServletRequest.class);
+        buildExpectationsForRequestPathAndAuthPath(request, REQUEST_CHILD_NODE, PROTECTED_PATH);
+
+        AuthenticationInfo authInfo = (AuthenticationInfo) PrivateAccessor.invoke(slingAuthenticator, "getAuthenticationInfo",
+                new Class[]{HttpServletRequest.class, HttpServletResponse.class}, new Object[]{request, context.mock(HttpServletResponse.class)});
+        /**
+         * The AUTH TYPE defined aboved should  be used for the path /test and his children: eg /test/childnode.
+         */
+        Assert.assertTrue(AUTH_TYPE.equals(authInfo.getAuthType()));
+    }
+
+    @Test
+    public void test_childNodeShouldHaveAuthenticationInfoRoot() throws Throwable {
+        final String AUTH_TYPE = "AUTH_TYPE_TEST";
+        final String PROTECTED_PATH = "/";
+        final String REQUEST_CHILD_NODE = "/content/en/test";
+
+        SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
+
+        PathBasedHolderCache<AbstractAuthenticationHandlerHolder> authRequiredCache = new PathBasedHolderCache<AbstractAuthenticationHandlerHolder>();
+        authRequiredCache.addHolder(buildAuthHolderForAuthTypeAndPath(AUTH_TYPE, PROTECTED_PATH));
 
+        PrivateAccessor.setField(slingAuthenticator, "authHandlerCache", authRequiredCache);
+        final HttpServletRequest request = context.mock(HttpServletRequest.class);
+        buildExpectationsForRequestPathAndAuthPath(request, REQUEST_CHILD_NODE, PROTECTED_PATH);
+
+        AuthenticationInfo authInfo = (AuthenticationInfo) PrivateAccessor.invoke(slingAuthenticator, "getAuthenticationInfo",
+                new Class[]{HttpServletRequest.class, HttpServletResponse.class}, new Object[]{request, context.mock(HttpServletResponse.class)});
+        /**
+         * The AUTH TYPE defined aboved should  be used for the path /test and his children: eg /test/childnode.
+         */
+        Assert.assertTrue(AUTH_TYPE.equals(authInfo.getAuthType()));
+    }
+    
 
     /**
      * JIRA: SLING-6053
@@ -157,7 +260,6 @@ public class SlingAuthenticatorTest {
      * @throws Throwable
      */
     @Test
-    @Ignore
     public void test_siblingNodeShouldNotHaveAuthenticationInfo() throws Throwable {
         final String AUTH_TYPE = "AUTH_TYPE_TEST";
         final String PROTECTED_PATH = "/content/en/test";
@@ -179,6 +281,78 @@ public class SlingAuthenticatorTest {
          */
         Assert.assertFalse(AUTH_TYPE.equals(authInfo.getAuthType()));
     }
+    
+    @Test
+    public void test_childNodeAuthenticationHandlerPath() throws Throwable {
+        final String requestPath = "/content/test/test2";
+        final String handlerPath = "/content/test";
+        SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
+       
+        Assert.assertTrue( (boolean)PrivateAccessor.invoke(slingAuthenticator, "isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new Object[] {requestPath, handlerPath}));
+    }
+    
+    @Test
+    public void test_siblingNodeAuthenticationHandlerPath() throws Throwable {
+        final String requestPath = "/content/test2.html/en/2016/09/19/test.html";
+        final String handlerPath = "/content/test";
+        SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
+        
+        Assert.assertFalse( (boolean)PrivateAccessor.invoke(slingAuthenticator, "isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new Object[] {requestPath, handlerPath}));
+    }
+
+    @Test
+    public void test_actualNodeAuthenticationHandlerPath() throws Throwable {
+        final String requestPath = "/content/test";
+        final String handlerPath = "/content/test";
+        SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
+        
+        Assert.assertTrue( (boolean)PrivateAccessor.invoke(slingAuthenticator, "isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new Object[] {requestPath, handlerPath}));
+    }
+
+    @Test
+    public void test_rootNodeAuthenticationHandlerPath() throws Throwable {
+        final String requestPath = "/content/test";
+        final String handlerPath = "/";
+        SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
+        
+        Assert.assertTrue( (boolean)PrivateAccessor.invoke(slingAuthenticator, "isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new Object[] {requestPath, handlerPath}));
+    }
+
+    @Test
+    public void test_requestPathSelectorsAreTakenInConsideration() throws Throwable {
+        final String requestPath = "/content/test.selector1.selector2.html/en/2016/test.html";
+        final String handlerPath = "/content/test";
+        SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
+        
+        Assert.assertTrue( (boolean)PrivateAccessor.invoke(slingAuthenticator, "isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new Object[] {requestPath, handlerPath}));
+    }
+
+    @Test
+    public void test_requestPathSelectorsSiblingAreTakenInConsideration() throws Throwable {
+        final String requestPath = "/content/test.selector1.selector2.html/en/2016/09/19/test.html";
+        final String handlerPath = "/content/test2";
+        SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
+        
+        Assert.assertFalse( (boolean)PrivateAccessor.invoke(slingAuthenticator, "isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new Object[] {requestPath, handlerPath}));
+    }
+
+    @Test
+    public void test_requestPathBackSlash() throws Throwable {
+        final String requestPath = "/page1\\somesubepage";
+        final String handlerPath = "/page";
+        SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
+
+        Assert.assertFalse( (boolean)PrivateAccessor.invoke(slingAuthenticator, "isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new Object[] {requestPath, handlerPath}));
+    }
+
+    @Test
+    public void test_emptyNodeAuthenticationHandlerPath() throws Throwable {
+        final String requestPath = "/content/test";
+        final String handlerPath = "";
+        SlingAuthenticator slingAuthenticator = new SlingAuthenticator();
+
+        Assert.assertTrue( (boolean)PrivateAccessor.invoke(slingAuthenticator, "isNodeRequiresAuthHandler", new Class[] {String.class, String.class}, new Object[] {requestPath, handlerPath}));
+    }
 
     //---------------------------- PRIVATE METHODS -----------------------------