Search in sources :

Example 1 with PortalConfig

use of com.ctrip.framework.apollo.portal.component.config.PortalConfig in project apollo by ctripcorp.

the class AuthUserPasswordCheckerTest method testIsWeakPassword2.

@Test
public void testIsWeakPassword2() {
    // use custom
    PortalConfig mock = Mockito.mock(PortalConfig.class);
    Mockito.when(mock.getUserPasswordNotAllowList()).thenReturn(Arrays.asList("1111", "2222"));
    AuthUserPasswordChecker checker = new AuthUserPasswordChecker(mock);
    Map<String, Boolean> cases = new HashMap<>();
    cases.put("a1234567", true);
    cases.put("b98765432", true);
    cases.put("c11111111", false);
    cases.put("d2222222", false);
    cases.put("e3333333", true);
    String exceptedErrMsg = "Passwords cannot be consecutive, regular letters or numbers. And cannot be commonly used.";
    for (Entry<String, Boolean> c : cases.entrySet()) {
        CheckResult res = checker.checkWeakPassword(c.getKey());
        Assert.assertEquals(res.isSuccess(), c.getValue());
        if (!c.getValue()) {
            Assert.assertTrue(res.getMessage().startsWith(exceptedErrMsg));
        }
    }
}
Also used : PortalConfig(com.ctrip.framework.apollo.portal.component.config.PortalConfig) HashMap(java.util.HashMap) CheckResult(com.ctrip.framework.apollo.portal.util.checker.CheckResult) AuthUserPasswordChecker(com.ctrip.framework.apollo.portal.util.checker.AuthUserPasswordChecker) Test(org.junit.Test)

Example 2 with PortalConfig

use of com.ctrip.framework.apollo.portal.component.config.PortalConfig in project apollo by ctripcorp.

the class AuthUserPasswordCheckerTest method testIsWeakPassword3.

@Test
public void testIsWeakPassword3() {
    // no limit
    PortalConfig mock = Mockito.mock(PortalConfig.class);
    Mockito.when(mock.getUserPasswordNotAllowList()).thenReturn(Collections.emptyList());
    AuthUserPasswordChecker checker = new AuthUserPasswordChecker(mock);
    Map<String, Boolean> cases = new HashMap<>();
    cases.put("a1234567", true);
    cases.put("b98765432", true);
    cases.put("c11111111", true);
    cases.put("d2222222", true);
    cases.put("e3333333", true);
    for (Entry<String, Boolean> c : cases.entrySet()) {
        CheckResult res = checker.checkWeakPassword(c.getKey());
        Assert.assertEquals(res.isSuccess(), c.getValue());
    }
    Mockito.when(mock.getUserPasswordNotAllowList()).thenReturn(null);
    checker = new AuthUserPasswordChecker(mock);
    for (Entry<String, Boolean> c : cases.entrySet()) {
        CheckResult res = checker.checkWeakPassword(c.getKey());
        Assert.assertEquals(res.isSuccess(), c.getValue());
    }
}
Also used : PortalConfig(com.ctrip.framework.apollo.portal.component.config.PortalConfig) HashMap(java.util.HashMap) CheckResult(com.ctrip.framework.apollo.portal.util.checker.CheckResult) AuthUserPasswordChecker(com.ctrip.framework.apollo.portal.util.checker.AuthUserPasswordChecker) Test(org.junit.Test)

Example 3 with PortalConfig

use of com.ctrip.framework.apollo.portal.component.config.PortalConfig in project apollo by ctripcorp.

the class AuthUserPasswordCheckerTest method testIsWeakPassword.

@Test
public void testIsWeakPassword() {
    // use default
    PortalDBPropertySource propertySource = Mockito.mock(PortalDBPropertySource.class);
    PortalConfig mock = new PortalConfig(propertySource);
    AuthUserPasswordChecker checker = new AuthUserPasswordChecker(mock);
    Map<String, Boolean> cases = new HashMap<>();
    cases.put("a1234567", false);
    cases.put("b98765432", false);
    cases.put("c11111111", false);
    cases.put("d2222222", false);
    cases.put("e3333333", false);
    cases.put("f4444444", false);
    cases.put("g5555555", false);
    cases.put("h6666666", false);
    cases.put("i7777777", false);
    cases.put("j8888888", false);
    cases.put("k9999999", false);
    cases.put("l0000000", false);
    cases.put("1q2w3e4r", false);
    cases.put("qwertyuiop1", false);
    cases.put("asdfghjkl2", false);
    cases.put("asdfghjkl3", false);
    cases.put("abcd1234", false);
    cases.put("1s39gvisk", true);
    String exceptedErrMsg = "Passwords cannot be consecutive, regular letters or numbers. And cannot be commonly used.";
    for (Entry<String, Boolean> c : cases.entrySet()) {
        CheckResult res = checker.checkWeakPassword(c.getKey());
        Assert.assertEquals(res.isSuccess(), c.getValue());
        if (!c.getValue()) {
            Assert.assertTrue(res.getMessage().startsWith(exceptedErrMsg));
        }
    }
}
Also used : PortalConfig(com.ctrip.framework.apollo.portal.component.config.PortalConfig) HashMap(java.util.HashMap) CheckResult(com.ctrip.framework.apollo.portal.util.checker.CheckResult) PortalDBPropertySource(com.ctrip.framework.apollo.portal.service.PortalDBPropertySource) AuthUserPasswordChecker(com.ctrip.framework.apollo.portal.util.checker.AuthUserPasswordChecker) Test(org.junit.Test)

Example 4 with PortalConfig

use of com.ctrip.framework.apollo.portal.component.config.PortalConfig in project apollo by ctripcorp.

the class AuthUserPasswordCheckerTest method testRegexMatch.

@Test
public void testRegexMatch() {
    PortalConfig mock = Mockito.mock(PortalConfig.class);
    AuthUserPasswordChecker checker = new AuthUserPasswordChecker(mock);
    List<String> unMatchList = Arrays.asList("11111111", "oibjdiel", "oso87b6", "0vb9xibowkd8bz9dsxbef", "", null);
    String exceptedErrMsg = "Password needs a number and letter and between 8~20 characters";
    for (String p : unMatchList) {
        CheckResult res = checker.checkWeakPassword(p);
        Assert.assertFalse(res.isSuccess());
        Assert.assertEquals(exceptedErrMsg, res.getMessage());
    }
    List<String> matchList = Arrays.asList("pziv0g87", "8f7zjpf8sci93", "Upz4jF8u2yjV3wn8zp6c");
    for (String p : matchList) {
        CheckResult res = checker.checkWeakPassword(p);
        Assert.assertTrue(res.isSuccess());
    }
}
Also used : PortalConfig(com.ctrip.framework.apollo.portal.component.config.PortalConfig) CheckResult(com.ctrip.framework.apollo.portal.util.checker.CheckResult) AuthUserPasswordChecker(com.ctrip.framework.apollo.portal.util.checker.AuthUserPasswordChecker) Test(org.junit.Test)

Aggregations

PortalConfig (com.ctrip.framework.apollo.portal.component.config.PortalConfig)4 AuthUserPasswordChecker (com.ctrip.framework.apollo.portal.util.checker.AuthUserPasswordChecker)4 CheckResult (com.ctrip.framework.apollo.portal.util.checker.CheckResult)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 PortalDBPropertySource (com.ctrip.framework.apollo.portal.service.PortalDBPropertySource)1