]> source.dussan.org Git - archiva.git/blob
d96ce80e2c0f48b22db108139f8566d3fed35373
[archiva.git] /
1 package org.apache.maven.archiva.webdav;
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.io.File;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.jackrabbit.webdav.DavException;
27 import org.apache.jackrabbit.webdav.DavResourceLocator;
28 import org.apache.jackrabbit.webdav.DavServletRequest;
29 import org.apache.jackrabbit.webdav.DavServletResponse;
30 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
31 import org.apache.maven.archiva.configuration.Configuration;
32 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
33 import org.apache.maven.archiva.configuration.RepositoryGroupConfiguration;
34 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
35 import org.apache.maven.archiva.repository.RepositoryContentFactory;
36 import org.apache.maven.archiva.repository.content.ManagedDefaultRepositoryContent;
37 import org.apache.maven.archiva.repository.content.RepositoryRequest;
38 import org.codehaus.plexus.spring.PlexusInSpringTestCase;
39 import org.easymock.MockControl;
40 import org.easymock.classextension.MockClassControl;
41
42 /**
43  * ArchivaDavResourceFactoryTest
44  */
45 public class ArchivaDavResourceFactoryTest
46     extends PlexusInSpringTestCase
47 {
48     private static final String RELEASES_REPO = "releases";
49
50     private static final String INTERNAL_REPO = "internal";
51
52     private static final String LOCAL_MIRROR_REPO = "local-mirror";
53
54     private static final String LOCAL_REPO_GROUP = "local";
55
56     private OverridingArchivaDavResourceFactory resourceFactory;
57
58     private MockControl requestControl;
59
60     private DavServletRequest request;
61
62     private MockControl repoRequestControl;
63
64     private RepositoryRequest repoRequest;
65
66     private DavServletResponse response;
67
68     private MockControl archivaConfigurationControl;
69
70     private ArchivaConfiguration archivaConfiguration;
71
72     private Configuration config;
73
74     private MockControl repoContentFactoryControl;
75
76     private RepositoryContentFactory repoFactory;
77
78     public void setUp()
79         throws Exception
80     {
81         super.setUp();
82
83         requestControl = MockControl.createControl( DavServletRequest.class );
84
85         request = (DavServletRequest) requestControl.getMock();
86
87         archivaConfigurationControl = MockControl.createControl( ArchivaConfiguration.class );
88
89         archivaConfiguration = (ArchivaConfiguration) archivaConfigurationControl.getMock();
90
91         config = new Configuration();
92         config.addManagedRepository( createManagedRepository(
93                                                               RELEASES_REPO,
94                                                               new File( getBasedir(), "target/test-classes/releases" ).getPath() ) );
95         config.addManagedRepository( createManagedRepository(
96                                                               INTERNAL_REPO,
97                                                               new File( getBasedir(), "target/test-classes/internal" ).getPath() ) );
98
99         RepositoryGroupConfiguration repoGroupConfig = new RepositoryGroupConfiguration();
100         repoGroupConfig.setId( LOCAL_REPO_GROUP );
101         repoGroupConfig.addRepository( RELEASES_REPO );
102         repoGroupConfig.addRepository( INTERNAL_REPO );
103
104         config.addRepositoryGroup( repoGroupConfig );
105
106         repoContentFactoryControl = MockClassControl.createControl( RepositoryContentFactory.class );
107         repoFactory = (RepositoryContentFactory) repoContentFactoryControl.getMock();
108
109         repoRequestControl = MockClassControl.createControl( RepositoryRequest.class );
110         repoRequest = (RepositoryRequest) repoRequestControl.getMock();
111
112         resourceFactory = new OverridingArchivaDavResourceFactory();
113         resourceFactory.setArchivaConfiguration( archivaConfiguration );
114         resourceFactory.setRepositoryFactory( repoFactory );
115         resourceFactory.setRepositoryRequest( repoRequest );
116     }
117
118     private ManagedRepositoryConfiguration createManagedRepository( String id, String location )
119     {
120         ManagedRepositoryConfiguration repoConfig = new ManagedRepositoryConfiguration();
121         repoConfig.setId( id );
122         repoConfig.setName( id );
123         repoConfig.setLocation( location );
124
125         return repoConfig;
126     }
127
128     private ManagedRepositoryContent createManagedRepositoryContent( String repoId )
129     {
130         ManagedRepositoryContent repoContent = new ManagedDefaultRepositoryContent();
131         repoContent.setRepository( config.findManagedRepositoryById( repoId ) );
132
133         return repoContent;
134     }
135
136     public void tearDown()
137         throws Exception
138     {
139         super.tearDown();
140     }
141
142     // MRM-1232 - Unable to get artifacts from repositories which requires Repository Manager role using repository group
143     public void testRepositoryGroupFirstRepositoryRequiresAuthentication()
144         throws Exception
145     {
146         DavResourceLocator locator =
147             new ArchivaDavResourceLocator( "", "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
148                                            LOCAL_REPO_GROUP, new ArchivaDavLocatorFactory() );
149
150         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
151
152         try
153         {
154             archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
155             requestControl.expectAndReturn( request.getMethod(), "GET", 2 );
156             repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( RELEASES_REPO ),
157                                                        createManagedRepositoryContent( RELEASES_REPO ) );
158             requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 2 );
159             requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
160             repoRequestControl.expectAndReturn(
161                                                 repoRequest.isSupportFile( "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
162                                                 false );
163             repoRequestControl.expectAndReturn(
164                                                 repoRequest.isDefault( "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
165                                                 false );
166             repoRequestControl.expectAndReturn(
167                                                 repoRequest.toArtifactReference( "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
168                                                 null );
169             repoRequestControl.expectAndReturn(
170                                                 repoRequest.toNativePath(
171                                                                           "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
172                                                                           internalRepo ),
173                                                 new File(
174                                                           config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
175                                                           "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
176             repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
177                                                        internalRepo );
178
179             archivaConfigurationControl.replay();
180             requestControl.replay();
181             repoContentFactoryControl.replay();
182             repoRequestControl.replay();
183
184             resourceFactory.createResource( locator, request, response );
185
186             archivaConfigurationControl.verify();
187             requestControl.verify();
188             repoContentFactoryControl.verify();
189             repoRequestControl.verify();
190
191             fail( "A DavException with 401 error code should have been thrown." );
192         }
193         catch ( DavException e )
194         {
195             assertEquals( 401, e.getErrorCode() );
196         }
197     }
198
199     public void testRepositoryGroupLastRepositoryRequiresAuthentication()
200         throws Exception
201     {
202         DavResourceLocator locator =
203             new ArchivaDavResourceLocator( "", "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
204                                            LOCAL_REPO_GROUP, new ArchivaDavLocatorFactory() );
205
206         List<RepositoryGroupConfiguration> repoGroups = new ArrayList<RepositoryGroupConfiguration>();
207         RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
208         repoGroup.setId( LOCAL_REPO_GROUP );
209         repoGroup.addRepository( INTERNAL_REPO );
210         repoGroup.addRepository( RELEASES_REPO );
211
212         repoGroups.add( repoGroup );
213
214         config.setRepositoryGroups( repoGroups );
215
216         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
217
218         try
219         {
220             archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
221             requestControl.expectAndReturn( request.getMethod(), "GET", 2 );
222             repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
223                                                        internalRepo );
224             repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( RELEASES_REPO ),
225                                                        createManagedRepositoryContent( RELEASES_REPO ) );
226             requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 2 );
227             requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 2 );
228             repoRequestControl.expectAndReturn(
229                                                 repoRequest.isSupportFile( "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
230                                                 false );
231             repoRequestControl.expectAndReturn(
232                                                 repoRequest.isDefault( "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
233                                                 false );
234             repoRequestControl.expectAndReturn(
235                                                 repoRequest.toArtifactReference( "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
236                                                 null );
237             repoRequestControl.expectAndReturn(
238                                                 repoRequest.toNativePath(
239                                                                           "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
240                                                                           internalRepo ),
241                                                 new File(
242                                                           config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
243                                                           "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
244
245             archivaConfigurationControl.replay();
246             requestControl.replay();
247             repoContentFactoryControl.replay();
248             repoRequestControl.replay();
249
250             resourceFactory.createResource( locator, request, response );
251
252             archivaConfigurationControl.verify();
253             requestControl.verify();
254             repoContentFactoryControl.verify();
255             repoRequestControl.verify();
256
257             fail( "A DavException with 401 error code should have been thrown." );
258         }
259         catch ( DavException e )
260         {
261             assertEquals( 401, e.getErrorCode() );
262         }
263     }
264
265     public void testRepositoryGroupArtifactDoesNotExistInAnyOfTheReposAuthenticationDisabled()
266         throws Exception
267     {
268         DavResourceLocator locator =
269             new ArchivaDavResourceLocator( "", "/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
270                                            LOCAL_REPO_GROUP, new ArchivaDavLocatorFactory() );
271
272         config.addManagedRepository( createManagedRepository( LOCAL_MIRROR_REPO,
273                                                               new File( getBasedir(),
274                                                                         "target/test-classes/local-mirror" ).getPath() ) );
275
276         List<RepositoryGroupConfiguration> repoGroups = new ArrayList<RepositoryGroupConfiguration>();
277         RepositoryGroupConfiguration repoGroup = new RepositoryGroupConfiguration();
278         repoGroup.setId( LOCAL_REPO_GROUP );
279         repoGroup.addRepository( INTERNAL_REPO );
280         repoGroup.addRepository( LOCAL_MIRROR_REPO );
281
282         repoGroups.add( repoGroup );
283
284         config.setRepositoryGroups( repoGroups );
285
286         ManagedRepositoryContent internalRepo = createManagedRepositoryContent( INTERNAL_REPO );
287         ManagedRepositoryContent localMirrorRepo = createManagedRepositoryContent( LOCAL_MIRROR_REPO );
288
289         try
290         {
291             archivaConfigurationControl.expectAndReturn( archivaConfiguration.getConfiguration(), config );
292             requestControl.expectAndReturn( request.getMethod(), "GET", 4 );
293             repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( INTERNAL_REPO ),
294                                                        internalRepo );
295             repoContentFactoryControl.expectAndReturn( repoFactory.getManagedRepositoryContent( LOCAL_MIRROR_REPO ),
296                                                        localMirrorRepo );
297             requestControl.expectAndReturn( request.getRemoteAddr(), "http://localhost:8080", 4 );
298             requestControl.expectAndReturn( request.getDavSession(), new ArchivaDavSession(), 4 );
299             repoRequestControl.expectAndReturn(
300                                                 repoRequest.isSupportFile( "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
301                                                 false, 2 );
302             repoRequestControl.expectAndReturn(
303                                                 repoRequest.isDefault( "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
304                                                 false, 2 );
305             repoRequestControl.expectAndReturn(
306                                                 repoRequest.toArtifactReference( "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ),
307                                                 null, 2 );
308             repoRequestControl.expectAndReturn(
309                                                 repoRequest.toNativePath(
310                                                                           "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
311                                                                           internalRepo ),
312                                                 new File(
313                                                           config.findManagedRepositoryById( INTERNAL_REPO ).getLocation(),
314                                                           "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
315
316             repoRequestControl.expectAndReturn(
317                                                 repoRequest.toNativePath(
318                                                                           "archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar",
319                                                                           localMirrorRepo ),
320                                                 new File(
321                                                           config.findManagedRepositoryById( LOCAL_MIRROR_REPO ).getLocation(),
322                                                           "target/test-classes/internal/org/apache/archiva/archiva/1.2-SNAPSHOT/archiva-1.2-SNAPSHOT.jar" ).getPath() );
323
324             archivaConfigurationControl.replay();
325             requestControl.replay();
326             repoContentFactoryControl.replay();
327             repoRequestControl.replay();
328
329             resourceFactory.createResource( locator, request, response );
330
331             archivaConfigurationControl.verify();
332             requestControl.verify();
333             repoContentFactoryControl.verify();
334             repoRequestControl.verify();
335
336             fail( "A DavException with 404 error code should have been thrown." );
337         }
338         catch ( DavException e )
339         {
340             assertEquals( 404, e.getErrorCode() );
341         }
342     }
343
344     class OverridingArchivaDavResourceFactory
345         extends ArchivaDavResourceFactory
346     {
347         protected boolean isAuthorized( DavServletRequest request, String repositoryId )
348             throws DavException
349         {
350             if ( RELEASES_REPO.equals( repositoryId ) )
351             {
352                 throw new UnauthorizedDavException( repositoryId,
353                                                     "You are not authenticated and authorized to access any repository." );
354             }
355             else
356             {
357                 return true;
358             }
359         }
360
361         protected String getActivePrincipal( DavServletRequest request )
362         {
363             return "guest";
364         }
365     }
366 }