Revision 8432
Added by Jing Tao about 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 USERNAME = "uid=tao,o=NCEAS,dc=ecoinformatics,dc=org"; |
|
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"; |
|
30 | 32 |
private static final String PASSWORD = "ecoinformatics"; |
31 | 33 |
/** |
32 | 34 |
* consstructor for the test |
... | ... | |
63 | 65 |
suite.addTest(new AuthFileTest("testGetUsers")); |
64 | 66 |
suite.addTest(new AuthFileTest("testGetGroups")); |
65 | 67 |
suite.addTest(new AuthFileTest("testChangePassword")); |
68 |
suite.addTest(new AuthFileTest("testAddRemoveUserToFromGroup")); |
|
69 |
suite.addTest(new AuthFileTest("testGetPrincipals")); |
|
66 | 70 |
return suite; |
67 | 71 |
} |
68 | 72 |
|
... | ... | |
154 | 158 |
public void testChangePassword() throws Exception { |
155 | 159 |
AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH); |
156 | 160 |
String password = authFile.resetPassword(USERNAME); |
161 |
authFile.authenticate(USERNAME, password); |
|
157 | 162 |
String newPassword = "hello"; |
158 | 163 |
authFile.modifyPassword(USERNAME, password, newPassword); |
159 |
|
|
164 |
authFile.authenticate(USERNAME, newPassword); |
|
160 | 165 |
try { |
161 | 166 |
authFile.resetPassword("user1"); |
162 | 167 |
assertTrue("Can't reach here since we tried to reset the password for an unexisting user ", false); |
163 | 168 |
} catch (AuthenticationException e) { |
164 |
|
|
169 |
System.out.println("Failed to reset the password for a user: "+e.getMessage()); |
|
165 | 170 |
} |
166 | 171 |
try { |
167 | 172 |
authFile.modifyPassword("user1", "old", "new"); |
168 | 173 |
assertTrue("Can't reach here since we tried to change the password for an unexisting user ", false); |
169 | 174 |
} catch (AuthenticationException e) { |
170 |
|
|
175 |
System.out.println("Failed to change the password for a user: "+e.getMessage()); |
|
171 | 176 |
} |
172 | 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 |
} |
|
173 | 245 |
} |
Also available in: Unified diff
Add the test method for the getPrincipals.