Revision 8477
Added by Jing Tao almost 11 years ago
test/edu/ucsb/nceas/metacat/authentication/AuthFileTest.java | ||
---|---|---|
26 | 26 |
public class AuthFileTest extends MCTestCase { |
27 | 27 |
private static final String PASSWORDFILEPATH = "build/password"; |
28 | 28 |
private static final String GROUPNAME = "nceas-dev"; |
29 |
private static final String DESCRIPITION = "Developers at NCEAS"; |
|
29 | 30 |
private static final String GROUPNAME2 = "dataone-dev"; |
31 |
private static final String DESCRIPITION2 = null; |
|
30 | 32 |
private static final String GROUPNAME3 = "dev"; |
33 |
private static final String DESCRIPITION3 = "Developers"; |
|
31 | 34 |
private static final String USERNAME = "uid=john,o=NCEAS,dc=ecoinformatics,dc=org"; |
32 | 35 |
private static final String USERNAME2="uid=smith,o=unaffiliated,dc=ecoinformatics,dc=org"; |
33 | 36 |
private static final String PLAINPASSWORD = "ecoinformatics"; |
... | ... | |
71 | 74 |
suite.addTest(new AuthFileTest("testAddUser")); |
72 | 75 |
suite.addTest(new AuthFileTest("testAuthenticate")); |
73 | 76 |
suite.addTest(new AuthFileTest("testChangePassword")); |
77 |
suite.addTest(new AuthFileTest("testGetUserInfo")); |
|
74 | 78 |
suite.addTest(new AuthFileTest("testGetUsers")); |
75 | 79 |
suite.addTest(new AuthFileTest("testGetGroups")); |
76 | 80 |
suite.addTest(new AuthFileTest("testAddRemoveUserToFromGroup")); |
... | ... | |
84 | 88 |
*/ |
85 | 89 |
public void testAddGroup() throws Exception{ |
86 | 90 |
AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH); |
87 |
authFile.addGroup(GROUPNAME, "Developers at NCEAS");
|
|
91 |
authFile.addGroup(GROUPNAME, DESCRIPITION);
|
|
88 | 92 |
try { |
89 | 93 |
authFile.addGroup(GROUPNAME, "Developers at NCEAS"); |
90 | 94 |
assertTrue("We can't reach here since we can't add the group twice", false); |
... | ... | |
142 | 146 |
} |
143 | 147 |
|
144 | 148 |
/** |
149 |
* Test the method getUserInfo |
|
150 |
* @throws Exception |
|
151 |
*/ |
|
152 |
public void testGetUserInfo() throws Exception { |
|
153 |
AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH); |
|
154 |
String[] userInfo = authFile.getUserInfo(USERNAME, null); |
|
155 |
assertTrue("The common name for the user "+USERNAME+" should be "+GIVENNAME+" "+SURNAME, userInfo[0].equals(GIVENNAME+" "+SURNAME)); |
|
156 |
assertTrue("The org name for the user "+USERNAME+" should be null ", userInfo[1]== null); |
|
157 |
assertTrue("The email address for the user "+USERNAME+" should be "+EMAILADDRESS, userInfo[2].equals(EMAILADDRESS)); |
|
158 |
userInfo = authFile.getUserInfo(USERNAME2, null); |
|
159 |
assertTrue("The common name for the user "+USERNAME2+" should be null.", userInfo[0] == null); |
|
160 |
assertTrue("The org name for the user "+USERNAME+" should be null.", userInfo[1]== null); |
|
161 |
assertTrue("The email address for the user "+USERNAME+" should be null.", userInfo[2]==null); |
|
162 |
} |
|
163 |
|
|
164 |
/** |
|
145 | 165 |
* Test the getUsers method |
146 | 166 |
* @throws Exception |
147 | 167 |
*/ |
... | ... | |
172 | 192 |
AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH); |
173 | 193 |
String[][] groups = authFile.getGroups(null, null); |
174 | 194 |
assertTrue("The file should have one group associated with "+USERNAME, groups[0][0].equals(GROUPNAME)); |
195 |
assertTrue("The group "+groups[0][0]+" should have the description "+DESCRIPITION, groups[0][1].equals(DESCRIPITION)); |
|
175 | 196 |
String[][]groupForUser = authFile.getGroups(null, null, USERNAME); |
176 | 197 |
assertTrue("There should be at least one group for user "+USERNAME, groupForUser[0][0].equals(GROUPNAME)); |
198 |
assertTrue("The group "+groups[0][0]+" should have the description "+DESCRIPITION, groups[0][1].equals(DESCRIPITION)); |
|
177 | 199 |
groupForUser = authFile.getGroups(null, null, "user1"); |
178 | 200 |
assertTrue("There shouldn't have any groups assoicated with user1 ", groupForUser==null); |
179 | 201 |
groupForUser = authFile.getGroups(null, null, USERNAME2); |
... | ... | |
240 | 262 |
authFile.addUserToGroup(USERNAME, GROUPNAME2); |
241 | 263 |
String[][]groups = authFile.getGroups(null, null, USERNAME); |
242 | 264 |
assertTrue("The user "+USERNAME+" should be in the group "+GROUPNAME2, groups[0][0].equals(GROUPNAME2)||groups[1][0].equals(GROUPNAME2)); |
265 |
if(groups[0][0].equals(GROUPNAME2)) { |
|
266 |
assertTrue("The description of the group "+GROUPNAME2+" should be null.", groups[0][1]==null); |
|
267 |
} |
|
268 |
if(groups[1][0].equals(GROUPNAME2)) { |
|
269 |
assertTrue("The description of the group "+GROUPNAME2+" should be null.", groups[1][1]==null); |
|
270 |
} |
|
243 | 271 |
|
244 | 272 |
|
245 | 273 |
try { |
... | ... | |
255 | 283 |
} catch(AuthenticationException e) { |
256 | 284 |
System.out.println("Failed to remove a user from a group "+e.getMessage()); |
257 | 285 |
} |
258 |
authFile.addGroup(GROUPNAME3, "Developers");
|
|
286 |
authFile.addGroup(GROUPNAME3, DESCRIPITION3);
|
|
259 | 287 |
try { |
260 | 288 |
authFile.removeUserFromGroup(USERNAME, GROUPNAME3); |
261 | 289 |
assertTrue("Can't reach here since the user is not in the group", false); |
... | ... | |
264 | 292 |
} |
265 | 293 |
authFile.removeUserFromGroup(USERNAME, GROUPNAME2); |
266 | 294 |
groups = authFile.getGroups(null, null, USERNAME); |
267 |
assertTrue("The size of groups of the user "+USERNAME+" shouldn be one rather than "+groups.length, groups.length ==1);
|
|
295 |
assertTrue("The size of groups of the user "+USERNAME+" should be one rather than "+groups.length, groups.length ==1); |
|
268 | 296 |
assertTrue("The user "+USERNAME+" shouldn't be in the group "+GROUPNAME2, !groups[0][0].equals(GROUPNAME2)); |
269 | 297 |
assertTrue("The user "+USERNAME+" should still be in the group "+GROUPNAME, groups[0][0].equals(GROUPNAME)); |
298 |
assertTrue("The group "+groups[0][0]+" should have the description "+DESCRIPITION, groups[0][1].equals(DESCRIPITION)); |
|
299 |
|
|
300 |
authFile.addUserToGroup(USERNAME2, GROUPNAME3); |
|
301 |
groups = authFile.getGroups(null, null, USERNAME2); |
|
302 |
assertTrue("The user "+USERNAME2+" should be in the group "+GROUPNAME3, groups[0][0].equals(GROUPNAME3)); |
|
303 |
assertTrue("The group "+groups[0][0]+" should have the description "+DESCRIPITION3, groups[0][1].equals(DESCRIPITION3)); |
|
304 |
String[] users = authFile.getUsers(null, null, GROUPNAME3); |
|
305 |
assertTrue("The user "+USERNAME2+" should be a member of the group "+GROUPNAME3, users[0].equals(USERNAME2)); |
|
306 |
try { |
|
307 |
authFile.removeUserFromGroup(USERNAME2, GROUPNAME); |
|
308 |
assertTrue("We can't reach here since the user "+USERNAME2+" is not in the group "+GROUPNAME, false); |
|
309 |
} catch (Exception e) { |
|
310 |
|
|
311 |
} |
|
312 |
|
|
270 | 313 |
} |
271 | 314 |
|
272 | 315 |
/** |
Also available in: Unified diff
Add the test method for the getUserInfo method.