Project

General

Profile

1
/**  '$RCSfile$'
2
 *  Copyright: 2010 Regents of the University of California and the
3
 *              National Center for Ecological Analysis and Synthesis
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 2 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
package edu.ucsb.nceas.metacat.authentication;
20

    
21
import junit.framework.Test;
22
import junit.framework.TestSuite;
23
import edu.ucsb.nceas.MCTestCase;
24

    
25

    
26
public class AuthFileTest extends MCTestCase {
27
    private static final String PASSWORDFILEPATH = "build/password";
28
    private static final String GROUPNAME = "nceas-dev";
29
    private static final String USERNAME = "uid=tao,o=NCEAS,dc=ecoinformatics,dc=org";
30
    private static final String PASSWORD = "ecoinformatics";
31
    /**
32
     * consstructor for the test
33
     */
34
     public AuthFileTest(String name)
35
     {
36
         super(name);
37
     }
38
   
39
     /**
40
      * Establish a testing framework by initializing appropriate objects
41
      */
42
     public void setUp() throws Exception 
43
     {
44
         super.setUp();
45
     }
46

    
47
     /**
48
      * Release any objects after tests are complete
49
      */
50
     public void tearDown() 
51
     {
52
     }
53

    
54
     /**
55
      * Create a suite of tests to be run together
56
      */
57
     public static Test suite() 
58
     {
59
         TestSuite suite = new TestSuite();
60
         suite.addTest(new AuthFileTest("testAddGroup"));
61
         suite.addTest(new AuthFileTest("testAddUser"));
62
         suite.addTest(new AuthFileTest("testAuthenticate"));
63
         suite.addTest(new AuthFileTest("testGetUsers"));
64
         return suite;
65
     }
66
     
67
     /**
68
      * Test the addGroup method
69
      * @throws Exception
70
      */
71
     public void testAddGroup() throws Exception{
72
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
73
         authFile.addGroup(GROUPNAME);
74
         try {
75
             authFile.addGroup(GROUPNAME);
76
             assertTrue("We can't reach here since we can't add the group twice", false);
77
         } catch (AuthenticationException e) {
78
             
79
         }
80
         
81
     }
82
     
83
     /**
84
      * Test the addGroup method
85
      * @throws Exception
86
      */
87
     public void testAddUser() throws Exception{
88
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
89
         String[]groups = {GROUPNAME};
90
         authFile.addUser(USERNAME, groups, PASSWORD);
91
         try {
92
             authFile.addUser(USERNAME, groups, PASSWORD);
93
             assertTrue("We can't reach here since we can't add the user twice", false);
94
         } catch (AuthenticationException e) {
95
             
96
         }
97
         
98
     }
99
     
100
     /**
101
      * Test the authentication method
102
      * @throws Exception
103
      */
104
     public void testAuthenticate() throws Exception {
105
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
106
         boolean success = authFile.authenticate(USERNAME, PASSWORD);
107
         if(!success) {
108
             assertTrue("The authentication should succeed.", false);
109
         }
110
         success = authFile.authenticate(USERNAME, "hello");
111
         if(success) {
112
             assertTrue("The authentication should NOT succeed.", false);
113
         }
114
         success = authFile.authenticate("hello", PASSWORD);
115
         if(success) {
116
             assertTrue("The authentication should NOT succeed.", false);
117
         }
118
     }
119
     
120
     /**
121
      * Test the getUsers method
122
      * @throws Exception
123
      */
124
     public void testGetUsers() throws Exception {
125
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
126
         String[][] users = authFile.getUsers(null, null);
127
         assertTrue("The file should have one user "+USERNAME, users[0][0].equals(USERNAME));
128
         String[]userInGroup = authFile.getUsers(null, null, GROUPNAME);
129
         assertTrue("There should be at least one user in the group "+GROUPNAME, userInGroup[0].equals(USERNAME));
130
         userInGroup = authFile.getUsers(null, null, "group1");
131
         assertTrue("There shouldn't have any users in the group1 ", userInGroup==null);
132
     }
133
}
    (1-1/1)