package com.xpn.xwiki.it;

import junit.framework.Test;

import com.xpn.xwiki.it.framework.XWikiLDAPTestSetup;
import com.xpn.xwiki.it.selenium.framework.AbstractXWikiTestCase;
import com.xpn.xwiki.it.selenium.framework.AlbatrossSkinExecutor;
import com.xpn.xwiki.it.selenium.framework.XWikiTestSuite;

/**
 * Verify the LDAP login and logout features.
 * 
 * @version $Id: $
 */
public class LDAPAuthTest extends AbstractXWikiTestCase
{
    public static Test suite()
    {
        XWikiTestSuite suite = new XWikiTestSuite("Verify the LDAP login and logout features");
        suite.addTestSuite(LDAPAuthTest.class, AlbatrossSkinExecutor.class);

        return suite;
    }

    /**
     * {@inheritDoc}
     * 
     * @see com.xpn.xwiki.it.selenium.framework.AbstractXWikiTestCase#setUp()
     */
    public void setUp() throws Exception
    {
        super.setUp();
        open("/xwiki/bin/view/Main/");

        // Verify that the user isn't logged in
        if (isAuthenticated()) {
            logout();
        }

        clickLogin();
    }

    /**
     * Validate that it tries to log as "common" XWiki login if user is not found in LDAP.
     */
    public void testLogAsXWikiUser()
    {
        setFieldValue("j_username", "Admin");
        setFieldValue("j_password", "admin");
        checkField("rememberme");
        submit();

        assertTrue("Admin user has not been authenticated", isAuthenticated());
    }

    /**
     * Validate that it success to authenticate with LDAP user. Also the user id contains space
     * character.
     */
    public void testLogAsLDAPUser()
    {
        setFieldValue("j_username", XWikiLDAPTestSetup.HORATIOHORNBLOWER_UID);
        setFieldValue("j_password", XWikiLDAPTestSetup.HORATIOHORNBLOWER_PWD);
        checkField("rememberme");
        submit();

        assertTrue(XWikiLDAPTestSetup.HORATIOHORNBLOWER_UID + " user has not been authenticated",
            isAuthenticated());

        logout();

        // ///////////////////
        // Validate XE-136: log with LDAP user then search for provided user uid/pass

        loginAsAdmin();

        open("/xwiki/bin/edit/XWiki/XWikiPreferences?editor=object");
        setFieldValue("XWiki.XWikiPreferences_0_ldap_bind_DN",
            XWikiLDAPTestSetup.HORATIOHORNBLOWER_DN);
        setFieldValue("XWiki.XWikiPreferences_0_ldap_bind_pass",
            XWikiLDAPTestSetup.HORATIOHORNBLOWER_PWD);
        setFieldValue("XWiki.XWikiPreferences_0_ldap_UID_attr",
            XWikiLDAPTestSetup.LDAP_USERUID_FIELD_UID);
        setFieldValue("XWiki.XWikiPreferences_0_ldap_fields_mapping", "name="
            + XWikiLDAPTestSetup.LDAP_USERUID_FIELD_UID
            + ",last_name=sn,first_name=givenname,fullname=description,email=mail,ldap_dn=dn");
        setFieldValue("XWiki.XWikiPreferences_0_ldap_group_mapping",
            "XWiki.XWikiAdminGroup=cn=HMS Lydia,ou=crews,ou=groups,o=sevenSeas");
        clickEditSaveAndView();

        logout();
        clickLogin();

        setFieldValue("j_username", XWikiLDAPTestSetup.WILLIAMBUSH_UID);
        setFieldValue("j_password", XWikiLDAPTestSetup.WILLIAMBUSH_PWD);
        checkField("rememberme");
        submit();

        assertTrue(XWikiLDAPTestSetup.WILLIAMBUSH_UID + " user has not been authenticated",
            isAuthenticated());
        
        // ///////////////////
        // Validate 
        //         - XWIKI-2205: case insensitive user uid
        //         - XWIKI-2202: LDAP user update corrupt XWiki user page
        
        logout();
        clickLogin();
        
        setFieldValue("j_username", XWikiLDAPTestSetup.WILLIAMBUSH_UID_MIXED);
        setFieldValue("j_password", XWikiLDAPTestSetup.WILLIAMBUSH_PWD);
        checkField("rememberme");
        submit();
        
        assertTrue(XWikiLDAPTestSetup.WILLIAMBUSH_UID_MIXED + " user has not been authenticated",
            isAuthenticated());

        // ///////////////////
        // Validate XWIKI-2201: LDAP group mapping defined in XWikiPreferences is not working

        open("/xwiki/bin/view/XWiki/XWikiAdminGroup");
        assertTextPresent("XWiki." + XWikiLDAPTestSetup.WILLIAMBUSH_UID);
    }

    /**
     * {@inheritDoc}
     * 
     * @see com.xpn.xwiki.it.selenium.framework.AbstractXWikiTestCase#tearDown()
     */
    protected void tearDown() throws Exception
    {
        super.tearDown();
    }
}

@@ -1532,23 +1532,16 @@
 ns space
-%0A     *
  charact
@@ -1898,36 +1898,24 @@
 henticated%22,
-%0A           
  isAuthentic
@@ -2218,28 +2218,16 @@
 ind_DN%22,
-%0A           
  XWikiLD
@@ -2325,28 +2325,16 @@
 d_pass%22,
-%0A           
  XWikiLD
@@ -2432,28 +2432,16 @@
 D_attr%22,
-%0A           
  XWikiLD
@@ -3218,36 +3218,24 @@
 henticated%22,
-%0A           
  isAuthentic
@@ -3243,24 +3243,16 @@
 ted());%0A
-        
 %0A       
@@ -3294,17 +3294,16 @@
 Validate
- 
 %0A       
@@ -3301,32 +3301,24 @@
 e%0A        //
-        
  - XWIKI-220
@@ -3356,24 +3356,16 @@
       //
-        
  - XWIKI
@@ -3412,24 +3412,16 @@
 er page%0A
-        
 %0A       
@@ -3449,32 +3449,24 @@
 ickLogin();%0A
-        
 %0A        set
@@ -3658,24 +3658,16 @@
 bmit();%0A
-        
 %0A       
@@ -3756,28 +3756,16 @@
 icated%22,
-%0A           
  isAuthe
@@ -3959,24 +3959,25 @@
 roup%22);%0A
+%0A
         
 assertTe
@@ -3972,26 +3972,30 @@
     
-assertTextPresent(
+String userFullName = 
 %22XWi
@@ -4031,24 +4031,183 @@
 LIAMBUSH_UID
+;%0A%0A        getSelenium().waitForCondition(%22selenium.page().bodyText().indexOf('%22 + userFullName + %22') != -1;%22, %222000%22);%0A%0A        assertTextPresent(userFullName
 );%0A    %7D%0A%0A  

Fix test