]> source.dussan.org Git - archiva.git/blob
430bbdac873192d7b790df81849b77d5308ca6d9
[archiva.git] /
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.RequestDispatcher;
39 import javax.servlet.ServletInputStream;
40 import javax.servlet.http.Cookie;
41 import javax.servlet.http.HttpServletRequest;
42 import javax.servlet.http.HttpServletResponse;
43 import javax.servlet.http.HttpSession;
44 import java.io.BufferedReader;
45 import java.io.IOException;
46 import java.io.UnsupportedEncodingException;
47 import java.security.Principal;
48 import java.util.Enumeration;
49 import java.util.Locale;
50 import java.util.Map;
51 import org.apache.archiva.test.ArchivaBlockJUnit4ClassRunner;
52 import org.junit.Before;
53 import org.junit.Test;
54 import org.junit.runner.RunWith;
55
56 @RunWith( ArchivaBlockJUnit4ClassRunner.class )
57 public class ArchivaDavSessionProviderTest
58     extends TestCase
59 {
60     private DavSessionProvider sessionProvider;
61
62     private WebdavRequest request;
63
64     @Override
65     @Before
66     public void setUp()
67         throws Exception
68     {
69         super.setUp();
70         sessionProvider = new ArchivaDavSessionProvider( new ServletAuthenticatorMock(), new HttpAuthenticatorMock() );
71         request = new WebdavRequestImpl( new HttpServletRequestMock(), null );
72     }
73
74     @Test
75     public void testAttachSession()
76         throws Exception
77     {
78         assertNull( request.getDavSession() );
79         sessionProvider.attachSession( request );
80         assertNotNull( request.getDavSession() );
81     }
82
83     @Test
84     public void testReleaseSession()
85         throws Exception
86     {
87         assertNull( request.getDavSession() );
88         sessionProvider.attachSession( request );
89         assertNotNull( request.getDavSession() );
90
91         sessionProvider.releaseSession( request );
92         assertNull( request.getDavSession() );
93     }
94
95     @SuppressWarnings( "unchecked" )
96     private class HttpServletRequestMock
97         implements HttpServletRequest
98     {
99         public Object getAttribute( String arg0 )
100         {
101             throw new UnsupportedOperationException( "Not supported yet." );
102         }
103
104         public Enumeration getAttributeNames()
105         {
106             throw new UnsupportedOperationException( "Not supported yet." );
107         }
108
109         public String getCharacterEncoding()
110         {
111             throw new UnsupportedOperationException( "Not supported yet." );
112         }
113
114         public int getContentLength()
115         {
116             throw new UnsupportedOperationException( "Not supported yet." );
117         }
118
119         public String getContentType()
120         {
121             throw new UnsupportedOperationException( "Not supported yet." );
122         }
123
124         public ServletInputStream getInputStream()
125             throws IOException
126         {
127             throw new UnsupportedOperationException( "Not supported yet." );
128         }
129
130         public String getLocalAddr()
131         {
132             throw new UnsupportedOperationException( "Not supported yet." );
133         }
134
135         public String getLocalName()
136         {
137             throw new UnsupportedOperationException( "Not supported yet." );
138         }
139
140         public int getLocalPort()
141         {
142             throw new UnsupportedOperationException( "Not supported yet." );
143         }
144
145         public Locale getLocale()
146         {
147             throw new UnsupportedOperationException( "Not supported yet." );
148         }
149
150         public Enumeration getLocales()
151         {
152             throw new UnsupportedOperationException( "Not supported yet." );
153         }
154
155         public String getParameter( String arg0 )
156         {
157             throw new UnsupportedOperationException( "Not supported yet." );
158         }
159
160         public Map getParameterMap()
161         {
162             throw new UnsupportedOperationException( "Not supported yet." );
163         }
164
165         public Enumeration getParameterNames()
166         {
167             throw new UnsupportedOperationException( "Not supported yet." );
168         }
169
170         public String[] getParameterValues( String arg0 )
171         {
172             throw new UnsupportedOperationException( "Not supported yet." );
173         }
174
175         public String getProtocol()
176         {
177             throw new UnsupportedOperationException( "Not supported yet." );
178         }
179
180         public BufferedReader getReader()
181             throws IOException
182         {
183             throw new UnsupportedOperationException( "Not supported yet." );
184         }
185
186         public String getRealPath( String arg0 )
187         {
188             throw new UnsupportedOperationException( "Not supported yet." );
189         }
190
191         public String getRemoteAddr()
192         {
193             throw new UnsupportedOperationException( "Not supported yet." );
194         }
195
196         public String getRemoteHost()
197         {
198             throw new UnsupportedOperationException( "Not supported yet." );
199         }
200
201         public int getRemotePort()
202         {
203             throw new UnsupportedOperationException( "Not supported yet." );
204         }
205
206         public RequestDispatcher getRequestDispatcher( String arg0 )
207         {
208             throw new UnsupportedOperationException( "Not supported yet." );
209         }
210
211         public String getScheme()
212         {
213             return "";
214         }
215
216         public String getServerName()
217         {
218             throw new UnsupportedOperationException( "Not supported yet." );
219         }
220
221         public int getServerPort()
222         {
223             throw new UnsupportedOperationException( "Not supported yet." );
224         }
225
226         public boolean isSecure()
227         {
228             throw new UnsupportedOperationException( "Not supported yet." );
229         }
230
231         public void removeAttribute( String arg0 )
232         {
233             throw new UnsupportedOperationException( "Not supported yet." );
234         }
235
236         public void setAttribute( String arg0, Object arg1 )
237         {
238             throw new UnsupportedOperationException( "Not supported yet." );
239         }
240
241         public void setCharacterEncoding( String arg0 )
242             throws UnsupportedEncodingException
243         {
244             throw new UnsupportedOperationException( "Not supported yet." );
245         }
246
247
248         public String getAuthType()
249         {
250             throw new UnsupportedOperationException( "Not supported yet." );
251         }
252
253         public String getContextPath()
254         {
255             return "/";
256         }
257
258         public Cookie[] getCookies()
259         {
260             throw new UnsupportedOperationException( "Not supported yet." );
261         }
262
263         public long getDateHeader( String arg0 )
264         {
265             throw new UnsupportedOperationException( "Not supported yet." );
266         }
267
268         public String getHeader( String arg0 )
269         {
270             return "";
271         }
272
273         public Enumeration getHeaderNames()
274         {
275             throw new UnsupportedOperationException( "Not supported yet." );
276         }
277
278         public Enumeration getHeaders( String arg0 )
279         {
280             throw new UnsupportedOperationException( "Not supported yet." );
281         }
282
283         public int getIntHeader( String arg0 )
284         {
285             throw new UnsupportedOperationException( "Not supported yet." );
286         }
287
288         public String getMethod()
289         {
290             throw new UnsupportedOperationException( "Not supported yet." );
291         }
292
293         public String getPathInfo()
294         {
295             throw new UnsupportedOperationException( "Not supported yet." );
296         }
297
298         public String getPathTranslated()
299         {
300             throw new UnsupportedOperationException( "Not supported yet." );
301         }
302
303         public String getQueryString()
304         {
305             throw new UnsupportedOperationException( "Not supported yet." );
306         }
307
308         public String getRemoteUser()
309         {
310             throw new UnsupportedOperationException( "Not supported yet." );
311         }
312
313         public String getRequestURI()
314         {
315             return "/";
316         }
317
318         public StringBuffer getRequestURL()
319         {
320             throw new UnsupportedOperationException( "Not supported yet." );
321         }
322
323         public String getRequestedSessionId()
324         {
325             throw new UnsupportedOperationException( "Not supported yet." );
326         }
327
328         public String getServletPath()
329         {
330             throw new UnsupportedOperationException( "Not supported yet." );
331         }
332
333         public HttpSession getSession( boolean arg0 )
334         {
335             throw new UnsupportedOperationException( "Not supported yet." );
336         }
337
338         public HttpSession getSession()
339         {
340             throw new UnsupportedOperationException( "Not supported yet." );
341         }
342
343         public Principal getUserPrincipal()
344         {
345             throw new UnsupportedOperationException( "Not supported yet." );
346         }
347
348         public boolean isRequestedSessionIdFromCookie()
349         {
350             throw new UnsupportedOperationException( "Not supported yet." );
351         }
352
353         public boolean isRequestedSessionIdFromURL()
354         {
355             throw new UnsupportedOperationException( "Not supported yet." );
356         }
357
358         public boolean isRequestedSessionIdFromUrl()
359         {
360             throw new UnsupportedOperationException( "Not supported yet." );
361         }
362
363         public boolean isRequestedSessionIdValid()
364         {
365             throw new UnsupportedOperationException( "Not supported yet." );
366         }
367
368         public boolean isUserInRole( String arg0 )
369         {
370             throw new UnsupportedOperationException( "Not supported yet." );
371         }
372     }
373
374     private class ServletAuthenticatorMock
375         implements ServletAuthenticator
376     {
377         public boolean isAuthenticated( HttpServletRequest arg0, AuthenticationResult arg1 )
378             throws AuthenticationException, AccountLockedException, MustChangePasswordException
379         {
380             return true;
381         }
382
383         public boolean isAuthorized( HttpServletRequest request, SecuritySession securitySession, String repositoryId,
384                                      String permission )
385             throws AuthorizationException, UnauthorizedException
386         {
387             return true;
388         }
389
390         public boolean isAuthorized( String principal, String repoId, String permission )
391             throws UnauthorizedException
392         {
393             return true;
394         }
395     }
396
397     private class HttpAuthenticatorMock
398         extends HttpAuthenticator
399     {
400         @Override
401         public void challenge( HttpServletRequest arg0, HttpServletResponse arg1, String arg2,
402                                AuthenticationException arg3 )
403             throws IOException
404         {
405             //Do nothing
406         }
407
408         @Override
409         public AuthenticationResult getAuthenticationResult( HttpServletRequest arg0, HttpServletResponse arg1 )
410             throws AuthenticationException, AccountLockedException, MustChangePasswordException
411         {
412             return new AuthenticationResult();
413         }
414
415
416         @Override
417         public AuthenticationResult authenticate( AuthenticationDataSource arg0, HttpSession httpSession )
418             throws AuthenticationException, AccountLockedException, MustChangePasswordException
419         {
420             return new AuthenticationResult();
421         }
422
423         @Override
424         public void authenticate( HttpServletRequest arg0, HttpServletResponse arg1 )
425             throws AuthenticationException
426         {
427             //Do nothing
428         }
429
430         @Override
431         public SecuritySession getSecuritySession( HttpSession httpSession )
432         {
433             return super.getSecuritySession( httpSession );
434         }
435
436         @Override
437         public User getSessionUser( HttpSession httpSession )
438         {
439             return super.getSessionUser( httpSession );
440         }
441
442         @Override
443         public boolean isAlreadyAuthenticated( HttpSession httpSession )
444         {
445             return super.isAlreadyAuthenticated( httpSession );
446         }
447     }
448 }