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 com.gargoylesoftware.htmlunit.WebRequest;
24 import com.gargoylesoftware.htmlunit.WebResponse;
25 import junit.framework.TestCase;
26 import net.sf.ehcache.CacheManager;
27 import org.apache.archiva.configuration.ArchivaConfiguration;
28 import org.apache.archiva.configuration.Configuration;
29 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
30 import org.apache.archiva.redback.authentication.AuthenticationException;
31 import org.apache.archiva.redback.authentication.AuthenticationResult;
32 import org.apache.archiva.redback.authorization.UnauthorizedException;
33 import org.apache.archiva.redback.integration.filter.authentication.HttpAuthenticator;
34 import org.apache.archiva.redback.system.DefaultSecuritySession;
35 import org.apache.archiva.redback.system.SecuritySession;
36 import org.apache.archiva.redback.users.User;
37 import org.apache.archiva.redback.users.memory.SimpleUser;
38 import org.apache.archiva.repository.audit.TestAuditListener;
39 import org.apache.archiva.security.ServletAuthenticator;
40 import org.apache.archiva.security.common.ArchivaRoleConstants;
41 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
42 import org.apache.archiva.webdav.util.MavenIndexerCleaner;
43 import org.apache.catalina.Container;
44 import org.apache.catalina.core.StandardContext;
45 import org.apache.catalina.deploy.ApplicationParameter;
46 import org.apache.catalina.startup.Tomcat;
47 import org.apache.commons.io.FileUtils;
48 import org.apache.commons.io.IOUtils;
49 import org.apache.commons.lang.StringUtils;
50 import org.apache.jackrabbit.webdav.DavSessionProvider;
51 import org.easymock.EasyMock;
52 import org.easymock.IMocksControl;
53 import org.junit.After;
54 import org.junit.Before;
55 import org.junit.Ignore;
56 import org.junit.Test;
57 import org.junit.runner.RunWith;
58 import org.springframework.context.ApplicationContext;
59 import org.springframework.mock.web.MockHttpServletRequest;
60 import org.springframework.mock.web.MockHttpServletResponse;
61 import org.springframework.test.context.ContextConfiguration;
62 import org.springframework.web.context.ContextLoaderListener;
64 import javax.inject.Inject;
65 import javax.servlet.Servlet;
66 import javax.servlet.http.HttpServletRequest;
67 import javax.servlet.http.HttpServletResponse;
68 import javax.servlet.http.HttpSession;
70 import java.io.IOException;
71 import java.io.InputStream;
72 import java.nio.charset.Charset;
74 import static org.easymock.EasyMock.anyObject;
75 import static org.easymock.EasyMock.eq;
78 * RepositoryServletSecurityTest Test the flow of the authentication and authorization checks. This does not necessarily
79 * perform redback security checking.
81 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
82 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
83 public class RepositoryServletSecurityTest
86 protected static final String REPOID_INTERNAL = "internal";
89 protected File repoRootInternal;
91 protected ArchivaConfiguration archivaConfiguration;
93 private DavSessionProvider davSessionProvider;
95 private IMocksControl servletAuthControl;
97 private ServletAuthenticator servletAuth;
99 private IMocksControl httpAuthControl;
101 private HttpAuthenticator httpAuth;
103 private RepositoryServlet servlet;
105 protected Tomcat tomcat;
107 protected static int port;
109 StandardContext context;
112 ApplicationContext applicationContext;
120 String appserverBase =
121 System.getProperty( "appserver.base", new File( "target/appserver-base" ).getAbsolutePath() );
123 File testConf = new File( "src/test/resources/repository-archiva.xml" );
124 File testConfDest = new File( appserverBase, "conf/archiva.xml" );
125 FileUtils.copyFile( testConf, testConfDest );
127 repoRootInternal = new File( appserverBase, "data/repositories/internal" );
129 archivaConfiguration = applicationContext.getBean( ArchivaConfiguration.class );
130 Configuration config = archivaConfiguration.getConfiguration();
132 if ( !config.getManagedRepositoriesAsMap().containsKey( REPOID_INTERNAL ) )
134 config.addManagedRepository(
135 createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal ) );
137 saveConfiguration( archivaConfiguration );
139 CacheManager.getInstance().clearAll();
141 tomcat = new Tomcat();
142 tomcat.setBaseDir( System.getProperty( "java.io.tmpdir" ) );
145 context = StandardContext.class.cast( tomcat.addContext( "", System.getProperty( "java.io.tmpdir" ) ) );
147 ApplicationParameter applicationParameter = new ApplicationParameter();
148 applicationParameter.setName( "contextConfigLocation" );
149 applicationParameter.setValue( getSpringConfigLocation() );
150 context.addApplicationParameter( applicationParameter );
152 context.addApplicationListener( ContextLoaderListener.class.getName() );
154 context.addApplicationListener( MavenIndexerCleaner.class.getName() );
156 Tomcat.addServlet( context, "repository", new UnauthenticatedRepositoryServlet() );
157 context.addServletMapping( "/repository/*", "repository" );
161 this.port = tomcat.getConnector().getLocalPort();
163 servletAuthControl = EasyMock.createControl();
165 servletAuth = servletAuthControl.createMock( ServletAuthenticator.class );
167 httpAuthControl = EasyMock.createControl();
169 httpAuth = httpAuthControl.createMock( HttpAuthenticator.class );
171 davSessionProvider = new ArchivaDavSessionProvider( servletAuth, httpAuth );
173 // FIXME use mock to avoid starting Tomcat
174 //RepositoryServlet repositoryServlet = new RepositoryServlet();
175 //MockServletConfig mockServletConfig = new MockServletConfig();
177 //MockServletContext mockServletContext = new MockServletContext( );
180 //repositoryServlet.init( mockServletConfig );
182 servlet = RepositoryServlet.class.cast( findServlet( "repository" ) );
185 protected String getSpringConfigLocation()
187 return "classpath*:/META-INF/spring-context.xml,classpath*:/spring-context-servlet-security-test.xml";
190 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location )
192 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
194 repo.setName( name );
195 repo.setLocation( location.getAbsolutePath() );
199 protected void saveConfiguration()
202 saveConfiguration( archivaConfiguration );
205 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
208 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
211 protected void setupCleanRepo( File repoRootDir )
214 FileUtils.deleteDirectory( repoRootDir );
215 if ( !repoRootDir.exists() )
217 repoRootDir.mkdirs();
223 public void tearDown()
227 if ( repoRootInternal.exists() )
229 FileUtils.deleteDirectory( repoRootInternal );
234 if ( this.tomcat != null )
242 protected Servlet findServlet( String name )
245 Container[] childs = context.findChildren();
246 for ( Container container : childs )
248 if ( StringUtils.equals( container.getName(), name ) )
250 Tomcat.ExistingStandardWrapper esw = Tomcat.ExistingStandardWrapper.class.cast( container );
251 Servlet servlet = esw.loadServlet();
259 // test deploy with invalid user, and guest has no write access to repo
260 // 401 must be returned
262 public void testPutWithInvalidUserAndGuestHasNoWriteAccess()
265 setupCleanRepo( repoRootInternal );
267 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
268 assertNotNull( "artifact.jar inputstream", is );
270 servlet.setDavSessionProvider( davSessionProvider );
272 AuthenticationResult result = new AuthenticationResult();
274 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
275 anyObject( HttpServletResponse.class ) ) ).andReturn(
278 servletAuth.isAuthenticated( EasyMock.anyObject( HttpServletRequest.class ),
279 EasyMock.anyObject( AuthenticationResult.class ) );
280 EasyMock.expectLastCall().andThrow( new AuthenticationException( "Authentication error" ) );
282 servletAuth.isAuthorized( "guest", "internal", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD );
284 EasyMock.expectLastCall().andThrow( new UnauthorizedException( "'guest' has no write access to repository" ) );
286 httpAuthControl.replay();
287 servletAuthControl.replay();
288 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
289 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
290 mockHttpServletRequest.setMethod( "PUT" );
291 mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
292 mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
293 mockHttpServletRequest.setContentType( "application/octet-stream" );
295 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
297 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
299 httpAuthControl.verify();
300 servletAuthControl.verify();
302 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
305 // test deploy with invalid user, but guest has write access to repo
307 public void testPutWithInvalidUserAndGuestHasWriteAccess()
310 setupCleanRepo( repoRootInternal );
312 servlet.setDavSessionProvider( davSessionProvider );
314 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
315 archivaDavResourceFactory.setHttpAuth( httpAuth );
316 archivaDavResourceFactory.setServletAuth( servletAuth );
318 servlet.setResourceFactory( archivaDavResourceFactory );
320 AuthenticationResult result = new AuthenticationResult();
322 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
323 anyObject( HttpServletResponse.class ) ) ).andReturn(
326 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
327 anyObject( AuthenticationResult.class ) ) ).andThrow(
328 new AuthenticationException( "Authentication error" ) );
330 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
331 ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ).andReturn(
334 // ArchivaDavResourceFactory#isAuthorized()
335 SecuritySession session = new DefaultSecuritySession();
337 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
338 anyObject( HttpServletResponse.class ) ) ).andReturn(
341 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
343 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andThrow(
344 new AuthenticationException( "Authentication error" ) );
346 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( null );
348 // check if guest has write access
349 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
350 ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ).andReturn(
353 httpAuthControl.replay();
354 servletAuthControl.replay();
356 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
357 assertNotNull( "artifact.jar inputstream", is );
359 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
360 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
361 mockHttpServletRequest.setMethod( "PUT" );
362 mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
363 mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
364 mockHttpServletRequest.setContentType( "application/octet-stream" );
366 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
368 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
370 httpAuthControl.verify();
371 servletAuthControl.verify();
373 assertEquals( HttpServletResponse.SC_CREATED, mockHttpServletResponse.getStatus() );
376 // test deploy with a valid user with no write access
378 public void testPutWithValidUserWithNoWriteAccess()
381 setupCleanRepo( repoRootInternal );
383 servlet.setDavSessionProvider( davSessionProvider );
385 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
386 archivaDavResourceFactory.setHttpAuth( httpAuth );
387 archivaDavResourceFactory.setServletAuth( servletAuth );
388 servlet.setResourceFactory( archivaDavResourceFactory );
390 AuthenticationResult result = new AuthenticationResult();
392 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
393 anyObject( HttpServletResponse.class ) ) ).andReturn(
396 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
397 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
399 // ArchivaDavResourceFactory#isAuthorized()
400 SecuritySession session = new DefaultSecuritySession();
402 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
403 anyObject( HttpServletResponse.class ) ) ).andReturn(
406 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
408 EasyMock.expect( httpAuth.getSecuritySession( mockHttpServletRequest.getSession( true ) ) ).andReturn(
411 EasyMock.expect( httpAuth.getSessionUser( mockHttpServletRequest.getSession() ) ).andReturn( new SimpleUser() );
413 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
417 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
418 eq( ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ) ).andThrow(
419 new UnauthorizedException( "User not authorized" ) );
420 httpAuthControl.replay();
421 servletAuthControl.replay();
423 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
424 assertNotNull( "artifact.jar inputstream", is );
426 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
427 mockHttpServletRequest.setMethod( "PUT" );
428 mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
429 mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
430 mockHttpServletRequest.setContentType( "application/octet-stream" );
432 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
434 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
436 httpAuthControl.verify();
437 servletAuthControl.verify();
439 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
442 // test deploy with a valid user with write access
444 public void testPutWithValidUserWithWriteAccess()
447 setupCleanRepo( repoRootInternal );
448 assertTrue( repoRootInternal.exists() );
450 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
451 String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
452 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
453 assertNotNull( "artifact.jar inputstream", is );
455 servlet.setDavSessionProvider( davSessionProvider );
457 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
458 archivaDavResourceFactory.setHttpAuth( httpAuth );
459 archivaDavResourceFactory.setServletAuth( servletAuth );
461 TestAuditListener listener = new TestAuditListener();
462 archivaDavResourceFactory.addAuditListener( listener );
463 servlet.setResourceFactory( archivaDavResourceFactory );
465 AuthenticationResult result = new AuthenticationResult();
467 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
468 anyObject( HttpServletResponse.class ) ) ).andReturn(
471 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
472 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
474 User user = new SimpleUser();
475 user.setUsername( "admin" );
477 // ArchivaDavResourceFactory#isAuthorized()
478 SecuritySession session = new DefaultSecuritySession();
480 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
481 anyObject( HttpServletResponse.class ) ) ).andReturn(
484 EasyMock.expect( httpAuth.getSecuritySession( mockHttpServletRequest.getSession() ) ).andReturn( session );
486 EasyMock.expect( httpAuth.getSessionUser( mockHttpServletRequest.getSession() ) ).andReturn( user );
488 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
492 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
493 eq( ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ) ).andReturn( true );
495 httpAuthControl.replay();
496 servletAuthControl.replay();
498 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
499 mockHttpServletRequest.setMethod( "PUT" );
500 mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
501 mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
502 mockHttpServletRequest.setContentType( "application/octet-stream" );
504 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
506 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
508 httpAuthControl.verify();
509 servletAuthControl.verify();
511 assertEquals( HttpServletResponse.SC_CREATED, mockHttpServletResponse.getStatus() );
513 assertEquals( "admin", listener.getEvents().get( 0 ).getUserId() );
516 // test get with invalid user, and guest has read access to repo
518 public void testGetWithInvalidUserAndGuestHasReadAccess()
521 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
522 String expectedArtifactContents = "dummy-commons-lang-artifact";
524 File artifactFile = new File( repoRootInternal, commonsLangJar );
525 artifactFile.getParentFile().mkdirs();
527 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
529 servlet.setDavSessionProvider( davSessionProvider );
531 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
532 archivaDavResourceFactory.setHttpAuth( httpAuth );
533 archivaDavResourceFactory.setServletAuth( servletAuth );
535 servlet.setResourceFactory( archivaDavResourceFactory );
537 AuthenticationResult result = new AuthenticationResult();
539 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
540 anyObject( HttpServletResponse.class ) ) ).andReturn(
543 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
544 anyObject( AuthenticationResult.class ) ) ).andThrow(
545 new AuthenticationException( "Authentication error" ) );
547 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
548 ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ).andReturn(
551 // ArchivaDavResourceFactory#isAuthorized()
552 SecuritySession session = new DefaultSecuritySession();
554 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
555 anyObject( HttpServletResponse.class ) ) ).andReturn(
558 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
560 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( null );
562 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
566 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
567 eq( ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ) ).andReturn( true );
568 httpAuthControl.replay();
569 servletAuthControl.replay();
571 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
572 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
573 mockHttpServletRequest.setMethod( "GET" );
574 mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
577 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
579 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
581 httpAuthControl.verify();
582 servletAuthControl.verify();
584 assertEquals( HttpServletResponse.SC_OK, mockHttpServletResponse.getStatus() );
586 assertEquals( "Expected file contents", expectedArtifactContents, mockHttpServletResponse.getContentAsString() );
589 // test get with invalid user, and guest has no read access to repo
591 public void testGetWithInvalidUserAndGuestHasNoReadAccess()
594 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
595 String expectedArtifactContents = "dummy-commons-lang-artifact";
597 File artifactFile = new File( repoRootInternal, commonsLangJar );
598 artifactFile.getParentFile().mkdirs();
600 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
602 servlet.setDavSessionProvider( davSessionProvider );
604 AuthenticationResult result = new AuthenticationResult();
606 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
607 anyObject( HttpServletResponse.class ) ) ).andReturn(
610 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
611 anyObject( AuthenticationResult.class ) ) ).andThrow(
612 new AuthenticationException( "Authentication error" ) );
614 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
615 ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ).andReturn(
617 httpAuthControl.replay();
618 servletAuthControl.replay();
620 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
621 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
622 mockHttpServletRequest.setMethod( "GET" );
623 mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
626 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
628 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
630 httpAuthControl.verify();
631 servletAuthControl.verify();
633 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
636 // test get with valid user with read access to repo
638 public void testGetWithAValidUserWithReadAccess()
641 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
642 String expectedArtifactContents = "dummy-commons-lang-artifact";
644 File artifactFile = new File( repoRootInternal, commonsLangJar );
645 artifactFile.getParentFile().mkdirs();
647 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
649 servlet.setDavSessionProvider( davSessionProvider );
651 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
652 archivaDavResourceFactory.setHttpAuth( httpAuth );
653 archivaDavResourceFactory.setServletAuth( servletAuth );
655 servlet.setResourceFactory( archivaDavResourceFactory );
657 AuthenticationResult result = new AuthenticationResult();
659 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
660 anyObject( HttpServletResponse.class ) ) ).andReturn(
663 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
664 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
665 // ArchivaDavResourceFactory#isAuthorized()
666 SecuritySession session = new DefaultSecuritySession();
668 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
669 anyObject( HttpServletResponse.class ) ) ).andReturn(
672 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
674 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( new SimpleUser() );
676 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
680 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
681 eq( ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ) ).andReturn( true );
683 httpAuthControl.replay();
684 servletAuthControl.replay();
686 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
687 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
688 mockHttpServletRequest.setMethod( "GET" );
689 mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
692 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
694 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
696 httpAuthControl.verify();
697 servletAuthControl.verify();
699 assertEquals( HttpServletResponse.SC_OK, mockHttpServletResponse.getStatus() );
700 assertEquals( "Expected file contents", expectedArtifactContents, mockHttpServletResponse.getContentAsString() );
703 // test get with valid user with no read access to repo
705 public void testGetWithAValidUserWithNoReadAccess()
708 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
709 String expectedArtifactContents = "dummy-commons-lang-artifact";
711 File artifactFile = new File( repoRootInternal, commonsLangJar );
712 artifactFile.getParentFile().mkdirs();
714 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
716 servlet.setDavSessionProvider( davSessionProvider );
718 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
719 archivaDavResourceFactory.setHttpAuth( httpAuth );
720 archivaDavResourceFactory.setServletAuth( servletAuth );
722 servlet.setResourceFactory( archivaDavResourceFactory );
724 AuthenticationResult result = new AuthenticationResult();
726 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
727 anyObject( HttpServletResponse.class ) ) ).andReturn(
730 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
731 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
733 // ArchivaDavResourceFactory#isAuthorized()
734 SecuritySession session = new DefaultSecuritySession();
736 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
737 anyObject( HttpServletResponse.class ) ) ).andReturn(
740 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
742 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( new SimpleUser() );
744 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
748 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
749 eq( ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ) ).andThrow(
750 new UnauthorizedException( "User not authorized to read repository." ) );
751 httpAuthControl.replay();
752 servletAuthControl.replay();
754 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
755 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
756 mockHttpServletRequest.setMethod( "GET" );
757 mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
760 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
762 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
764 httpAuthControl.verify();
765 servletAuthControl.verify();
767 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );