]> source.dussan.org Git - archiva.git/commitdiff
fix more unit tests in archiva-security
authorOlivier Lamy <olamy@apache.org>
Wed, 8 Jun 2011 15:59:41 +0000 (15:59 +0000)
committerOlivier Lamy <olamy@apache.org>
Wed, 8 Jun 2011 15:59:41 +0000 (15:59 +0000)
git-svn-id: https://svn.apache.org/repos/asf/archiva/trunk@1133448 13f79535-47bb-0310-9956-ffa450edef68

archiva-modules/archiva-base/archiva-proxy/src/test/java/org/apache/maven/archiva/proxy/HttpProxyTransferTest.java
archiva-modules/archiva-web/archiva-security/pom.xml
archiva-modules/archiva-web/archiva-security/src/main/java/org/apache/maven/archiva/security/ArchivaServletAuthenticator.java
archiva-modules/archiva-web/archiva-security/src/main/java/org/apache/maven/archiva/security/DefaultUserRepositories.java
archiva-modules/archiva-web/archiva-security/src/test/java/org/apache/maven/archiva/security/AbstractSecurityTest.java
archiva-modules/archiva-web/archiva-security/src/test/java/org/apache/maven/archiva/security/ArchivaServletAuthenticatorTest.java
archiva-modules/archiva-web/archiva-security/src/test/java/org/apache/maven/archiva/security/DefaultUserRepositoriesTest.java
archiva-modules/archiva-web/archiva-security/src/test/resources/spring-context.xml
pom.xml

index 2f2bca3dbc7d0e98abc41bf421face8c745cecad..3df886208ec262824e85e46b43fa84fea1f0e854 100644 (file)
@@ -159,8 +159,6 @@ public class HttpProxyTransferTest
 
         config.getConfiguration().addRemoteRepository( repoConfig );
 
-        CacheManager.getInstance().clearAll();
-
     }
 
     @After
index b8092f9deadfee1ccb19b7cfffd56a1c4f65768f..510fda0733f4e618957427d15a413fa9b12ea4f0 100644 (file)
       <artifactId>derby</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>com.google.guava</groupId>
+      <artifactId>guava</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 </project>
index 6180f75d9cdfe00346bb6d7734be43ea0d03e04f..a73c9e4a657fe11d3c012a02f41c2d94e901fa90 100644 (file)
@@ -40,7 +40,7 @@ import org.springframework.stereotype.Service;
 
 /**
  * @version
- * @plexus.component role="org.apache.maven.archiva.security.ServletAuthenticator" role-hint="default"
+ * plexus.component role="org.apache.maven.archiva.security.ServletAuthenticator" role-hint="default"
  */
 @Service("servletAuthenticator")
 public class ArchivaServletAuthenticator
@@ -49,7 +49,7 @@ public class ArchivaServletAuthenticator
     private Logger log = LoggerFactory.getLogger( ArchivaServletAuthenticator.class );
 
     /**
-     * @plexus.requirement
+     * plexus.requirement
      */
     @Inject
     private SecuritySystem securitySystem;
@@ -119,5 +119,13 @@ public class ArchivaServletAuthenticator
     }
 
 
+    public SecuritySystem getSecuritySystem()
+    {
+        return securitySystem;
+    }
 
+    public void setSecuritySystem( SecuritySystem securitySystem )
+    {
+        this.securitySystem = securitySystem;
+    }
 }
index 36e3d799245b48174e2b45c8236388c09a50a635..88a28e2ac25de692b09f31e8f80c7abb8516d3e5 100644 (file)
@@ -43,26 +43,26 @@ import javax.inject.Inject;
  * DefaultUserRepositories
  * 
  * @version $Id$
