Commit b8e726e4dc5b1581c3b40d9093fdf37e6218b28e

Authored by Cleverson Sacramento
1 parent d2ac773f
Exists in master

IN PROGRESS - issue FWK-119: Testes da extensão Servlet

https://demoiselle.atlassian.net/browse/FWK-119
impl/extension/jsf/src/test/java/exception/handler/authentication/AuthenticationExceptionTest.java
@@ -59,7 +59,7 @@ public class AuthenticationExceptionTest { @@ -59,7 +59,7 @@ public class AuthenticationExceptionTest {
59 @ArquillianResource 59 @ArquillianResource
60 private URL deploymentUrl; 60 private URL deploymentUrl;
61 61
62 - private static final String PATH = "src/test/resources/security-authentication"; 62 + private static final String PATH = "src/test/resources/security.authentication.form-authentication";
63 63
64 @Deployment(testable = false) 64 @Deployment(testable = false)
65 public static WebArchive createDeployment() { 65 public static WebArchive createDeployment() {
impl/extension/servlet/src/test/java/security/SecurityServlet.java
@@ -1,41 +0,0 @@ @@ -1,41 +0,0 @@
1 -package security;  
2 -  
3 -import static org.apache.http.HttpStatus.SC_FORBIDDEN;  
4 -import static org.apache.http.HttpStatus.SC_OK;  
5 -  
6 -import java.io.IOException;  
7 -  
8 -import javax.servlet.ServletException;  
9 -import javax.servlet.annotation.WebServlet;  
10 -import javax.servlet.http.HttpServlet;  
11 -import javax.servlet.http.HttpServletRequest;  
12 -import javax.servlet.http.HttpServletResponse;  
13 -  
14 -import br.gov.frameworkdemoiselle.security.AuthenticationException;  
15 -import br.gov.frameworkdemoiselle.security.Credentials;  
16 -import br.gov.frameworkdemoiselle.security.SecurityContext;  
17 -import br.gov.frameworkdemoiselle.util.Beans;  
18 -  
19 -@WebServlet("/login")  
20 -public class SecurityServlet extends HttpServlet {  
21 -  
22 - private static final long serialVersionUID = 1L;  
23 -  
24 - @Override  
25 - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
26 - String result = request.getHeader("Authorization");  
27 - result = (result == null ? request.getHeader("authorization") : result);  
28 -  
29 - Credentials credentials = Beans.getReference(Credentials.class);  
30 - credentials.setUsername(request.getParameter("username"));  
31 - credentials.setPassword(request.getParameter("password"));  
32 -  
33 - try {  
34 - Beans.getReference(SecurityContext.class).login();  
35 - response.setStatus(SC_OK);  
36 -  
37 - } catch (AuthenticationException e) {  
38 - response.setStatus(SC_FORBIDDEN);  
39 - }  
40 - }  
41 -}  
impl/extension/servlet/src/test/java/security/SecurityTest.java
@@ -1,66 +0,0 @@ @@ -1,66 +0,0 @@
1 -package security;  
2 -  
3 -import static org.apache.http.HttpStatus.SC_FORBIDDEN;  
4 -import static org.apache.http.HttpStatus.SC_OK;  
5 -import static org.junit.Assert.assertEquals;  
6 -  
7 -import java.io.IOException;  
8 -import java.net.URISyntaxException;  
9 -import java.net.URL;  
10 -  
11 -import org.apache.http.HttpResponse;  
12 -import org.apache.http.client.ClientProtocolException;  
13 -import org.apache.http.client.methods.HttpGet;  
14 -import org.apache.http.client.utils.URIBuilder;  
15 -import org.apache.http.impl.client.HttpClientBuilder;  
16 -import org.jboss.arquillian.container.test.api.Deployment;  
17 -import org.jboss.arquillian.junit.Arquillian;  
18 -import org.jboss.arquillian.test.api.ArquillianResource;  
19 -import org.jboss.shrinkwrap.api.spec.WebArchive;  
20 -import org.junit.Test;  
21 -import org.junit.runner.RunWith;  
22 -  
23 -import test.Tests;  
24 -  
25 -import com.sun.enterprise.security.auth.login.FileLoginModule;  
26 -  
27 -@RunWith(Arquillian.class)  
28 -public class SecurityTest {  
29 -  
30 - private static final String PATH = "src/test/resources/security";  
31 -  
32 - @ArquillianResource  
33 - private URL deploymentUrl;  
34 -  
35 - @Deployment(testable = false)  
36 - public static WebArchive createDeployment() {  
37 - return Tests.createDeployment().addClasses(SecurityServlet.class, FileLoginModule.class)  
38 - .addAsWebInfResource(Tests.createFileAsset(PATH + "/web.xml"), "web.xml");  
39 - }  
40 -  
41 - @Test  
42 - public void loginSucessfull() throws ClientProtocolException, IOException, URISyntaxException {  
43 - URIBuilder uriBuilder = new URIBuilder(deploymentUrl + "/login");  
44 - uriBuilder.setParameter("username", "demoiselle");  
45 - uriBuilder.setParameter("password", "changeit");  
46 -  
47 - HttpGet httpGet = new HttpGet(uriBuilder.build());  
48 - HttpResponse httpResponse = HttpClientBuilder.create().build().execute(httpGet);  
49 -  
50 - int status = httpResponse.getStatusLine().getStatusCode();  
51 - assertEquals(SC_OK, status);  
52 - }  
53 -  
54 - @Test  
55 - public void loginFailed() throws ClientProtocolException, IOException, URISyntaxException {  
56 - URIBuilder uriBuilder = new URIBuilder(deploymentUrl + "/login");  
57 - uriBuilder.setParameter("username", "invalid");  
58 - uriBuilder.setParameter("password", "invalid");  
59 -  
60 - HttpGet get = new HttpGet(uriBuilder.build());  
61 - HttpResponse response = HttpClientBuilder.create().build().execute(get);  
62 -  
63 - int status = response.getStatusLine().getStatusCode();  
64 - assertEquals(SC_FORBIDDEN, status);  
65 - }  
66 -}  
impl/extension/servlet/src/test/java/security/authentication/form/HelperServlet.java 0 → 100644
@@ -0,0 +1,41 @@ @@ -0,0 +1,41 @@
  1 +package security.authentication.form;
  2 +
  3 +import static org.apache.http.HttpStatus.SC_FORBIDDEN;
  4 +import static org.apache.http.HttpStatus.SC_OK;
  5 +
  6 +import java.io.IOException;
  7 +
  8 +import javax.servlet.ServletException;
  9 +import javax.servlet.annotation.WebServlet;
  10 +import javax.servlet.http.HttpServlet;
  11 +import javax.servlet.http.HttpServletRequest;
  12 +import javax.servlet.http.HttpServletResponse;
  13 +
  14 +import br.gov.frameworkdemoiselle.security.AuthenticationException;
  15 +import br.gov.frameworkdemoiselle.security.Credentials;
  16 +import br.gov.frameworkdemoiselle.security.SecurityContext;
  17 +import br.gov.frameworkdemoiselle.util.Beans;
  18 +
  19 +@WebServlet("/login")
  20 +public class HelperServlet extends HttpServlet {
  21 +
  22 + private static final long serialVersionUID = 1L;
  23 +
  24 + @Override
  25 + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  26 + String result = request.getHeader("Authorization");
  27 + result = (result == null ? request.getHeader("authorization") : result);
  28 +
  29 + Credentials credentials = Beans.getReference(Credentials.class);
  30 + credentials.setUsername(request.getParameter("username"));
  31 + credentials.setPassword(request.getParameter("password"));
  32 +
  33 + try {
  34 + Beans.getReference(SecurityContext.class).login();
  35 + response.setStatus(SC_OK);
  36 +
  37 + } catch (AuthenticationException e) {
  38 + response.setStatus(SC_FORBIDDEN);
  39 + }
  40 + }
  41 +}
impl/extension/servlet/src/test/java/security/authentication/form/ServletAuthenticatorTest.java 0 → 100644
@@ -0,0 +1,66 @@ @@ -0,0 +1,66 @@
  1 +package security.authentication.form;
  2 +
  3 +import static org.apache.http.HttpStatus.SC_FORBIDDEN;
  4 +import static org.apache.http.HttpStatus.SC_OK;
  5 +import static org.junit.Assert.assertEquals;
  6 +
  7 +import java.io.IOException;
  8 +import java.net.URISyntaxException;
  9 +import java.net.URL;
  10 +
  11 +import org.apache.http.HttpResponse;
  12 +import org.apache.http.client.ClientProtocolException;
  13 +import org.apache.http.client.methods.HttpGet;
  14 +import org.apache.http.client.utils.URIBuilder;
  15 +import org.apache.http.impl.client.HttpClientBuilder;
  16 +import org.jboss.arquillian.container.test.api.Deployment;
  17 +import org.jboss.arquillian.junit.Arquillian;
  18 +import org.jboss.arquillian.test.api.ArquillianResource;
  19 +import org.jboss.shrinkwrap.api.spec.WebArchive;
  20 +import org.junit.Test;
  21 +import org.junit.runner.RunWith;
  22 +
  23 +import test.Tests;
  24 +
  25 +import com.sun.enterprise.security.auth.login.FileLoginModule;
  26 +
  27 +@RunWith(Arquillian.class)
  28 +public class ServletAuthenticatorTest {
  29 +
  30 + private static final String PATH = "src/test/resources/security/authentication/form";
  31 +
  32 + @ArquillianResource
  33 + private URL deploymentUrl;
  34 +
  35 + @Deployment(testable = false)
  36 + public static WebArchive createDeployment() {
  37 + return Tests.createDeployment().addClasses(HelperServlet.class, FileLoginModule.class)
  38 + .addAsWebInfResource(Tests.createFileAsset(PATH + "/web.xml"), "web.xml");
  39 + }
  40 +
  41 + @Test
  42 + public void loginSucessfull() throws ClientProtocolException, IOException, URISyntaxException {
  43 + URIBuilder uriBuilder = new URIBuilder(deploymentUrl + "/helper");
  44 + uriBuilder.setParameter("username", "demoiselle");
  45 + uriBuilder.setParameter("password", "changeit");
  46 +
  47 + HttpGet httpGet = new HttpGet(uriBuilder.build());
  48 + HttpResponse httpResponse = HttpClientBuilder.create().build().execute(httpGet);
  49 +
  50 + int status = httpResponse.getStatusLine().getStatusCode();
  51 + assertEquals(SC_OK, status);
  52 + }
  53 +
  54 + @Test
  55 + public void loginFailed() throws ClientProtocolException, IOException, URISyntaxException {
  56 + URIBuilder uriBuilder = new URIBuilder(deploymentUrl + "/helper");
  57 + uriBuilder.setParameter("username", "invalid");
  58 + uriBuilder.setParameter("password", "invalid");
  59 +
  60 + HttpGet get = new HttpGet(uriBuilder.build());
  61 + HttpResponse response = HttpClientBuilder.create().build().execute(get);
  62 +
  63 + int status = response.getStatusLine().getStatusCode();
  64 + assertEquals(SC_FORBIDDEN, status);
  65 + }
  66 +}
impl/extension/servlet/src/test/resources/security/authentication/basic/web.xml
@@ -57,25 +57,5 @@ @@ -57,25 +57,5 @@
57 <servlet-mapping> 57 <servlet-mapping>
58 <servlet-name>Helper Servlet</servlet-name> 58 <servlet-name>Helper Servlet</servlet-name>
59 <url-pattern>/helper</url-pattern> 59 <url-pattern>/helper</url-pattern>
60 - </servlet-mapping>  
61 -  
62 - <!--  
63 - <security-constraint>  
64 - <web-resource-collection>  
65 - <web-resource-name>Private URL</web-resource-name>  
66 - <url-pattern>/helper</url-pattern>  
67 - </web-resource-collection>  
68 - <auth-constraint>  
69 - <role-name>*</role-name>  
70 - </auth-constraint>  
71 - </security-constraint>  
72 -  
73 - <login-config>  
74 - <auth-method>BASIC</auth-method>  
75 - </login-config>  
76 -  
77 - <security-role>  
78 - <role-name>*</role-name>  
79 - </security-role>  
80 - --> 60 + </servlet-mapping>
81 </web-app> 61 </web-app>
82 \ No newline at end of file 62 \ No newline at end of file
impl/extension/servlet/src/test/resources/security/authentication/form/web.xml 0 → 100644
@@ -0,0 +1,61 @@ @@ -0,0 +1,61 @@
  1 +<!--
  2 + Demoiselle Framework
  3 + Copyright (C) 2010 SERPRO
  4 + ============================================================================
  5 + This file is part of Demoiselle Framework.
  6 +
  7 + Demoiselle Framework is free software; you can redistribute it and/or
  8 + modify it under the terms of the GNU Lesser General Public License version 3
  9 + as published by the Free Software Foundation.
  10 +
  11 + This program 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
  14 + GNU General Public License for more details.
  15 +
  16 + You should have received a copy of the GNU Lesser General Public License version 3
  17 + along with this program; if not, see <http://www.gnu.org/licenses />
  18 + or write to the Free Software Foundation, Inc., 51 Franklin Street,
  19 + Fifth Floor, Boston, MA 02110-1301, USA.
  20 + ============================================================================
  21 + Este arquivo é parte do Framework Demoiselle.
  22 +
  23 + O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou
  24 + modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação
  25 + do Software Livre (FSF).
  26 +
  27 + Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA
  28 + GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou
  29 + APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português
  30 + para maiores detalhes.
  31 +
  32 + Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título
  33 + "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses />
  34 + ou escreva para a Fundação do Software Livre (FSF) Inc.,
  35 + 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.
  36 +-->
  37 +<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  38 + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  39 +
  40 + <listener>
  41 + <listener-class>br.gov.frameworkdemoiselle.util.ServletListener</listener-class>
  42 + </listener>
  43 +
  44 + <filter>
  45 + <filter-name>Demoiselle Servlet Filter</filter-name>
  46 + <filter-class>br.gov.frameworkdemoiselle.util.ServletFilter</filter-class>
  47 + </filter>
  48 + <filter-mapping>
  49 + <filter-name>Demoiselle Servlet Filter</filter-name>
  50 + <url-pattern>/*</url-pattern>
  51 + </filter-mapping>
  52 +
  53 + <servlet>
  54 + <servlet-name>Helper Servlet</servlet-name>
  55 + <servlet-class>security.authentication.form.HelperServlet</servlet-class>
  56 + </servlet>
  57 + <servlet-mapping>
  58 + <servlet-name>Helper Servlet</servlet-name>
  59 + <url-pattern>/helper</url-pattern>
  60 + </servlet-mapping>
  61 +</web-app>
0 \ No newline at end of file 62 \ No newline at end of file
impl/extension/servlet/src/test/resources/security/web.xml
@@ -1,82 +0,0 @@ @@ -1,82 +0,0 @@
1 -<!--  
2 - Demoiselle Framework  
3 - Copyright (C) 2010 SERPRO  
4 - ============================================================================  
5 - This file is part of Demoiselle Framework.  
6 -  
7 - Demoiselle Framework is free software; you can redistribute it and/or  
8 - modify it under the terms of the GNU Lesser General Public License version 3  
9 - as published by the Free Software Foundation.  
10 -  
11 - This program 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  
14 - GNU General Public License for more details.  
15 -  
16 - You should have received a copy of the GNU Lesser General Public License version 3  
17 - along with this program; if not, see <http://www.gnu.org/licenses />  
18 - or write to the Free Software Foundation, Inc., 51 Franklin Street,  
19 - Fifth Floor, Boston, MA 02110-1301, USA.  
20 - ============================================================================  
21 - Este arquivo é parte do Framework Demoiselle.  
22 -  
23 - O Framework Demoiselle é um software livre; você pode redistribuí-lo e/ou  
24 - modificá-lo dentro dos termos da GNU LGPL versão 3 como publicada pela Fundação  
25 - do Software Livre (FSF).  
26 -  
27 - Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA  
28 - GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou  
29 - APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU/LGPL em português  
30 - para maiores detalhes.  
31 -  
32 - Você deve ter recebido uma cópia da GNU LGPL versão 3, sob o título  
33 - "LICENCA.txt", junto com esse programa. Se não, acesse <http://www.gnu.org/licenses />  
34 - ou escreva para a Fundação do Software Livre (FSF) Inc.,  
35 - 51 Franklin St, Fifth Floor, Boston, MA 02111-1301, USA.  
36 --->  
37 -<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
38 - xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">  
39 -  
40 - <listener>  
41 - <listener-class>br.gov.frameworkdemoiselle.util.ServletListener</listener-class>  
42 - </listener>  
43 -  
44 - <filter>  
45 - <filter-name>Demoiselle Servlet Filter</filter-name>  
46 - <filter-class>br.gov.frameworkdemoiselle.util.ServletFilter</filter-class>  
47 - </filter>  
48 - <filter-mapping>  
49 - <filter-name>Demoiselle Servlet Filter</filter-name>  
50 - <url-pattern>/*</url-pattern>  
51 - </filter-mapping>  
52 -  
53 - <servlet>  
54 - <servlet-name>Security Class</servlet-name>  
55 - <servlet-class>security.SecurityServlet</servlet-class>  
56 - </servlet>  
57 - <servlet-mapping>  
58 - <servlet-name>Security Class</servlet-name>  
59 - <url-pattern>/login</url-pattern>  
60 - </servlet-mapping>  
61 -  
62 - <!--  
63 - <security-constraint>  
64 - <web-resource-collection>  
65 - <web-resource-name>Private URL</web-resource-name>  
66 - <url-pattern>/login</url-pattern>  
67 - </web-resource-collection>  
68 - <auth-constraint>  
69 - <role-name>*</role-name>  
70 - </auth-constraint>  
71 - </security-constraint>  
72 -  
73 - <login-config>  
74 - <auth-method>BASIC</auth-method>  
75 - <realm-name>fileRealm</realm-name>  
76 - </login-config>  
77 -  
78 - <security-role>  
79 - <role-name>*</role-name>  
80 - </security-role>  
81 - -->  
82 -</web-app>  
83 \ No newline at end of file 0 \ No newline at end of file