View Javadoc

1   /*
2    * SymmetricDS is an open source database synchronization solution.
3    *   
4    * Copyright (C) Keith Naas <knaas@users.sourceforge.net>
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 3 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, see
18   * <http://www.gnu.org/licenses/>.
19   */
20  
21  package org.jumpmind.symmetric.web;
22  
23  import java.util.Arrays;
24  import java.util.Collection;
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.jumpmind.symmetric.test.TestSetupUtil;
31  import org.junit.Assert;
32  import org.junit.Test;
33  import org.junit.runner.RunWith;
34  import org.junit.runners.Parameterized;
35  import org.junit.runners.Parameterized.Parameters;
36  import org.springframework.mock.web.MockFilterChain;
37  import org.springframework.mock.web.MockFilterConfig;
38  import org.springframework.mock.web.MockHttpServletRequest;
39  import org.springframework.mock.web.MockHttpServletResponse;
40  
41  /***
42   * This simply makes sure the SymmetricFilter is setup correctly.
43   */
44  @RunWith(Parameterized.class)
45  public class AuthenticationFilterTest extends AbstractSymmetricFilterTest {
46  
47      public AuthenticationFilterTest(String method, String uri, Map<String, String> parameters) throws Exception {
48          super(method, uri, parameters);
49      }
50  
51      @Parameters
52      public static Collection<Object[]> authenticationFilterParams() {
53          final Map<String, String> goodAuthentication = new HashMap<String, String>();
54          goodAuthentication.put(WebConstants.SECURITY_TOKEN, MockNodeService.GOOD_SECURITY_TOKEN);
55          goodAuthentication.put(WebConstants.NODE_ID, MockNodeService.GOOD_NODE_ID);
56          return Arrays.asList(new Object[][] { { "GET", "/ack", goodAuthentication }, { "POST", "/ack", goodAuthentication },
57                  { "PUT", "/ack", goodAuthentication }, { "DELETE", "/ack", goodAuthentication },
58                  { "TRACE", "/ack", goodAuthentication }, { "OPTIONS", "/ack", goodAuthentication },
59                  { "HEAD", "/ack", goodAuthentication }, { "GET", "/pull", goodAuthentication },
60                  { "POST", "/pull", goodAuthentication }, { "PUT", "/pull", goodAuthentication },
61                  { "DELETE", "/pull", goodAuthentication }, { "TRACE", "/pull", goodAuthentication },
62                  { "OPTIONS", "/pull", goodAuthentication }, { "HEAD", "/pull", goodAuthentication },
63                  { "GET", "/push", goodAuthentication }, { "POST", "/push", goodAuthentication },
64                  { "PUT", "/push", goodAuthentication }, { "DELETE", "/push", goodAuthentication },
65                  { "TRACE", "/push", goodAuthentication }, { "OPTIONS", "/push", goodAuthentication },
66                  { "HEAD", "/push", goodAuthentication } });
67      }
68  
69      @Test
70      public void testAuthenticationFilter() throws Exception {
71  
72          final SymmetricFilter filter = new SymmetricFilter();
73          filter.init(new MockFilterConfig(servletContext));
74  
75          final MockHttpServletRequest request = TestSetupUtil.createMockHttpServletRequest(servletContext, method, uri, parameters);
76          final MockHttpServletResponse response = new MockHttpServletResponse();
77          filter.doFilter(request, response, new MockFilterChain());
78          Assert.assertEquals(response.getStatus(), HttpServletResponse.SC_FORBIDDEN);
79          filter.destroy();
80      }
81  
82  }