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.Context;
44 import org.apache.catalina.deploy.ApplicationParameter;
45 import org.apache.catalina.startup.Tomcat;
46 import org.apache.commons.io.FileUtils;
47 import org.apache.jackrabbit.webdav.DavSessionProvider;
48 import org.easymock.EasyMock;
49 import static org.easymock.EasyMock.*;
50 import org.easymock.IMocksControl;
51 import org.junit.After;
52 import org.junit.Before;
53 import org.junit.Ignore;
54 import org.junit.Test;
55 import org.junit.runner.RunWith;
56 import org.springframework.context.ApplicationContext;
57 import org.springframework.test.context.ContextConfiguration;
58 import org.springframework.web.context.ContextLoaderListener;
60 import javax.inject.Inject;
61 import javax.servlet.http.HttpServletRequest;
62 import javax.servlet.http.HttpServletResponse;
63 import javax.servlet.http.HttpSession;
65 import java.io.IOException;
66 import java.io.InputStream;
67 import java.nio.charset.Charset;
70 * RepositoryServletSecurityTest Test the flow of the authentication and authorization checks. This does not necessarily
71 * perform redback security checking.
73 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
74 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
75 public class RepositoryServletSecurityTest
78 protected static final String REPOID_INTERNAL = "internal";
81 protected File repoRootInternal;
83 protected ArchivaConfiguration archivaConfiguration;
85 private DavSessionProvider davSessionProvider;
87 private IMocksControl servletAuthControl;
89 private ServletAuthenticator servletAuth;
91 private IMocksControl httpAuthControl;
93 private HttpAuthenticator httpAuth;
95 private RepositoryServlet servlet;
97 protected Tomcat tomcat;
99 protected static int port;
102 ApplicationContext applicationContext;
110 String appserverBase =
111 System.getProperty( "appserver.base", new File( "target/appserver-base" ).getAbsolutePath() );
113 File testConf = new File( "src/test/resources/repository-archiva.xml" );
114 File testConfDest = new File( appserverBase, "conf/archiva.xml" );
115 FileUtils.copyFile( testConf, testConfDest );
117 repoRootInternal = new File( appserverBase, "data/repositories/internal" );
119 archivaConfiguration = applicationContext.getBean( ArchivaConfiguration.class );
120 Configuration config = archivaConfiguration.getConfiguration();
122 if ( !config.getManagedRepositoriesAsMap().containsKey( REPOID_INTERNAL ) )
124 config.addManagedRepository(
125 createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal ) );
127 saveConfiguration( archivaConfiguration );
129 CacheManager.getInstance().clearAll();
132 sr = new ServletRunner( new File( "src/test/resources/WEB-INF/repository-servlet-security-test/web.xml" ) );
133 sr.registerServlet( "/repository/*", RepositoryServlet.class.getName() );
138 tomcat = new Tomcat();
139 tomcat.setBaseDir( System.getProperty( "java.io.tmpdir" ) );
142 Context context = tomcat.addContext( "", System.getProperty( "java.io.tmpdir" ) );
144 ApplicationParameter applicationParameter = new ApplicationParameter();
145 applicationParameter.setName( "contextConfigLocation" );
146 applicationParameter.setValue( getSpringConfigLocation() );
147 context.addApplicationParameter( applicationParameter );
149 context.addApplicationListener( ContextLoaderListener.class.getName() );
151 context.addApplicationListener( MavenIndexerCleaner.class.getName() );
153 Tomcat.addServlet( context, "repository", new UnauthenticatedRepositoryServlet() );
154 context.addServletMapping( "/repository/*", "repository" );
158 this.port = tomcat.getConnector().getLocalPort();
161 servletAuthControl = EasyMock.createControl();
163 servletAuth = servletAuthControl.createMock( ServletAuthenticator.class );
165 httpAuthControl = EasyMock.createControl();
167 httpAuth = httpAuthControl.createMock( HttpAuthenticator.class );
169 davSessionProvider = new ArchivaDavSessionProvider( servletAuth, httpAuth );
172 protected String getSpringConfigLocation()
174 return "classpath*:/META-INF/spring-context.xml,classpath*:/spring-context-servlet-security-test.xml";
177 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location )
179 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
181 repo.setName( name );
182 repo.setLocation( location.getAbsolutePath() );
186 protected void saveConfiguration()
189 saveConfiguration( archivaConfiguration );
192 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
195 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
198 protected void setupCleanRepo( File repoRootDir )
201 FileUtils.deleteDirectory( repoRootDir );
202 if ( !repoRootDir.exists() )
204 repoRootDir.mkdirs();
210 public void tearDown()
215 if ( repoRootInternal.exists() )
217 FileUtils.deleteDirectory( repoRootInternal );
222 if (this.tomcat != null)
230 // test deploy with invalid user, and guest has no write access to repo
231 // 401 must be returned
233 public void testPutWithInvalidUserAndGuestHasNoWriteAccess()
236 setupCleanRepo( repoRootInternal );
238 String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
239 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
240 assertNotNull( "artifact.jar inputstream", is );
242 WebRequest request = new AbstractRepositoryServletTestCase.PutMethodWebRequest( putUrl, is, "application/octet-stream" );
243 //InvocationContext ic = sc.newInvocation( request );
244 //servlet = (RepositoryServlet) ic.getServlet();
245 servlet.setDavSessionProvider( davSessionProvider );
247 AuthenticationResult result = new AuthenticationResult();
249 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
250 anyObject( HttpServletResponse.class ) ) ).andReturn( result );
252 servletAuth.isAuthenticated( EasyMock.anyObject( HttpServletRequest.class ),
253 EasyMock.anyObject( AuthenticationResult.class ) );
254 EasyMock.expectLastCall().andThrow( new AuthenticationException( "Authentication error" ) );
256 servletAuth.isAuthorized( "guest", "internal", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD );
258 EasyMock.expectLastCall().andThrow( new UnauthorizedException( "'guest' has no write access to repository" ) );
260 httpAuthControl.replay();
261 servletAuthControl.replay();
263 //servlet.service( ic.getRequest(), ic.getResponse() );
265 httpAuthControl.verify();
266 servletAuthControl.verify();
268 //assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode());
271 // test deploy with invalid user, but guest has write access to repo
273 public void testPutWithInvalidUserAndGuestHasWriteAccess()
276 setupCleanRepo( repoRootInternal );
278 String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
279 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
280 assertNotNull( "artifact.jar inputstream", is );
282 WebRequest request = new AbstractRepositoryServletTestCase.PutMethodWebRequest( putUrl, is, "application/octet-stream" );
284 //InvocationContext ic = sc.newInvocation( request );
285 //servlet = (RepositoryServlet) ic.getServlet();
286 servlet.setDavSessionProvider( davSessionProvider );
288 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
289 archivaDavResourceFactory.setHttpAuth( httpAuth );
290 archivaDavResourceFactory.setServletAuth( servletAuth );
292 servlet.setResourceFactory( archivaDavResourceFactory );
294 AuthenticationResult result = new AuthenticationResult();
296 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
297 anyObject( HttpServletResponse.class ) ) ).andReturn( result );
299 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
300 anyObject( AuthenticationResult.class ) ) ).andThrow(
301 new AuthenticationException( "Authentication error" ) );
303 EasyMock.expect(servletAuth.isAuthorized( "guest", "internal", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD )).andReturn( true );
305 // ArchivaDavResourceFactory#isAuthorized()
306 SecuritySession session = new DefaultSecuritySession();
308 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
309 anyObject( HttpServletResponse.class ) ) ).andReturn( result );
311 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
313 EasyMock.expect(servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq(result) )).andThrow( new AuthenticationException( "Authentication error" ) );
315 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( null );
317 // check if guest has write access
318 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
319 ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ).andReturn(
322 httpAuthControl.replay();
323 servletAuthControl.replay();
325 //servlet.service( ic.getRequest(), ic.getResponse() );
327 httpAuthControl.verify();
328 servletAuthControl.verify();
330 // assertEquals( HttpServletResponse.SC_CREATED, response.getResponseCode() );
333 // test deploy with a valid user with no write access
335 public void testPutWithValidUserWithNoWriteAccess()
338 setupCleanRepo( repoRootInternal );
340 String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
341 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
342 assertNotNull( "artifact.jar inputstream", is );
344 WebRequest request = new AbstractRepositoryServletTestCase.PutMethodWebRequest( putUrl, is, "application/octet-stream" );
346 //InvocationContext ic = sc.newInvocation( request );
347 //servlet = (RepositoryServlet) ic.getServlet();
348 servlet.setDavSessionProvider( davSessionProvider );
350 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
351 archivaDavResourceFactory.setHttpAuth( httpAuth );
352 archivaDavResourceFactory.setServletAuth( servletAuth );
353 servlet.setResourceFactory( archivaDavResourceFactory );
355 AuthenticationResult result = new AuthenticationResult();
357 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
358 anyObject( HttpServletResponse.class ) ) ).andReturn( result );
360 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
361 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
363 // ArchivaDavResourceFactory#isAuthorized()
364 SecuritySession session = new DefaultSecuritySession();
366 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
367 anyObject( HttpServletResponse.class ) ) ).andReturn( result );
369 //EasyMock.expect( httpAuth.getSecuritySession( ic.getRequest().getSession( true ) ) ).andReturn( session );
371 //EasyMock.expect( httpAuth.getSessionUser( ic.getRequest().getSession() ) ).andReturn( new SimpleUser() );
373 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
374 eq( result ) ) ).andReturn( true );
376 EasyMock.expect( 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 //servlet.service( ic.getRequest(), ic.getResponse() );
384 httpAuthControl.verify();
385 servletAuthControl.verify();
387 // assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode());
390 // test deploy with a valid user with write access
392 public void testPutWithValidUserWithWriteAccess()
395 setupCleanRepo( repoRootInternal );
396 assertTrue( repoRootInternal.exists() );
398 String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
399 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
400 assertNotNull( "artifact.jar inputstream", is );
402 WebRequest request = new AbstractRepositoryServletTestCase.PutMethodWebRequest( putUrl, is, "application/octet-stream" );
404 //InvocationContext ic = sc.newInvocation( request );
405 //servlet = (RepositoryServlet) ic.getServlet();
406 servlet.setDavSessionProvider( davSessionProvider );
408 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
409 archivaDavResourceFactory.setHttpAuth( httpAuth );
410 archivaDavResourceFactory.setServletAuth( servletAuth );
412 TestAuditListener listener = new TestAuditListener();
413 archivaDavResourceFactory.addAuditListener( listener );
414 servlet.setResourceFactory( archivaDavResourceFactory );
416 AuthenticationResult result = new AuthenticationResult();
418 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
419 anyObject( HttpServletResponse.class) )).andReturn( result );
421 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
422 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
424 User user = new SimpleUser();
425 user.setUsername( "admin" );
427 // ArchivaDavResourceFactory#isAuthorized()
428 SecuritySession session = new DefaultSecuritySession();
430 EasyMock.expect( httpAuth.getAuthenticationResult(anyObject( HttpServletRequest.class ),
431 anyObject( HttpServletResponse.class) ) ).andReturn( result );
433 //EasyMock.expect( httpAuth.getSecuritySession( ic.getRequest().getSession( true ) ) ).andReturn( session );
435 //EasyMock.expect( httpAuth.getSessionUser( ic.getRequest().getSession() ) ).andReturn( user );
437 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq(result) ) ).andReturn(
440 EasyMock.expect( servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq(session), eq("internal"),
441 eq(ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD) ) ).andReturn(
444 httpAuthControl.replay();
445 servletAuthControl.replay();
447 //servlet.service( ic.getRequest(), ic.getResponse() );
449 httpAuthControl.verify();
450 servletAuthControl.verify();
452 // assertEquals(HttpServletResponse.SC_CREATED, response.getResponseCode());
454 assertEquals( "admin", listener.getEvents().get( 0 ).getUserId() );
457 // test get with invalid user, and guest has read access to repo
459 public void testGetWithInvalidUserAndGuestHasReadAccess()
462 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
463 String expectedArtifactContents = "dummy-commons-lang-artifact";
465 File artifactFile = new File( repoRootInternal, commonsLangJar );
466 artifactFile.getParentFile().mkdirs();
468 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
470 WebRequest request = new AbstractRepositoryServletTestCase.GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangJar );
471 //InvocationContext ic = sc.newInvocation( request );
472 //servlet = (RepositoryServlet) ic.getServlet();
473 servlet.setDavSessionProvider( davSessionProvider );
475 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
476 archivaDavResourceFactory.setHttpAuth( httpAuth );
477 archivaDavResourceFactory.setServletAuth( servletAuth );
479 servlet.setResourceFactory( archivaDavResourceFactory );
481 AuthenticationResult result = new AuthenticationResult();
483 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), anyObject( HttpServletResponse.class ) ) )
484 .andReturn( result );
486 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), anyObject( AuthenticationResult.class ) ) ).andThrow(
487 new AuthenticationException( "Authentication error" ) );
489 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
490 ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ).andReturn(
493 // ArchivaDavResourceFactory#isAuthorized()
494 SecuritySession session = new DefaultSecuritySession();
496 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), anyObject( HttpServletResponse.class ) ) ).andReturn( result );
498 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
500 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( null );
502 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq(result) ) ).andReturn(
505 EasyMock.expect( servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq(session), eq("internal"),
506 eq(ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS) ) ).andReturn( true );
507 httpAuthControl.replay();
508 servletAuthControl.replay();
510 WebResponse response = null;// sc.getResponse( request );
512 httpAuthControl.verify();
513 servletAuthControl.verify();
515 assertEquals( HttpServletResponse.SC_OK, response.getStatusCode() );
516 assertEquals( "Expected file contents", expectedArtifactContents, response.getContentAsString() );
519 // test get with invalid user, and guest has no read access to repo
521 public void testGetWithInvalidUserAndGuestHasNoReadAccess()
524 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
525 String expectedArtifactContents = "dummy-commons-lang-artifact";
527 File artifactFile = new File( repoRootInternal, commonsLangJar );
528 artifactFile.getParentFile().mkdirs();
530 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
532 WebRequest request = new AbstractRepositoryServletTestCase.GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangJar );
533 //InvocationContext ic = sc.newInvocation( request );
534 //servlet = (RepositoryServlet) ic.getServlet();
535 servlet.setDavSessionProvider( davSessionProvider );
537 AuthenticationResult result = new AuthenticationResult();
539 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), anyObject( HttpServletResponse.class ) ) ).andReturn( result );
541 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), anyObject( AuthenticationResult.class ) ) ).andThrow(
542 new AuthenticationException( "Authentication error" ) );
544 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
545 ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ).andReturn(
547 httpAuthControl.replay();
548 servletAuthControl.replay();
550 WebResponse response = null;//sc.getResponse( request );
552 httpAuthControl.verify();
553 servletAuthControl.verify();
555 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, response.getStatusCode() );
558 // test get with valid user with read access to repo
560 public void testGetWithAValidUserWithReadAccess()
563 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
564 String expectedArtifactContents = "dummy-commons-lang-artifact";
566 File artifactFile = new File( repoRootInternal, commonsLangJar );
567 artifactFile.getParentFile().mkdirs();
569 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
571 WebRequest request = new AbstractRepositoryServletTestCase.GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangJar );
572 //InvocationContext ic = sc.newInvocation( request );
573 //servlet = (RepositoryServlet) ic.getServlet();
574 servlet.setDavSessionProvider( davSessionProvider );
576 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
577 archivaDavResourceFactory.setHttpAuth( httpAuth );
578 archivaDavResourceFactory.setServletAuth( servletAuth );
580 servlet.setResourceFactory( archivaDavResourceFactory );
582 AuthenticationResult result = new AuthenticationResult();
584 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), anyObject( HttpServletResponse.class ) ) ).andReturn( result );
586 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), anyObject( AuthenticationResult.class ) ) ).andReturn( true );
587 // ArchivaDavResourceFactory#isAuthorized()
588 SecuritySession session = new DefaultSecuritySession();
590 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), anyObject( HttpServletResponse.class ) ) ).andReturn( result );
592 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
594 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( new SimpleUser() );
596 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq(result) ) ).andReturn(
599 EasyMock.expect( servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq(session), eq("internal"),
600 eq(ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS) ) ).andReturn(
603 httpAuthControl.replay();
604 servletAuthControl.replay();
606 WebResponse response = null;// sc.getResponse( request );
608 httpAuthControl.verify();
609 servletAuthControl.verify();
611 assertEquals( HttpServletResponse.SC_OK, response.getStatusCode() );
612 assertEquals( "Expected file contents", expectedArtifactContents, response.getContentAsString() );
615 // test get with valid user with no read access to repo
617 public void testGetWithAValidUserWithNoReadAccess()
620 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
621 String expectedArtifactContents = "dummy-commons-lang-artifact";
623 File artifactFile = new File( repoRootInternal, commonsLangJar );
624 artifactFile.getParentFile().mkdirs();
626 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
628 WebRequest request = new AbstractRepositoryServletTestCase.GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangJar );
629 //InvocationContext ic = sc.newInvocation( request );
630 //servlet = (RepositoryServlet) ic.getServlet();
631 servlet.setDavSessionProvider( davSessionProvider );
633 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
634 archivaDavResourceFactory.setHttpAuth( httpAuth );
635 archivaDavResourceFactory.setServletAuth( servletAuth );
637 servlet.setResourceFactory( archivaDavResourceFactory );
639 AuthenticationResult result = new AuthenticationResult();
641 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), anyObject( HttpServletResponse.class ) ) ).andReturn( result );
643 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), anyObject( AuthenticationResult.class ) ) ).andReturn( true );
645 // ArchivaDavResourceFactory#isAuthorized()
646 SecuritySession session = new DefaultSecuritySession();
648 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), anyObject( HttpServletResponse.class ) ) ).andReturn( result );
650 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class) ) ).andReturn( session );
652 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class) ) ).andReturn( new SimpleUser() );
654 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq(result) ) ).andReturn(
657 EasyMock.expect( servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq(session), eq("internal"),
658 eq(ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS) ) ).andThrow(
659 new UnauthorizedException( "User not authorized to read repository." ) );
660 httpAuthControl.replay();
661 servletAuthControl.replay();
663 WebResponse response = null;//sc.getResponse( request );
665 httpAuthControl.verify();
666 servletAuthControl.verify();
668 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, response.getStatusCode() );