Spring 게시판 10 - 회원 Validate

춤인형의 개발일지·2025년 1월 17일

Spring실습

목록 보기
11/40

validate 설정

1. 프로필만 보는 Request

  • UserName은 최소 1과 최대 10 사이에 넣어주어야 한다.
  • userName은 절대 빈칸이면 안된다.
public record UserProfileRequest(
        Long id,
        @Size(min = 1, max = 10)
        @NotBlank
        String userName
) {
}

사용자 정보 다 보는 Request

  • UserName(사용자 이름)
    • 최소 1과 최대 10 사이에 넣어주어야 한다.
    • 절대 빈칸이면 안된다.
  • userInfoId(사용자 ID)
    • 최소 1과 최대 10 사이에 넣어주어야 한다.
    • 절대 빈칸이면 안된다.
  • password(비밀번호)
    • 최소 8 이상 (나는 최대값을 안넣어줬는데, 보통 넣어준다 한다)
    • 빈칸이면 안된다.
    • 소문자, 대문자, 특수문자
  • Email
    • 이메일값
public record UserRequest(
        @Size(min = 1, max = 10)
        @NotBlank
        String userName,

        @Size(min = 1, max = 10)
        @NotBlank
        String userInfoId,

        @Size(min = 8)
        @NotBlank
        @Pattern(regexp = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,}$")
        String userPassword,

        @Email
        String userEmail
) {
}

제한을 걸어두고, Controller에 @Valid 설정을 해둔다.
이걸 테스트코드로 작성해준다.

 @Test
    void 사용자_생성_아이디제한() {
        RestAssured
                .given().log().all()
                .contentType(ContentType.JSON)
                .body(new UserRequest("추민영", "", "abcABC123!!!", "chu@naver.com"))
                .when()
                .post("/users/infos")
                .then().log().all()
                .statusCode(400);

        RestAssured
                .given().log().all()
                .contentType(ContentType.JSON)
                .body(new UserRequest("추민영", "1234567891011", "abcABC123!!!", "chu@naver.com"))
                .when()
                .post("/users/infos")
                .then().log().all()
                .statusCode(400);
    }

    @Test
    void 사용자_생성_비밀번호제한() {
        RestAssured
                .given().log().all()
                .contentType(ContentType.JSON)
                .body(new UserRequest("추민영", "chuchu", "abc", "chu@naver.com"))
                .when()
                .post("/users/infos")
                .then().log().all()
                .statusCode(400);

        RestAssured
                .given().log().all()
                .contentType(ContentType.JSON)
                .body(new UserRequest("추민영", "1234567891011", "abcABC", "chu@naver.com"))
                .when()
                .post("/users/infos")
                .then().log().all()
                .statusCode(400);
    }

    @Test
    void 사용자_생성_이메일제한() {
        RestAssured
                .given().log().all()
                .contentType(ContentType.JSON)
                .body(new UserRequest("추민영", "chuchu", "abcABC!!!", "chunaver.com"))
                .when()
                .post("/users/infos")
                .then().log().all()
                .statusCode(400);

    }

제한을 걸어둔게 제대로 실행하는지 테스트 해야한다. 그래야 실제로 동작할 때 제대로 돌아가는지 확인을 안해도 되기 때문이다.

0개의 댓글