]> source.dussan.org Git - archiva.git/blob
374d953a8ef303ff13b477a7977044f328fdd19c
[archiva.git] /
1 package org.apache.archiva.web.rss;
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
23 import junit.framework.TestCase;
24 import org.apache.archiva.common.filelock.DefaultFileLockManager;
25 import org.apache.archiva.configuration.ArchivaConfiguration;
26 import org.apache.archiva.repository.base.BasicManagedRepository;
27 import org.apache.archiva.repository.RepositoryRegistry;
28 import org.apache.archiva.repository.storage.FilesystemStorage;
29 import org.apache.archiva.test.utils.ArchivaSpringJUnit4ClassRunner;
30 import org.apache.commons.codec.Encoder;
31 import org.apache.commons.codec.binary.Base64;
32 import org.junit.After;
33 import org.junit.AfterClass;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.springframework.beans.BeansException;
39 import org.springframework.beans.factory.BeanFactory;
40 import org.springframework.beans.factory.NoSuchBeanDefinitionException;
41 import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
42 import org.springframework.context.ApplicationContext;
43 import org.springframework.context.ApplicationEvent;
44 import org.springframework.context.MessageSourceResolvable;
45 import org.springframework.context.NoSuchMessageException;
46 import org.springframework.core.ResolvableType;
47 import org.springframework.core.env.Environment;
48 import org.springframework.core.io.Resource;
49 import org.springframework.mock.web.MockHttpServletRequest;
50 import org.springframework.mock.web.MockHttpServletResponse;
51 import org.springframework.mock.web.MockServletConfig;
52 import org.springframework.mock.web.MockServletContext;
53 import org.springframework.test.context.ContextConfiguration;
54 import org.springframework.web.context.WebApplicationContext;
55
56 import javax.inject.Inject;
57 import javax.servlet.ServletContext;
58 import javax.servlet.http.HttpServletResponse;
59 import java.io.IOException;
60 import java.lang.annotation.Annotation;
61 import java.nio.file.Paths;
62 import java.util.Locale;
63 import java.util.Map;
64
65 @RunWith( ArchivaSpringJUnit4ClassRunner.class )
66 @ContextConfiguration(
67     locations = { "classpath*:/META-INF/spring-context.xml", "classpath*:/spring-context-test-common.xml",
68         "classpath*:/spring-context-rss-servlet.xml" } )
69 public class RssFeedServletTest
70     extends TestCase
71 {
72     private RssFeedServlet rssFeedServlet = new RssFeedServlet();
73
74     static String PREVIOUS_ARCHIVA_PATH;
75
76     @Inject
77     protected ApplicationContext applicationContext;
78
79     @Inject
80     protected RepositoryRegistry repositoryRegistry;
81
82     @BeforeClass
83     public static void initConfigurationPath()
84         throws Exception
85     {
86         PREVIOUS_ARCHIVA_PATH = System.getProperty(ArchivaConfiguration.USER_CONFIG_PROPERTY);
87         System.setProperty( ArchivaConfiguration.USER_CONFIG_PROPERTY,
88                             System.getProperty( "test.resources.path" ) + "/empty-archiva.xml" );
89     }
90
91
92     @AfterClass
93     public static void restoreConfigurationPath()
94         throws Exception
95     {
96         System.setProperty( ArchivaConfiguration.USER_CONFIG_PROPERTY, PREVIOUS_ARCHIVA_PATH );
97     }
98
99     @Before
100     @Override
101     public void setUp()
102         throws Exception
103     {
104         final MockServletContext mockServletContext = new MockServletContext();
105
106         WebApplicationContext webApplicationContext =
107             new TestWebapplicationContext( applicationContext, mockServletContext );
108
109         mockServletContext.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
110                                          webApplicationContext );
111
112         MockServletConfig mockServletConfig = new MockServletConfig()
113         {
114             @Override
115             public ServletContext getServletContext()
116             {
117                 return mockServletContext;
118             }
119         };
120
121         repositoryRegistry.reload();
122         repositoryRegistry.putRepository( new BasicManagedRepository( "internal", "internal",
123             new FilesystemStorage( Paths.get( "target/appserver-base/repositories/internal" ), new DefaultFileLockManager( ) ) ) );
124         rssFeedServlet.init( mockServletConfig );
125     }
126
127     @After
128     @Override
129     public void tearDown()
130         throws Exception
131     {
132         super.tearDown();
133     }
134
135     public static class TestWebapplicationContext
136         implements WebApplicationContext
137     {
138         private ApplicationContext applicationContext;
139
140         private ServletContext servletContext;
141
142         TestWebapplicationContext( ApplicationContext applicationContext, ServletContext servletContext )
143         {
144             this.applicationContext = applicationContext;
145         }
146
147         @Override
148         public ServletContext getServletContext()
149         {
150             return servletContext;
151         }
152
153         @Override
154         public String getId()
155         {
156             return applicationContext.getId();
157         }
158
159         @Override
160         public String getApplicationName()
161         {
162             return applicationContext.getApplicationName();
163         }
164
165         @Override
166         public String getDisplayName()
167         {
168             return applicationContext.getDisplayName();
169         }
170
171         @Override
172         public long getStartupDate()
173         {
174             return applicationContext.getStartupDate();
175         }
176
177         @Override
178         public ApplicationContext getParent()
179         {
180             return applicationContext.getParent();
181         }
182
183         @Override
184         public AutowireCapableBeanFactory getAutowireCapableBeanFactory()
185             throws IllegalStateException
186         {
187             return applicationContext.getAutowireCapableBeanFactory();
188         }
189
190         @Override
191         public void publishEvent( ApplicationEvent applicationEvent )
192         {
193             applicationContext.publishEvent( applicationEvent );
194         }
195
196         @Override
197         public Environment getEnvironment()
198         {
199             return applicationContext.getEnvironment();
200         }
201
202         @Override
203         public BeanFactory getParentBeanFactory()
204         {
205             return applicationContext.getParentBeanFactory();
206         }
207
208         @Override
209         public boolean containsLocalBean( String s )
210         {
211             return applicationContext.containsLocalBean( s );
212         }
213
214         @Override
215         public boolean containsBeanDefinition( String s )
216         {
217             return applicationContext.containsBeanDefinition( s );
218         }
219
220         @Override
221         public int getBeanDefinitionCount()
222         {
223             return applicationContext.getBeanDefinitionCount();
224         }
225
226         @Override
227         public String[] getBeanDefinitionNames()
228         {
229             return applicationContext.getBeanDefinitionNames();
230         }
231
232         @Override
233         public String[] getBeanNamesForType( Class<?> aClass )
234         {
235             return applicationContext.getBeanNamesForType( aClass );
236         }
237
238         @Override
239         public String[] getBeanNamesForType( Class<?> aClass, boolean b, boolean b2 )
240         {
241             return applicationContext.getBeanNamesForType( aClass, b, b2 );
242         }
243
244         @Override
245         public <T> Map<String, T> getBeansOfType( Class<T> tClass )
246             throws BeansException
247         {
248             return applicationContext.getBeansOfType( tClass );
249         }
250
251         @Override
252         public <T> T getBean( Class<T> aClass, Object... objects )
253             throws BeansException
254         {
255             return applicationContext.getBean( aClass, objects );
256         }
257
258         @Override
259         public <T> Map<String, T> getBeansOfType( Class<T> tClass, boolean b, boolean b2 )
260             throws BeansException
261         {
262             return applicationContext.getBeansOfType( tClass, b, b2 );
263         }
264
265         @Override
266         public String[] getBeanNamesForAnnotation( Class<? extends Annotation> aClass )
267         {
268             return applicationContext.getBeanNamesForAnnotation( aClass );
269         }
270
271         @Override
272         public Map<String, Object> getBeansWithAnnotation( Class<? extends Annotation> aClass )
273             throws BeansException
274         {
275             return applicationContext.getBeansWithAnnotation( aClass );
276         }
277
278         @Override
279         public <A extends Annotation> A findAnnotationOnBean( String s, Class<A> aClass )
280             throws NoSuchBeanDefinitionException
281         {
282             return applicationContext.findAnnotationOnBean( s, aClass );
283         }
284
285         @Override
286         public Object getBean( String s )
287             throws BeansException
288         {
289             return applicationContext.getBean( s );
290         }
291
292         @Override
293         public <T> T getBean( String s, Class<T> tClass )
294             throws BeansException
295         {
296             return applicationContext.getBean( s, tClass );
297         }
298
299         @Override
300         public <T> T getBean( Class<T> tClass )
301             throws BeansException
302         {
303             return applicationContext.getBean( tClass );
304         }
305
306         @Override
307         public Object getBean( String s, Object... objects )
308             throws BeansException
309         {
310             return applicationContext.getBean( s, objects );
311         }
312
313         @Override
314         public boolean containsBean( String s )
315         {
316             return applicationContext.containsBean( s );
317         }
318
319         @Override
320         public boolean isSingleton( String s )
321             throws NoSuchBeanDefinitionException
322         {
323             return applicationContext.isSingleton( s );
324         }
325
326         @Override
327         public boolean isPrototype( String s )
328             throws NoSuchBeanDefinitionException
329         {
330             return applicationContext.isPrototype( s );
331         }
332
333         @Override
334         public boolean isTypeMatch( String s, Class<?> aClass )
335             throws NoSuchBeanDefinitionException
336         {
337             return applicationContext.isTypeMatch( s, aClass );
338         }
339
340         @Override
341         public Class<?> getType( String s )
342             throws NoSuchBeanDefinitionException
343         {
344             return applicationContext.getType( s );
345         }
346
347         @Override
348         public String[] getAliases( String s )
349         {
350             return applicationContext.getAliases( s );
351         }
352
353         @Override
354         public String getMessage( String s, Object[] objects, String s2, Locale locale )
355         {
356             return applicationContext.getMessage( s, objects, s2, locale );
357         }
358
359         @Override
360         public String getMessage( String s, Object[] objects, Locale locale )
361             throws NoSuchMessageException
362         {
363             return applicationContext.getMessage( s, objects, locale );
364         }
365
366         @Override
367         public String getMessage( MessageSourceResolvable messageSourceResolvable, Locale locale )
368             throws NoSuchMessageException
369         {
370             return applicationContext.getMessage( messageSourceResolvable, locale );
371         }
372
373         @Override
374         public Resource[] getResources( String s )
375             throws IOException
376         {
377             return applicationContext.getResources( s );
378         }
379
380         @Override
381         public Resource getResource( String s )
382         {
383             return applicationContext.getResource( s );
384         }
385
386         @Override
387         public ClassLoader getClassLoader()
388         {
389             return applicationContext.getClassLoader();
390         }
391
392         @Override
393         public void publishEvent( Object o )
394         {
395             // no op
396         }
397
398         @Override
399         public String[] getBeanNamesForType( ResolvableType resolvableType )
400         {
401             return new String[0];
402         }
403
404         @Override
405         public boolean isTypeMatch( String s, ResolvableType resolvableType )
406             throws NoSuchBeanDefinitionException
407         {
408             return false;
409         }
410     }
411
412
413     @Test
414     public void testRequestNewArtifactsInRepo()
415         throws Exception
416     {
417         MockHttpServletRequest request = new MockHttpServletRequest();
418         request.setRequestURI( "/feeds/test-repo" );
419         request.addHeader( "User-Agent", "Apache Archiva unit test" );
420         request.setMethod( "GET" );
421
422         Base64 encoder = new Base64( 0, new byte[0] );
423         String userPass = "user1:password1";
424         String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
425         request.addHeader( "Authorization", "BASIC " + encodedUserPass );
426
427         MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
428
429         rssFeedServlet.doGet( request, mockHttpServletResponse );
430
431         assertEquals( RssFeedServlet.MIME_TYPE, mockHttpServletResponse.getHeader( "CONTENT-TYPE" ) );
432         assertNotNull( "Should have recieved a response", mockHttpServletResponse.getContentAsString() );
433         assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK,
434                       mockHttpServletResponse.getStatus() );
435
436     }
437
438     @Test
439     public void testRequestNewVersionsOfArtifact()
440         throws Exception
441     {
442         MockHttpServletRequest request = new MockHttpServletRequest();
443         request.setRequestURI( "/feeds/org/apache/archiva/artifact-two" );
444         request.addHeader( "User-Agent", "Apache Archiva unit test" );
445         request.setMethod( "GET" );
446
447         //WebRequest request = new GetMethodWebRequest( "http://localhost/feeds/org/apache/archiva/artifact-two" );
448
449         Base64 encoder = new Base64( 0, new byte[0] );
450         String userPass = "user1:password1";
451         String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
452         request.addHeader( "Authorization", "BASIC " + encodedUserPass );
453
454         MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
455
456         rssFeedServlet.doGet( request, mockHttpServletResponse );
457
458         assertEquals( RssFeedServlet.MIME_TYPE, mockHttpServletResponse.getHeader( "CONTENT-TYPE" ) );
459         assertNotNull( "Should have recieved a response", mockHttpServletResponse.getContentAsString() );
460         assertEquals( "Should have been an OK response code.", HttpServletResponse.SC_OK,
461                       mockHttpServletResponse.getStatus() );
462     }
463
464     @Test
465     public void testInvalidRequest()
466         throws Exception
467     {
468         MockHttpServletRequest request = new MockHttpServletRequest();
469         request.setRequestURI( "/feeds?invalid_param=xxx" );
470         request.addHeader( "User-Agent", "Apache Archiva unit test" );
471         request.setMethod( "GET" );
472
473         MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
474
475         rssFeedServlet.doGet( request, mockHttpServletResponse );
476
477         assertEquals( HttpServletResponse.SC_BAD_REQUEST, mockHttpServletResponse.getStatus() );
478
479     }
480
481     @Test
482     public void testInvalidAuthenticationRequest()
483         throws Exception
484     {
485
486         MockHttpServletRequest request = new MockHttpServletRequest();
487         request.setRequestURI( "/feeds/unauthorized-repo" );
488         request.addHeader( "User-Agent", "Apache Archiva unit test" );
489         request.setMethod( "GET" );
490
491         Encoder encoder = new Base64();
492         String userPass = "unauthUser:unauthPass";
493         String encodedUserPass = new String( (byte[]) encoder.encode( userPass.getBytes() ) );
494         request.addHeader( "Authorization", "BASIC " + encodedUserPass );
495
496         MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
497         rssFeedServlet.doGet( request, mockHttpServletResponse );
498
499         assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
500
501     }
502
503     @Test
504     public void testUnauthorizedRequest()
505         throws Exception
506     {
507
508         MockHttpServletRequest request = new MockHttpServletRequest();
509         request.setRequestURI( "/feeds/unauthorized-repo" );
510         request.addHeader( "User-Agent", "Apache Archiva unit test" );
511         request.setMethod( "GET" );
512
513         Base64 encoder = new Base64( 0, new byte[0] );
514         String userPass = "user1:password1";
515         String encodedUserPass = encoder.encodeToString( userPass.getBytes() );
516         request.addHeader( "Authorization", "BASIC " + encodedUserPass );
517
518         MockHttpServletResponse mockHttpServletResponse = new MockHttpServletResponse();
519         rssFeedServlet.doGet( request, mockHttpServletResponse );
520
521         assertEquals( HttpServletResponse.SC_UNAUTHORIZED, mockHttpServletResponse.getStatus() );
522
523     }
524
525
526 }