Commit 2dbf503c356c147963aff22372dbcb1e07c187cd

Authored by Luciano Borges
1 parent b8e726e4
Exists in master

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

https://demoiselle.atlassian.net/browse/FWK-119
impl/extension/servlet/src/test/java/security/authentication/form/HelperServlet.java
@@ -16,7 +16,6 @@ import br.gov.frameworkdemoiselle.security.Credentials; @@ -16,7 +16,6 @@ import br.gov.frameworkdemoiselle.security.Credentials;
16 import br.gov.frameworkdemoiselle.security.SecurityContext; 16 import br.gov.frameworkdemoiselle.security.SecurityContext;
17 import br.gov.frameworkdemoiselle.util.Beans; 17 import br.gov.frameworkdemoiselle.util.Beans;
18 18
19 -@WebServlet("/login")  
20 public class HelperServlet extends HttpServlet { 19 public class HelperServlet extends HttpServlet {
21 20
22 private static final long serialVersionUID = 1L; 21 private static final long serialVersionUID = 1L;
impl/extension/servlet/src/test/java/security/unauthentication/form/HelperServletUnauthenticationFail.java 0 → 100644
@@ -0,0 +1,37 @@ @@ -0,0 +1,37 @@
  1 +package security.unauthentication.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.http.HttpServlet;
  10 +import javax.servlet.http.HttpServletRequest;
  11 +import javax.servlet.http.HttpServletResponse;
  12 +
  13 +import br.gov.frameworkdemoiselle.security.AuthenticationException;
  14 +import br.gov.frameworkdemoiselle.security.Credentials;
  15 +import br.gov.frameworkdemoiselle.security.SecurityContext;
  16 +import br.gov.frameworkdemoiselle.util.Beans;
  17 +
  18 +public class HelperServletUnauthenticationFail extends HttpServlet {
  19 +
  20 + private static final long serialVersionUID = 1L;
  21 +
  22 + @Override
  23 + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  24 + String result = request.getHeader("Authorization");
  25 + result = (result == null ? request.getHeader("authorization") : result);
  26 +
  27 + Credentials credentials = Beans.getReference(Credentials.class);
  28 + credentials.setUsername(request.getParameter("username"));
  29 + credentials.setPassword(request.getParameter("password"));
  30 + try {
  31 + Beans.getReference(SecurityContext.class).logout();
  32 + response.setStatus(SC_OK);
  33 + } catch (AuthenticationException e) {
  34 + response.setStatus(SC_FORBIDDEN);
  35 + }
  36 + }
  37 +}
impl/extension/servlet/src/test/java/security/unauthentication/form/HelperServletUnauthenticationSuccess.java 0 → 100644
@@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
  1 +package security.unauthentication.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 +public class HelperServletUnauthenticationSuccess extends HttpServlet {
  20 +
  21 + private static final long serialVersionUID = 1L;
  22 +
  23 + @Override
  24 + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  25 + String result = request.getHeader("Authorization");
  26 + result = (result == null ? request.getHeader("authorization") : result);
  27 +
  28 + Credentials credentials = Beans.getReference(Credentials.class);
  29 + credentials.setUsername(request.getParameter("username"));
  30 + credentials.setPassword(request.getParameter("password"));
  31 + try {
  32 + Beans.getReference(SecurityContext.class).login();
  33 + Beans.getReference(SecurityContext.class).logout();
  34 + response.setStatus(SC_OK);
  35 + } catch (AuthenticationException e) {
  36 + response.setStatus(SC_FORBIDDEN);
  37 + }
  38 + }
  39 +}
impl/extension/servlet/src/test/java/security/unauthentication/form/ServletAuthenticatorTest.java 0 → 100644
@@ -0,0 +1,68 @@ @@ -0,0 +1,68 @@
  1 +package security.unauthentication.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/unauthentication/form";
  31 +
  32 + @ArquillianResource
  33 + private URL deploymentUrl;
  34 +
  35 + @Deployment(testable = false)
  36 + public static WebArchive createDeployment() {
  37 + return Tests.createDeployment().addClasses(HelperServletUnauthenticationSuccess.class, FileLoginModule.class)
  38 + .addAsWebInfResource(Tests.createFileAsset(PATH + "/web.xml"), "web.xml");
  39 + }
  40 +
  41 + @Test
  42 + public void logoutSucessfull() throws ClientProtocolException, IOException, URISyntaxException {
  43 + URIBuilder uriBuilder = new URIBuilder(deploymentUrl + "/helperauthsuccess");
  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 logoutFailed() throws ClientProtocolException, IOException, URISyntaxException {
  56 + URIBuilder uriBuilder = new URIBuilder(deploymentUrl + "/helperauthfail");
  57 + uriBuilder.setParameter("username", "demoiselle");
  58 + uriBuilder.setParameter("password", "changeit");
  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 +
  67 +
  68 +}
impl/extension/servlet/src/test/resources/security/unauthentication/form/web.xml 0 → 100644
@@ -0,0 +1,71 @@ @@ -0,0 +1,71 @@
  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 Unauth Success</servlet-name>
  55 + <servlet-class>security.unauthentication.form.HelperServletUnauthenticationSuccess</servlet-class>
  56 + </servlet>
  57 + <servlet-mapping>
  58 + <servlet-name>Helper Servlet Unauth Success</servlet-name>
  59 + <url-pattern>/helperauthsuccess</url-pattern>
  60 + </servlet-mapping>
  61 +
  62 + <servlet>
  63 + <servlet-name>Helper Servlet Unauth Fail</servlet-name>
  64 + <servlet-class>security.unauthentication.form.HelperServletUnauthenticationFail</servlet-class>
  65 + </servlet>
  66 + <servlet-mapping>
  67 + <servlet-name>Helper Servlet Unauth Fail</servlet-name>
  68 + <url-pattern>/helperauthfail</url-pattern>
  69 + </servlet-mapping>
  70 +
  71 +</web-app>
0 \ No newline at end of file 72 \ No newline at end of file