Commit 4ec22be0 authored by Alinga Yeung's avatar Alinga Yeung
Browse files

Story 1869. Updated codes based on Brian's second code review. Mock...

Story 1869. Updated codes based on Brian's second code review. Mock UserPersistence is still not returning the programmed exception.
parent 12ff3b54
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -136,12 +136,13 @@ public interface UserPersistence<T extends Principal>
     * @return User instance.
     *
     * @throws UserNotFoundException when the user is not found.
     * @throws UserAlreadyExistsException A user with the email address already exists
     * @throws TransientException If an temporary, unexpected problem occurred.
     * @throws AccessControlException If the operation is not permitted.
     */
    User<Principal> getUserByEmailAddress(String emailAddress)
            throws UserNotFoundException, TransientException,
            AccessControlException;
            throws UserNotFoundException, UserAlreadyExistsException, 
            TransientException, AccessControlException;

    /**
     * Get the user specified by userID whose account is pending approval.
+12 −22
Original line number Diff line number Diff line
@@ -134,7 +134,7 @@ import com.unboundid.ldap.sdk.extensions.PasswordModifyExtendedResult;
public class LdapUserDAO<T extends Principal> extends LdapDAO
{
    public static final String EMAIL_ADDRESS_CONFLICT_MESSAGE = 
            "More than one user with email address ";
            "email address ";
    
    private static final Logger logger = Logger.getLogger(LdapUserDAO.class);

@@ -307,25 +307,13 @@ public class LdapUserDAO<T extends Principal> extends LdapDAO
        }
        catch (UserNotFoundException ok) { }

        // check if email address is already in use
        try
        {
            String emailAddress = getEmailAddress(userRequest); 
            getUserByEmailAddress(emailAddress, usersDN);
            final String error = "email address " + emailAddress +
                                 " found in " + usersDN;
            throw new UserAlreadyExistsException(error);
        }
        catch (UserNotFoundException unfe) 
        { 
            if (unfe.getMessage().contains(EMAIL_ADDRESS_CONFLICT_MESSAGE))
            {
                throw new UserAlreadyExistsException(unfe.getMessage());
            }
            else
            {
                // ok, user not found
            }
        }
        catch (UserNotFoundException ok) { }
    }
    
    /**
@@ -434,10 +422,11 @@ public class LdapUserDAO<T extends Principal> extends LdapDAO
     * @throws UserNotFoundException  when the user is not found in the main tree.
     * @throws TransientException If an temporary, unexpected problem occurred.
     * @throws AccessControlException If the operation is not permitted.
     * @throws UserAlreadyExistsException A user with the same email address already exists
     */
    public User<Principal> getUserByEmailAddress(final String emailAddress)
            throws UserNotFoundException, TransientException,
            AccessControlException
            AccessControlException, UserAlreadyExistsException
    {
        return getUserByEmailAddress(emailAddress, config.getUsersDN());
    }