- * @plexus.component role="org.apache.maven.archiva.security.UserRepositories" role-hint="default"
+ * plexus.component role="org.apache.maven.archiva.security.UserRepositories" role-hint="default"
  */
 @Service("userRepositories")
 public class DefaultUserRepositories
     implements UserRepositories
 {
     /**
-     * @plexus.requirement
+     * plexus.requirement
      */
     @Inject
     private SecuritySystem securitySystem;
 
     /**
-     * @plexus.requirement role-hint="default"
+     * plexus.requirement role-hint="default"
      */
     @Inject
     private RoleManager roleManager;
 
     /**
-     * @plexus.requirement
+     * plexus.requirement
      */
     @Inject
     private ArchivaConfiguration archivaConfiguration;
index 15eba1e26d7891a25522a11e1eaf2994464ab6aa..84d6b8c51938fd8f05bb393e5b2b6da027b9ae51 100644 (file)
@@ -19,17 +19,23 @@ package org.apache.maven.archiva.security;
  * under the License.
  */
 
+import com.google.common.collect.Lists;
 import junit.framework.TestCase;
+import net.sf.ehcache.CacheManager;
 import org.apache.commons.io.FileUtils;
 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
 import org.codehaus.plexus.redback.rbac.RBACManager;
+import org.codehaus.plexus.redback.rbac.RbacObjectNotFoundException;
+import org.codehaus.plexus.redback.rbac.UserAssignment;
 import org.codehaus.plexus.redback.role.RoleManager;
 import org.codehaus.plexus.redback.system.SecuritySystem;
 import org.codehaus.plexus.redback.users.User;
 import org.codehaus.plexus.redback.users.UserManager;
 import org.junit.Before;
 import org.junit.runner.RunWith;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
@@ -47,6 +53,9 @@ import java.io.File;
 public abstract class AbstractSecurityTest
     extends TestCase
 {
+
+    protected Logger log = LoggerFactory.getLogger( getClass() );
+
     protected static final String USER_GUEST = "guest";
 
     protected static final String USER_ADMIN = "admin";
@@ -59,7 +68,7 @@ public abstract class AbstractSecurityTest
 
     @Inject
     @Named( value = "rBACManager#memory" )
-    private RBACManager rbacManager;
+    protected RBACManager rbacManager;
 
     @Inject
     protected RoleManager roleManager;
@@ -132,4 +141,22 @@ public abstract class AbstractSecurityTest
         User guestUser = createUser( USER_GUEST, "Guest User" );
         roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, guestUser.getPrincipal().toString() );
     }
+
+    protected void restoreGuestInitialValues( String userId )
+        throws Exception
+    {
+        UserAssignment userAssignment = null;
+        try
+        {
+            userAssignment = rbacManager.getUserAssignment( userId );
+        }
+        catch ( RbacObjectNotFoundException e )
+        {
+            log.info( "ignore RbacObjectNotFoundException for id {} during restoreGuestInitialValues", userId );
+            return;
+        }
+        userAssignment.setRoleNames( Lists.newArrayList( "Guest" ) );
+        rbacManager.saveUserAssignment( userAssignment );
+        CacheManager.getInstance().clearAll();
+    }
 }
index 7a0ec20d184263fc0e1fdc7d1ff5bdc720215aea..a24bb82287fa201df9d724a0e6cfde3ce616dc9d 100644 (file)
@@ -19,30 +19,33 @@ package org.apache.maven.archiva.security;
  * under the License.
  */
 
-import javax.inject.Inject;
-import javax.servlet.http.HttpServletRequest;
-
+import com.google.common.collect.Lists;
+import net.sf.ehcache.CacheManager;
 import org.codehaus.plexus.redback.authentication.AuthenticationException;
 import org.codehaus.plexus.redback.authentication.AuthenticationResult;
 import org.codehaus.plexus.redback.authorization.UnauthorizedException;
+import org.codehaus.plexus.redback.rbac.UserAssignment;
 import org.codehaus.plexus.redback.system.DefaultSecuritySession;
 import org.codehaus.plexus.redback.system.SecuritySession;
 import org.codehaus.plexus.redback.users.User;
 import org.codehaus.plexus.redback.users.UserManager;
