org.springframework.restdocs.constraints.ValidatorConstraintResolver Java Examples

The following examples show how to use org.springframework.restdocs.constraints.ValidatorConstraintResolver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UserMvcTests.java    From jakduk-api with MIT License 5 votes vote down vote up
@Test
@WithAnonymousUser
public void findPasswordTest() throws Exception {

    Map<String, Object> form = new HashMap<String, Object>() {{
        put("email", jakdukUser.getEmail());
        put("callbackUrl", "http://dev-wev.jakduk/find/password");
    }};

    UserPasswordFindResponse expectResponse = new UserPasswordFindResponse(form.get("email").toString(),
            JakdukUtils.getMessageSource("user.msg.reset.password.send.email"));

    when(userService.sendEmailToResetPassword(anyString(), anyString()))
            .thenReturn(expectResponse);

    ConstraintDescriptions userConstraints = new ConstraintDescriptions(UserPasswordFindForm.class, new ValidatorConstraintResolver(),
            new ResourceBundleConstraintDescriptionResolver(ResourceBundle.getBundle("ValidationMessages")));

    mvc.perform(
            post("/api/user/password/find")
                    .contentType(MediaType.APPLICATION_JSON)
                    .with(csrf())
                    .content(ObjectMapperUtils.writeValueAsString(form)))
            .andExpect(status().isOk())
            .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
            .andExpect(content().json(ObjectMapperUtils.writeValueAsString(expectResponse)))
            .andDo(
                    document("user-find-password",
                            requestFields(
                                    fieldWithPath("email").type(JsonFieldType.STRING).description("이메일 주소. " +
                                            userConstraints.descriptionsForProperty("email")),
                                    fieldWithPath("callbackUrl").type(JsonFieldType.STRING).description("콜백 받을 URL. " +
                                            userConstraints.descriptionsForProperty("callbackUrl"))
                            ),
                            responseFields(this.getPasswordFindDescriptor())
                    ));
}
 
Example #2
Source File: UserMvcTests.java    From jakduk-api with MIT License 5 votes vote down vote up
@Test
@WithMockUser
public void resetPasswordTest() throws Exception {

    Map<String, Object> form = new HashMap<String, Object>() {{
        put("code", "16948f83-1af8-4736-9e12-cf57f03a981c");
        put("password", "1112");
    }};

    UserPasswordFindResponse expectResponse = new UserPasswordFindResponse(jakdukUser.getEmail(),
            JakdukUtils.getMessageSource("user.msg.success.change.password"));

    when(userService.resetPasswordWithToken(anyString(), anyString()))
            .thenReturn(expectResponse);

    ConstraintDescriptions userConstraints = new ConstraintDescriptions(UserPasswordResetForm.class, new ValidatorConstraintResolver(),
            new ResourceBundleConstraintDescriptionResolver(ResourceBundle.getBundle("ValidationMessages")));

    mvc.perform(
            post("/api/user/password/reset")
                    .contentType(MediaType.APPLICATION_JSON)
                    .with(csrf())
                    .content(ObjectMapperUtils.writeValueAsString(form)))
            .andExpect(status().isOk())
            .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
            .andExpect(content().json(ObjectMapperUtils.writeValueAsString(expectResponse)))
            .andDo(
                    document("user-reset-password",
                            requestFields(
                                    fieldWithPath("code").type(JsonFieldType.STRING).description("임시 발행한 토큰 코드. (5분간 유지) " +
                                            userConstraints.descriptionsForProperty("code")),
                                    fieldWithPath("password").type(JsonFieldType.STRING).description("바꿀 비밀번호 " +
                                            userConstraints.descriptionsForProperty("password"))
                            ),
                            responseFields(this.getPasswordFindDescriptor())
                    ));
}
 
