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
         suite.addTest(new AuthFileTest("testGetGroups"));
65
         suite.addTest(new AuthFileTest("testChangePassword"));
66
         return suite;
67
     }
68
     
69
     /**
70
      * Test the addGroup method
71
      * @throws Exception
72
      */
73
     public void testAddGroup() throws Exception{
74
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
75
         authFile.addGroup(GROUPNAME);
76
         try {
77
             authFile.addGroup(GROUPNAME);
78
             assertTrue("We can't reach here since we can't add the group twice", false);
79
         } catch (AuthenticationException e) {
80
             
81
         }
82
         
83
     }
84
     
85
     /**
86
      * Test the addGroup method
87
      * @throws Exception
88
      */
89
     public void testAddUser() throws Exception{
90
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
91
         String[]groups = {GROUPNAME};
92
         authFile.addUser(USERNAME, groups, PASSWORD);
93
         try {
94
             authFile.addUser(USERNAME, groups, PASSWORD);
95
             assertTrue("We can't reach here since we can't add the user twice", false);
96
         } catch (AuthenticationException e) {
97
             
98
         }
99
         
100
     }
101
     
102
     /**
103
      * Test the authentication method
104
      * @throws Exception
105
      */
106
     public void testAuthenticate() throws Exception {
107
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
108
         boolean success = authFile.authenticate(USERNAME, PASSWORD);
109
         if(!success) {
110
             assertTrue("The authentication should succeed.", false);
111
         }
112
         success = authFile.authenticate(USERNAME, "hello");
113
         if(success) {
114
             assertTrue("The authentication should NOT succeed.", false);
115
         }
116
         success = authFile.authenticate("hello", PASSWORD);
117
         if(success) {
118
             assertTrue("The authentication should NOT succeed.", false);
119
         }
120
     }
121
     
122
     /**
123
      * Test the getUsers method
124
      * @throws Exception
125
      */
126
     public void testGetUsers() throws Exception {
127
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
128
         String[][] users = authFile.getUsers(null, null);
129
         assertTrue("The file should have one user "+USERNAME, users[0][0].equals(USERNAME));
130
         String[]userInGroup = authFile.getUsers(null, null, GROUPNAME);
131
         assertTrue("There should be at least one user in the group "+GROUPNAME, userInGroup[0].equals(USERNAME));
132
         userInGroup = authFile.getUsers(null, null, "group1");
133
         assertTrue("There shouldn't have any users in the group1 ", userInGroup==null);
134
     }
135
     
136
     /**
137
      * Test the getGroups method
138
      * @throws Exception
139
      */
140
     public void testGetGroups() throws Exception {
141
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
142
         String[][] groups = authFile.getGroups(null, null);
143
         assertTrue("The file should have one group associated with "+USERNAME, groups[0][0].equals(GROUPNAME));
144
         String[][]groupForUser = authFile.getGroups(null, null, USERNAME);
145
         assertTrue("There should be at least one group for user "+USERNAME, groupForUser[0][0].equals(GROUPNAME));
146
         groupForUser = authFile.getGroups(null, null, "user1");
147
         assertTrue("There shouldn't have any groups assoicated with user1 ", groupForUser==null);
148
     }
149
     
150
     /**
151
      * Test the change password methods
152
      * @throws Exception
153
      */
154
     public void testChangePassword() throws Exception {
155
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
156
         String password = authFile.resetPassword(USERNAME);
157
         String newPassword = "hello";
158
         authFile.modifyPassword(USERNAME, password, newPassword);
159
         
160
         try {
161
             authFile.resetPassword("user1");
162
             assertTrue("Can't reach here since we tried to reset the password for an unexisting user ", false);
163
         } catch (AuthenticationException e) {
164
             
165
         }
166
         try {
167
             authFile.modifyPassword("user1", "old", "new");
168
             assertTrue("Can't reach here since we tried to change the password for an unexisting user ", false);
169
         } catch (AuthenticationException e) {
170
             
171
         }
172
     }
173
}
    (1-1/1)