Skip to content
Snippets Groups Projects
Commit a3fe9be2 authored by yvesn's avatar yvesn
Browse files

SSDM-5415: when using the change command, the fields which are not given as...

SSDM-5415: when using the change command, the fields which are not given as parameters are not set to empty

SVN: 38574
parent b3b618ae
No related branches found
No related tags found
No related merge requests found
......@@ -229,6 +229,11 @@ final class Parameters
return firstNameOrNull == null ? "" : firstNameOrNull;
}
final Boolean isFirstNameSet()
{
return firstNameOrNull != null;
}
/**
* Returns the last name of the user, or an empty string otherwise.
*/
......@@ -238,6 +243,11 @@ final class Parameters
return lastNameOrNull == null ? "" : lastNameOrNull;
}
final Boolean isLastNameSet()
{
return lastNameOrNull != null;
}
/**
* Returns the email of the user, or an empty string otherwise.
*/
......@@ -247,6 +257,11 @@ final class Parameters
return emailOrNull == null ? "" : emailOrNull;
}
final Boolean isEmailSet()
{
return emailOrNull != null;
}
/**
* Returns <code>true</code>, if the password should be changed.
* <p>
......
......@@ -22,11 +22,10 @@ import java.util.Date;
import org.apache.commons.lang.StringUtils;
import jline.ConsoleReader;
import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException;
import ch.systemsx.cisd.common.exceptions.HighLevelException;
import ch.systemsx.cisd.common.time.DateFormatThreadLocal;
import jline.ConsoleReader;
/**
* A class to create and edit password entries.
......@@ -157,25 +156,7 @@ public class PasswordEditorCommand
System.exit(1);
return; // Fake: convince compiler that it is save to dereference userOrNull
}
if (params.getFirstName() != null)
{
userOrNull.setFirstName(params.getFirstName());
}
if (params.getLastName() != null)
{
userOrNull.setLastName(params.getLastName());
}
if (params.getEmail() != null)
{
userOrNull.setEmail(params.getEmail());
}
if (params.tryGetPassword() != null)
{
userOrNull.setPassword(params.tryGetPassword());
} else if (params.isChangePassword())
{
userOrNull.setPassword(readPassword(ENTER_NEW_PASSWORD_MSG));
}
applyParamsToExistingUser(params, userOrNull);
userStore.addOrUpdateUser(userOrNull);
break;
}
......@@ -235,6 +216,29 @@ public class PasswordEditorCommand
}
}
protected static void applyParamsToExistingUser(Parameters params, final UserEntry user)
{
if (params.isFirstNameSet())
{
user.setFirstName(params.getFirstName());
}
if (params.isLastNameSet())
{
user.setLastName(params.getLastName());
}
if (params.isEmailSet())
{
user.setEmail(params.getEmail());
}
if (params.tryGetPassword() != null)
{
user.setPassword(params.tryGetPassword());
} else if (params.isChangePassword())
{
user.setPassword(readPassword(ENTER_NEW_PASSWORD_MSG));
}
}
private static void executeCache(Parameters params)
{
final IUserStore<UserCacheEntry> userStore =
......
package ch.systemsx.cisd.authentication.file;
import static org.testng.Assert.assertEquals;
import org.testng.annotations.Test;
public class PasswordEditorCommandTest {
/**
* Changing one field (e.g. password) should not change other fields.
*/
@Test
public void testPartialChange()
{
// given
UserEntry existingUser = new UserEntry("markwatney", "watney@mars.com", "Mark", "Watney", "oldPassword");
Parameters params = new Parameters(new String[] {"change", "markwatney", "-p", "newPassword"}, false);
// when
PasswordEditorCommand.applyParamsToExistingUser(params, existingUser);
// then
assertEquals("Mark", existingUser.getFirstName());
assertEquals("Watney", existingUser.getLastName());
assertEquals("watney@mars.com", existingUser.getEmail());
}
/**
* Changing a field to an empty string should work.
*/
@Test
public void testChangeToEmpty()
{
// given
UserEntry existingUser = new UserEntry("markwatney", "watney@mars.com", "Mark", "Watney", "oldPassword");
Parameters params = new Parameters(new String[] {"change", "markwatney", "-f", ""}, false);
// when
PasswordEditorCommand.applyParamsToExistingUser(params, existingUser);
// then
assertEquals("", existingUser.getFirstName());
assertEquals("Watney", existingUser.getLastName());
assertEquals("watney@mars.com", existingUser.getEmail());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment