Project

General

Profile

« Previous | Next » 

Revision 8472

Added by Jing Tao over 10 years ago

Modified the junit test file according to the change in the class.

View differences:

test/edu/ucsb/nceas/metacat/authentication/AuthFileTest.java
30 30
    private static final String GROUPNAME3 = "dev";
31 31
    private static final String USERNAME = "uid=john,o=NCEAS,dc=ecoinformatics,dc=org";
32 32
    private static final String USERNAME2="uid=smith,o=unaffiliated,dc=ecoinformatics,dc=org";
33
    private static final String PASSWORD = "ecoinformatics";
33
    private static final String PLAINPASSWORD = "ecoinformatics";
34
    private static final String PLAINPASSWORD2 = "n%cea4s";
35
    private static final String HASHEDPASSWORD2 = "$2a$10$iMZXvVYs8nEUAWDFfcCF8ePEvzcnak32tx7TQAecsZcPGRouqSdse";
36
    private static final String PLAINPASSWORD3 = "q8w*er";
37
    private static final String HASHEDPASSWORD3 = "$2a$10$zO4Cw1p38xWeUh4DneMGCecg67yo2SN25m0wzWCJ9zu7FfRwLTvue";
38
    private static final String EMAILADDRESS = "john@nceas.ucsb.edu";
39
    private static final String SURNAME = "John";
40
    private static final String GIVENNAME = "Joe";
34 41
    /**
35 42
     * consstructor for the test
36 43
     */
......
63 70
         suite.addTest(new AuthFileTest("testAddGroup"));
64 71
         suite.addTest(new AuthFileTest("testAddUser"));
65 72
         suite.addTest(new AuthFileTest("testAuthenticate"));
73
         suite.addTest(new AuthFileTest("testChangePassword"));
66 74
         suite.addTest(new AuthFileTest("testGetUsers"));
67 75
         suite.addTest(new AuthFileTest("testGetGroups"));
68
         suite.addTest(new AuthFileTest("testChangePassword"));
69 76
         suite.addTest(new AuthFileTest("testAddRemoveUserToFromGroup"));
70 77
         suite.addTest(new AuthFileTest("testGetPrincipals"));
71 78
         return suite;
