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 GROUPNAME2 = "dataone-dev";
30
    private static final String GROUPNAME3 = "dev";
31
    private static final String USERNAME = "uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
32
    private static final String PASSWORD = "ecoinformatics";
33
    /**
34
     * consstructor for the test
35
     */
36
     public AuthFileTest(String name)
37
     {
38
         super(name);
39
     }
40
   
41
     /**
42
      * Establish a testing framework by initializing appropriate objects
43
      */
44
     public void setUp() throws Exception 
45
     {
46
         super.setUp();
47
     }
48

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

    
56
     /**
57
      * Create a suite of tests to be run together
58
      */
59
     public static Test suite() 
60
     {
61
         TestSuite suite = new TestSuite();
62
         suite.addTest(new AuthFileTest("testAddGroup"));
63
         suite.addTest(new AuthFileTest("testAddUser"));
64
         suite.addTest(new AuthFileTest("testAuthenticate"));
65
         suite.addTest(new AuthFileTest("testGetUsers"));
66
         suite.addTest(new AuthFileTest("testGetGroups"));
67
         suite.addTest(new AuthFileTest("testChangePassword"));
68
         suite.addTest(new AuthFileTest("testAddRemoveUserToFromGroup"));
69
         suite.addTest(new AuthFileTest("testGetPrincipals"));
70
         return suite;
71
     }
72
     
73
     /**
74
      * Test the addGroup method
75
      * @throws Exception
76
      */
77
     public void testAddGroup() throws Exception{
78
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
79
         authFile.addGroup(GROUPNAME);
80
         try {
81
             authFile.addGroup(GROUPNAME);
82
             assertTrue("We can't reach here since we can't add the group twice", false);
83
         } catch (AuthenticationException e) {
84
             
85
         }
86
         
87
     }
88
     
89
     /**
90
      * Test the addGroup method
91
      * @throws Exception
92
      */
93
     public void testAddUser() throws Exception{
94
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
95
         String[]groups = {GROUPNAME};
96
         authFile.addUser(USERNAME, groups, PASSWORD);
97
         try {
98
             authFile.addUser(USERNAME, groups, PASSWORD);
99
             assertTrue("We can't reach here since we can't add the user twice", false);
100
         } catch (AuthenticationException e) {
101
             
102
         }
103
         
104
     }
105
     
106
     /**
107
      * Test the authentication method
108
      * @throws Exception
109
      */
110
     public void testAuthenticate() throws Exception {
111
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
112
         boolean success = authFile.authenticate(USERNAME, PASSWORD);
113
         if(!success) {
114
             assertTrue("The authentication should succeed.", false);
115
         }
116
         success = authFile.authenticate(USERNAME, "hello");
117
         if(success) {
118
             assertTrue("The authentication should NOT succeed.", false);
119
         }
120
         success = authFile.authenticate("hello", PASSWORD);
121
         if(success) {
122
             assertTrue("The authentication should NOT succeed.", false);
123
         }
124
     }
125
     
126
     /**
127
      * Test the getUsers method
128
      * @throws Exception
129
      */
130
     public void testGetUsers() throws Exception {
131
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
132
         String[][] users = authFile.getUsers(null, null);
133
         assertTrue("The file should have one user "+USERNAME, users[0][0].equals(USERNAME));
134
         String[]userInGroup = authFile.getUsers(null, null, GROUPNAME);
135
         assertTrue("There should be at least one user in the group "+GROUPNAME, userInGroup[0].equals(USERNAME));
136
         userInGroup = authFile.getUsers(null, null, "group1");
137
         assertTrue("There shouldn't have any users in the group1 ", userInGroup==null);
138
     }
139
     
140
     /**
141
      * Test the getGroups method
142
      * @throws Exception
143
      */
144
     public void testGetGroups() throws Exception {
145
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
146
         String[][] groups = authFile.getGroups(null, null);
147
         assertTrue("The file should have one group associated with "+USERNAME, groups[0][0].equals(GROUPNAME));
148
         String[][]groupForUser = authFile.getGroups(null, null, USERNAME);
149
         assertTrue("There should be at least one group for user "+USERNAME, groupForUser[0][0].equals(GROUPNAME));
150
         groupForUser = authFile.getGroups(null, null, "user1");
151
         assertTrue("There shouldn't have any groups assoicated with user1 ", groupForUser==null);
152
     }
153
     
