Project

General

Profile

1 3748 tao
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements a metadata catalog as a java Servlet
4
 *  Copyright: 2006 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Matt Jones, Dan Higgins, Jivka Bojilova, Chad Berkley, Matthew Perry
7
 *
8
 *   '$Author$'
9
 *     '$Date$'
10
 * '$Revision$'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26
package edu.ucsb.nceas.metacattest;
27
28 4384 daigle
import edu.ucsb.nceas.MCTestCase;
29 6156 tao
import edu.ucsb.nceas.metacat.properties.PropertyService;
30
31 3748 tao
import edu.ucsb.nceas.metacat.AuthLdap;
32
import junit.framework.Test;
33
import junit.framework.TestSuite;
34
35
36
/*
37
 * A junit test class to test public methods in AuthLdap class.
38
 */
39 4384 daigle
public class AuthLdapTest extends MCTestCase
40 3748 tao
{
41
42
	/**
43
     * Constructor to build the test
44
     *
45
     * @param name the name of the test method
46
     */
47
    public AuthLdapTest(String name)
48
    {
49
        super(name);
50
51
    }
52
53
    /**
54
     * Create a suite of tests to be run together
55
     */
56
    public static Test suite()
57
    {
58
        TestSuite suite = new TestSuite();
59
        suite.addTest(new AuthLdapTest("initialize"));
60
        suite.addTest(new AuthLdapTest("getPrincipals"));
61 8559 tao
        suite.addTest(new AuthLdapTest("testAliasedAccount"));
62 3748 tao
        return suite;
63
    }
64
65
    /**
66
     * Run an initial test that always passes to check that the test
67
     * harness is working.
68
     */
69
    public void initialize()
70
    {
71 6156 tao
    	//System.out.println("here");
72
        assertTrue(1 == 1);
73 3748 tao
    }
74
75
    public void getPrincipals()
76
    {
77
    	try
78
    	{
79 6156 tao
    	    String lterUser = PropertyService.getProperty("test.lterUser");
80 4080 daigle
        	//System.out.println("before initilizing authldap object");
81
        	AuthLdap ldap = new AuthLdap();
82
        	//System.out.println("after initilizing authldap object");
83
84 3748 tao
    		//System.out.println("before calling the getPrincipals method");
85 6156 tao
    	    String response = ldap.getPrincipals(username, password);
86 3748 tao
    	    //System.out.println("after calling the getPrincipals method \n"+response);
87
    	    if ( response != null)
88
    	    {
89 6156 tao
    	       assertTrue("Couldn't find user "+anotheruser,response.indexOf(anotheruser) != -1);
90
    	       assertTrue("Couldn't find user "+lterUser,response.indexOf(lterUser) != -1);
91 3748 tao
    	    }
92
    	    else
93
    	    {
94
    	    	fail("the response is null in getPrincipal method");
95
    	    }
96
    	}
97
    	catch (Exception e)
98
    	{
99
    		//System.out.println("Error to get principals "+e.getMessage());
100
    		fail("There is an exception in getPrincipals "+e.getMessage());
101
    	}
102
103
    }
104
105
    /**
106
     * To Do: add more methods test
107
     */
108 8559 tao
109
    public void testAliasedAccount() throws Exception {
110
        String alias = "uid=test2,o=unaffiliated,dc=ecoinformatics,dc=org";
111
        AuthLdap ldap = new AuthLdap();
112
        assertTrue("We should authenticate the alias dn "+alias,ldap.authenticate(alias, "test"));
113
        String[] info =ldap.getUserInfo(alias, null);
114
        assertTrue("The email address should be tao@nceas.ucsb.edu and should be "+info[2], info[2].equals("tao@nceas.ucsb.edu"));
115
    }
116 3748 tao
117
}