Skip to content
This repository has been archived by the owner on Apr 8, 2019. It is now read-only.

Commit

Permalink
add test for login module
Browse files Browse the repository at this point in the history
  • Loading branch information
nttuyen committed Aug 1, 2013
1 parent 433fe98 commit 77506ec
Show file tree
Hide file tree
Showing 46 changed files with 5,326 additions and 1 deletion.
6 changes: 6 additions & 0 deletions portal/web/pom.xml
Expand Up @@ -281,6 +281,12 @@
<version>2.14.1</version>
<configuration>
<reuseForks>false</reuseForks>
<systemProperties>
<property>
<name>java.security.auth.login.config</name>
<value>${basedir}/src/main/tomcatconf/jaas.conf</value>
</property>
</systemProperties>
</configuration>
</plugin>

Expand Down
Expand Up @@ -14,7 +14,7 @@
<button type="button" class="close" data-dismiss="alert">&times;</button>
${message}
</div>
<form method="post" action="@{Controller.actionLogin()}">
<form id="login-form" method="post" action="@{Controller.actionLogin()}">
<table>
<tbody>
<tr>
Expand Down
183 changes: 183 additions & 0 deletions portal/web/src/test/java/org/gatein/portal/PortalLoginTestCase.java
@@ -0,0 +1,183 @@
package org.gatein.portal;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;

import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import junit.framework.AssertionFailedError;
import juzu.impl.common.RunMode;
import juzu.impl.common.Tools;
import juzu.impl.inject.spi.InjectorProvider;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.drone.api.annotation.Drone;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.junit.InSequence;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.Filters;
import org.jboss.shrinkwrap.api.GenericArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.importer.ExplodedImporter;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

/**
* Created with IntelliJ IDEA.
* User: tuyennt
* Date: 7/4/13
* Time: 10:35 AM
* To change this template use File | Settings | File Templates.
*/
@RunWith(Arquillian.class)
public class PortalLoginTestCase extends AbstractPortalTestCase {

@Deployment(testable = false)
public static WebArchive createPortal() {
//WebArchive portal = AbstractPortalTestCase.createPortal(InjectorProvider.INJECT_GUICE, RunMode.DEV);
WebArchive portal = ShrinkWrap.create(WebArchive.class, "portal.war");

String servlet;
try {
servlet = Tools.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("web.xml"));
} catch (IOException e) {
AssertionFailedError afe = new AssertionFailedError("Could not read web xml deployment descriptor");
afe.initCause(e);
throw afe;
}

servlet = String.format(servlet, InjectorProvider.INJECT_GUICE.getValue(), RunMode.DEV.getValue());

portal.setWebXML(new StringAsset(servlet));

portal.merge(ShrinkWrap.
create(GenericArchive.class).
as(ExplodedImporter.class).
importDirectory("src/test/resources/WEB-INF").
as(GenericArchive.class), "/WEB-INF", Filters.exclude("web.xml"));
portal.merge(ShrinkWrap.
create(GenericArchive.class).
as(ExplodedImporter.class).
importDirectory("src/test/resources/META-INF").
as(GenericArchive.class), "/META-INF", Filters.exclude("web.xml"));

portal.addAsWebInfResource(new StringAsset(descriptor(Portlet1.class).exportAsString()), "portlet.xml");

return portal;
}

@ArquillianResource
URL deploymentURL;

@Drone
WebDriver driver;

@Test
@RunAsClient
public void testHasLoginForm() {
String url = deploymentURL.toString() + "/login";
driver.get(url);
WebElement form = driver.findElement(By.tagName("form"));
WebElement username = form.findElement(By.name("username"));
WebElement password = form.findElement(By.name("password"));

Assert.assertNotNull(form);
Assert.assertNotNull(username);
Assert.assertNotNull(password);
}

@Test
@RunAsClient
public void testNotLogin() throws InterruptedException, URISyntaxException, MalformedURLException {
String portletURL = deploymentURL + "page1";
driver.get(portletURL);
WebElement body = driver.findElement(By.tagName("body"));
WebElement linkElement = driver.findElement(By.id("login-user"));
Assert.assertNotNull(linkElement);
String username = linkElement.getText();
Assert.assertEquals("__GUEST__", username);
}

@Test
@RunAsClient
public void testDoLogin() throws InterruptedException {
String url = deploymentURL.toString() + "/dologin?initURL="+ deploymentURL + "page1";
driver.get(url);
WebElement form = driver.findElement(By.tagName("form"));
WebElement username = form.findElement(By.name("username"));
WebElement password = form.findElement(By.name("password"));

Assert.assertNotNull(form);
Assert.assertNotNull(username);
Assert.assertNotNull(password);

username.sendKeys("root");
password.sendKeys("gtn");
form.submit();

String portletURL = deploymentURL + "page1";
driver.get(portletURL);

WebElement linkElement = driver.findElement(By.id("login-user"));
Assert.assertNotNull(linkElement);
String name = linkElement.getText();
Assert.assertEquals("root", name);
}

@Test
@RunAsClient
public void testLoginFailure() {
String url = deploymentURL.toString() + "/dologin?initURL="+ deploymentURL + "page1";
driver.get(url);
WebElement form = driver.findElement(By.tagName("form"));
WebElement username = form.findElement(By.name("username"));
WebElement password = form.findElement(By.name("password"));

Assert.assertNotNull(form);
Assert.assertNotNull(username);
Assert.assertNotNull(password);

username.sendKeys("root");
password.sendKeys("gtn1111");
form.submit();

WebElement body = driver.findElement(By.tagName("body"));
Assert.assertTrue(body.getText().contains("Username or password incorrect!"));

String portletURL = deploymentURL + "page1";
driver.get(portletURL);

WebElement linkElement = driver.findElement(By.id("login-user"));
Assert.assertNotNull(linkElement);
String name = linkElement.getText();
Assert.assertEquals("__GUEST__", name);
}


