View Javadoc

1   package org.apache.maven.archiva.security;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import java.util.List;
23  
24  import org.apache.commons.lang.StringUtils;
25  
26  /**
27   * DefaultUserRepositoriesTest 
28   *
29   * @version $Id: DefaultUserRepositoriesTest.java 718864 2008-11-19 06:33:35Z brett $
30   */
31  public class DefaultUserRepositoriesTest
32      extends AbstractSecurityTest
33  {   
34      @Override
35      protected String getPlexusConfigLocation()
36      {
37          return "org/apache/maven/archiva/security/DefaultUserRepositoriesTest.xml";
38      }
39      
40      public void testGetObservableRepositoryIds()
41          throws Exception
42      {
43          // create some users.
44          createUser( USER_ALPACA, "Al 'Archiva' Paca" );
45  
46          assertEquals( "Expected users", 3, securitySystem.getUserManager().getUsers().size() );
47  
48          // some unassigned repo observer roles.
49          setupRepository( "central" );
50          setupRepository( "corporate" );
51          setupRepository( "internal" );
52          setupRepository( "snapshots" );
53          setupRepository( "secret" );
54  
55          // some assigned repo observer roles.
56          assignRepositoryObserverRole( USER_ALPACA, "corporate" );
57          assignRepositoryObserverRole( USER_ALPACA, "central" );
58          assignRepositoryObserverRole( USER_GUEST, "corporate" );
59          // the global repo observer role.
60          assignGlobalRepositoryObserverRole( USER_ADMIN );
61  
62          assertRepoIds( new String[] { "central", "corporate" }, userRepos.getObservableRepositoryIds( USER_ALPACA ) );
63          assertRepoIds( new String[] { "coporate" }, userRepos.getObservableRepositoryIds( USER_GUEST ) );
64          assertRepoIds( new String[] { "central", "internal", "corporate", "snapshots", "secret" }, userRepos
65              .getObservableRepositoryIds( USER_ADMIN ) );
66      }
67  
68      private void assertRepoIds( String[] expectedRepoIds, List<String> observableRepositoryIds )
69      {
70          assertNotNull( "Observable Repository Ids cannot be null.", observableRepositoryIds );
71  
72          if ( expectedRepoIds.length != observableRepositoryIds.size() )
73          {
74              fail( "Size of Observable Repository Ids wrong, expected <" + expectedRepoIds.length + "> but got <"
75                  + observableRepositoryIds.size() + "> instead. \nExpected: [" + StringUtils.join( expectedRepoIds, "," )
76                  + "]\nActual: [" + StringUtils.join( observableRepositoryIds.iterator(), "," ) + "]" );
77          }
78      }
79  
80      private void assignGlobalRepositoryObserverRole( String principal )
81          throws Exception
82      {
83          roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_GLOBAL_REPOSITORY_OBSERVER, principal );
84      }
85  }