-
 import org.easymock.MockControl;
 import org.junit.Before;
 import org.junit.Test;
 
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.servlet.http.HttpServletRequest;
+import java.util.Arrays;
+
 /**
  * ArchivaServletAuthenticatorTest
- * 
- * @version
  */
 public class ArchivaServletAuthenticatorTest
     extends AbstractSecurityTest
 {
     @Inject
+    @Named( value = "servletAuthenticator#test" )
     private ServletAuthenticator servletAuth;
 
     private MockControl httpServletRequestControl;
@@ -116,6 +119,8 @@ public class ArchivaServletAuthenticatorTest
             servletAuth.isAuthorized( request, session, "corporate", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD );
 
         assertTrue( isAuthorized );
+
+        restoreGuestInitialValues( USER_ALPACA );
     }
 
     @Test
@@ -148,6 +153,8 @@ public class ArchivaServletAuthenticatorTest
         }
 
         httpServletRequestControl.verify();
+
+        restoreGuestInitialValues( USER_ALPACA );
     }
 
     @Test
@@ -168,6 +175,8 @@ public class ArchivaServletAuthenticatorTest
             servletAuth.isAuthorized( request, session, "corporate", ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS );
 
         assertTrue( isAuthorized );
+
+        restoreGuestInitialValues( USER_ALPACA );
     }
 
     @Test
@@ -191,6 +200,8 @@ public class ArchivaServletAuthenticatorTest
         {
             assertEquals( "Access denied for repository corporate", e.getMessage() );
         }
+
+        restoreGuestInitialValues( USER_ALPACA );
     }
 
     @Test
@@ -202,6 +213,10 @@ public class ArchivaServletAuthenticatorTest
             servletAuth.isAuthorized( USER_GUEST, "corporate", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD );
 
         assertTrue( isAuthorized );
+
+        // cleanup previously add karma
+        restoreGuestInitialValues(USER_GUEST);
+
     }
 
     @Test
@@ -213,6 +228,10 @@ public class ArchivaServletAuthenticatorTest
         boolean isAuthorized =
             servletAuth.isAuthorized( USER_GUEST, "corporate", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD );
         assertFalse( isAuthorized );
+
+        // cleanup previously add karma
+        restoreGuestInitialValues(USER_GUEST);
+
     }
 
     @Test
@@ -225,6 +244,9 @@ public class ArchivaServletAuthenticatorTest
             servletAuth.isAuthorized( USER_GUEST, "corporate", ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS );
 
         assertTrue( isAuthorized );
+
+        // cleanup previously add karma
+        restoreGuestInitialValues(USER_GUEST);
     }
 
     @Test
@@ -236,4 +258,5 @@ public class ArchivaServletAuthenticatorTest
 
         assertFalse( isAuthorized );
     }
+
 }
index d314e1c37346f0f0556b8d74551ba7d360d64e5d..bc11b9436b7c057355ace82565556f9e5052a7b1 100644 (file)
@@ -22,6 +22,7 @@ package org.apache.maven.archiva.security;
 import java.util.List;
 
 import org.apache.commons.lang.StringUtils;
+import org.junit.Before;
 import org.junit.Test;
 
 /**
@@ -31,10 +32,16 @@ import org.junit.Test;
  */
 public class DefaultUserRepositoriesTest
     extends AbstractSecurityTest
