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
22 import com.meterware.httpunit.GetMethodWebRequest;
23 import com.meterware.httpunit.HttpUnitOptions;
24 import com.meterware.httpunit.PutMethodWebRequest;
25 import com.meterware.httpunit.WebRequest;
26 import com.meterware.httpunit.WebResponse;
27 import com.meterware.servletunit.InvocationContext;
28 import com.meterware.servletunit.ServletRunner;
29 import com.meterware.servletunit.ServletUnitClient;
30 import junit.framework.TestCase;
31 import net.sf.ehcache.CacheManager;
32 import org.apache.archiva.configuration.ArchivaConfiguration;
33 import org.apache.archiva.configuration.Configuration;
34 import org.apache.archiva.configuration.ManagedRepositoryConfiguration;
35 import org.apache.archiva.redback.authentication.AuthenticationException;
36 import org.apache.archiva.redback.authentication.AuthenticationResult;
37 import org.apache.archiva.redback.authorization.UnauthorizedException;
38 import org.apache.archiva.redback.integration.filter.authentication.HttpAuthenticator;
39 import org.apache.archiva.redback.system.DefaultSecuritySession;
40 import org.apache.archiva.redback.system.SecuritySession;
41 import org.apache.archiva.redback.users.User;
42 import org.apache.archiva.redback.users.memory.SimpleUser;
43 import org.apache.archiva.repository.audit.TestAuditListener;
44 import org.apache.archiva.security.ServletAuthenticator;
45 import org.apache.archiva.security.common.ArchivaRoleConstants;
46 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
47 import org.apache.commons.io.FileUtils;
48 import org.apache.jackrabbit.webdav.DavSessionProvider;
49 import org.easymock.EasyMock;
50 import static org.easymock.EasyMock.*;
51 import org.easymock.IMocksControl;
52 import org.junit.After;
53 import org.junit.Before;
54 import org.junit.Test;
55 import org.junit.runner.RunWith;
56 import org.springframework.context.ApplicationContext;
57 import org.springframework.test.context.ContextConfiguration;
59 import javax.inject.Inject;
60 import javax.servlet.http.HttpServletRequest;
61 import javax.servlet.http.HttpServletResponse;
62 import javax.servlet.http.HttpSession;
64 import java.io.IOException;
65 import java.io.InputStream;
66 import java.nio.charset.Charset;
69 * RepositoryServletSecurityTest Test the flow of the authentication and authorization checks. This does not necessarily
70 * perform redback security checking.
72 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
73 @ContextConfiguration( locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context.xml" } )
74 public class RepositoryServletSecurityTest
77 protected static final String REPOID_INTERNAL = "internal";
79 protected ServletUnitClient sc;
81 protected File repoRootInternal;
83 private ServletRunner sr;
85 protected ArchivaConfiguration archivaConfiguration;
87 private DavSessionProvider davSessionProvider;
89 private IMocksControl servletAuthControl;
91 private ServletAuthenticator servletAuth;
93 private IMocksControl httpAuthControl;
95 private HttpAuthenticator httpAuth;
97 private RepositoryServlet servlet;
100 ApplicationContext applicationContext;
108 String appserverBase =
109 System.getProperty( "appserver.base", new File( "target/appserver-base" ).getAbsolutePath() );
111 File testConf = new File( "src/test/resources/repository-archiva.xml" );
112 File testConfDest = new File( appserverBase, "conf/archiva.xml" );
113 FileUtils.copyFile( testConf, testConfDest );
115 repoRootInternal = new File( appserverBase, "data/repositories/internal" );
117 archivaConfiguration = applicationContext.getBean( ArchivaConfiguration.class );
118 Configuration config = archivaConfiguration.getConfiguration();
120 if ( !config.getManagedRepositoriesAsMap().containsKey( REPOID_INTERNAL ) )
122 config.addManagedRepository(
123 createManagedRepository( REPOID_INTERNAL, "Internal Test Repo", repoRootInternal ) );
125 saveConfiguration( archivaConfiguration );
127 CacheManager.getInstance().clearAll();
129 HttpUnitOptions.setExceptionsThrownOnErrorStatus( false );
131 sr = new ServletRunner( new File( "src/test/resources/WEB-INF/repository-servlet-security-test/web.xml" ) );
132 sr.registerServlet( "/repository/*", RepositoryServlet.class.getName() );
135 servletAuthControl = EasyMock.createControl();
137 servletAuth = servletAuthControl.createMock( ServletAuthenticator.class );
139 httpAuthControl = EasyMock.createControl();
141 httpAuth = httpAuthControl.createMock( HttpAuthenticator.class );
143 davSessionProvider = new ArchivaDavSessionProvider( servletAuth, httpAuth );
146 protected ManagedRepositoryConfiguration createManagedRepository( String id, String name, File location )
148 ManagedRepositoryConfiguration repo = new ManagedRepositoryConfiguration();
150 repo.setName( name );
151 repo.setLocation( location.getAbsolutePath() );
155 protected void saveConfiguration()
158 saveConfiguration( archivaConfiguration );
161 protected void saveConfiguration( ArchivaConfiguration archivaConfiguration )
164 archivaConfiguration.save( archivaConfiguration.getConfiguration() );
167 protected void setupCleanRepo( File repoRootDir )
170 FileUtils.deleteDirectory( repoRootDir );
171 if ( !repoRootDir.exists() )
173 repoRootDir.mkdirs();
179 public void tearDown()
192 if ( repoRootInternal.exists() )
194 FileUtils.deleteDirectory( repoRootInternal );
202 // test deploy with invalid user, and guest has no write access to repo
203 // 401 must be returned
205 public void testPutWithInvalidUserAndGuestHasNoWriteAccess()
208 setupCleanRepo( repoRootInternal );
210 String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
211 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
212 assertNotNull( "artifact.jar inputstream", is );
214 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
215 InvocationContext ic = sc.newInvocation( request );
216 servlet = (RepositoryServlet) ic.getServlet();
217 servlet.setDavSessionProvider( davSessionProvider );
219 AuthenticationResult result = new AuthenticationResult();
221 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
222 anyObject( HttpServletResponse.class ) ) ).andReturn( result );
224 servletAuth.isAuthenticated( EasyMock.anyObject( HttpServletRequest.class ),
225 EasyMock.anyObject( AuthenticationResult.class ) );
226 EasyMock.expectLastCall().andThrow( new AuthenticationException( "Authentication error" ) );
228 servletAuth.isAuthorized( "guest", "internal", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD );
230 EasyMock.expectLastCall().andThrow( new UnauthorizedException( "'guest' has no write access to repository" ) );
232 httpAuthControl.replay();
233 servletAuthControl.replay();
235 servlet.service( ic.getRequest(), ic.getResponse() );
237 httpAuthControl.verify();
238 servletAuthControl.verify();
240 //assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode());
243 // test deploy with invalid user, but guest has write access to repo
245 public void testPutWithInvalidUserAndGuestHasWriteAccess()
248 setupCleanRepo( repoRootInternal );
250 String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
251 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
252 assertNotNull( "artifact.jar inputstream", is );
254 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
256 InvocationContext ic = sc.newInvocation( request );
257 servlet = (RepositoryServlet) ic.getServlet();
258 servlet.setDavSessionProvider( davSessionProvider );
260 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
261 archivaDavResourceFactory.setHttpAuth( httpAuth );
262 archivaDavResourceFactory.setServletAuth( servletAuth );
264 servlet.setResourceFactory( archivaDavResourceFactory );
266 AuthenticationResult result = new AuthenticationResult();
268 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
269 anyObject( HttpServletResponse.class ) ) ).andReturn( result );
271 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
272 anyObject( AuthenticationResult.class ) ) ).andThrow(
273 new AuthenticationException( "Authentication error" ) );
275 EasyMock.expect(servletAuth.isAuthorized( "guest", "internal", ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD )).andReturn( true );
277 // ArchivaDavResourceFactory#isAuthorized()
278 SecuritySession session = new DefaultSecuritySession();
280 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
281 anyObject( HttpServletResponse.class ) ) ).andReturn( result );
283 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
285 EasyMock.expect(servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq(result) )).andThrow( new AuthenticationException( "Authentication error" ) );
287 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( null );
289 // check if guest has write access
290 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
291 ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD ) ).andReturn(
294 httpAuthControl.replay();
295 servletAuthControl.replay();
297 servlet.service( ic.getRequest(), ic.getResponse() );
299 httpAuthControl.verify();
300 servletAuthControl.verify();
302 // assertEquals( HttpServletResponse.SC_CREATED, response.getResponseCode() );
305 // test deploy with a valid user with no write access
307 public void testPutWithValidUserWithNoWriteAccess()
310 setupCleanRepo( repoRootInternal );
312 String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
313 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
314 assertNotNull( "artifact.jar inputstream", is );
316 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
318 InvocationContext ic = sc.newInvocation( request );
319 servlet = (RepositoryServlet) ic.getServlet();
320 servlet.setDavSessionProvider( davSessionProvider );
322 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
323 archivaDavResourceFactory.setHttpAuth( httpAuth );
324 archivaDavResourceFactory.setServletAuth( servletAuth );
325 servlet.setResourceFactory( archivaDavResourceFactory );
327 AuthenticationResult result = new AuthenticationResult();
329 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
330 anyObject( HttpServletResponse.class ) ) ).andReturn( result );
332 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
333 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
335 // ArchivaDavResourceFactory#isAuthorized()
336 SecuritySession session = new DefaultSecuritySession();
338 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
339 anyObject( HttpServletResponse.class ) ) ).andReturn( result );
341 EasyMock.expect( httpAuth.getSecuritySession( ic.getRequest().getSession( true ) ) ).andReturn( session );
343 EasyMock.expect( httpAuth.getSessionUser( ic.getRequest().getSession() ) ).andReturn( new SimpleUser() );
345 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
346 eq( result ) ) ).andReturn( true );
348 EasyMock.expect( servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq(session), eq("internal"),
349 eq(ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD) ) ).andThrow(
350 new UnauthorizedException( "User not authorized" ) );
351 httpAuthControl.replay();
352 servletAuthControl.replay();
354 servlet.service( ic.getRequest(), ic.getResponse() );
356 httpAuthControl.verify();
357 servletAuthControl.verify();
359 // assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode());
362 // test deploy with a valid user with write access
364 public void testPutWithValidUserWithWriteAccess()
367 setupCleanRepo( repoRootInternal );
368 assertTrue( repoRootInternal.exists() );
370 String putUrl = "http://machine.com/repository/internal/path/to/artifact.jar";
371 InputStream is = getClass().getResourceAsStream( "/artifact.jar" );
372 assertNotNull( "artifact.jar inputstream", is );
374 WebRequest request = new PutMethodWebRequest( putUrl, is, "application/octet-stream" );
376 InvocationContext ic = sc.newInvocation( request );
377 servlet = (RepositoryServlet) ic.getServlet();
378 servlet.setDavSessionProvider( davSessionProvider );
380 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
381 archivaDavResourceFactory.setHttpAuth( httpAuth );
382 archivaDavResourceFactory.setServletAuth( servletAuth );
384 TestAuditListener listener = new TestAuditListener();
385 archivaDavResourceFactory.addAuditListener( listener );
386 servlet.setResourceFactory( archivaDavResourceFactory );
388 AuthenticationResult result = new AuthenticationResult();
390 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ),
391 anyObject( HttpServletResponse.class) )).andReturn( result );
393 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ),
394 anyObject( AuthenticationResult.class ) ) ).andReturn( true );
396 User user = new SimpleUser();
397 user.setUsername( "admin" );
399 // ArchivaDavResourceFactory#isAuthorized()
400 SecuritySession session = new DefaultSecuritySession();
402 EasyMock.expect( httpAuth.getAuthenticationResult(anyObject( HttpServletRequest.class ),
403 anyObject( HttpServletResponse.class) ) ).andReturn( result );
405 EasyMock.expect( httpAuth.getSecuritySession( ic.getRequest().getSession( true ) ) ).andReturn( session );
407 EasyMock.expect( httpAuth.getSessionUser( ic.getRequest().getSession() ) ).andReturn( user );
409 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq(result) ) ).andReturn(
412 EasyMock.expect( servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq(session), eq("internal"),
413 eq(ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD) ) ).andReturn(
416 httpAuthControl.replay();
417 servletAuthControl.replay();
419 servlet.service( ic.getRequest(), ic.getResponse() );
421 httpAuthControl.verify();
422 servletAuthControl.verify();
424 // assertEquals(HttpServletResponse.SC_CREATED, response.getResponseCode());
426 assertEquals( "admin", listener.getEvents().get( 0 ).getUserId() );
429 // test get with invalid user, and guest has read access to repo
431 public void testGetWithInvalidUserAndGuestHasReadAccess()
434 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
435 String expectedArtifactContents = "dummy-commons-lang-artifact";
437 File artifactFile = new File( repoRootInternal, commonsLangJar );
438 artifactFile.getParentFile().mkdirs();
440 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
442 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangJar );
443 InvocationContext ic = sc.newInvocation( request );
444 servlet = (RepositoryServlet) ic.getServlet();
445 servlet.setDavSessionProvider( davSessionProvider );
447 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
448 archivaDavResourceFactory.setHttpAuth( httpAuth );
449 archivaDavResourceFactory.setServletAuth( servletAuth );
451 servlet.setResourceFactory( archivaDavResourceFactory );
453 AuthenticationResult result = new AuthenticationResult();
455 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), anyObject( HttpServletResponse.class ) ) )
456 .andReturn( result );
458 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), anyObject( AuthenticationResult.class ) ) ).andThrow(
459 new AuthenticationException( "Authentication error" ) );
461 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
462 ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ).andReturn(
465 // ArchivaDavResourceFactory#isAuthorized()
466 SecuritySession session = new DefaultSecuritySession();
468 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), anyObject( HttpServletResponse.class ) ) ).andReturn( result );
470 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
472 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( null );
474 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq(result) ) ).andReturn(
477 EasyMock.expect( servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq(session), eq("internal"),
478 eq(ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS) ) ).andReturn(
480 httpAuthControl.replay();
481 servletAuthControl.replay();
483 WebResponse response = sc.getResponse( request );
485 httpAuthControl.verify();
486 servletAuthControl.verify();
488 assertEquals( HttpServletResponse.SC_OK, response.getResponseCode() );
489 assertEquals( "Expected file contents", expectedArtifactContents, response.getText() );
492 // test get with invalid user, and guest has no read access to repo
494 public void testGetWithInvalidUserAndGuestHasNoReadAccess()
497 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
498 String expectedArtifactContents = "dummy-commons-lang-artifact";
500 File artifactFile = new File( repoRootInternal, commonsLangJar );
501 artifactFile.getParentFile().mkdirs();
503 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
505 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangJar );
506 InvocationContext ic = sc.newInvocation( request );
507 servlet = (RepositoryServlet) ic.getServlet();
508 servlet.setDavSessionProvider( davSessionProvider );
510 AuthenticationResult result = new AuthenticationResult();
512 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), anyObject( HttpServletResponse.class ) ) ).andReturn( result );
514 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), anyObject( AuthenticationResult.class ) ) ).andThrow(
515 new AuthenticationException( "Authentication error" ) );
517 EasyMock.expect( servletAuth.isAuthorized( "guest", "internal",
518 ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS ) ).andReturn(
520 httpAuthControl.replay();
521 servletAuthControl.replay();
523 WebResponse response = sc.getResponse( request );
525 httpAuthControl.verify();
526 servletAuthControl.verify();
528 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode() );
531 // test get with valid user with read access to repo
533 public void testGetWithAValidUserWithReadAccess()
536 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
537 String expectedArtifactContents = "dummy-commons-lang-artifact";
539 File artifactFile = new File( repoRootInternal, commonsLangJar );
540 artifactFile.getParentFile().mkdirs();
542 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
544 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangJar );
545 InvocationContext ic = sc.newInvocation( request );
546 servlet = (RepositoryServlet) ic.getServlet();
547 servlet.setDavSessionProvider( davSessionProvider );
549 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
550 archivaDavResourceFactory.setHttpAuth( httpAuth );
551 archivaDavResourceFactory.setServletAuth( servletAuth );
553 servlet.setResourceFactory( archivaDavResourceFactory );
555 AuthenticationResult result = new AuthenticationResult();
557 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), anyObject( HttpServletResponse.class ) ) ).andReturn( result );
559 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), anyObject( AuthenticationResult.class ) ) ).andReturn( true );
560 // ArchivaDavResourceFactory#isAuthorized()
561 SecuritySession session = new DefaultSecuritySession();
563 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), anyObject( HttpServletResponse.class ) ) ).andReturn( result );
565 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class ) ) ).andReturn( session );
567 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class ) ) ).andReturn( new SimpleUser() );
569 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq(result) ) ).andReturn(
572 EasyMock.expect( servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq(session), eq("internal"),
573 eq(ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS) ) ).andReturn(
576 httpAuthControl.replay();
577 servletAuthControl.replay();
579 WebResponse response = sc.getResponse( request );
581 httpAuthControl.verify();
582 servletAuthControl.verify();
584 assertEquals( HttpServletResponse.SC_OK, response.getResponseCode() );
585 assertEquals( "Expected file contents", expectedArtifactContents, response.getText() );
588 // test get with valid user with no read access to repo
590 public void testGetWithAValidUserWithNoReadAccess()
593 String commonsLangJar = "commons-lang/commons-lang/2.1/commons-lang-2.1.jar";
594 String expectedArtifactContents = "dummy-commons-lang-artifact";
596 File artifactFile = new File( repoRootInternal, commonsLangJar );
597 artifactFile.getParentFile().mkdirs();
599 FileUtils.writeStringToFile( artifactFile, expectedArtifactContents, Charset.defaultCharset() );
601 WebRequest request = new GetMethodWebRequest( "http://machine.com/repository/internal/" + commonsLangJar );
602 InvocationContext ic = sc.newInvocation( request );
603 servlet = (RepositoryServlet) ic.getServlet();
604 servlet.setDavSessionProvider( davSessionProvider );
606 ArchivaDavResourceFactory archivaDavResourceFactory = (ArchivaDavResourceFactory) servlet.getResourceFactory();
607 archivaDavResourceFactory.setHttpAuth( httpAuth );
608 archivaDavResourceFactory.setServletAuth( servletAuth );
610 servlet.setResourceFactory( archivaDavResourceFactory );
612 AuthenticationResult result = new AuthenticationResult();
614 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), anyObject( HttpServletResponse.class ) ) ).andReturn( result );
616 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), anyObject( AuthenticationResult.class ) ) ).andReturn( true );
618 // ArchivaDavResourceFactory#isAuthorized()
619 SecuritySession session = new DefaultSecuritySession();
621 EasyMock.expect( httpAuth.getAuthenticationResult( anyObject( HttpServletRequest.class ), anyObject( HttpServletResponse.class ) ) ).andReturn( result );
623 EasyMock.expect( httpAuth.getSecuritySession( anyObject( HttpSession.class) ) ).andReturn( session );
625 EasyMock.expect( httpAuth.getSessionUser( anyObject( HttpSession.class) ) ).andReturn( new SimpleUser() );
627 EasyMock.expect( servletAuth.isAuthenticated( anyObject( HttpServletRequest.class ), eq(result) ) ).andReturn(
630 EasyMock.expect( servletAuth.isAuthorized( anyObject( HttpServletRequest.class ), eq(session), eq("internal"),
631 eq(ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS) ) ).andThrow(
632 new UnauthorizedException( "User not authorized to read repository." ) );
633 httpAuthControl.replay();
634 servletAuthControl.replay();
636 WebResponse response = sc.getResponse( request );
638 httpAuthControl.verify();
639 servletAuthControl.verify();
641 assertEquals( HttpServletResponse.SC_UNAUTHORIZED, response.getResponseCode() );