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
23 import java.util.List;
25 import org.apache.commons.io.FileUtils;
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
28 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
30 import org.codehaus.plexus.redback.rbac.RBACManager;
31 import org.codehaus.plexus.redback.role.RoleManager;
32 import org.codehaus.plexus.redback.system.SecuritySystem;
33 import org.codehaus.plexus.redback.users.User;
34 import org.codehaus.plexus.redback.users.UserManager;
37 * DefaultUserRepositoriesTest
39 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
42 public class DefaultUserRepositoriesTest
43 extends PlexusInSpringTestCase
45 private static final String USER_GUEST = "guest";
47 private static final String USER_ADMIN = "admin";
49 private static final String USER_ALPACA = "alpaca";
51 private SecuritySystem securitySystem;
53 private RBACManager rbacManager;
55 private RoleManager roleManager;
57 private ArchivaConfiguration archivaConfiguration;
59 private UserRepositories userRepos;
61 public void testGetObservableRepositoryIds()
65 createUser( USER_ALPACA, "Al 'Archiva' Paca" );
67 assertEquals( "Expected users", 3, securitySystem.getUserManager().getUsers().size() );
69 // some unassigned repo observer roles.
70 setupRepository( "central" );
71 setupRepository( "corporate" );
72 setupRepository( "internal" );
73 setupRepository( "snapshots" );
74 setupRepository( "secret" );
76 // some assigned repo observer roles.
77 assignRepositoryObserverRole( USER_ALPACA, "corporate" );
78 assignRepositoryObserverRole( USER_ALPACA, "central" );
79 assignRepositoryObserverRole( USER_GUEST, "corporate" );
80 // the global repo observer role.
81 assignGlobalRepositoryObserverRole( USER_ADMIN );
83 assertRepoIds( new String[] { "central", "corporate" }, userRepos.getObservableRepositoryIds( USER_ALPACA ) );
84 assertRepoIds( new String[] { "coporate" }, userRepos.getObservableRepositoryIds( USER_GUEST ) );
85 assertRepoIds( new String[] { "central", "internal", "corporate", "snapshots", "secret" }, userRepos
86 .getObservableRepositoryIds( USER_ADMIN ) );
89 private void assertRepoIds( String[] expectedRepoIds, List<String> observableRepositoryIds )
91 assertNotNull( "Observable Repository Ids cannot be null.", observableRepositoryIds );
93 if ( expectedRepoIds.length != observableRepositoryIds.size() )
95 fail( "Size of Observable Repository Ids wrong, expected <" + expectedRepoIds.length + "> but got <"
96 + observableRepositoryIds.size() + "> instead. \nExpected: [" + StringUtils.join( expectedRepoIds, "," )
97 + "]\nActual: [" + StringUtils.join( observableRepositoryIds.iterator(), "," ) + "]" );
101 private void setupRepository( String repoId )
104 // Add repo to configuration.
105 ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
106 repoConfig.setId( repoId );
107 repoConfig.setName( "Testable repo <" + repoId + ">" );
108 repoConfig.setLocation( getTestPath( "target/test-repo/" + repoId ) );
109 archivaConfiguration.getConfiguration().addManagedRepository( repoConfig );
111 // Add repo roles to security.
112 userRepos.createMissingRepositoryRoles( repoId );
115 private void assignGlobalRepositoryObserverRole( String principal )
118 roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_GLOBAL_REPOSITORY_OBSERVER, principal );
121 private void assignRepositoryObserverRole( String principal, String repoId )
124 roleManager.assignTemplatedRole( ArchivaRoleConstants.TEMPLATE_REPOSITORY_OBSERVER, repoId, principal );
127 private User createUser( String principal, String fullname )
129 UserManager userManager = securitySystem.getUserManager();
131 User user = userManager.createUser( principal, fullname, principal + "@testable.archiva.apache.org" );
132 securitySystem.getPolicy().setEnabled( false );
133 userManager.addUser( user );
134 securitySystem.getPolicy().setEnabled( true );
140 protected void setUp()
145 File srcConfig = getTestFile( "src/test/resources/repository-archiva.xml" );
146 File destConfig = getTestFile( "target/test-conf/archiva.xml" );
148 destConfig.getParentFile().mkdirs();
151 FileUtils.copyFile( srcConfig, destConfig );
153 securitySystem = (SecuritySystem) lookup( SecuritySystem.class, "testable" );
154 rbacManager = (RBACManager) lookup( RBACManager.class, "memory" );
155 roleManager = (RoleManager) lookup( RoleManager.class, "default" );
156 userRepos = (UserRepositories) lookup( UserRepositories.class, "default" );
157 archivaConfiguration = (ArchivaConfiguration) lookup( ArchivaConfiguration.class );
159 // Some basic asserts.
160 assertNotNull( securitySystem );
161 assertNotNull( rbacManager );
162 assertNotNull( roleManager );
163 assertNotNull( userRepos );
164 assertNotNull( archivaConfiguration );
167 User adminUser = createUser( USER_ADMIN, "Admin User" );
168 roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_SYSTEM_ADMIN, adminUser.getPrincipal().toString() );
171 User guestUser = createUser( USER_GUEST, "Guest User" );
172 roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_GUEST, guestUser.getPrincipal().toString() );