001: package com.ecyrd.jspwiki.auth.user;
002:
003: import java.security.Principal;
004: import java.util.Properties;
005:
006: import org.apache.commons.lang.ArrayUtils;
007:
008: import junit.framework.TestCase;
009:
010: import com.ecyrd.jspwiki.TestEngine;
011: import com.ecyrd.jspwiki.WikiEngine;
012: import com.ecyrd.jspwiki.auth.NoSuchPrincipalException;
013: import com.ecyrd.jspwiki.auth.Users;
014: import com.ecyrd.jspwiki.auth.WikiPrincipal;
015: import com.ecyrd.jspwiki.auth.WikiSecurityException;
016:
017: /**
018: * @author Andrew Jaquith
019: */
020: public class XMLUserDatabaseTest extends TestCase {
021:
022: private XMLUserDatabase m_db;
023:
024: /**
025: * @see junit.framework.TestCase#setUp()
026: */
027: protected void setUp() throws Exception {
028: super .setUp();
029: Properties props = new Properties();
030: props.load(TestEngine.findTestProperties());
031: props.put(XMLUserDatabase.PROP_USERDATABASE,
032: "tests/etc/userdatabase.xml");
033: WikiEngine engine = new TestEngine(props);
034: m_db = new XMLUserDatabase();
035: m_db.initialize(engine, props);
036: }
037:
038: public void testDeleteByLoginName() throws WikiSecurityException {
039: // First, count the number of users in the db now.
040: int oldUserCount = m_db.getWikiNames().length;
041:
042: // Create a new user with random name
043: String loginName = "TestUser"
044: + String.valueOf(System.currentTimeMillis());
045: UserProfile profile = new DefaultUserProfile();
046: profile.setEmail("testuser@testville.com");
047: profile.setLoginName(loginName);
048: profile.setFullname("FullName" + loginName);
049: profile.setPassword("password");
050: m_db.save(profile);
051:
052: // Make sure the profile saved successfully
053: profile = m_db.findByLoginName(loginName);
054: assertEquals(loginName, profile.getLoginName());
055: assertEquals(oldUserCount + 1, m_db.getWikiNames().length);
056:
057: // Now delete the profile; should be back to old count
058: m_db.deleteByLoginName(loginName);
059: assertEquals(oldUserCount, m_db.getWikiNames().length);
060: }
061:
062: public void testFindByEmail() {
063: try {
064: UserProfile profile = m_db.findByEmail("janne@ecyrd.com");
065: assertEquals("janne", profile.getLoginName());
066: assertEquals("Janne Jalkanen", profile.getFullname());
067: assertEquals("JanneJalkanen", profile.getWikiName());
068: assertEquals(
069: "{SHA}457b08e825da547c3b77fbc1ff906a1d00a7daee",
070: profile.getPassword());
071: assertEquals("janne@ecyrd.com", profile.getEmail());
072: } catch (NoSuchPrincipalException e) {
073: assertTrue(false);
074: }
075: try {
076: m_db.findByEmail("foo@bar.org");
077: // We should never get here
078: assertTrue(false);
079: } catch (NoSuchPrincipalException e) {
080: assertTrue(true);
081: }
082: }
083:
084: public void testFindByWikiName() {
085: try {
086: UserProfile profile = m_db.findByWikiName("JanneJalkanen");
087: assertEquals("janne", profile.getLoginName());
088: assertEquals("Janne Jalkanen", profile.getFullname());
089: assertEquals("JanneJalkanen", profile.getWikiName());
090: assertEquals(
091: "{SHA}457b08e825da547c3b77fbc1ff906a1d00a7daee",
092: profile.getPassword());
093: assertEquals("janne@ecyrd.com", profile.getEmail());
094: } catch (NoSuchPrincipalException e) {
095: assertTrue(false);
096: }
097: try {
098: m_db.findByEmail("foo");
099: // We should never get here
100: assertTrue(false);
101: } catch (NoSuchPrincipalException e) {
102: assertTrue(true);
103: }
104: }
105:
106: public void testFindByLoginName() {
107: try {
108: UserProfile profile = m_db.findByLoginName("janne");
109: assertEquals("janne", profile.getLoginName());
110: assertEquals("Janne Jalkanen", profile.getFullname());
111: assertEquals("JanneJalkanen", profile.getWikiName());
112: assertEquals(
113: "{SHA}457b08e825da547c3b77fbc1ff906a1d00a7daee",
114: profile.getPassword());
115: assertEquals("janne@ecyrd.com", profile.getEmail());
116: } catch (NoSuchPrincipalException e) {
117: assertTrue(false);
118: }
119: try {
120: m_db.findByEmail("FooBar");
121: // We should never get here
122: assertTrue(false);
123: } catch (NoSuchPrincipalException e) {
124: assertTrue(true);
125: }
126: }
127:
128: public void testGetWikiNames() throws WikiSecurityException {
129: // There are 8 test users in the database
130: Principal[] p = m_db.getWikiNames();
131: assertEquals(8, p.length);
132: assertTrue(ArrayUtils.contains(p, new WikiPrincipal(
133: "JanneJalkanen", WikiPrincipal.WIKI_NAME)));
134: assertTrue(ArrayUtils.contains(p, new WikiPrincipal("",
135: WikiPrincipal.WIKI_NAME)));
136: assertTrue(ArrayUtils.contains(p, new WikiPrincipal(
137: "Administrator", WikiPrincipal.WIKI_NAME)));
138: assertTrue(ArrayUtils.contains(p, new WikiPrincipal(
139: Users.ALICE, WikiPrincipal.WIKI_NAME)));
140: assertTrue(ArrayUtils.contains(p, new WikiPrincipal(Users.BOB,
141: WikiPrincipal.WIKI_NAME)));
142: assertTrue(ArrayUtils.contains(p, new WikiPrincipal(
143: Users.CHARLIE, WikiPrincipal.WIKI_NAME)));
144: assertTrue(ArrayUtils.contains(p, new WikiPrincipal(
145: "FredFlintstone", WikiPrincipal.WIKI_NAME)));
146: assertTrue(ArrayUtils.contains(p, new WikiPrincipal(Users.BIFF,
147: WikiPrincipal.WIKI_NAME)));
148: }
149:
150: public void testRename() throws Exception {
151: // Try renaming a non-existent profile; it should fail
152: try {
153: m_db.rename("nonexistentname", "renameduser");
154: fail("Should not have allowed rename...");
155: } catch (NoSuchPrincipalException e) {
156: // Cool; that's what we expect
157: }
158:
159: // Create new user & verify it saved ok
160: UserProfile profile = new DefaultUserProfile();
161: profile.setEmail("renamed@example.com");
162: profile.setFullname("Renamed User");
163: profile.setLoginName("olduser");
164: profile.setPassword("password");
165: m_db.save(profile);
166: profile = m_db.findByLoginName("olduser");
167: assertNotNull(profile);
168:
169: // Try renaming to a login name that's already taken; it should fail
170: try {
171: m_db.rename("olduser", "janne");
172: fail("Should not have allowed rename...");
173: } catch (DuplicateUserException e) {
174: // Cool; that's what we expect
175: }
176:
177: // Now, rename it to an unused name
178: m_db.rename("olduser", "renameduser");
179:
180: // The old user shouldn't be found
181: try {
182: profile = m_db.findByLoginName("olduser");
183: fail("Old user was found, but it shouldn't have been.");
184: } catch (NoSuchPrincipalException e) {
185: // Cool, it's gone
186: }
187:
188: // The new profile should be found, and its properties should match the old ones
189: profile = m_db.findByLoginName("renameduser");
190: assertEquals("renamed@example.com", profile.getEmail());
191: assertEquals("Renamed User", profile.getFullname());
192: assertEquals("renameduser", profile.getLoginName());
193: assertEquals("{SHA}5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8",
194: profile.getPassword());
195:
196: // Delete the user
197: m_db.deleteByLoginName("renameduser");
198: }
199:
200: public void testSave() {
201: try {
202: UserProfile profile = new DefaultUserProfile();
203: profile.setEmail("user@example.com");
204: profile.setLoginName("user");
205: profile.setPassword("password");
206: m_db.save(profile);
207: profile = m_db.findByEmail("user@example.com");
208: assertEquals("user@example.com", profile.getEmail());
209: assertEquals(
210: "{SHA}5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8",
211: profile.getPassword());
212: } catch (NoSuchPrincipalException e) {
213: assertTrue(false);
214: } catch (WikiSecurityException e) {
215: assertTrue(false);
216: }
217: }
218:
219: public void testValidatePassword() {
220: assertFalse(m_db.validatePassword("janne", "test"));
221: assertTrue(m_db.validatePassword("janne", "myP@5sw0rd"));
222: assertTrue(m_db.validatePassword("user", "password"));
223: }
224:
225: }
|