1 package org.apache.archiva.webdav;
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 junit.framework.TestCase;
24 import net.sf.ehcache.CacheManager;
25 import org.apache.archiva.configuration.ArchivaConfiguration;
26 import org.apache.archiva.configuration.Configuration;
27 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
28 import org.apache.archiva.redback.authentication.AuthenticationException;
29 import org.apache.archiva.redback.authentication.AuthenticationResult;
30 import org.apache.archiva.redback.authorization.UnauthorizedException;
31 import org.apache.archiva.redback.integration.filter.authentication.HttpAuthenticator;
32 import org.apache.archiva.redback.system.DefaultSecuritySession;
33 import org.apache.archiva.redback.system.SecuritySession;
34 import org.apache.archiva.redback.users.User;
35 import org.apache.archiva.redback.users.memory.SimpleUser;
36 import org.apache.archiva.repository.audit.TestAuditListener;
37 import org.apache.archiva.security.ServletAuthenticator;
38 import org.apache.archiva.security.common.ArchivaRoleConstants;
39 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
40 import org.apache.archiva.webdav.util.MavenIndexerCleaner;
41 import org.apache.commons.io.FileUtils;
42 import org.apache.commons.io.IOUtils;
43 import org.apache.jackrabbit.webdav.DavSessionProvider;
44 import org.easymock.EasyMock;
45 import org.easymock.IMocksControl;
46 import org.junit.After;
47 import org.junit.Before;
48 import org.junit.Rule;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.springframework.context.ApplicationContext;
52 import org.springframework.mock.web.MockHttpServletRequest;
53 import org.springframework.mock.web.MockHttpServletResponse;
54 import org.springframework.mock.web.MockServletConfig;
55 import org.springframework.mock.web.MockServletContext;
56 import org.springframework.test.context.ContextConfiguration;
57 import org.springframework.web.context.WebApplicationContext;
59 import javax.inject.Inject;
60 import javax.servlet.ServletContext;
61 import javax.servlet.http.HttpServletRequest;
62 import javax.servlet.http.HttpServletResponse;
63 import javax.servlet.http.HttpSession;
64 import java.io.InputStream;
65 import java.nio.charset.Charset;
66 import java.nio.file.Files;
67 import java.nio.file.Path;
68 import java.nio.file.Paths;
69 import java.util.ArrayList;
70 import java.util.List;
72 import static org.easymock.EasyMock.anyObject;
73 import static org.easymock.EasyMock.eq;
76 * RepositoryServletSecurityTest Test the flow of the authentication and authorization checks. This does not necessarily
77 * perform redback security checking.
79 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
80 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context-servlet-security-test.xml" } )
81 public class RepositoryServletSecurityTest
84 protected static final String REPOID_INTERNAL = "internal";
87 protected ArchivaConfiguration archivaConfiguration;
89 private DavSessionProvider davSessionProvider;
91 private IMocksControl servletAuthControl;
93 private ServletAuthenticator servletAuth;
95 private IMocksControl httpAuthControl;
97 private HttpAuthenticator httpAuth;
99 private RepositoryServlet servlet;
102 ApplicationContext applicationContext;
106 public ArchivaTemporaryFolderRule repoRootInternal = new ArchivaTemporaryFolderRule();
116 String appserverBase =
117 System.getProperty( "appserver.base", Paths.get( "target/appserver-base" ).toAbsolutePath().toString() );
119 Path testConf = Paths.get( "src/test/resources/repository-archiva.xml" );
120 Path testConfDest = Paths.get(appserverBase, "conf/archiva.xml" );
121 FileUtils.copyFile( testConf.toFile(), testConfDest.toFile() );
125 Configuration config = archivaConfiguration.getConfiguration();
126 // clear managed repository
127 List<ManagedRepositoryConfiguration> f1 = new ArrayList<>(config.getManagedRepositories());
128 for (ManagedRepositoryConfiguration f: f1 ) {
129 config.removeManagedRepository(f);
131 assertEquals(0,config.getManagedRepositories().size());
133 config.addManagedRepository(
134 createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal.getRoot() ) );
136 saveConfiguration( archivaConfiguration );
138 CacheManager.getInstance().clearAll();
141 servletAuthControl = EasyMock.createControl();
143 servletAuth = servletAuthControl.createMock( ServletAuthenticator.class );
145 httpAuthControl = EasyMock.createControl();
147 httpAuth = httpAuthControl.createMock( HttpAuthenticator.class );
149 davSessionProvider = new ArchivaDavSessionProvider( servletAuth, httpAuth );
151 final MockServletContext mockServletContext = new MockServletContext();
153 WebApplicationContext webApplicationContext =
154 new AbstractRepositoryServletTestCase.TestWebapplicationContext( applicationContext, mockServletContext );
156 mockServletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
157 webApplicationContext );
159 MockServletConfig mockServletConfig = new MockServletConfig()
162 public ServletContext getServletContext()
164 return mockServletContext;
168 servlet = new RepositoryServlet();
170 servlet.init( mockServletConfig );
173 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, Path location )
175 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
177 repo.setName( name );
178 repo.setLocation( location.toAbsolutePath().toString() );
182 /*protected void saveConfiguration()
185 saveConfiguration( archivaConfiguration );
188 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
191 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
194 /*protected void setupCleanRepo( File repoRootDir )
201 public void tearDown()
205 /* if ( repoRootInternal.exists() )
207 FileUtils.deleteDirectory( repoRootInternal );
210 applicationContext.getBean( MavenIndexerCleaner.class ).cleanupIndex();
217 // test deploy with invalid user, and guest has no write access to repo
218 // 401 must be returned
220 public void testPutWithInvalidUserAndGuestHasNoWriteAccess()
224 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
225 assertNotNull( "artifact.jar inputstream", is );
227 servlet.setDavSessionProvider( davSessionProvider );
229 AuthenticationResult result = new AuthenticationResult();
231 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
232 anyObject( HttpServletResponse.class ) ) ).andReturn(
235 servletAuth.isAuthenticated( EasyMock.anyObject( HttpServletRequest.class ),
236 EasyMock.anyObject( AuthenticationResult.class ) );
237 EasyMock.expectLastCall().andThrow( new AuthenticationException( "Authentication error" ) );
239 servletAuth.isAuthorized( "guest", "internal", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD );
241 EasyMock.expectLastCall().andThrow( new UnauthorizedException( "'guest' has no write access to repository" ) );
243 httpAuthControl.replay();
244 servletAuthControl.replay();
245 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
246 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
247 mockHttpServletRequest.setMethod( "PUT" );
248 mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
249 mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
250 mockHttpServletRequest.setContentType( "application/octet-stream" );
252 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
254 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
256 httpAuthControl.verify();
257 servletAuthControl.verify();
259 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
262 // test deploy with invalid user, but guest has write access to repo
264 public void testPutWithInvalidUserAndGuestHasWriteAccess()
268 servlet.setDavSessionProvider( davSessionProvider );
270 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
271 archivaDavResourceFactory.setHttpAuth( httpAuth );
272 archivaDavResourceFactory.setServletAuth( servletAuth );
274 servlet.setResourceFactory( archivaDavResourceFactory );
276 AuthenticationResult result = new AuthenticationResult();
278 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
279 anyObject( HttpServletResponse.class ) ) ).andReturn(
282 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
283 anyObject( AuthenticationResult.class ) ) ).andThrow(
284 new AuthenticationException( "Authentication error" ) );
286 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
287 ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ).andReturn(
290 // ArchivaDavResourceFactory#isAuthorized()
291 SecuritySession session = new DefaultSecuritySession();
293 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
294 anyObject( HttpServletResponse.class ) ) ).andReturn(
297 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
299 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andThrow(
300 new AuthenticationException( "Authentication error" ) );
302 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( null );
304 // check if guest has write access
305 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
306 ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ).andReturn(
309 httpAuthControl.replay();
310 servletAuthControl.replay();
312 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
313 assertNotNull( "artifact.jar inputstream", is );
315 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
316 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
317 mockHttpServletRequest.setMethod( "PUT" );
318 mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
319 mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
320 mockHttpServletRequest.setContentType( "application/octet-stream" );
322 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
324 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
326 httpAuthControl.verify();
327 servletAuthControl.verify();
329 assertEquals( HttpServletResponse.SC_CREATED, mockHttpServletResponse.getStatus() );
332 // test deploy with a valid user with no write access
334 public void testPutWithValidUserWithNoWriteAccess()
338 servlet.setDavSessionProvider( davSessionProvider );
340 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
341 archivaDavResourceFactory.setHttpAuth( httpAuth );
342 archivaDavResourceFactory.setServletAuth( servletAuth );
343 servlet.setResourceFactory( archivaDavResourceFactory );
345 AuthenticationResult result = new AuthenticationResult();
347 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
348 anyObject( HttpServletResponse.class ) ) ).andReturn(
351 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
352 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
354 // ArchivaDavResourceFactory#isAuthorized()
355 SecuritySession session = new DefaultSecuritySession();
357 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
358 anyObject( HttpServletResponse.class ) ) ).andReturn(
361 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
363 EasyMock.expect( httpAuth.getSecuritySession( mockHttpServletRequest.getSession( true ) ) ).andReturn(
366 EasyMock.expect( httpAuth.getSessionUser( mockHttpServletRequest.getSession() ) ).andReturn( new SimpleUser() );
368 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
372 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
373 eq( ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ) ).andThrow(
374 new UnauthorizedException( "User not authorized" ) );
375 httpAuthControl.replay();
376 servletAuthControl.replay();
378 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
379 assertNotNull( "artifact.jar inputstream", is );
381 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
382 mockHttpServletRequest.setMethod( "PUT" );
383 mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
384 mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
385 mockHttpServletRequest.setContentType( "application/octet-stream" );
387 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
389 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
391 httpAuthControl.verify();
392 servletAuthControl.verify();
394 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
397 // test deploy with a valid user with write access
399 public void testPutWithValidUserWithWriteAccess()
402 assertTrue( Files.exists(repoRootInternal.getRoot()) );
404 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
405 String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
406 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
407 assertNotNull( "artifact.jar inputstream", is );
409 servlet.setDavSessionProvider( davSessionProvider );
411 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
412 archivaDavResourceFactory.setHttpAuth( httpAuth );
413 archivaDavResourceFactory.setServletAuth( servletAuth );
415 TestAuditListener listener = new TestAuditListener();
416 archivaDavResourceFactory.addAuditListener( listener );
417 servlet.setResourceFactory( archivaDavResourceFactory );
419 AuthenticationResult result = new AuthenticationResult();
421 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
422 anyObject( HttpServletResponse.class ) ) ).andReturn(
425 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
426 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
428 User user = new SimpleUser();
429 user.setUsername( "admin" );
431 // ArchivaDavResourceFactory#isAuthorized()
432 SecuritySession session = new DefaultSecuritySession();
434 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
435 anyObject( HttpServletResponse.class ) ) ).andReturn(
438 EasyMock.expect( httpAuth.getSecuritySession( mockHttpServletRequest.getSession() ) ).andReturn( session );
440 EasyMock.expect( httpAuth.getSessionUser( mockHttpServletRequest.getSession() ) ).andReturn( user );
442 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
446 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
447 eq( ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ) ).andReturn( true );
449 httpAuthControl.replay();
450 servletAuthControl.replay();
452 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
453 mockHttpServletRequest.setMethod( "PUT" );
454 mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
455 mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
456 mockHttpServletRequest.setContentType( "application/octet-stream" );
458 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
460 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
462 httpAuthControl.verify();
463 servletAuthControl.verify();
465 assertEquals( HttpServletResponse.SC_CREATED, mockHttpServletResponse.getStatus() );
467 assertEquals( "admin", listener.getEvents().get( 0 ).getUserId() );
470 // test get with invalid user, and guest has read access to repo
472 public void testGetWithInvalidUserAndGuestHasReadAccess()
475 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
476 String expectedArtifactContents = "dummy-commons-lang-artifact";
478 Path artifactFile = repoRootInternal.getRoot().resolve( commonsLangJar );
479 Files.createDirectories(artifactFile.getParent());
481 org.apache.archiva.common.utils.FileUtils.writeStringToFile( artifactFile, Charset.defaultCharset() , expectedArtifactContents);
483 servlet.setDavSessionProvider( davSessionProvider );
485 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
486 archivaDavResourceFactory.setHttpAuth( httpAuth );
487 archivaDavResourceFactory.setServletAuth( servletAuth );
489 servlet.setResourceFactory( archivaDavResourceFactory );
491 AuthenticationResult result = new AuthenticationResult();
493 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
494 anyObject( HttpServletResponse.class ) ) ).andReturn(
497 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
498 anyObject( AuthenticationResult.class ) ) ).andThrow(
499 new AuthenticationException( "Authentication error" ) );
501 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
502 ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ).andReturn(
505 // ArchivaDavResourceFactory#isAuthorized()
506 SecuritySession session = new DefaultSecuritySession();
508 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
509 anyObject( HttpServletResponse.class ) ) ).andReturn(
512 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
514 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( null );
516 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
520 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
521 eq( ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ) ).andReturn( true );
522 httpAuthControl.replay();
523 servletAuthControl.replay();
525 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
526 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
527 mockHttpServletRequest.setMethod( "GET" );
528 mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
530 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
532 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
534 httpAuthControl.verify();
535 servletAuthControl.verify();
537 assertEquals( HttpServletResponse.SC_OK, mockHttpServletResponse.getStatus() );
539 assertEquals( "Expected file contents", expectedArtifactContents, mockHttpServletResponse.getContentAsString() );
542 // test get with invalid user, and guest has no read access to repo
544 public void testGetWithInvalidUserAndGuestHasNoReadAccess()
547 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
548 String expectedArtifactContents = "dummy-commons-lang-artifact";
550 Path artifactFile = repoRootInternal.getRoot().resolve( commonsLangJar );
551 Files.createDirectories(artifactFile.getParent());
553 org.apache.archiva.common.utils.FileUtils.writeStringToFile( artifactFile, Charset.defaultCharset() , expectedArtifactContents);
555 servlet.setDavSessionProvider( davSessionProvider );
557 AuthenticationResult result = new AuthenticationResult();
559 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
560 anyObject( HttpServletResponse.class ) ) ).andReturn(
563 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
564 anyObject( AuthenticationResult.class ) ) ).andThrow(
565 new AuthenticationException( "Authentication error" ) );
567 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
568 ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ).andReturn(
570 httpAuthControl.replay();
571 servletAuthControl.replay();
573 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
574 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
575 mockHttpServletRequest.setMethod( "GET" );
576 mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
578 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
580 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
582 httpAuthControl.verify();
583 servletAuthControl.verify();
585 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
588 // test get with valid user with read access to repo
590 public void testGetWithAValidUserWithReadAccess()
593 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
594 String expectedArtifactContents = "dummy-commons-lang-artifact";
596 Path artifactFile = repoRootInternal.getRoot().resolve( commonsLangJar );
597 Files.createDirectories(artifactFile.getParent());
599 org.apache.archiva.common.utils.FileUtils.writeStringToFile( artifactFile, Charset.defaultCharset() , expectedArtifactContents);
601 servlet.setDavSessionProvider( davSessionProvider );
603 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
604 archivaDavResourceFactory.setHttpAuth( httpAuth );
605 archivaDavResourceFactory.setServletAuth( servletAuth );
607 servlet.setResourceFactory( archivaDavResourceFactory );
609 AuthenticationResult result = new AuthenticationResult();
611 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
612 anyObject( HttpServletResponse.class ) ) ).andReturn(
615 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
616 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
617 // ArchivaDavResourceFactory#isAuthorized()
618 SecuritySession session = new DefaultSecuritySession();
620 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
621 anyObject( HttpServletResponse.class ) ) ).andReturn(
624 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
626 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( new SimpleUser() );
628 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
632 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
633 eq( ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ) ).andReturn( true );
635 httpAuthControl.replay();
636 servletAuthControl.replay();
638 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
639 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
640 mockHttpServletRequest.setMethod( "GET" );
641 mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
643 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
645 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
647 httpAuthControl.verify();
648 servletAuthControl.verify();
650 assertEquals( HttpServletResponse.SC_OK, mockHttpServletResponse.getStatus() );
651 assertEquals( "Expected file contents", expectedArtifactContents, mockHttpServletResponse.getContentAsString() );
654 // test get with valid user with no read access to repo
656 public void testGetWithAValidUserWithNoReadAccess()
659 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
660 String expectedArtifactContents = "dummy-commons-lang-artifact";
662 Path artifactFile = repoRootInternal.getRoot().resolve( commonsLangJar );
663 Files.createDirectories(artifactFile.getParent());
665 org.apache.archiva.common.utils.FileUtils.writeStringToFile( artifactFile, Charset.defaultCharset() , expectedArtifactContents);
667 servlet.setDavSessionProvider( davSessionProvider );
669 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
670 archivaDavResourceFactory.setHttpAuth( httpAuth );
671 archivaDavResourceFactory.setServletAuth( servletAuth );
673 servlet.setResourceFactory( archivaDavResourceFactory );
675 AuthenticationResult result = new AuthenticationResult();
677 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
678 anyObject( HttpServletResponse.class ) ) ).andReturn(
681 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
682 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
684 // ArchivaDavResourceFactory#isAuthorized()
685 SecuritySession session = new DefaultSecuritySession();
687 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
688 anyObject( HttpServletResponse.class ) ) ).andReturn(
691 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
693 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( new SimpleUser() );
695 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
699 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
700 eq( ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ) ).andThrow(
701 new UnauthorizedException( "User not authorized to read repository." ) );
702 httpAuthControl.replay();
703 servletAuthControl.replay();
705 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
706 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
707 mockHttpServletRequest.setMethod( "GET" );
708 mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
711 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
713 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
715 httpAuthControl.verify();
716 servletAuthControl.verify();
718 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );