View Javadoc
1   package org.apache.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 org.apache.archiva.security.common.ArchivaRoleConstants;
23  import org.apache.commons.lang.StringUtils;
24  import org.junit.After;
25  import org.junit.Before;
26  import org.junit.Test;
27  
28  import java.util.List;
29  
30  /**
31   * DefaultUserRepositoriesTest
32   *
33   *
34   */
35  public class DefaultUserRepositoriesTest
36      extends AbstractSecurityTest
37  {
38  
39  
40      @Before
41      @Override
42      public void setUp()
43          throws Exception
44      {
45          super.setUp();
46          restoreGuestInitialValues( USER_ALPACA );
47          restoreGuestInitialValues( USER_GUEST );
48          restoreGuestInitialValues( USER_ADMIN );
49      }
50  
51      @Test
52      public void testGetObservableRepositoryIds()
53          throws Exception
54      {
55          // create some users.
56          createUser( USER_ALPACA, "Al 'Archiva' Paca" );
57  
58          assertEquals( "Expected users", 3, securitySystem.getUserManager().getUsers().size() );
59  
60          // some unassigned repo observer roles.
61          setupRepository( "central" );
62          setupRepository( "corporate" );
63          setupRepository( "internal" );
64          setupRepository( "snapshots" );
65          setupRepository( "secret" );
66  
67          // some assigned repo observer roles.
68          assignRepositoryObserverRole( USER_ALPACA, "corporate" );
69          assignRepositoryObserverRole( USER_ALPACA, "central" );
70          assignRepositoryObserverRole( USER_GUEST, "corporate" );
71          // the global repo observer role.
72          assignGlobalRepositoryObserverRole( USER_ADMIN );
73  
74          assertRepoIds( new String[]{ "central", "corporate" }, userRepos.getObservableRepositoryIds( USER_ALPACA ) );
75          assertRepoIds( new String[]{ "coporate" }, userRepos.getObservableRepositoryIds( USER_GUEST ) );
76          assertRepoIds( new String[]{ "central", "internal", "corporate", "snapshots", "secret" },
77                         userRepos.getObservableRepositoryIds( USER_ADMIN ) );
78  
79      }
80  
81      @After
82      @Override
83      public void tearDown()
84          throws Exception
85      {
86          super.tearDown();
87          restoreGuestInitialValues( USER_ALPACA );
88          restoreGuestInitialValues( USER_GUEST );
89          restoreGuestInitialValues( USER_ADMIN );
90      }
91  
92      private void assertRepoIds( String[] expectedRepoIds, List<String> observableRepositoryIds )
93      {
94          assertNotNull( "Observable Repository Ids cannot be null.", observableRepositoryIds );
95  
96          if ( expectedRepoIds.length != observableRepositoryIds.size() )
97          {
98              fail( "Size of Observable Repository Ids wrong, expected <" + expectedRepoIds.length + "> but got <"
99                        + observableRepositoryIds.size() + "> instead. \nExpected: ["
100                       + StringUtils.join( expectedRepoIds, "," ) + "]\nActual: ["
101                       + StringUtils.join( observableRepositoryIds.iterator(), "," ) + "]" );
102         }
103     }
104 
105     private void assignGlobalRepositoryObserverRole( String principal )
106         throws Exception
107     {
108         roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_GLOBAL_REPOSITORY_OBSERVER, principal );
109     }
110 }