@@ -571,11 +560,12 @@ public class LdapUserDAO<T extends Principal> extends LdapDAO
     * @throws UserNotFoundException  when the user is not found.
     * @throws TransientException     If an temporary, unexpected problem occurred.
     * @throws AccessControlException If the operation is not permitted.
     * @throws UserAlreadyExistsException A user with the same email address already exists
     */
    private User<Principal> getUserByEmailAddress(final String emailAddress, 
            final String usersDN)
            throws UserNotFoundException, TransientException,
            AccessControlException
            AccessControlException, UserAlreadyExistsException
    {
        SearchResultEntry searchResult = null;
        Filter filter = null;
@@ -593,9 +583,9 @@ public class LdapUserDAO<T extends Principal> extends LdapDAO
        {
            if (e.getResultCode() == ResultCode.SIZE_LIMIT_EXCEEDED)
            {
                String msg = EMAIL_ADDRESS_CONFLICT_MESSAGE + emailAddress + " found";
                String msg = EMAIL_ADDRESS_CONFLICT_MESSAGE + emailAddress + " already in use";
                logger.debug(msg);
                throw new UserNotFoundException(msg);
                throw new UserAlreadyExistsException(msg);
            }
            else
            {
@@ -621,9 +611,9 @@ public class LdapUserDAO<T extends Principal> extends LdapDAO
            {
                if (e.getResultCode() == ResultCode.SIZE_LIMIT_EXCEEDED)
                {
                    String msg = EMAIL_ADDRESS_CONFLICT_MESSAGE + emailAddress + " found";
                    String msg = EMAIL_ADDRESS_CONFLICT_MESSAGE + emailAddress + " already in use";
                    logger.debug(msg);
                    throw new UserNotFoundException(msg);
                    throw new UserAlreadyExistsException(msg);
                }
                else
                {
+3 −1
Original line number Diff line number Diff line
@@ -197,9 +197,11 @@ public class LdapUserPersistence<T extends Principal> extends LdapPersistence im
     * @throws UserNotFoundException when the user is not found.
     * @throws TransientException If an temporary, unexpected problem occurred.
     * @throws AccessControlException If the operation is not permitted.
     * @throws UserAlreadyExistsException A user with the same email address already exists
     */
    public User<Principal> getUserByEmailAddress(String emailAddress)
            throws UserNotFoundException, TransientException, AccessControlException
            throws UserNotFoundException, TransientException, 
            AccessControlException, UserAlreadyExistsException
        {
            LdapConnections conns = new LdapConnections(this);
            try
+21 −21
Original line number Diff line number Diff line
@@ -69,6 +69,7 @@
package ca.nrc.cadc.ac.server.web;

import ca.nrc.cadc.ac.User;
import ca.nrc.cadc.ac.UserAlreadyExistsException;
import ca.nrc.cadc.ac.UserNotFoundException;
import ca.nrc.cadc.ac.server.ACScopeValidator;
import ca.nrc.cadc.ac.server.PluginFactory;
@@ -138,7 +139,6 @@ public class ResetPasswordServlet extends HttpServlet
            {
                x500List = x500Users.split(" ");
                httpList = httpUsers.split(" ");
            }

                if (x500List.length != httpList.length)
                {
@@ -153,6 +153,7 @@ public class ResetPasswordServlet extends HttpServlet
                    s.getPrincipals().add(new HttpPrincipal(httpList[i]));
                    privilegedSubjects.add(s);
                }
            }

            PluginFactory pluginFactory = new PluginFactory();
            userPersistence = pluginFactory.createUserPersistence();
@@ -289,19 +290,18 @@ public class ResetPasswordServlet extends HttpServlet
                
                throw t;
            }
            catch (UserNotFoundException e)
            catch (UserAlreadyExistsException e)
            {
                log.debug(e.getMessage(), e);
                logInfo.setMessage(e.getMessage());
                if (e.getMessage().contains("More than one user"))
                {
                response.setStatus(HttpServletResponse.SC_CONFLICT);
            }
                else
            catch (UserNotFoundException e)
            {
                log.debug(e.getMessage(), e);
                logInfo.setMessage(e.getMessage());
                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
            }
            }
            catch (IllegalArgumentException e)
            {
                log.debug(e.getMessage(), e);
+3 −1
Original line number Diff line number Diff line
@@ -92,6 +92,7 @@ import org.junit.Test;

import ca.nrc.cadc.ac.PersonalDetails;
import ca.nrc.cadc.ac.User;
import ca.nrc.cadc.ac.UserAlreadyExistsException;
import ca.nrc.cadc.ac.UserDetails;
import ca.nrc.cadc.ac.UserNotFoundException;
import ca.nrc.cadc.ac.UserRequest;
@@ -461,7 +462,7 @@ public class LdapUserDAOTest extends AbstractLdapDAOTest
            {
                Exception e = pae.getException();
                Throwable t = e.getCause();
                assertTrue(e.getCause() instanceof UserNotFoundException);
                assertTrue(e.getCause() instanceof UserAlreadyExistsException);
                assertTrue(e.getCause().getMessage().contains(LdapUserDAO.EMAIL_ADDRESS_CONFLICT_MESSAGE));
            }
            finally
@@ -475,6 +476,7 @@ public class LdapUserDAOTest extends AbstractLdapDAOTest
        }
        
    }
    
    @Test
    public void testGetPendingUser() throws Exception
    {
Loading