1 package org.apache.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 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;
28 import java.util.List;
31 * DefaultUserRepositoriesTest
35 public class DefaultUserRepositoriesTest
36 extends AbstractSecurityTest
45 restoreGuestInitialValues( USER_ALPACA );
46 restoreGuestInitialValues( USER_GUEST );
47 restoreGuestInitialValues( USER_ADMIN );
51 public void testGetObservableRepositoryIds()
55 createUser( USER_ALPACA, "Al 'Archiva' Paca" );
57 assertEquals( "Expected users", 3, securitySystem.getUserManager().getUsers().size() );
59 // some unassigned repo observer roles.
60 setupRepository( "central" );
61 setupRepository( "corporate" );
62 setupRepository( "internal" );
63 setupRepository( "snapshots" );
64 setupRepository( "secret" );
66 // some assigned repo observer roles.
67 assignRepositoryObserverRole( USER_ALPACA, "corporate" );
68 assignRepositoryObserverRole( USER_ALPACA, "central" );
69 assignRepositoryObserverRole( USER_GUEST, "corporate" );
70 // the global repo observer role.
71 assignGlobalRepositoryObserverRole( USER_ADMIN );
73 assertRepoIds( new String[]{ "central", "corporate" }, userRepos.getObservableRepositoryIds( USER_ALPACA ) );
74 assertRepoIds( new String[]{ "coporate" }, userRepos.getObservableRepositoryIds( USER_GUEST ) );
75 assertRepoIds( new String[]{ "central", "internal", "corporate", "snapshots", "secret" },
76 userRepos.getObservableRepositoryIds( USER_ADMIN ) );
81 public void tearDown()
85 restoreGuestInitialValues( USER_ALPACA );
86 restoreGuestInitialValues( USER_GUEST );
87 restoreGuestInitialValues( USER_ADMIN );
90 private void assertRepoIds( String[] expectedRepoIds, List<String> observableRepositoryIds )
92 assertNotNull( "Observable Repository Ids cannot be null.", observableRepositoryIds );
94 if ( expectedRepoIds.length != observableRepositoryIds.size() )
96 fail( "Size of Observable Repository Ids wrong, expected <" + expectedRepoIds.length + "> but got <"
97 + observableRepositoryIds.size() + "> instead. \nExpected: ["
98 + StringUtils.join( expectedRepoIds, "," ) + "]\nActual: ["
99 + StringUtils.join( observableRepositoryIds.iterator(), "," ) + "]" );
103 private void assignGlobalRepositoryObserverRole( String principal )
106 roleManager.assignRole( ArchivaRoleConstants.TEMPLATE_GLOBAL_REPOSITORY_OBSERVER, principal );