......
94 101
     public void testAddUser() throws Exception{
95 102
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
96 103
         String[]groups = {GROUPNAME};
97
         authFile.addUser(USERNAME, groups, PASSWORD, null, null, null, null);
98
         authFile.addUser(USERNAME2, null, PASSWORD, null, null, null,null);
104
         authFile.addUser(USERNAME, groups, PLAINPASSWORD, null, EMAILADDRESS, SURNAME, GIVENNAME);
105
         //user a hash value of the PASSWORD
106
         authFile.addUser(USERNAME2, null, null, HASHEDPASSWORD2, null, null,null);
99 107
         try {
100
             authFile.addUser(USERNAME, groups, PASSWORD, null, null, null, null);
108
             authFile.addUser(USERNAME, groups, PLAINPASSWORD, null, null, null, null);
101 109
             assertTrue("We can't reach here since we can't add the user twice", false);
102 110
         } catch (AuthenticationException e) {
103 111
             
......
111 119
      */
112 120
     public void testAuthenticate() throws Exception {
113 121
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
114
         boolean success = authFile.authenticate(USERNAME, PASSWORD);
122
         boolean success = authFile.authenticate(USERNAME, PLAINPASSWORD);
115 123
         if(!success) {
116 124
             assertTrue("The authentication should succeed.", false);
117 125
         }
......
119 127
         if(success) {
120 128
             assertTrue("The authentication should NOT succeed.", false);
121 129
         }
122
         success = authFile.authenticate("hello", PASSWORD);
130
         success = authFile.authenticate("hello", PLAINPASSWORD);
123 131
         if(success) {
124 132
             assertTrue("The authentication should NOT succeed.", false);
125 133
         }
134
         success = authFile.authenticate(USERNAME2, PLAINPASSWORD2);
135
         if(!success) {
136
             assertTrue("The authentication for "+USERNAME2 +" should succeed.", false);
137
         }
138
         success = authFile.authenticate(USERNAME2, HASHEDPASSWORD2);
139
         if(success) {
140
             assertTrue("The authentication should NOT succeed.", false);
141
         }
126 142
     }
127 143
     
128 144
     /**
......
133 149
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
134 150
         String[][] users = authFile.getUsers(null, null);
135 151
         assertTrue("The file should have one user "+USERNAME, users[0][0].equals(USERNAME));
152
         assertTrue("The common name for the user "+USERNAME+" should be "+GIVENNAME+" "+SURNAME, users[0][1].equals(GIVENNAME+" "+SURNAME));
153
         assertTrue("The org name for the user "+USERNAME+" should be null ", users[0][2]== null);
154
         assertTrue("The org unit name for the user "+USERNAME+" should be null ", users[0][3]== null);
155
         assertTrue("The email address for the user "+USERNAME+" should be "+EMAILADDRESS, users[0][4].equals(EMAILADDRESS));
156
         assertTrue("The file should have one user "+USERNAME2, users[1][0].equals(USERNAME2));
157
         assertTrue("The common name for the user "+USERNAME2+" should be null", users[1][1]==null);
158
         assertTrue("The org name for the user "+USERNAME2+" should be null ", users[1][2]== null);
159
         assertTrue("The org unit name for the user "+USERNAME2+" should be null ", users[1][3]== null);
160
         assertTrue("The email address for the user "+USERNAME2+" should be null.", users[1][4]==null);
136 161
         String[]userInGroup = authFile.getUsers(null, null, GROUPNAME);
137 162
         assertTrue("There should be at least one user in the group "+GROUPNAME, userInGroup[0].equals(USERNAME));
138 163
         userInGroup = authFile.getUsers(null, null, "group1");
......
151 176
         assertTrue("There should be at least one group for user "+USERNAME, groupForUser[0][0].equals(GROUPNAME));
152 177
         groupForUser = authFile.getGroups(null, null, "user1");
153 178
         assertTrue("There shouldn't have any groups assoicated with user1 ", groupForUser==null);
179
         groupForUser = authFile.getGroups(null, null, USERNAME2);
180
         assertTrue("There shouldn't have any groups assoicated with user "+USERNAME2, groupForUser==null);
154 181
     }
155 182
     
156 183
     /**
......
159 186
      */
160 187
     public void testChangePassword() throws Exception {
161 188
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
162
         authFile.authenticate(USERNAME, password);
189
         authFile.authenticate(USERNAME, PLAINPASSWORD);
163 190
         String newPassword = "hello";
164
         authFile.modifyPassWithHash(USERNAME,newPassword);
165
         authFile.authenticate(USERNAME, newPassword);
191
         authFile.modifyPassWithPlain(USERNAME,newPassword);
192
         boolean success = authFile.authenticate(USERNAME, newPassword);
193
         assertTrue("The authentication should be successful with the new password", success);
166 194
         try {
167 195
             authFile.modifyPassWithPlain("user1", "new");
168 196
             assertTrue("Can't reach here since we tried to change the password for an unexisting user ", false);
169 197
         } catch (AuthenticationException e) {
170
             System.out.println("Failed to change the password for a user: "+e.getMessage());
198
             //System.out.println("Failed to change the password for a user: "+e.getMessage());
171 199
         }
200
         
201
         success = authFile.authenticate(USERNAME, "qws");
202
         assertTrue("The authentication should fail with a wrong password", !success);
203
         
204
         //test change the password with hashed version
205
         authFile.modifyPassWithHash(USERNAME, HASHEDPASSWORD3);
206
         success = authFile.authenticate(USERNAME, PLAINPASSWORD3);
207
         assertTrue("The authentication should be successful with the new password (after modifying the password with a hashed value", success);
208
         success = authFile.authenticate(USERNAME, HASHEDPASSWORD3);
209
         assertTrue("The authentication should faile when the user directly use the hash password.", !success);
210
         success = authFile.authenticate(USERNAME, newPassword);
211
         assertTrue("The authentication should be successful with a wrong password", !success);
172 212
     }
173 213
     
174 214
     /**

Also available in: Unified diff