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