Example #3
Source File: UserMvcTests.java    From jakduk-api with MIT License 4 votes vote down vote up
@Test
@WithAnonymousUser
public void createJakdukUserTest() throws Exception {

    this.whenCustomValdation();

    UserForm form = new UserForm();
    form.setEmail(jakdukUser.getEmail());
    form.setUsername(jakdukUser.getUsername());
    form.setPassword("1111");
    form.setPasswordConfirm("1111");
    form.setAbout(jakdukUser.getAbout());
    form.setFootballClub(footballClub.getId());
    form.setUserPictureId(userPicture.getId());

    when(userService.createJakdukUser(anyString(), anyString(), anyString(), anyString(), anyString(), anyString()))
            .thenReturn(jakdukUser);

    ConstraintDescriptions userConstraints = new ConstraintDescriptions(UserForm.class, new ValidatorConstraintResolver(),
            new ResourceBundleConstraintDescriptionResolver(ResourceBundle.getBundle("ValidationMessages")));

    mvc.perform(
            post("/api/user")
                    .contentType(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON)
                    .with(csrf())
                    .content(ObjectMapperUtils.writeValueAsString(form)))
            .andExpect(status().isOk())
            .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
            .andExpect(content().json(ObjectMapperUtils.writeValueAsString(EmptyJsonResponse.newInstance())))
            .andDo(
                    document("create-jakduk-user",
                            requestFields(
                                    fieldWithPath("email").type(JsonFieldType.STRING).description("이메일 주소. " +
                                            userConstraints.descriptionsForProperty("email")),
                                    fieldWithPath("username").type(JsonFieldType.STRING).description("별명. " +
                                            userConstraints.descriptionsForProperty("username")),
                                    fieldWithPath("password").type(JsonFieldType.STRING).description("비밀번호. " +
                                            userConstraints.descriptionsForProperty("password")),
                                    fieldWithPath("passwordConfirm").type(JsonFieldType.STRING).description("확인 비밀번호. " +
                                            userConstraints.descriptionsForProperty("passwordConfirm")),
                                    fieldWithPath("footballClub").type(JsonFieldType.STRING).description("(optional) 축구단 ID"),
                                    fieldWithPath("about").type(JsonFieldType.STRING).description("(optional) 자기 소개"),
                                    fieldWithPath("userPictureId").type(JsonFieldType.STRING).description("(optional) 프로필 사진 ID")
                            ),
                            responseHeaders(
                                    headerWithName("Set-Cookie").description("인증 쿠키. value는 JSESSIONID=키값").optional()
                            )
                    ));
}
 
