Search in sources :

Example 1 with ICaptchaService

use of org.neusoft.neubbs.service.ICaptchaService in project neubbs by nuitcoder.

the class AccountControllerTest method testValidateCaptchaSuccess.

/**
 * 测试 /api/account/check-captcha
 *      - 检查用户输入验证码匹配成功
 *          - 设置 session 属性,再传入用户输入验证码
 */
@Test
public void testValidateCaptchaSuccess() throws Exception {
    // get captcha service bean object
    ICaptchaService captchaService = (ICaptchaService) this.webApplicationContext.getBean("captchaServiceImpl");
    String captcha = captchaService.getCaptchaText();
    mockMvc.perform(MockMvcRequestBuilders.get("/api/account/validate-captcha").sessionAttr(SetConst.SESSION_CAPTCHA, captcha).param(ParamConst.CAPTCHA, captcha)).andExpect(MockMvcResultMatchers.jsonPath("$.success").value(true)).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("")).andExpect(MockMvcResultMatchers.jsonPath("$.model").exists());
    util.printSuccessMessage();
}
Also used : ICaptchaService(org.neusoft.neubbs.service.ICaptchaService) Test(org.junit.Test)

Example 2 with ICaptchaService

use of org.neusoft.neubbs.service.ICaptchaService in project neubbs by nuitcoder.

the class AccountControllerTest method testValidateCaptchaException.

/**
 * 测试 /api/account/check-captcha
 *      - 检查用户输入验证码匹配异常
 *          - request param error
 *              - [✔] null
 *              - [✔] format not norm
 *          - service exception
 *              - [✔] not pass '/api/account/captcha' api to generate captcha picture, session not exist captcha text
 */
@Test
public void testValidateCaptchaException() throws Exception {
    // build captcha
    ICaptchaService captchaService = (ICaptchaService) webApplicationContext.getBean("captchaServiceImpl");
    String captcha = captchaService.getCaptchaText();
    // 1. request param error and 2. input captcha error
    String[] params = { null, "123", "abc", "ABC", "123abcABC", "0*=123", "55555" };
    for (String param : params) {
        System.out.println("input captcha = " + param);
        try {
            mockMvc.perform(MockMvcRequestBuilders.get("/api/account/validate-captcha").sessionAttr(SetConst.SESSION_CAPTCHA, captcha).param(ParamConst.CAPTCHA, param)).andExpect(MockMvcResultMatchers.jsonPath("$.success").value(false)).andExpect(MockMvcResultMatchers.jsonPath("$.message").value(ApiMessage.CAPTCHA_INCORRECT)).andExpect(MockMvcResultMatchers.jsonPath("$.model").value(CoreMatchers.notNullValue()));
        } catch (NestedServletException ne) {
            Assert.assertThat(ne.getRootCause(), CoreMatchers.anyOf(CoreMatchers.instanceOf(ParamsErrorException.class), CoreMatchers.instanceOf(ServiceException.class)));
        }
    }
    // not pass '/api/account/validate' api to generate captcha picture, session not exist captcha text
    try {
        mockMvc.perform(MockMvcRequestBuilders.get("/api/account/validate-captcha").param(ParamConst.CAPTCHA, captcha)).andExpect(MockMvcResultMatchers.jsonPath("$.success").value(false)).andExpect(MockMvcResultMatchers.jsonPath("$.success").value(ApiMessage.CAPTCHA_INCORRECT)).andExpect(MockMvcResultMatchers.jsonPath("$.model").value(CoreMatchers.notNullValue()));
    } catch (NestedServletException ne) {
        Assert.assertSame(ServiceException.class, ne.getRootCause().getClass());
        Assert.assertEquals(ApiMessage.NO_GENERATE_CAPTCHA, ne.getRootCause().getMessage());
    }
    util.printSuccessMessage();
}
Also used : ParamsErrorException(org.neusoft.neubbs.exception.ParamsErrorException) NestedServletException(org.springframework.web.util.NestedServletException) ServiceException(org.neusoft.neubbs.exception.ServiceException) ICaptchaService(org.neusoft.neubbs.service.ICaptchaService) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 ICaptchaService (org.neusoft.neubbs.service.ICaptchaService)2 ParamsErrorException (org.neusoft.neubbs.exception.ParamsErrorException)1 ServiceException (org.neusoft.neubbs.exception.ServiceException)1 NestedServletException (org.springframework.web.util.NestedServletException)1