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.commons.io.FileUtils;
44 import org.apache.commons.io.IOUtils;
45 import org.apache.commons.lang.StringUtils;
46 import org.apache.jackrabbit.webdav.DavSessionProvider;
47 import org.easymock.EasyMock;
48 import org.easymock.IMocksControl;
49 import org.junit.After;
50 import org.junit.Before;
51 import org.junit.Ignore;
52 import org.junit.Test;
53 import org.junit.runner.RunWith;
54 import org.springframework.context.ApplicationContext;
55 import org.springframework.mock.web.MockHttpServletRequest;
56 import org.springframework.mock.web.MockHttpServletResponse;
57 import org.springframework.mock.web.MockServletConfig;
58 import org.springframework.mock.web.MockServletContext;
59 import org.springframework.test.context.ContextConfiguration;
60 import org.springframework.web.context.ContextLoaderListener;
61 import org.springframework.web.context.WebApplicationContext;
63 import javax.inject.Inject;
64 import javax.servlet.Servlet;
65 import javax.servlet.ServletContext;
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-servlet-security-test.xml" } )
83 public class RepositoryServletSecurityTest
86 protected static final String REPOID_INTERNAL = "internal";
89 protected File repoRootInternal;
92 protected ArchivaConfiguration archivaConfiguration;
94 private DavSessionProvider davSessionProvider;
96 private IMocksControl servletAuthControl;
98 private ServletAuthenticator servletAuth;
100 private IMocksControl httpAuthControl;
102 private HttpAuthenticator httpAuth;
104 private RepositoryServlet servlet;
107 ApplicationContext applicationContext;
116 String appserverBase =
117 System.getProperty( "appserver.base", new File( "target/appserver-base" ).getAbsolutePath() );
119 File testConf = new File( "src/test/resources/repository-archiva.xml" );
120 File testConfDest = new File( appserverBase, "conf/archiva.xml" );
121 FileUtils.copyFile( testConf, testConfDest );
123 repoRootInternal = new File( appserverBase, "data/repositories/internal" );
125 Configuration config = archivaConfiguration.getConfiguration();
127 if ( !config.getManagedRepositoriesAsMap().containsKey( REPOID_INTERNAL ) )
129 config.addManagedRepository(
130 createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal ) );
132 saveConfiguration( archivaConfiguration );
134 CacheManager.getInstance().clearAll();
137 servletAuthControl = EasyMock.createControl();
139 servletAuth = servletAuthControl.createMock( ServletAuthenticator.class );
141 httpAuthControl = EasyMock.createControl();
143 httpAuth = httpAuthControl.createMock( HttpAuthenticator.class );
145 davSessionProvider = new ArchivaDavSessionProvider( servletAuth, httpAuth );
147 final MockServletContext mockServletContext = new MockServletContext();
149 WebApplicationContext webApplicationContext =
150 new AbstractRepositoryServletTestCase.TestWebapplicationContext( applicationContext, mockServletContext );
152 mockServletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
153 webApplicationContext );
155 MockServletConfig mockServletConfig = new MockServletConfig()
158 public ServletContext getServletContext()
160 return mockServletContext;
164 servlet = new RepositoryServlet();
166 servlet.init( mockServletConfig );
169 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location )
171 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
173 repo.setName( name );
174 repo.setLocation( location.getAbsolutePath() );
178 protected void saveConfiguration()
181 saveConfiguration( archivaConfiguration );
184 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
187 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
190 protected void setupCleanRepo( File repoRootDir )
193 FileUtils.deleteDirectory( repoRootDir );
194 if ( !repoRootDir.exists() )
196 repoRootDir.mkdirs();
202 public void tearDown()
206 if ( repoRootInternal.exists() )
208 FileUtils.deleteDirectory( repoRootInternal );
211 applicationContext.getBean( MavenIndexerCleaner.class ).cleanupIndex();
218 // test deploy with invalid user, and guest has no write access to repo
219 // 401 must be returned
221 public void testPutWithInvalidUserAndGuestHasNoWriteAccess()
224 setupCleanRepo( repoRootInternal );
226 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
227 assertNotNull( "artifact.jar inputstream", is );
229 servlet.setDavSessionProvider( davSessionProvider );
231 AuthenticationResult result = new AuthenticationResult();
233 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
234 anyObject( HttpServletResponse.class ) ) ).andReturn(
237 servletAuth.isAuthenticated( EasyMock.anyObject( HttpServletRequest.class ),
238 EasyMock.anyObject( AuthenticationResult.class ) );
239 EasyMock.expectLastCall().andThrow( new AuthenticationException( "Authentication error" ) );
241 servletAuth.isAuthorized( "guest", "internal", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD );
243 EasyMock.expectLastCall().andThrow( new UnauthorizedException( "'guest' has no write access to repository" ) );
245 httpAuthControl.replay();
246 servletAuthControl.replay();
247 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
248 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
249 mockHttpServletRequest.setMethod( "PUT" );
250 mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
251 mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
252 mockHttpServletRequest.setContentType( "application/octet-stream" );
254 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
256 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
258 httpAuthControl.verify();
259 servletAuthControl.verify();
261 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
264 // test deploy with invalid user, but guest has write access to repo
266 public void testPutWithInvalidUserAndGuestHasWriteAccess()
269 setupCleanRepo( repoRootInternal );
271 servlet.setDavSessionProvider( davSessionProvider );
273 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
274 archivaDavResourceFactory.setHttpAuth( httpAuth );
275 archivaDavResourceFactory.setServletAuth( servletAuth );
277 servlet.setResourceFactory( archivaDavResourceFactory );
279 AuthenticationResult result = new AuthenticationResult();
281 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
282 anyObject( HttpServletResponse.class ) ) ).andReturn(
285 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
286 anyObject( AuthenticationResult.class ) ) ).andThrow(
287 new AuthenticationException( "Authentication error" ) );
289 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
290 ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ).andReturn(
293 // ArchivaDavResourceFactory#isAuthorized()
294 SecuritySession session = new DefaultSecuritySession();
296 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
297 anyObject( HttpServletResponse.class ) ) ).andReturn(
300 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
302 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andThrow(
303 new AuthenticationException( "Authentication error" ) );
305 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( null );
307 // check if guest has write access
308 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
309 ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ).andReturn(
312 httpAuthControl.replay();
313 servletAuthControl.replay();
315 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
316 assertNotNull( "artifact.jar inputstream", is );
318 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
319 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
320 mockHttpServletRequest.setMethod( "PUT" );
321 mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
322 mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
323 mockHttpServletRequest.setContentType( "application/octet-stream" );
325 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
327 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
329 httpAuthControl.verify();
330 servletAuthControl.verify();
332 assertEquals( HttpServletResponse.SC_CREATED, mockHttpServletResponse.getStatus() );
335 // test deploy with a valid user with no write access
337 public void testPutWithValidUserWithNoWriteAccess()
340 setupCleanRepo( repoRootInternal );
342 servlet.setDavSessionProvider( davSessionProvider );
344 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
345 archivaDavResourceFactory.setHttpAuth( httpAuth );
346 archivaDavResourceFactory.setServletAuth( servletAuth );
347 servlet.setResourceFactory( archivaDavResourceFactory );
349 AuthenticationResult result = new AuthenticationResult();
351 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
352 anyObject( HttpServletResponse.class ) ) ).andReturn(
355 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
356 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
358 // ArchivaDavResourceFactory#isAuthorized()
359 SecuritySession session = new DefaultSecuritySession();
361 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
362 anyObject( HttpServletResponse.class ) ) ).andReturn(
365 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
367 EasyMock.expect( httpAuth.getSecuritySession( mockHttpServletRequest.getSession( true ) ) ).andReturn(
370 EasyMock.expect( httpAuth.getSessionUser( mockHttpServletRequest.getSession() ) ).andReturn( new SimpleUser() );
372 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
376 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
377 eq( ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ) ).andThrow(
378 new UnauthorizedException( "User not authorized" ) );
379 httpAuthControl.replay();
380 servletAuthControl.replay();
382 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
383 assertNotNull( "artifact.jar inputstream", is );
385 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
386 mockHttpServletRequest.setMethod( "PUT" );
387 mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
388 mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
389 mockHttpServletRequest.setContentType( "application/octet-stream" );
391 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
393 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
395 httpAuthControl.verify();
396 servletAuthControl.verify();
398 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
401 // test deploy with a valid user with write access
403 public void testPutWithValidUserWithWriteAccess()
406 setupCleanRepo( repoRootInternal );
407 assertTrue( repoRootInternal.exists() );
409 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
410 String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
411 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
412 assertNotNull( "artifact.jar inputstream", is );
414 servlet.setDavSessionProvider( davSessionProvider );
416 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
417 archivaDavResourceFactory.setHttpAuth( httpAuth );
418 archivaDavResourceFactory.setServletAuth( servletAuth );
420 TestAuditListener listener = new TestAuditListener();
421 archivaDavResourceFactory.addAuditListener( listener );
422 servlet.setResourceFactory( archivaDavResourceFactory );
424 AuthenticationResult result = new AuthenticationResult();
426 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
427 anyObject( HttpServletResponse.class ) ) ).andReturn(
430 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
431 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
433 User user = new SimpleUser();
434 user.setUsername( "admin" );
436 // ArchivaDavResourceFactory#isAuthorized()
437 SecuritySession session = new DefaultSecuritySession();
439 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
440 anyObject( HttpServletResponse.class ) ) ).andReturn(
443 EasyMock.expect( httpAuth.getSecuritySession( mockHttpServletRequest.getSession() ) ).andReturn( session );
445 EasyMock.expect( httpAuth.getSessionUser( mockHttpServletRequest.getSession() ) ).andReturn( user );
447 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
451 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
452 eq( ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ) ).andReturn( true );
454 httpAuthControl.replay();
455 servletAuthControl.replay();
457 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
458 mockHttpServletRequest.setMethod( "PUT" );
459 mockHttpServletRequest.setRequestURI( "/repository/internal/path/to/artifact.jar" );
460 mockHttpServletRequest.setContent( IOUtils.toByteArray( is ) );
461 mockHttpServletRequest.setContentType( "application/octet-stream" );
463 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
465 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
467 httpAuthControl.verify();
468 servletAuthControl.verify();
470 assertEquals( HttpServletResponse.SC_CREATED, mockHttpServletResponse.getStatus() );
472 assertEquals( "admin", listener.getEvents().get( 0 ).getUserId() );
475 // test get with invalid user, and guest has read access to repo
477 public void testGetWithInvalidUserAndGuestHasReadAccess()
480 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
481 String expectedArtifactContents = "dummy-commons-lang-artifact";
483 File artifactFile = new File( repoRootInternal, commonsLangJar );
484 artifactFile.getParentFile().mkdirs();
486 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
488 servlet.setDavSessionProvider( davSessionProvider );
490 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
491 archivaDavResourceFactory.setHttpAuth( httpAuth );
492 archivaDavResourceFactory.setServletAuth( servletAuth );
494 servlet.setResourceFactory( archivaDavResourceFactory );
496 AuthenticationResult result = new AuthenticationResult();
498 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
499 anyObject( HttpServletResponse.class ) ) ).andReturn(
502 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
503 anyObject( AuthenticationResult.class ) ) ).andThrow(
504 new AuthenticationException( "Authentication error" ) );
506 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
507 ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ).andReturn(
510 // ArchivaDavResourceFactory#isAuthorized()
511 SecuritySession session = new DefaultSecuritySession();
513 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
514 anyObject( HttpServletResponse.class ) ) ).andReturn(
517 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
519 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( null );
521 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
525 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
526 eq( ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ) ).andReturn( true );
527 httpAuthControl.replay();
528 servletAuthControl.replay();
530 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
531 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
532 mockHttpServletRequest.setMethod( "GET" );
533 mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
536 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
538 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
540 httpAuthControl.verify();
541 servletAuthControl.verify();
543 assertEquals( HttpServletResponse.SC_OK, mockHttpServletResponse.getStatus() );
545 assertEquals( "Expected file contents", expectedArtifactContents, mockHttpServletResponse.getContentAsString() );
548 // test get with invalid user, and guest has no read access to repo
550 public void testGetWithInvalidUserAndGuestHasNoReadAccess()
553 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
554 String expectedArtifactContents = "dummy-commons-lang-artifact";
556 File artifactFile = new File( repoRootInternal, commonsLangJar );
557 artifactFile.getParentFile().mkdirs();
559 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
561 servlet.setDavSessionProvider( davSessionProvider );
563 AuthenticationResult result = new AuthenticationResult();
565 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
566 anyObject( HttpServletResponse.class ) ) ).andReturn(
569 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
570 anyObject( AuthenticationResult.class ) ) ).andThrow(
571 new AuthenticationException( "Authentication error" ) );
573 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
574 ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ).andReturn(
576 httpAuthControl.replay();
577 servletAuthControl.replay();
579 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
580 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
581 mockHttpServletRequest.setMethod( "GET" );
582 mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
585 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
587 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
589 httpAuthControl.verify();
590 servletAuthControl.verify();
592 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
595 // test get with valid user with read access to repo
597 public void testGetWithAValidUserWithReadAccess()
600 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
601 String expectedArtifactContents = "dummy-commons-lang-artifact";
603 File artifactFile = new File( repoRootInternal, commonsLangJar );
604 artifactFile.getParentFile().mkdirs();
606 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
608 servlet.setDavSessionProvider( davSessionProvider );
610 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
611 archivaDavResourceFactory.setHttpAuth( httpAuth );
612 archivaDavResourceFactory.setServletAuth( servletAuth );
614 servlet.setResourceFactory( archivaDavResourceFactory );
616 AuthenticationResult result = new AuthenticationResult();
618 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
619 anyObject( HttpServletResponse.class ) ) ).andReturn(
622 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
623 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
624 // ArchivaDavResourceFactory#isAuthorized()
625 SecuritySession session = new DefaultSecuritySession();
627 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
628 anyObject( HttpServletResponse.class ) ) ).andReturn(
631 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
633 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( new SimpleUser() );
635 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
639 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
640 eq( ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ) ).andReturn( true );
642 httpAuthControl.replay();
643 servletAuthControl.replay();
645 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
646 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
647 mockHttpServletRequest.setMethod( "GET" );
648 mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
651 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
653 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
655 httpAuthControl.verify();
656 servletAuthControl.verify();
658 assertEquals( HttpServletResponse.SC_OK, mockHttpServletResponse.getStatus() );
659 assertEquals( "Expected file contents", expectedArtifactContents, mockHttpServletResponse.getContentAsString() );
662 // test get with valid user with no read access to repo
664 public void testGetWithAValidUserWithNoReadAccess()
667 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
668 String expectedArtifactContents = "dummy-commons-lang-artifact";
670 File artifactFile = new File( repoRootInternal, commonsLangJar );
671 artifactFile.getParentFile().mkdirs();
673 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
675 servlet.setDavSessionProvider( davSessionProvider );
677 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
678 archivaDavResourceFactory.setHttpAuth( httpAuth );
679 archivaDavResourceFactory.setServletAuth( servletAuth );
681 servlet.setResourceFactory( archivaDavResourceFactory );
683 AuthenticationResult result = new AuthenticationResult();
685 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
686 anyObject( HttpServletResponse.class ) ) ).andReturn(
689 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
690 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
692 // ArchivaDavResourceFactory#isAuthorized()
693 SecuritySession session = new DefaultSecuritySession();
695 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
696 anyObject( HttpServletResponse.class ) ) ).andReturn(
699 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
701 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( new SimpleUser() );
703 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq( result ) ) ).andReturn(
707 servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq( session ), eq( "internal" ),
708 eq( ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ) ).andThrow(
709 new UnauthorizedException( "User not authorized to read repository." ) );
710 httpAuthControl.replay();
711 servletAuthControl.replay();
713 MockHttpServletRequest mockHttpServletRequest = new MockHttpServletRequest();
714 mockHttpServletRequest.addHeader( "User-Agent", "foo" );
715 mockHttpServletRequest.setMethod( "GET" );
716 mockHttpServletRequest.setRequestURI( "/repository/internal/" + commonsLangJar );
719 MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
721 servlet.service( mockHttpServletRequest, mockHttpServletResponse );
723 httpAuthControl.verify();
724 servletAuthControl.verify();
726 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );