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
         return suite;
64
     }
65
     
66
     /**
67
      * Test the addGroup method
68
      * @throws Exception
69
      */
70
     public void testAddGroup() throws Exception{
71
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
72
         authFile.addGroup(GROUPNAME);
73
         try {
74
             authFile.addGroup(GROUPNAME);
75
             assertTrue("We can't reach here since we can't add the group twice", false);
76
         } catch (AuthenticationException e) {
77
             
78
         }
79
         
80
     }
81
     
82
     /**
83
      * Test the addGroup method
84
      * @throws Exception
85
      */
86
     public void testAddUser() throws Exception{
87
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
88
         String[]groups = {GROUPNAME};
89
         authFile.addUser(USERNAME, groups, PASSWORD);
90
         try {
91
             authFile.addUser(USERNAME, groups, PASSWORD);
92
             assertTrue("We can't reach here since we can't add the user twice", false);
93
         } catch (AuthenticationException e) {
94
             
95
         }
96
         
97
     }
98
     
99
     /**
100
      * Test the authentication method
101
      * @throws Exception
102
      */
103
     public void testAuthenticate() throws Exception {
104
         AuthFile authFile = AuthFile.getInstance(PASSWORDFILEPATH);
105
         boolean success = authFile.authenticate(USERNAME, PASSWORD);
106
         if(!success) {
107
             assertTrue("The authentication should succeed.", false);
108
         }
109
         success = authFile.authenticate(USERNAME, "hello");
110
         if(success) {
111
             assertTrue("The authentication should NOT succeed.", false);
112
         }
113
         success = authFile.authenticate("hello", PASSWORD);
114
         if(success) {
115
             assertTrue("The authentication should NOT succeed.", false);
116
         }
117
     }
118
}
    (1-1/1)