154
     /**
155
      * Test the change password methods
156
      * @throws Exception
157
      */
158
     public void testChangePassword() throws Exception {
159
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
160
         String password = authFile.resetPassword(USERNAME);
161
         authFile.authenticate(USERNAME, password);
162
         String newPassword = "hello";
163
         authFile.modifyPassword(USERNAME, password, newPassword);
164
         authFile.authenticate(USERNAME, newPassword);
165
         try {
166
             authFile.resetPassword("user1");
167
             assertTrue("Can't reach here since we tried to reset the password for an unexisting user ", false);
168
         } catch (AuthenticationException e) {
169
             System.out.println("Failed to reset the password for a user: "+e.getMessage());
170
         }
171
         try {
172
             authFile.modifyPassword("user1", "old", "new");
173
             assertTrue("Can't reach here since we tried to change the password for an unexisting user ", false);
174
         } catch (AuthenticationException e) {
175
             System.out.println("Failed to change the password for a user: "+e.getMessage());
176
         }
177
     }
178
     
179
     /**
180
      * Test the addUserToGroup and removeUserFromGroup methods
181
      * @throws Exception
182
      */
183
     public void testAddRemoveUserToFromGroup() throws Exception{
184
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
185
         try {
186
             authFile.addUserToGroup("user1", GROUPNAME);
187
             assertTrue("Can't reach here since we tried to add an unexisting user to a group", false);
188
         } catch(AuthenticationException e) {
189
             System.out.println("Failed to add a user to a group "+e.getMessage());
190
         }
191
         
192
         try {
193
             authFile.addUserToGroup(USERNAME, "group2");
194
             assertTrue("Can't reach here since we tried to add a user to an unexisting group", false);
195
         } catch(AuthenticationException e) {
196
             System.out.println("Failed to add a user to a group "+e.getMessage());
197
         }
198
         try {
199
             authFile.addUserToGroup(USERNAME, GROUPNAME);
200
             assertTrue("Can't reach here since the user is already in the group", false);
201
         } catch(AuthenticationException e) {
202
             System.out.println("Failed to add a user to a group "+e.getMessage());
203
         }
204
         authFile.addGroup(GROUPNAME2);
205
         authFile.addUserToGroup(USERNAME, GROUPNAME2);
206
         String[][]groups = authFile.getGroups(null, null, USERNAME);
207
         assertTrue("The user "+USERNAME+" should be in the group "+GROUPNAME2, groups[0][0].equals(GROUPNAME2)||groups[1][0].equals(GROUPNAME2));
208
         
209
         
210
         try {
211
             authFile.removeUserFromGroup("user1", GROUPNAME);
212
             assertTrue("Can't reach here since we tried to remove an unexisting user from a group", false);
213
         } catch(AuthenticationException e) {
214
             System.out.println("Failed to remove a user from a group "+e.getMessage());
215
         }
216
         
217
         try {
218
             authFile.removeUserFromGroup(USERNAME, "group2");
219
             assertTrue("Can't reach here since we tried to remove a user from an unexisting group", false);
220
         } catch(AuthenticationException e) {
221
             System.out.println("Failed to remove a user from a group "+e.getMessage());
222
         }
223
         authFile.addGroup(GROUPNAME3);
224
         try {
225
             authFile.removeUserFromGroup(USERNAME, GROUPNAME3);
226
             assertTrue("Can't reach here since the user is not in the group", false);
227
         } catch(AuthenticationException e) {
228
             System.out.println("Failed to remove a user from a group "+e.getMessage());
229
         }
230
         authFile.removeUserFromGroup(USERNAME, GROUPNAME2);
231
         groups = authFile.getGroups(null, null, USERNAME);
232
         assertTrue("The size of groups of the user "+USERNAME+" shouldn be one rather than "+groups.length, groups.length ==1);
233
         assertTrue("The user "+USERNAME+" shouldn't be in the group "+GROUPNAME2, !groups[0][0].equals(GROUPNAME2));
234
         assertTrue("The user "+USERNAME+" should still be in the group "+GROUPNAME, groups[0][0].equals(GROUPNAME));
235
     }
236
     
237
     /**
238
      * Test the getPrincipal
239
      * @throws Exception
240
      */
241
     public void testGetPrincipals() throws Exception {
242
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
243
         System.out.println(""+authFile.getPrincipals(null, null));
244
     }
245
}
    (1-1/1)