Example #4
Source File: UserMvcTests.java    From jakduk-api with MIT License 4 votes vote down vote up
@Test
@WithMockUser
public void createSocialUserTest() throws Exception {

    this.whenCustomValdation();

    AttemptSocialUser attemptSocialUser = new AttemptSocialUser();
    attemptSocialUser.setUsername("daumUser01");
    attemptSocialUser.setProviderId(Constants.ACCOUNT_TYPE.FACEBOOK);
    attemptSocialUser.setProviderUserId("abc123");
    attemptSocialUser.setExternalSmallPictureUrl("https://img1.daumcdn.net/thumb/R55x55/?fname=http%3A%2F%2Ftwg.tset.daumcdn.net%2Fprofile%2F6enovyMT1pI0&t=1507478752861");
    attemptSocialUser.setExternalLargePictureUrl("https://img1.daumcdn.net/thumb/R158x158/?fname=http%3A%2F%2Ftwg.tset.daumcdn.net%2Fprofile%2F6enovyMT1pI0&t=1507478752861");

    MockHttpSession mockHttpSession = new MockHttpSession();
    mockHttpSession.setAttribute(Constants.PROVIDER_SIGNIN_ATTEMPT_SESSION_ATTRIBUTE, attemptSocialUser);

    SocialUserForm form = new SocialUserForm();
    form.setEmail("[email protected]");
    form.setUsername("SocialUser");
    form.setAbout("안녕하세요.");
    form.setFootballClub(footballClub.getId());
    form.setUserPictureId(userPicture.getId());
    form.setExternalLargePictureUrl("https://img1.daumcdn.net/thumb/R158x158/?fname=http%3A%2F%2Ftwg.tset.daumcdn.net%2Fprofile%2FSjuNejHmr8o0&t=1488000722876");

    User expectUser = new User();
    expectUser.setId("597df86caaf4fc0545d4f3e9");
    expectUser.setEmail(form.getEmail());
    expectUser.setUsername(form.getUsername());
    expectUser.setPassword("841db2bc28e4730906bd82d79e69c80633747570d96ffade7dd77f58270f31a222e129e005cb70d2");
    expectUser.setProviderId(attemptSocialUser.getProviderId());
    expectUser.setProviderUserId(attemptSocialUser.getProviderUserId());
    expectUser.setAbout(form.getAbout());
    expectUser.setRoles(Arrays.asList(JakdukAuthority.ROLE_USER_02.getCode()));
    expectUser.setSupportFC(footballClub);
    expectUser.setLastLogged(LocalDateTime.now());

    when(userService.createSocialUser(anyString(), anyString(), any(Constants.ACCOUNT_TYPE.class), anyString(),
            anyString(), anyString(), anyString(), anyString()))
            .thenReturn(expectUser);

    ConstraintDescriptions userConstraints = new ConstraintDescriptions(SocialUserForm.class, new ValidatorConstraintResolver(),
            new ResourceBundleConstraintDescriptionResolver(ResourceBundle.getBundle("ValidationMessages")));

    mvc.perform(
            post("/api/user/social")
                    .session(mockHttpSession)
                    .contentType(MediaType.APPLICATION_JSON)
                    .with(csrf())
                    .content(ObjectMapperUtils.writeValueAsString(form)))
            .andExpect(status().isOk())
            .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
            .andExpect(content().json(ObjectMapperUtils.writeValueAsString(EmptyJsonResponse.newInstance())))
            .andDo(
                    document("create-sns-user",
                            requestFields(
                                    fieldWithPath("email").type(JsonFieldType.STRING).description("이메일 주소. " +
                                            userConstraints.descriptionsForProperty("email")),
                                    fieldWithPath("username").type(JsonFieldType.STRING).description("별명. " +
                                            userConstraints.descriptionsForProperty("username")),
                                    fieldWithPath("footballClub").type(JsonFieldType.STRING).description("(optional) 축구단 ID"),
                                    fieldWithPath("about").type(JsonFieldType.STRING).description("(optional) 자기 소개"),
                                    fieldWithPath("userPictureId").type(JsonFieldType.STRING).description("(optional) 프로필 사진 ID"),
                                    fieldWithPath("externalLargePictureUrl").type(JsonFieldType.STRING)
                                            .description("(optional) SNS계정에서 제공하는 회원 큰 사진 URL. userPictureId가 null 이어야 한다.")
                            ),
                            responseHeaders(
                                    headerWithName("Set-Cookie").description("인증 쿠키. value는 JSESSIONID=키값").optional()
                            )
                    ));
}
 