public static class Portlet1 extends GenericPortlet {
@Override
protected void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
String username = request.getRemoteUser();
if(username == null || username.isEmpty()) {
username = "__GUEST__";
}
writer.append("<span id='login-user'>"+username+"</span>");
writer.close();
}
}
}
31 changes: 31 additions & 0 deletions portal/web/src/test/resources/META-INF/context.xml
@@ -0,0 +1,31 @@
<!--
Copyright (C) 2009 eXo Platform SAS.
This is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of
the License, or (at your option) any later version.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this software; if not, write to the Free
Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA, or see the FSF site: http://www.fsf.org.
-->

<Context privileged="true">
<Realm className='org.apache.catalina.realm.JAASRealm'
appName='gatein-domain'
userClassNames='org.exoplatform.services.security.jaas.UserPrincipal'
roleClassNames='org.exoplatform.services.security.jaas.RolePrincipal'/>
<Valve
className='org.apache.catalina.authenticator.FormAuthenticator'
characterEncoding='UTF-8'/>
<!--org.gatein.portal.jaas.BytesLoungeLoginModule required debug=true;-->
</Context>
@@ -0,0 +1,96 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.picketlink.idm.impl.model.hibernate.HibernateIdentityObject"
table="jbid_io">
<cache usage="transactional"/>
<id name="id"
type="java.lang.Long"
access="field">
<column name="ID"/>
<generator class="native"/>
</id>
<set name="attributes"
batch-size="20"
inverse="true"
cascade="all, delete-orphan"
lazy="extra"
fetch="subselect">
<cache usage="transactional"/>
<key>
<column name="IDENTITY_OBJECT_ID"/>
</key>
<one-to-many class="org.picketlink.idm.impl.model.hibernate.HibernateIdentityObjectAttribute"/>
</set>
<set name="credentials"
inverse="true"
cascade="all, delete-orphan"
lazy="extra"
fetch="subselect">
<cache usage="transactional"/>
<key>
<column name="IDENTITY_OBJECT_ID"/>
</key>
<one-to-many class="org.picketlink.idm.impl.model.hibernate.HibernateIdentityObjectCredential"/>
</set>
<set name="fromRelationships"
inverse="true"
lazy="extra"
fetch="subselect">
<cache usage="transactional"/>
<key>
<column name="FROM_IDENTITY"/>
</key>
<one-to-many class="org.picketlink.idm.impl.model.hibernate.HibernateIdentityObjectRelationship"/>
</set>
<many-to-one name="identityType"
class="org.picketlink.idm.impl.model.hibernate.HibernateIdentityObjectType"
access="field"
fetch="join"
lazy="false">
<column name="IDENTITY_TYPE"
not-null="true"
unique-key="id"/>
</many-to-one>
<property name="name"
type="java.lang.String"
access="field"
lazy="false">
<column name="NAME"
not-null="true"
unique-key="id"/>
</property>
<map name="properties"
table="jbid_io_props"
cascade="all, delete-orphan"
lazy="extra"
fetch="subselect">
<cache usage="transactional"/>
<key column="PROP_ID"/>
<map-key type="string"
column="PROP_NAME"/>
<element type="string"
column="PROP_VALUE"
not-null="true"/>
</map>
<many-to-one name="realm"
class="org.picketlink.idm.impl.model.hibernate.HibernateRealm"
access="field"
fetch="select">
<column name="REALM"
not-null="true"
unique-key="id"/>
</many-to-one>
<set name="toRelationships"
inverse="true"
lazy="extra"
fetch="subselect">
<cache usage="transactional"/>
<key>
<column name="TO_IDENTITY"/>
</key>
<one-to-many class="org.picketlink.idm.impl.model.hibernate.HibernateIdentityObjectRelationship"/>
</set>
</class>
</hibernate-mapping>
@@ -0,0 +1,56 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.picketlink.idm.impl.model.hibernate.HibernateIdentityObjectAttribute"
table="jbid_io_attr">
<cache usage="transactional"/>
<id name="id"
type="java.lang.Long"
access="field">
<column name="ATTRIBUTE_ID"/>
<generator class="native"/>
</id>
<many-to-one name="identityObject"
class="org.picketlink.idm.impl.model.hibernate.HibernateIdentityObject"
access="field"
fetch="join">
<column name="IDENTITY_OBJECT_ID" not-null="true" unique-key="id"/>
</many-to-one>
<property name="name"
type="java.lang.String"
access="property"
lazy="false">
<column name="NAME"
unique-key="id"/>
</property>
<property name="type"
type="java.lang.String"
access="field"
lazy="false"
not-null="true">
<column name="ATTRIBUTE_TYPE"/>
</property>
<set name="textValues"
table="jbid_io_attr_text_values"
cascade="all, delete-orphan"
access="field"
lazy="false"
fetch="join"
batch-size="20">
<cache usage="transactional"/>
<key column="TEXT_ATTR_VALUE_ID"/>
<element type="string"
column="ATTR_VALUE"/>
</set>
<many-to-one name="binaryValue"
class="org.picketlink.idm.impl.model.hibernate.HibernateIdentityObjectAttributeBinaryValue"
not-null="false"
column="BIN_VALUE_ID"
unique="false"
lazy="proxy"
access="field"
fetch="select"
cascade="all"/>
</class>
</hibernate-mapping>

0 comments on commit 77506ec

Please sign in to comment.