View Javadoc
1   package org.apache.archiva.webdav;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import junit.framework.TestCase;
23  import org.apache.archiva.redback.authentication.AuthenticationException;
24  import org.apache.archiva.redback.policy.AccountLockedException;
25  import org.apache.archiva.redback.policy.MustChangePasswordException;
26  import org.apache.archiva.redback.users.User;
27  import org.apache.jackrabbit.webdav.DavSessionProvider;
28  import org.apache.jackrabbit.webdav.WebdavRequest;
29  import org.apache.jackrabbit.webdav.WebdavRequestImpl;
30  import org.apache.archiva.security.ServletAuthenticator;
31  import org.apache.archiva.redback.authentication.AuthenticationDataSource;
32  import org.apache.archiva.redback.authentication.AuthenticationResult;
33  import org.apache.archiva.redback.authorization.AuthorizationException;
34  import org.apache.archiva.redback.authorization.UnauthorizedException;
35  import org.apache.archiva.redback.system.SecuritySession;
36  import org.apache.archiva.redback.integration.filter.authentication.HttpAuthenticator;
37  
38  import javax.servlet.AsyncContext;
39  import javax.servlet.DispatcherType;
40  import javax.servlet.RequestDispatcher;
41  import javax.servlet.ServletContext;
42  import javax.servlet.ServletException;
43  import javax.servlet.ServletInputStream;
44  import javax.servlet.ServletRequest;
45  import javax.servlet.ServletResponse;
46  import javax.servlet.http.Cookie;
47  import javax.servlet.http.HttpServletRequest;
48  import javax.servlet.http.HttpServletResponse;
49  import javax.servlet.http.HttpSession;
50  import javax.servlet.http.Part;
51  import java.io.BufferedReader;
52  import java.io.IOException;
53  import java.io.UnsupportedEncodingException;
54  import java.security.Principal;
55  import java.util.Collection;
56  import java.util.Enumeration;
57  import java.util.Locale;
58  import java.util.Map;
59  import org.apache.archiva.test.utils.ArchivaBlockJUnit4ClassRunner;
60  import org.junit.Before;
61  import org.junit.Test;
62  import org.junit.runner.RunWith;
63  
64  @RunWith( ArchivaBlockJUnit4ClassRunner.class )
65  public class ArchivaDavSessionProviderTest
66      extends TestCase
67  {
68      private DavSessionProvider sessionProvider;
69  
70      private WebdavRequest request;
71  
72      @Override
73      @Before
74      public void setUp()
75          throws Exception
76      {
77          super.setUp();
78          sessionProvider = new ArchivaDavSessionProvider( new ServletAuthenticatorMock(), new HttpAuthenticatorMock() );
79          request = new WebdavRequestImpl( new HttpServletRequestMock(), null );
80      }
81  
82      @Test
83      public void testAttachSession()
84          throws Exception
85      {
86          assertNull( request.getDavSession() );
87          sessionProvider.attachSession( request );
88          assertNotNull( request.getDavSession() );
89      }
90  
91      @Test
92      public void testReleaseSession()
93          throws Exception
94      {
95          assertNull( request.getDavSession() );
96          sessionProvider.attachSession( request );
97          assertNotNull( request.getDavSession() );
98  
99          sessionProvider.releaseSession( request );
100         assertNull( request.getDavSession() );
101     }
102 
103     @SuppressWarnings( "unchecked" )
104     private class HttpServletRequestMock
105         implements HttpServletRequest
106     {
107 
108         @Override
109         public boolean authenticate( HttpServletResponse httpServletResponse )
110             throws IOException, ServletException
111         {
112             throw new UnsupportedOperationException( "Not supported yet." );
113         }
114 
115         @Override
116         public void login( String s, String s1 )
117             throws ServletException
118         {
119             throw new UnsupportedOperationException( "Not supported yet." );
120         }
121 
122         @Override
123         public void logout()
124             throws ServletException
125         {
126             throw new UnsupportedOperationException( "Not supported yet." );
127         }
128 
129         @Override
130         public Collection<Part> getParts()
131             throws IOException, ServletException
132         {
133             throw new UnsupportedOperationException( "Not supported yet." );
134         }
135 
136         @Override
137         public Part getPart( String s )
138             throws IOException, ServletException
139         {
140             throw new UnsupportedOperationException( "Not supported yet." );
141         }
142 
143         @Override
144         public ServletContext getServletContext()
145         {
146             throw new UnsupportedOperationException( "Not supported yet." );
147         }
148 
149         @Override
150         public AsyncContext startAsync()
151             throws IllegalStateException
152         {
153             throw new UnsupportedOperationException( "Not supported yet." );
154         }
155 
156         @Override
157         public AsyncContext startAsync( ServletRequest servletRequest, ServletResponse servletResponse )
158             throws IllegalStateException
159         {
160             throw new UnsupportedOperationException( "Not supported yet." );
161         }
162 
163         @Override
164         public boolean isAsyncStarted()
165         {
166             throw new UnsupportedOperationException( "Not supported yet." );
167         }
168 
169         @Override
170         public boolean isAsyncSupported()
171         {
172             throw new UnsupportedOperationException( "Not supported yet." );
173         }
174 
175         @Override
176         public AsyncContext getAsyncContext()
177         {
178             throw new UnsupportedOperationException( "Not supported yet." );
179         }
180 
181         @Override
182         public DispatcherType getDispatcherType()
183         {
184             throw new UnsupportedOperationException( "Not supported yet." );
185         }
186 
187         @Override
188         public Object getAttribute( String arg0 )
189         {
190             throw new UnsupportedOperationException( "Not supported yet." );
191         }
192 
193         @Override
194         public Enumeration getAttributeNames()
195         {
196             throw new UnsupportedOperationException( "Not supported yet." );
197         }
198 
199         @Override
200         public String getCharacterEncoding()
201         {
202             throw new UnsupportedOperationException( "Not supported yet." );
203         }
204 
205         @Override
206         public int getContentLength()
207         {
208             throw new UnsupportedOperationException( "Not supported yet." );
209         }
210 
211         @Override
212         public String getContentType()
213         {
214             throw new UnsupportedOperationException( "Not supported yet." );
215         }
216 
217         @Override
218         public ServletInputStream getInputStream()
219             throws IOException
220         {
221             throw new UnsupportedOperationException( "Not supported yet." );
222         }
223 
224         @Override
225         public String getLocalAddr()
226         {
227             throw new UnsupportedOperationException( "Not supported yet." );
228         }
229 
230         @Override
231         public String getLocalName()
232         {
233             throw new UnsupportedOperationException( "Not supported yet." );
234         }
235 
236         @Override
237         public int getLocalPort()
238         {
239             throw new UnsupportedOperationException( "Not supported yet." );
240         }
241 
242         @Override
243         public Locale getLocale()
244         {
245             throw new UnsupportedOperationException( "Not supported yet." );
246         }
247 
248         @Override
249         public Enumeration getLocales()
250         {
251             throw new UnsupportedOperationException( "Not supported yet." );
252         }
253 
254         @Override
255         public String getParameter( String arg0 )
256         {
257             throw new UnsupportedOperationException( "Not supported yet." );
258         }
259 
260         @Override
261         public Map getParameterMap()
262         {
263             throw new UnsupportedOperationException( "Not supported yet." );
264         }
265 
266         @Override
267         public Enumeration getParameterNames()
268         {
269             throw new UnsupportedOperationException( "Not supported yet." );
270         }
271 
272         @Override
273         public String[] getParameterValues( String arg0 )
274         {
275             throw new UnsupportedOperationException( "Not supported yet." );
276         }
277 
278         @Override
279         public String getProtocol()
280         {
281             throw new UnsupportedOperationException( "Not supported yet." );
282         }
283 
284         @Override
285         public BufferedReader getReader()
286             throws IOException
287         {
288             throw new UnsupportedOperationException( "Not supported yet." );
289         }
290 
291         @Override
292         public String getRealPath( String arg0 )
293         {
294             throw new UnsupportedOperationException( "Not supported yet." );
295         }
296 
297         @Override
298         public String getRemoteAddr()
299         {
300             throw new UnsupportedOperationException( "Not supported yet." );
301         }
302 
303         @Override
304         public String getRemoteHost()
305         {
306             throw new UnsupportedOperationException( "Not supported yet." );
307         }
308 
309         @Override
310         public int getRemotePort()
311         {
312             throw new UnsupportedOperationException( "Not supported yet." );
313         }
314 
315         @Override
316         public RequestDispatcher getRequestDispatcher( String arg0 )
317         {
318             throw new UnsupportedOperationException( "Not supported yet." );
319         }
320 
321         @Override
322         public String getScheme()
323         {
324             return "";
325         }
326 
327         @Override
328         public String getServerName()
329         {
330             throw new UnsupportedOperationException( "Not supported yet." );
331         }
332 
333         @Override
334         public int getServerPort()
335         {
336             throw new UnsupportedOperationException( "Not supported yet." );
337         }
338 
339         @Override
340         public boolean isSecure()
341         {
342             throw new UnsupportedOperationException( "Not supported yet." );
343         }
344 
345         @Override
346         public void removeAttribute( String arg0 )
347         {
348             throw new UnsupportedOperationException( "Not supported yet." );
349         }
350 
351         @Override
352         public void setAttribute( String arg0, Object arg1 )
353         {
354             throw new UnsupportedOperationException( "Not supported yet." );
355         }
356 
357         @Override
358         public void setCharacterEncoding( String arg0 )
359             throws UnsupportedEncodingException
360         {
361             throw new UnsupportedOperationException( "Not supported yet." );
362         }
363 
364 
365         @Override
366         public String getAuthType()
367         {
368             throw new UnsupportedOperationException( "Not supported yet." );
369         }
370 
371         @Override
372         public String getContextPath()
373         {
374             return "/";
375         }
376 
377         @Override
378         public Cookie[] getCookies()
379         {
380             throw new UnsupportedOperationException( "Not supported yet." );
381         }
382 
383         @Override
384         public long getDateHeader( String arg0 )
385         {
386             throw new UnsupportedOperationException( "Not supported yet." );
387         }
388 
389         @Override
390         public String getHeader( String arg0 )
391         {
392             return "";
393         }
394 
395         @Override
396         public Enumeration getHeaderNames()
397         {
398             throw new UnsupportedOperationException( "Not supported yet." );
399         }
400 
401         @Override
402         public Enumeration getHeaders( String arg0 )
403         {
404             throw new UnsupportedOperationException( "Not supported yet." );
405         }
406 
407         @Override
408         public int getIntHeader( String arg0 )
409         {
410             throw new UnsupportedOperationException( "Not supported yet." );
411         }
412 
413         @Override
414         public String getMethod()
415         {
416             throw new UnsupportedOperationException( "Not supported yet." );
417         }
418 
419         @Override
420         public String getPathInfo()
421         {
422             throw new UnsupportedOperationException( "Not supported yet." );
423         }
424 
425         @Override
426         public String getPathTranslated()
427         {
428             throw new UnsupportedOperationException( "Not supported yet." );
429         }
430 
431         @Override
432         public String getQueryString()
433         {
434             throw new UnsupportedOperationException( "Not supported yet." );
435         }
436 
437         @Override
438         public String getRemoteUser()
439         {
440             throw new UnsupportedOperationException( "Not supported yet." );
441         }
442 
443         @Override
444         public String getRequestURI()
445         {
446             return "/";
447         }
448 
449         @Override
450         public StringBuffer getRequestURL()
451         {
452             throw new UnsupportedOperationException( "Not supported yet." );
453         }
454 
455         @Override
456         public String getRequestedSessionId()
457         {
458             throw new UnsupportedOperationException( "Not supported yet." );
459         }
460 
461         @Override
462         public String getServletPath()
463         {
464             throw new UnsupportedOperationException( "Not supported yet." );
465         }
466 
467         @Override
468         public HttpSession getSession( boolean arg0 )
469         {
470             throw new UnsupportedOperationException( "Not supported yet." );
471         }
472 
473         @Override
474         public HttpSession getSession()
475         {
476             throw new UnsupportedOperationException( "Not supported yet." );
477         }
478 
479         @Override
480         public Principal getUserPrincipal()
481         {
482             throw new UnsupportedOperationException( "Not supported yet." );
483         }
484 
485         @Override
486         public boolean isRequestedSessionIdFromCookie()
487         {
488             throw new UnsupportedOperationException( "Not supported yet." );
489         }
490 
491         @Override
492         public boolean isRequestedSessionIdFromURL()
493         {
494             throw new UnsupportedOperationException( "Not supported yet." );
495         }
496 
497         @Override
498         public boolean isRequestedSessionIdFromUrl()
499         {
500             throw new UnsupportedOperationException( "Not supported yet." );
501         }
502 
503         @Override
504         public boolean isRequestedSessionIdValid()
505         {
506             throw new UnsupportedOperationException( "Not supported yet." );
507         }
508 
509         @Override
510         public boolean isUserInRole( String arg0 )
511         {
512             throw new UnsupportedOperationException( "Not supported yet." );
513         }
514     }
515 
516     private class ServletAuthenticatorMock
517         implements ServletAuthenticator
518     {
519         @Override
520         public boolean isAuthenticated( HttpServletRequest arg0, AuthenticationResult arg1 )
521             throws AuthenticationException, AccountLockedException, MustChangePasswordException
522         {
523             return true;
524         }
525 
526         @Override
527         public boolean isAuthorized( HttpServletRequest request, SecuritySession securitySession, String repositoryId,
528                                      String permission )
529             throws AuthorizationException, UnauthorizedException
530         {
531             return true;
532         }
533 
534         @Override
535         public boolean isAuthorized( String principal, String repoId, String permission )
536             throws UnauthorizedException
537         {
538             return true;
539         }
540     }
541 
542     private class HttpAuthenticatorMock
543         extends HttpAuthenticator
544     {
545         @Override
546         public void challenge( HttpServletRequest arg0, HttpServletResponse arg1, String arg2,
547                                AuthenticationException arg3 )
548             throws IOException
549         {
550             //Do nothing
551         }
552 
553         @Override
554         public AuthenticationResult getAuthenticationResult( HttpServletRequest arg0, HttpServletResponse arg1 )
555             throws AuthenticationException, AccountLockedException, MustChangePasswordException
556         {
557             return new AuthenticationResult();
558         }
559 
560 
561         @Override
562         public AuthenticationResult authenticate( AuthenticationDataSource arg0, HttpSession httpSession )
563             throws AuthenticationException, AccountLockedException, MustChangePasswordException
564         {
565             return new AuthenticationResult();
566         }
567 
568         @Override
569         public void authenticate( HttpServletRequest arg0, HttpServletResponse arg1 )
570             throws AuthenticationException
571         {
572             //Do nothing
573         }
574 
575         @Override
576         public SecuritySession getSecuritySession( HttpSession httpSession )
577         {
578             return super.getSecuritySession( httpSession );
579         }
580 
581         @Override
582         public User getSessionUser( HttpSession httpSession )
583         {
584             return super.getSessionUser( httpSession );
585         }
586 
587         @Override
588         public boolean isAlreadyAuthenticated( HttpSession httpSession )
589         {
590             return super.isAlreadyAuthenticated( httpSession );
591         }
592     }
593 }