LDAPAuthService extends DefaultService implements AuthService { static boolean _isUsernameValid(String username) { Matcher matcher = Pattern.compile(EMAIL_REGEX).matcher(username); return matcher.matches(); } @Inject  LDAPAuthService(SystemConfiguration config, UserService userService, MonitorService monitorService); @Override PrincipalUser getUser(String username, String password); @Override Properties getServiceProperties();  }
@Test public void testLDAPValidUsername() { String[] usernames = { "f", "fubar", "fubar@myCompany", "fu_bar+100", "fu_bar+100-1", "fu_bar+100-1@myCompany", "fu_bar+100-1@myCompany.com", "fubar@myCompany.com" }; Set<String> failures = new TreeSet<>(); for (String username : usernames) { if (!LDAPAuthService._isUsernameValid(username)) { failures.add(username); } } assertTrue(failures.toString(), failures.isEmpty()); }
@Test public void testLDAPInvalidUsername() { String[] usernames = { "*", "fu*", "fu*r", "*@myCompany", "fu*@myCompany", "fu*r@myCompany", "*@myCompany.com", "fu*@myCompany.com", "fu*r@myCompany.com", "*@.", "*@.myCompany", "fu*@.myCompany", "fu*r@.myCompany", "*@.myCompany.com", "fu*@.myCompany.com", "fu*r@.myCompany.com", "fubar.@myCompany.com", "*@.myCompany.", "fu*@.myCompany.", "fu*r@.myCompany.", "*@.myCompany.com.", "fu*@.myCompany.com.", "fu*r@.myCompany.com.", "fubar.@myCompany.com.", }; Set<String> failures = new TreeSet<>(); for (String username : usernames) { if (LDAPAuthService._isUsernameValid(username)) { failures.add(username); } } assertTrue(failures.toString(), failures.isEmpty()); }