1 package org.apache.maven.archiva.security;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import junit.framework.TestCase;
23 import org.apache.commons.io.FileUtils;
24 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.codehaus.plexus.redback.rbac.RBACManager;
27 import org.codehaus.plexus.redback.role.RoleManager;
28 import org.codehaus.plexus.redback.system.SecuritySystem;
29 import org.codehaus.plexus.redback.users.User;
30 import org.codehaus.plexus.redback.users.UserManager;
31 import org.junit.Before;
32 import org.junit.runner.RunWith;
33 import org.springframework.test.context.ContextConfiguration;
34 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
36 import javax.inject.Inject;
37 import javax.inject.Named;
41 * AbstractSecurityTest
43 * @version $Id: AbstractSecurityTest
45 @RunWith( SpringJUnit4ClassRunner.class )
46 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
47 public abstract class AbstractSecurityTest
50 protected static final String USER_GUEST = "guest";
52 protected static final String USER_ADMIN = "admin";
54 protected static final String USER_ALPACA = "alpaca";
57 @Named( value = "securitySystem#testable" )
58 protected SecuritySystem securitySystem;
61 @Named( value = "rBACManager#memory" )
62 private RBACManager rbacManager;
65 protected RoleManager roleManager;
68 private ArchivaConfiguration archivaConfiguration;
71 protected UserRepositories userRepos;
73 protected void setupRepository( String repoId )
76 // Add repo to configuration.
77 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
78 repoConfig.setId( repoId );
79 repoConfig.setName( "Testable repo <" + repoId + ">" );
80 repoConfig.setLocation( new File( "./target/test-repo/" + repoId ).getPath() );
81 archivaConfiguration.getConfiguration().addManagedRepository( repoConfig );
83 // Add repo roles to security.
84 userRepos.createMissingRepositoryRoles( repoId );
87 protected void assignRepositoryObserverRole( String principal, String repoId )
90 roleManager.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId, principal );
93 protected User createUser( String principal, String fullname )
95 UserManager userManager = securitySystem.getUserManager();
97 User user = userManager.createUser( principal, fullname, principal + "@testable.archiva.apache.org" );
98 securitySystem.getPolicy().setEnabled( false );
99 userManager.addUser( user );
100 securitySystem.getPolicy().setEnabled( true );
112 File srcConfig = new File( "./src/test/resources/repository-archiva.xml" );
113 File destConfig = new File( "./target/test-conf/archiva.xml" );
115 destConfig.getParentFile().mkdirs();
118 FileUtils.copyFile( srcConfig, destConfig );
120 // Some basic asserts.
121 assertNotNull( securitySystem );
122 assertNotNull( rbacManager );
123 assertNotNull( roleManager );
124 assertNotNull( userRepos );
125 assertNotNull( archivaConfiguration );
128 User adminUser = createUser( USER_ADMIN, "Admin User" );
129 roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_SYSTEM_ADMIN, adminUser.getPrincipal().toString() );
132 User guestUser = createUser( USER_GUEST, "Guest User" );
133 roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, guestUser.getPrincipal().toString() );