-{   
-    protected String getPlexusConfigLocation()
+{
+
+
+    @Before
+    public void setUp() throws Exception
     {
-        return "org/apache/maven/archiva/security/DefaultUserRepositoriesTest.xml";
+        super.setUp();
+        restoreGuestInitialValues( USER_ALPACA );
+        restoreGuestInitialValues( USER_GUEST );
+        restoreGuestInitialValues( USER_ADMIN );
     }
 
     @Test
index 30e0cbd56ed23886ef5a10765f176808748c5427..44e71526ca0b965b38b26714cc2520a52a9ec481 100644 (file)
 
   <bean name="roleManager" class="org.codehaus.plexus.redback.role.DefaultRoleManager">
     <property name="rbacManager" ref="rBACManager#memory"/>
+    <property name="modelProcessor" ref="roleModelProcessor"/>
+    <property name="templateProcessor" ref="roleTemplateProcessor#test"/>
   </bean>
 
   <bean name="rBACManager#cached" class="org.codehaus.plexus.redback.rbac.cached.CachedRbacManager">
     <property name="rbacImpl" ref="rBACManager#memory"/>
   </bean>
 
-  <!--
+  <bean name="roleModelProcessor" class="org.codehaus.plexus.redback.role.processor.DefaultRoleModelProcessor">
+    <property name="rbacManager" ref="rBACManager#memory"/>
+  </bean>
 
+  <alias name="roleModelProcessor" alias="roleModelProcessor#test"/>
 
-    <component>
-      <role>org.codehaus.plexus.redback.role.processor.RoleModelProcessor</role>
-      <role-hint>default</role-hint>
-      <implementation>org.codehaus.plexus.redback.role.processor.DefaultRoleModelProcessor</implementation>
-      <description>DefaultRoleModelProcessor: inserts the components of the model that can be populated into the rbac manager</description>
-      <requirements>
-        <requirement>
-          <role>org.codehaus.plexus.redback.rbac.RBACManager</role>
-          <role-hint>memory</role-hint>
-          <field-name>rbacManager</field-name>
-        </requirement>
-      </requirements>
-    </component>
+  <bean name="roleTemplateProcessor" class="org.codehaus.plexus.redback.role.template.DefaultRoleTemplateProcessor">
+    <property name="rbacManager" ref="rBACManager#memory"/>
+  </bean>
 
-    <component>
-      <role>org.codehaus.plexus.redback.role.template.RoleTemplateProcessor</role>
-      <role-hint>default</role-hint>
-      <implementation>org.codehaus.plexus.redback.role.template.DefaultRoleTemplateProcessor</implementation>
-      <description>DefaultRoleTemplateProcessor: inserts the components of a template into the rbac manager</description>
-      <requirements>
-        <requirement>
-          <role>org.codehaus.plexus.redback.rbac.RBACManager</role>
-          <role-hint>memory</role-hint>
-          <field-name>rbacManager</field-name>
-        </requirement>
-      </requirements>
-    </component>
+  <alias name="roleTemplateProcessor" alias="roleTemplateProcessor#test"/>
+
+  <bean name="servletAuthenticator" class="org.apache.maven.archiva.security.ArchivaServletAuthenticator">
+    <property name="securitySystem" ref="securitySystem#testable"/>
+  </bean>
+
+  <alias name="servletAuthenticator" alias="servletAuthenticator#test"/>
+
+  <!--
 
     <component>
       <role>org.apache.maven.archiva.configuration.ArchivaConfiguration</role>
     </component>
 
 
-    ignored ??
-
-       <component>
-      <role>org.apache.maven.archiva.security.ServletAuthenticator</role>
-      <role-hint>default</role-hint>
-      <implementation>org.apache.maven.archiva.security.ArchivaServletAuthenticator</implementation>
-      <description>ArchivaServletAuthenticator</description>
-      <requirements>
-        <requirement>
-          <role>org.codehaus.plexus.redback.system.SecuritySystem</role>
-          <role-hint>testable</role-hint>
-          <field-name>securitySystem</field-name>
-        </requirement>
-      </requirements>
-    </component>
-
     partially configured
 
     <component>
diff --git a/pom.xml b/pom.xml
index a0aa3df7182aab94c78e703b6e27bdce75301606..de9307630fd7bffe990d859c1d8726207cdb9896 100644 (file)
--- a/pom.xml
+++ b/pom.xml
       </dependency>
 
 
+      <dependency>
+        <groupId>com.google.guava</groupId>
+        <artifactId>guava</artifactId>
+        <version>r09</version>
+      </dependency>
       <dependency>
         <groupId>stax</groupId>
         <artifactId>stax</artifactId>