diff --git a/ccm-core/src/main/java/org/libreccm/core/User.java b/ccm-core/src/main/java/org/libreccm/core/User.java index 18acb2c8e..6735243b9 100644 --- a/ccm-core/src/main/java/org/libreccm/core/User.java +++ b/ccm-core/src/main/java/org/libreccm/core/User.java @@ -57,8 +57,8 @@ import javax.xml.bind.annotation.XmlTransient; @NamedQueries({ @NamedQuery(name = "findUserByScreenName", query = "SELECT u FROM User u WHERE u.screenName = :screenname"), - @NamedQuery(name = "findUserByEmail", - query = "SELECT u FROM User u JOIN u.emailAddresses e" + @NamedQuery(name = "findUserByEmailAddress", + query = "SELECT u FROM User u JOIN u.emailAddresses e " + "WHERE e.address = :emailAddress")}) @XmlRootElement(name = "user", namespace = CORE_XML_NS) //Supressing a few warnings from PMD because they misleading here. diff --git a/ccm-core/src/main/java/org/libreccm/core/authentication/LocalLoginModule.java b/ccm-core/src/main/java/org/libreccm/core/authentication/LocalLoginModule.java index 636f47c0a..b5deeb740 100644 --- a/ccm-core/src/main/java/org/libreccm/core/authentication/LocalLoginModule.java +++ b/ccm-core/src/main/java/org/libreccm/core/authentication/LocalLoginModule.java @@ -81,15 +81,15 @@ public class LocalLoginModule extends PasswordLoginModule { * @param password The password to verify. * * @return {@code true} if the password matches the password in the - * database, {@code false} if not or if there is no user account - * identified by the provided user name. + * database, {@code false} if not or if there is no user account identified + * by the provided user name. * * @throws LoginException If an error occurs in the process. */ @Override protected boolean checkPassword(final String username, final String password) - throws LoginException { + throws LoginException { //Depending on the configured user identifier retrieve the user account //using the screen name or the email address. @@ -103,7 +103,7 @@ public class LocalLoginModule extends PasswordLoginModule { //If no matching user is found report this by throwing an exception. if (user == null) { throw new AccountNotFoundException(String.format( - "No user account identified by '%s' found.", username)); + "No user account identified by '%s' found.", username)); } // Verify the password. The algorithm used for hashing is stored in the @@ -111,26 +111,25 @@ public class LocalLoginModule extends PasswordLoginModule { // first. try { final MessageDigest digest = MessageDigest.getInstance(user - .getHashAlgorithm()); + .getHashAlgorithm()); final String saltedPassword = String.format("%s%s", password, user.getSalt()); final String passwordHash = new String(digest.digest( - saltedPassword.getBytes())); + saltedPassword.getBytes())); if (passwordHash.equals(user.getPassword())) { - subject.getPrincipals().add(new SubjectPrincipal(user - .getSubjectId())); + subject.getPrincipals().add(new UserPrincipal(user)); return true; } else { return false; } } catch (NoSuchAlgorithmException ex) { throw new LoginException(String.format( - "Failed to validate password because the password stored for " + "Failed to validate password because the password stored for " + "user '%s' in the database is hashed with algorithm '%s' " - + "which is not avialable.", - username, user.getHashAlgorithm())); + + "which is not avialable.", + username, user.getHashAlgorithm())); } } diff --git a/ccm-core/src/main/java/org/libreccm/core/authentication/LoginManager.java b/ccm-core/src/main/java/org/libreccm/core/authentication/LoginManager.java index 2c35b342a..6eb98a0ed 100644 --- a/ccm-core/src/main/java/org/libreccm/core/authentication/LoginManager.java +++ b/ccm-core/src/main/java/org/libreccm/core/authentication/LoginManager.java @@ -72,19 +72,19 @@ public class LoginManager { loginContext.login(); final Subject subject = loginContext.getSubject(); - final Set principals = subject.getPrincipals( - SubjectPrincipal.class); + final Set principals = subject.getPrincipals( + UserPrincipal.class); if (principals.isEmpty()) { throw new LoginException("No principal set"); } else { - final Iterator iterator = principals.iterator(); - final SubjectPrincipal principal = iterator.next(); - final User user = userRepository.findById(principal.getSubjectId()); + final Iterator iterator = principals.iterator(); + final UserPrincipal principal = iterator.next(); + final User user = principal.getUser(); sessionContext.setCurrentParty(user); } } - + private class LoginCallbackHandler implements CallbackHandler { private final String username; diff --git a/ccm-core/src/main/java/org/libreccm/core/authentication/SubjectPrincipal.java b/ccm-core/src/main/java/org/libreccm/core/authentication/UserPrincipal.java similarity index 72% rename from ccm-core/src/main/java/org/libreccm/core/authentication/SubjectPrincipal.java rename to ccm-core/src/main/java/org/libreccm/core/authentication/UserPrincipal.java index b9bc0180e..7ad86cb4e 100644 --- a/ccm-core/src/main/java/org/libreccm/core/authentication/SubjectPrincipal.java +++ b/ccm-core/src/main/java/org/libreccm/core/authentication/UserPrincipal.java @@ -19,32 +19,34 @@ package org.libreccm.core.authentication; import java.security.Principal; +import java.util.Objects; +import org.libreccm.core.User; /** * * @author Jens Pelzetter */ -public final class SubjectPrincipal implements Principal { +public final class UserPrincipal implements Principal { - private final long subjectId; + private final User user; - public SubjectPrincipal(final long subjectId) { - this.subjectId = subjectId; + public UserPrincipal(final User user) { + this.user = user; } - public long getSubjectId() { - return subjectId; + public User getUser() { + return user; } @Override public String getName() { - return Long.toString(subjectId); + return user.getScreenName(); } @Override public int hashCode() { int hash = 5; - hash = 41 * hash + (int) (this.subjectId ^ (this.subjectId >>> 32)); + hash = 41 * Objects.hash(user); return hash; } @@ -56,17 +58,17 @@ public final class SubjectPrincipal implements Principal { if (getClass() != obj.getClass()) { return false; } - final SubjectPrincipal other = (SubjectPrincipal) obj; - return this.subjectId == other.getSubjectId(); + final UserPrincipal other = (UserPrincipal) obj; + return Objects.equals(user, other.getUser()); } @Override public String toString() { return String.format("%s{ " - + "subjectId = %d" + + "user = %s" + " }", super.toString(), - subjectId); + Objects.toString(user)); } } diff --git a/ccm-core/src/test/java/com/arsdigita/util/AssertTest.java b/ccm-core/src/test/java/com/arsdigita/util/AssertTest.java new file mode 100644 index 000000000..68a0272d2 --- /dev/null +++ b/ccm-core/src/test/java/com/arsdigita/util/AssertTest.java @@ -0,0 +1,157 @@ +/* + * Copyright (C) 2015 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package com.arsdigita.util; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.libreccm.tests.categories.UnitTest; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.*; + +/** + * + * @author Jens Pelzetter + */ +@Category(UnitTest.class) +public class AssertTest { + + public AssertTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Check if {@link Assert#setEnabled(boolean)} works correctly. + */ + @Test + public void assertDisable() { + Assert.setEnabled(false); + + assertThat(Assert.isEnabled(), is(false)); + + Assert.setEnabled(true); + } + + /** + * Check if {@link Assert#isTrue(boolean)} fails if condition evaluates to + * {@code false}. + */ + @Test(expected = AssertionError.class) + public void checkIfIsTrueFails() { + Assert.isTrue(false); + } + + /** + * Check if {@link Assert#isTrue(boolean, java.lang.String)} fails if + * condition evaluates to {@code false}. + */ + @Test(expected = AssertionError.class) + public void checkIfIsTrueWithMessageFails() { + Assert.isTrue(false, "Expected true"); + } + + @Test(expected = AssertionError.class) + public void checkIfIsFalseFails() { + Assert.isFalse(true); + } + + @Test(expected = AssertionError.class) + public void checkIfIsFalseWithMessageFails() { + Assert.isFalse(true, "Expected false"); + } + + @Test(expected = AssertionError.class) + public void checkIfExistsFailsForNull() { + Assert.exists(null); + } + + @Test + public void checkExists() { + Assert.exists("foo"); + } + + @Test(expected = AssertionError.class) + public void checkIfExistsWithClassFailsForNull() { + Assert.exists(null, Object.class); + } + + @Test(expected = AssertionError.class) + public void checkIfExistsFailsForNullWithMessage() { + Assert.exists(null, "None null object extected"); + } + + @Test(expected = AssertionError.class) + public void checkIfIsLockedFails() { + final Lockable unlocked = new LockableImpl(); + + Assert.isLocked(unlocked); + } + + @Test(expected = AssertionError.class) + public void checkIfIsUnLockedFails() { + final Lockable locked = new LockableImpl(); + locked.lock(); + + Assert.isUnlocked(locked); + } + + @Test(expected = AssertionError.class) + public void checkIfIsEqualFailsForUnequalObjects() { + Assert.isEqual("foo", "bar"); + } + + @Test(expected = AssertionError.class) + public void checkIfIsEqualFailsForNullAndObject() { + Assert.isEqual(null, "bar"); + } + + @Test(expected = AssertionError.class) + public void checkIfIsEqualFailsForObjectAndNull() { + Assert.isEqual("foo", null); + } + + @Test(expected = AssertionError.class) + public void checkIfIsNotEqualFailsForEqualObjects() { + Assert.isNotEqual("foo", "foo"); + } + + @Test(expected = AssertionError.class) + public void checkIfIsNotEqualFailsIfBothAreNull() { + Assert.isNotEqual(null, null); + } +} diff --git a/ccm-core/src/test/java/org/libreccm/core/CcmObjectRepositoryTest.java b/ccm-core/src/test/java/org/libreccm/core/CcmObjectRepositoryTest.java index 424aba5d3..53134f2ac 100644 --- a/ccm-core/src/test/java/org/libreccm/core/CcmObjectRepositoryTest.java +++ b/ccm-core/src/test/java/org/libreccm/core/CcmObjectRepositoryTest.java @@ -90,38 +90,38 @@ public class CcmObjectRepositoryTest { @Deployment public static WebArchive createDeployment() { final PomEquippedResolveStage pom = Maven - .resolver() - .loadPomFromFile("pom.xml"); + .resolver() + .loadPomFromFile("pom.xml"); final PomEquippedResolveStage dependencies = pom - .importCompileAndRuntimeDependencies(); + .importCompileAndRuntimeDependencies(); final File[] libs = dependencies.resolve().withTransitivity().asFile(); for (File lib : libs) { - System.err.printf("Adding file '%s' to test archive...\n", + System.err.printf("Adding file '%s' to test archive...%n", lib.getName()); } return ShrinkWrap - .create(WebArchive.class, - "LibreCCM-org.libreccm.core.CcmObjectRepositoryTest.war"). - addPackage(CcmObject.class.getPackage()) - .addPackage(org.libreccm.web.Application.class.getPackage()) - .addPackage(org.libreccm.categorization.Category.class. - getPackage()) - .addPackage(org.libreccm.l10n.LocalizedString.class.getPackage()). - addPackage(org.libreccm.jpa.EntityManagerProducer.class - .getPackage()) - .addPackage(org.libreccm.jpa.utils.MimeTypeConverter.class - .getPackage()) - .addPackage(org.libreccm.testutils.EqualsVerifier.class. - getPackage()) - .addPackage(org.libreccm.tests.categories.IntegrationTest.class - .getPackage()) - .addAsLibraries(libs) - .addAsResource("test-persistence.xml", - "META-INF/persistence.xml") - .addAsWebInfResource("test-web.xml", "WEB-INF/web.xml") - .addAsWebInfResource(EmptyAsset.INSTANCE, "WEB-INF/beans.xml"); + .create(WebArchive.class, + "LibreCCM-org.libreccm.core.CcmObjectRepositoryTest.war"). + addPackage(CcmObject.class.getPackage()) + .addPackage(org.libreccm.web.Application.class.getPackage()) + .addPackage(org.libreccm.categorization.Category.class. + getPackage()) + .addPackage(org.libreccm.l10n.LocalizedString.class.getPackage()). + addPackage(org.libreccm.jpa.EntityManagerProducer.class + .getPackage()) + .addPackage(org.libreccm.jpa.utils.MimeTypeConverter.class + .getPackage()) + .addPackage(org.libreccm.testutils.EqualsVerifier.class. + getPackage()) + .addPackage(org.libreccm.tests.categories.IntegrationTest.class + .getPackage()) + .addAsLibraries(libs) + .addAsResource("test-persistence.xml", + "META-INF/persistence.xml") + .addAsWebInfResource("test-web.xml", "WEB-INF/web.xml") + .addAsWebInfResource(EmptyAsset.INSTANCE, "WEB-INF/beans.xml"); } @Test @@ -136,7 +136,7 @@ public class CcmObjectRepositoryTest { @Test @UsingDataSet( - "datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json") + "datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json") @InSequence(4) public void datasetOnly() { System.out.println("Dataset loaded successfully."); @@ -144,7 +144,7 @@ public class CcmObjectRepositoryTest { @Test @UsingDataSet( - "datasets/org/libreccm/core/CcmObjectRepositoryTest/after-save-changed.json") + "datasets/org/libreccm/core/CcmObjectRepositoryTest/after-save-changed.json") @InSequence(4) public void datasetOnly2() { System.out.println("Dataset loaded successfully."); @@ -152,7 +152,7 @@ public class CcmObjectRepositoryTest { @Test @UsingDataSet( - "datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json") + "datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json") @InSequence(5) public void entityManagerFindCcmObjectByLongPrimitive() { final CcmObject obj1 = entityManager.find(CcmObject.class, -10L); @@ -177,7 +177,7 @@ public class CcmObjectRepositoryTest { @Test @UsingDataSet( - "datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json") + "datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json") @InSequence(6) public void entityManagerFindCcmObjectByLongClass() { final CcmObject obj1 = entityManager.find(CcmObject.class, @@ -206,7 +206,7 @@ public class CcmObjectRepositoryTest { @Test @UsingDataSet( - "datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json") + "datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json") @InSequence(10) public void findCcmObjectById() { final CcmObject obj1 = ccmObjectRepository.findById(-10L); @@ -231,7 +231,7 @@ public class CcmObjectRepositoryTest { @Test @UsingDataSet( - "datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json") + "datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json") @InSequence(10) public void findAllCcmObjects() { final List objects = ccmObjectRepository.findAll(); @@ -241,9 +241,9 @@ public class CcmObjectRepositoryTest { @Test @UsingDataSet( - "datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json") + "datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json") @ShouldMatchDataSet(value - = "datasets/org/libreccm/core/CcmObjectRepositoryTest/after-save-new.json", + = "datasets/org/libreccm/core/CcmObjectRepositoryTest/after-save-new.json", excludeColumns = {"object_id"}) @InSequence(300) public void saveNewCcmObject() { @@ -255,9 +255,9 @@ public class CcmObjectRepositoryTest { @Test @UsingDataSet( - "datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json") + "datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json") @ShouldMatchDataSet(value - = "datasets/org/libreccm/core/CcmObjectRepositoryTest/after-save-changed.json", + = "datasets/org/libreccm/core/CcmObjectRepositoryTest/after-save-changed.json", excludeColumns = {"object_id"}) @InSequence(400) public void saveChangedCcmObject() { @@ -276,9 +276,9 @@ public class CcmObjectRepositoryTest { @Test @UsingDataSet( - "datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json") + "datasets/org/libreccm/core/CcmObjectRepositoryTest/data.json") @ShouldMatchDataSet(value - = "datasets/org/libreccm/core/CcmObjectRepositoryTest/after-delete.json", + = "datasets/org/libreccm/core/CcmObjectRepositoryTest/after-delete.json", excludeColumns = {"object_id"}) @InSequence(600) public void deleteCcmObject() { diff --git a/ccm-core/src/test/java/org/libreccm/core/UserRepositoryTest.java b/ccm-core/src/test/java/org/libreccm/core/UserRepositoryTest.java new file mode 100644 index 000000000..4a0e6c7ec --- /dev/null +++ b/ccm-core/src/test/java/org/libreccm/core/UserRepositoryTest.java @@ -0,0 +1,234 @@ +/* + * Copyright (C) 2015 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.core; + +import java.io.File; +import javax.inject.Inject; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import org.junit.Test; + +import static org.hamcrest.Matchers.*; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.arquillian.junit.InSequence; +import org.jboss.arquillian.persistence.PersistenceTest; +import org.jboss.arquillian.persistence.UsingDataSet; +import org.jboss.arquillian.transaction.api.annotation.TransactionMode; +import org.jboss.arquillian.transaction.api.annotation.Transactional; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.jboss.shrinkwrap.resolver.api.maven.Maven; +import org.jboss.shrinkwrap.resolver.api.maven.PomEquippedResolveStage; +import org.junit.After; +import org.junit.AfterClass; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.libreccm.tests.categories.IntegrationTest; + +/** + * + * @author Jens Pelzetter + */ +@Category(IntegrationTest.class) +@RunWith(Arquillian.class) +@PersistenceTest +@Transactional(TransactionMode.COMMIT) +public class UserRepositoryTest { + + private static final String NOBODY = "nobody"; + private static final String JOE = "joe"; + private static final String MMUSTER = "mmuster"; + private static final String JDOE = "jdoe"; + + @Inject + private transient UserRepository userRepository; + + @PersistenceContext + private transient EntityManager entityManager; + + public UserRepositoryTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + @Deployment + public static WebArchive createDeployment() { + final PomEquippedResolveStage pom = Maven + .resolver() + .loadPomFromFile("pom.xml"); + final PomEquippedResolveStage dependencies = pom. + importCompileAndRuntimeDependencies(); + final File[] libs = dependencies.resolve().withTransitivity().asFile(); + + for (File lib : libs) { + System.err.printf("Adding file '%s' to test archive...%n", + lib.getName()); + } + + return ShrinkWrap + .create(WebArchive.class, + "LibreCCM-org.libreccm.core.UserRepositoryTest.war") + .addPackage(User.class.getPackage()) + .addPackage(org.libreccm.web.Application.class.getPackage()) + .addPackage(org.libreccm.categorization.Category.class. + getPackage()) + .addPackage(org.libreccm.l10n.LocalizedString.class.getPackage()). + addPackage(org.libreccm.jpa.EntityManagerProducer.class + .getPackage()) + .addPackage(org.libreccm.jpa.utils.MimeTypeConverter.class + .getPackage()) + .addPackage(org.libreccm.testutils.EqualsVerifier.class. + getPackage()) + .addPackage(org.libreccm.tests.categories.IntegrationTest.class + .getPackage()) + .addAsLibraries(libs) + .addAsResource("test-persistence.xml", + "META-INF/persistence.xml") + .addAsWebInfResource("test-web.xml", "WEB-INF/web.xml") + .addAsWebInfResource(EmptyAsset.INSTANCE, "WEB-INF/beans.xml"); + + } + + @Test + public void repoIsInjected() { + assertThat(userRepository, is(not(nullValue()))); + } + + @Test + public void entityManagerIsInjected() { + assertThat(entityManager, is(not(nullValue()))); + } + + @Test + @UsingDataSet( + "datasets/org/libreccm/core/UserRepositoryTest/data.json") + @InSequence(10) + public void findUserByScreenName() { + final User jdoe = userRepository.findByScreenName(JDOE); + final User mmuster = userRepository.findByScreenName(MMUSTER); + final User joe = userRepository.findByScreenName(JOE); + final User nobody = userRepository.findByScreenName(NOBODY); + + assertThat(jdoe, is(not(nullValue()))); + assertThat(jdoe.getSubjectId(), is(-10L)); + assertThat(jdoe.getScreenName(), is(JDOE)); + assertThat(jdoe.getName().getFamilyName(), is(equalTo("Doe"))); + assertThat(jdoe.getName().getMiddleName(), is(nullValue())); + assertThat(jdoe.getName().getGivenName(), is(equalTo("John"))); + assertThat(jdoe.getHashAlgorithm(), is("MD5")); + assertThat(jdoe.getPassword(), is("604622dc8a888eb093454ebd77ca1675")); + assertThat(jdoe.getSalt(), is("axg8ira8fa")); + + assertThat(mmuster, is(not(nullValue()))); + assertThat(mmuster.getSubjectId(), is(-20L)); + assertThat(mmuster.getScreenName(), is(equalTo(MMUSTER))); + assertThat(mmuster.getName().getFamilyName(), is(equalTo("Mustermann"))); + assertThat(mmuster.getName().getMiddleName(), is(nullValue())); + assertThat(mmuster.getName().getGivenName(), is(equalTo("Max"))); + assertThat(mmuster.getHashAlgorithm(), is(equalTo("SHA-512"))); + assertThat(mmuster.getPassword(), is(equalTo( + "1c9626af429a6291766d15cbfb38689bd8d49450520765973de70aecaf644b7d4fda711266ba9ec8fb6df30c8ab391d40330829aa85adf371bcde6b4c9bc01e6"))); + assertThat(mmuster.getSalt(), is(equalTo("fjiajhigafgapoa"))); + + assertThat(joe, is(not(nullValue()))); + assertThat(joe.getSubjectId(), is(-30L)); + assertThat(joe.getScreenName(), is(equalTo(JOE))); + assertThat(joe.getName().getFamilyName(), is(equalTo("Public"))); + assertThat(joe.getName().getMiddleName(), is(nullValue())); + assertThat(joe.getName().getGivenName(), is(equalTo("Joe"))); + assertThat(joe.getHashAlgorithm(), is(equalTo("SHA-512"))); + assertThat(joe.getPassword(), is(equalTo( + "axg8ira8fa"))); + assertThat(joe.getSalt(), is(equalTo("fjiajhigafgapoa"))); + + assertThat(nobody, is(nullValue())); + } + + @Test + @UsingDataSet( + "datasets/org/libreccm/core/UserRepositoryTest/data.json") + @InSequence(20) + public void findUserByEmail() { + final User jdoe = userRepository.findByEmailAddress("john.doe@example.com"); + final User mmuster1 = userRepository.findByEmailAddress("max.mustermann@example.org"); + final User mmuster2 = userRepository.findByEmailAddress("mm@example.com"); + final User joe = userRepository.findByEmailAddress("joe.public@example.com"); + final User nobody = userRepository.findByScreenName("nobody@example.org"); + + assertThat(jdoe, is(not(nullValue()))); + assertThat(jdoe.getSubjectId(), is(-10L)); + assertThat(jdoe.getScreenName(), is(JDOE)); + assertThat(jdoe.getName().getFamilyName(), is(equalTo("Doe"))); + assertThat(jdoe.getName().getMiddleName(), is(nullValue())); + assertThat(jdoe.getName().getGivenName(), is(equalTo("John"))); + assertThat(jdoe.getHashAlgorithm(), is("MD5")); + assertThat(jdoe.getPassword(), is("604622dc8a888eb093454ebd77ca1675")); + assertThat(jdoe.getSalt(), is("axg8ira8fa")); + + assertThat(mmuster1, is(not(nullValue()))); + assertThat(mmuster1.getSubjectId(), is(-20L)); + assertThat(mmuster1.getScreenName(), is(equalTo(MMUSTER))); + assertThat(mmuster1.getName().getFamilyName(), is(equalTo("Mustermann"))); + assertThat(mmuster1.getName().getMiddleName(), is(nullValue())); + assertThat(mmuster1.getName().getGivenName(), is(equalTo("Max"))); + assertThat(mmuster1.getHashAlgorithm(), is(equalTo("SHA-512"))); + assertThat(mmuster1.getPassword(), is(equalTo( + "1c9626af429a6291766d15cbfb38689bd8d49450520765973de70aecaf644b7d4fda711266ba9ec8fb6df30c8ab391d40330829aa85adf371bcde6b4c9bc01e6"))); + assertThat(mmuster1.getSalt(), is(equalTo("fjiajhigafgapoa"))); + + assertThat(mmuster2, is(equalTo(mmuster1))); + + assertThat(joe, is(not(nullValue()))); + assertThat(joe.getSubjectId(), is(-30L)); + assertThat(joe.getScreenName(), is(equalTo(JOE))); + assertThat(joe.getName().getFamilyName(), is(equalTo("Public"))); + assertThat(joe.getName().getMiddleName(), is(nullValue())); + assertThat(joe.getName().getGivenName(), is(equalTo("Joe"))); + assertThat(joe.getHashAlgorithm(), is(equalTo("SHA-512"))); + assertThat(joe.getPassword(), is(equalTo( + "axg8ira8fa"))); + assertThat(joe.getSalt(), is(equalTo("fjiajhigafgapoa"))); + + assertThat(nobody, is(nullValue())); + + } + +} diff --git a/ccm-core/src/test/java/org/libreccm/core/authentication/EqualsAndHashCodeTest.java b/ccm-core/src/test/java/org/libreccm/core/authentication/EqualsAndHashCodeTest.java new file mode 100644 index 000000000..9adf57106 --- /dev/null +++ b/ccm-core/src/test/java/org/libreccm/core/authentication/EqualsAndHashCodeTest.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2015 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.core.authentication; + +import java.util.Arrays; +import java.util.Collection; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.libreccm.tests.categories.UnitTest; +import org.libreccm.testutils.EqualsVerifier; + +/** + * + * @author Jens Pelzetter + */ +@RunWith(Parameterized.class) +@Category(UnitTest.class) +public class EqualsAndHashCodeTest extends EqualsVerifier { + + @Parameterized.Parameters(name = "{0}") + public static Collection> data() { + return Arrays.asList(new Class[] { + UserPrincipal.class + }); + } + + public EqualsAndHashCodeTest(final Class entityClass) { + super(entityClass); + } + +} diff --git a/ccm-core/src/test/java/org/libreccm/core/authentication/UserPrincipalToStringTest.java b/ccm-core/src/test/java/org/libreccm/core/authentication/UserPrincipalToStringTest.java new file mode 100644 index 000000000..73b3bb755 --- /dev/null +++ b/ccm-core/src/test/java/org/libreccm/core/authentication/UserPrincipalToStringTest.java @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2015 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package org.libreccm.core.authentication; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * + * @author Jens Pelzetter + */ +public class UserPrincipalToStringTest { + + public UserPrincipalToStringTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + @Test + public void verifyToString() throws IllegalArgumentException, + IllegalAccessException { + final UserPrincipal principal = new UserPrincipal(null); + + final Field[] fields = principal.getClass().getDeclaredFields(); + for (Field field : fields) { + if (!Modifier.isStatic(field.getModifiers()) + && !field.getType().isPrimitive()) { + field.setAccessible(true); + field.set(principal, null); + } + } + + try { + principal.toString(); + } catch (NullPointerException ex) { + final StringWriter strWriter = new StringWriter(); + final PrintWriter writer = new PrintWriter(strWriter); + ex.printStackTrace(writer); + Assert.fail(String.format( + "toString() implementation of of class \"%s\" " + + "is not null safe:%n %s", + principal.getClass().getName(), + strWriter.toString())); + + } + } +} diff --git a/ccm-core/src/test/resources-tomee-embedded/arquillian.xml b/ccm-core/src/test/resources-tomee-embedded/arquillian.xml index cf1ca4d0f..04ca8e83c 100644 --- a/ccm-core/src/test/resources-tomee-embedded/arquillian.xml +++ b/ccm-core/src/test/resources-tomee-embedded/arquillian.xml @@ -9,7 +9,7 @@ - testdb + java:openejb/Resource/org-libreccm-ccm-core.testdb org.hibernate.jpa.HibernatePersistenceProvider - testdb + java:openejb/Resource/org-libreccm-ccm-core.testdb diff --git a/ccm-core/src/test/resources/datasets/org/libreccm/core/UserRepositoryTest/after-save-new.json b/ccm-core/src/test/resources/datasets/org/libreccm/core/UserRepositoryTest/after-save-new.json index 8749220ed..92977ad05 100644 --- a/ccm-core/src/test/resources/datasets/org/libreccm/core/UserRepositoryTest/after-save-new.json +++ b/ccm-core/src/test/resources/datasets/org/libreccm/core/UserRepositoryTest/after-save-new.json @@ -108,7 +108,7 @@ "middle_name": "Anna", "title_post": null, "title_pre": "Dr.", - "password": "9f8ec7f4bcd376d1736ae9f5250412e15bed1783219fd5a03491cddf167ab2582763392a29ed1859a4e3bce81da65a6d861eda80b96fa0dcb88336c33f432183", + "password": "32d2a830fb03f201bda975ae70a62c207716705a049e054cf6701de1cec546d3a9e03a094be2e98e4d125af996ebbfa5a7754754a1e9d2fe063a0d9921cb201d", "password_answer": null, "password_question": null, "password_reset_required": false, diff --git a/ccm-core/src/test/resources/datasets/org/libreccm/core/UserRepositoryTest/data.json b/ccm-core/src/test/resources/datasets/org/libreccm/core/UserRepositoryTest/data.json index f46bdd38f..76bb65a10 100644 --- a/ccm-core/src/test/resources/datasets/org/libreccm/core/UserRepositoryTest/data.json +++ b/ccm-core/src/test/resources/datasets/org/libreccm/core/UserRepositoryTest/data.json @@ -65,7 +65,7 @@ "middle_name": null, "title_post": null, "title_pre": null, - "password": "3428dfa9bb199f3372eb73c5feb038aef5bcbbfd4fada24c44026a25c883a9f2af8b84410a3b2684430d359f78400ad92ff8c71634f2c9bf437f0cd56c5d90c3", + "password": "1c9626af429a6291766d15cbfb38689bd8d49450520765973de70aecaf644b7d4fda711266ba9ec8fb6df30c8ab391d40330829aa85adf371bcde6b4c9bc01e6", "password_answer": null, "password_question": null, "password_reset_required": false, @@ -82,7 +82,7 @@ "middle_name": null, "title_post": null, "title_pre": null, - "password": "e9db883a56149f7521a00713c4d7b0c967ae42cc3f0dfcf96ccd0180c498abf57209a90db9cb5df3a7d23d4111e87578e09ea610e9c1d95c4695710e28c5ab02", + "password": "4e39eba7f2927182a532cd8700bf251e58d4b0359fbb832e6af21db7501d7a49e6d8b950e0d4b15b1841af0f786c8edaa0c09ef7f474804254f7e895969d2975", "password_answer": null, "password_question": null, "password_reset_required": false, diff --git a/ccm-core/src/test/resources/datasets/org/libreccm/core/UserRepositoryTest/passwords.txt b/ccm-core/src/test/resources/datasets/org/libreccm/core/UserRepositoryTest/passwords.txt index 5f3767e1e..7ead5884d 100644 --- a/ccm-core/src/test/resources/datasets/org/libreccm/core/UserRepositoryTest/passwords.txt +++ b/ccm-core/src/test/resources/datasets/org/libreccm/core/UserRepositoryTest/passwords.txt @@ -1,3 +1,13 @@ +data: + +John Doe......: foo123 +Max Mustermann: foo123 +Joe Public....: foo123 + after-save-changed: -John Doe: Een5vuwa \ No newline at end of file +John Doe: Een5vuwa + +after-save-new: + +Jane Anna Doe: bar123 \ No newline at end of file