Example #5
Source File: UserMvcTests.java    From jakduk-api with MIT License 4 votes vote down vote up
@Test
@WithMockJakdukUser
public void editProfileMeTest() throws Exception {

    when(userProfileRepository.findByNEIdAndEmail(anyString(), anyString()))
            .thenReturn(Optional.empty());

    when(userProfileRepository.findByNEIdAndUsername(anyString(), anyString()))
            .thenReturn(Optional.empty());

    Map<String, Object> form = new HashMap<String, Object>() {{
        put("email", jakdukUser.getEmail());
        put("username", jakdukUser.getUsername());
        put("about", jakdukUser.getAbout());
        put("footballClub", footballClub.getId());
        put("userPictureId", userPicture.getId());
    }};

    when(userService.editUserProfile(anyString(), anyString(), anyString(), anyString(), anyString(), anyString()))
            .thenReturn(jakdukUser);

    ConstraintDescriptions userConstraints = new ConstraintDescriptions(UserProfileEditForm.class, new ValidatorConstraintResolver(),
            new ResourceBundleConstraintDescriptionResolver(ResourceBundle.getBundle("ValidationMessages")));

    mvc.perform(
            put("/api/user/profile/me")
                    .cookie(new Cookie("JSESSIONID", "3F0E029648484BEAEF6B5C3578164E99"))
                    .contentType(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON)
                    .with(csrf())
                    .content(ObjectMapperUtils.writeValueAsString(form)))
            .andExpect(status().isOk())
            .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
            .andExpect(content().json(ObjectMapperUtils.writeValueAsString(EmptyJsonResponse.newInstance())))
            .andDo(
                    document("user-edit-profile-me",
                            requestHeaders(
                                    headerWithName("Cookie").optional().description("인증 쿠키. value는 JSESSIONID=키값")
                            ),
                            requestFields(
                                    fieldWithPath("email").type(JsonFieldType.STRING).description("이메일 주소. " +
                                            userConstraints.descriptionsForProperty("email")),
                                    fieldWithPath("username").type(JsonFieldType.STRING).description("별명. " +
                                            userConstraints.descriptionsForProperty("username")),
                                    fieldWithPath("footballClub").type(JsonFieldType.STRING).description("(optional) 축구단 ID"),
                                    fieldWithPath("about").type(JsonFieldType.STRING).description("(optional) 자기 소개"),
                                    fieldWithPath("userPictureId").type(JsonFieldType.STRING).description("(optional) 프로필 사진 ID")
                            )
                    ));
}
 
Example #6
Source File: UserMvcTests.java    From jakduk-api with MIT License 4 votes vote down vote up
@Test
@WithMockJakdukUser
public void editPasswordTest() throws Exception {

    when(userService.findUserOnPasswordUpdateById(anyString()))
            .thenReturn(new UserOnPasswordUpdate(jakdukUser.getId(), jakdukUser.getPassword()));

    Map<String, Object> form = new HashMap<String, Object>() {{
        put("password", "1111");
        put("newPassword", "1112");
        put("newPasswordConfirm", "1112");
    }};

    doNothing().when(userService)
            .updateUserPassword(anyString(), anyString());

    ConstraintDescriptions userConstraints = new ConstraintDescriptions(UserPasswordForm.class, new ValidatorConstraintResolver(),
            new ResourceBundleConstraintDescriptionResolver(ResourceBundle.getBundle("ValidationMessages")));

    mvc.perform(
            put("/api/user/password")
                    .cookie(new Cookie("JSESSIONID", "3F0E029648484BEAEF6B5C3578164E99"))
                    .contentType(MediaType.APPLICATION_JSON)
                    .accept(MediaType.APPLICATION_JSON)
                    .with(csrf())
                    .content(ObjectMapperUtils.writeValueAsString(form)))
            .andExpect(status().isOk())
            .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
            .andExpect(content().json(ObjectMapperUtils.writeValueAsString(EmptyJsonResponse.newInstance())))
            .andDo(
                    document("user-edit-password",
                            requestHeaders(
                                    headerWithName("Cookie").optional().description("인증 쿠키. value는 JSESSIONID=키값")
                            ),
                            requestFields(
                                    fieldWithPath("password").type(JsonFieldType.STRING).description("현재 비밀번호. " +
                                            userConstraints.descriptionsForProperty("password")),
                                    fieldWithPath("newPassword").type(JsonFieldType.STRING).description("새 비밀번호. " +
                                            userConstraints.descriptionsForProperty("newPassword")),
                                    fieldWithPath("newPasswordConfirm").type(JsonFieldType.STRING).description("확인 새 비밀번호. " +
                                            userConstraints.descriptionsForProperty("newPasswordConfirm"))
                            )
                    ));
}