Skip to content

Commit

Permalink
Allow to use single hyphen in the middle of account name
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergii Leschenko committed Mar 30, 2017
1 parent 5a84182 commit d7cbe17
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@
public class AccountValidator {
private static final Logger LOG = LoggerFactory.getLogger(AccountValidator.class);

private static final Pattern ILLEGAL_ACCOUNT_NAME_CHARACTERS = Pattern.compile("[^a-zA-Z0-9]");
private static final Pattern VALID_ACCOUNT_NAME = Pattern.compile("^[a-zA-Z0-9]*");
private static final Pattern ILLEGAL_ACCOUNT_NAME_CHARACTERS = Pattern.compile("[^a-zA-Z0-9-]" + // all character except allowed
"|(?<=-)-+" + // non single hyphens
"|^-+|" + // hyphens at the beginning
"-+$"); // hyphens at the end
private static final Pattern VALID_ACCOUNT_NAME = Pattern.compile("^(?:[a-zA-Z0-9]-?)*[a-zA-Z0-9]+");

private final AccountManager accountManager;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class AccountValidatorTest {
@InjectMocks
private AccountValidator accountValidator;

@Test(dataProvider = "normalizeNames")
@Test(dataProvider = "namesToNormalize")
public void testNormalizeAccountName(String input, String expected) throws Exception {
doThrow(NotFoundException.class).when(accountManager).getByName(anyString());

Expand All @@ -53,40 +53,48 @@ public void testNormalizeAccountNameWhenInputDoesNotContainAnyValidCharacter() t
Assert.assertTrue(accountValidator.normalizeAccountName("#", "name").startsWith("name"));
}

@Test(dataProvider = "validNames")
@Test(dataProvider = "namesToValidate")
public void testValidUserName(String input, boolean expected) throws Exception {
doThrow(NotFoundException.class).when(accountManager).getByName(anyString());

Assert.assertEquals(accountValidator.isValidName(input), expected);
}

@DataProvider(name = "normalizeNames")
public Object[][] normalizeNames() {
@DataProvider
public Object[][] namesToNormalize() {
return new Object[][] {{"test", "test"},
{"test123", "test123"},
{"test 123", "test123"},
{"test@gmail.com", "testgmailcom"},
{"TEST", "TEST"},
{"test-", "test"},
{"te-st", "test"},
{"-test", "test"},
{"te_st", "test"},
{"te#st", "test"}
{"te#st", "test"},
{"-test", "test"},
{"test-", "test"},
{"--test--", "test"},
{"t-----e--s-t", "t-e-s-t"}
};
}

@DataProvider(name = "validNames")
public Object[][] validNames() {
@DataProvider
public Object[][] namesToValidate() {
return new Object[][] {{"test", true},
{"t-e-s-t", true},
{"test123", true},
{"TEST", true},
{"te-st", true},
{"test 123", false},
{"test@gmail.com", false},
{"TEST", true},
{"test-", false},
{"te-st", false},
{"-test", false},
{"te_st", false},
{"te#st", false}
{"te#st", false},
{"-test", false},
{"test-", false},
{"--test--", false},
{"te--st", false}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void checkUser(User user) throws BadRequestException {
throw new BadRequestException("User name required");
}
if (!isValidName(user.getName())) {
throw new BadRequestException("Username must contain only letters and digits");
throw new BadRequestException("Username may only contain alphanumeric characters or single hyphens inside");
}
if (isNullOrEmpty(user.getEmail())) {
throw new BadRequestException("User email required");
Expand Down

0 comments on commit d7cbe17

Please sign in to comment.