-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ [STMT-146] 닉네임 유효성 검사 API 테스트 코드 추가 (#64)
- Loading branch information
Showing
4 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/test/java/com/stumeet/server/member/adapter/in/web/MemberValidApiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package com.stumeet.server.member.adapter.in.web; | ||
|
||
import com.stumeet.server.common.auth.model.AuthenticationHeader; | ||
import com.stumeet.server.member.adapter.out.persistence.JpaMemberRepository; | ||
import com.stumeet.server.member.adapter.out.persistence.MemberJpaEntity; | ||
import com.stumeet.server.stub.MemberStub; | ||
import com.stumeet.server.stub.TokenStub; | ||
import com.stumeet.server.template.ApiTest; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.test.context.support.WithMockUser; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName; | ||
import static org.springframework.restdocs.headers.HeaderDocumentation.requestHeaders; | ||
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; | ||
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get; | ||
import static org.springframework.restdocs.operation.preprocess.Preprocessors.*; | ||
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath; | ||
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields; | ||
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName; | ||
import static org.springframework.restdocs.request.RequestDocumentation.queryParameters; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
|
||
@Transactional | ||
class MemberValidApiTest extends ApiTest { | ||
|
||
@Autowired | ||
private JpaMemberRepository jpaMemberRepository; | ||
|
||
@Nested | ||
@DisplayName("닉네임 중복 검증") | ||
class IsDuplicateNickname { | ||
|
||
private final String path = "/api/v1/members/validate-nickname"; | ||
private MemberJpaEntity member; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
member = jpaMemberRepository.save(MemberStub.getMemberEntity()); | ||
} | ||
|
||
@Test | ||
@WithMockUser | ||
@DisplayName("[성공] 유효한 닉네임을 입력하면 중복 검증에 통과한다.") | ||
void successTest() throws Exception { | ||
String nickname = "닉네임"; | ||
mockMvc.perform(get(path) | ||
.header(AuthenticationHeader.ACCESS_TOKEN.getName(), TokenStub.getMockAccessToken()) | ||
.param("nickname", nickname)) | ||
.andExpect(status().isOk()) | ||
.andDo(document("validate_nickname/success", | ||
preprocessRequest(prettyPrint()), | ||
preprocessResponse(prettyPrint()), | ||
requestHeaders( | ||
headerWithName(AuthenticationHeader.ACCESS_TOKEN.getName()).description("서버로부터 전달받은 액세스 토큰") | ||
), | ||
queryParameters( | ||
parameterWithName("nickname").description("검증할 닉네임") | ||
), | ||
responseFields( | ||
fieldWithPath("code").description("응답에 대한 결과 코드"), | ||
fieldWithPath("message").description("응답에 대한 메시지") | ||
))); | ||
} | ||
|
||
@Test | ||
@WithMockUser | ||
@DisplayName("[실패] 유효하지 않은 닉네임을 입력하면 검증에 실패합니다.") | ||
void invalidRequestTest() throws Exception { | ||
String nickname = "닉"; | ||
mockMvc.perform(get(path) | ||
.header(AuthenticationHeader.ACCESS_TOKEN.getName(), TokenStub.getMockAccessToken()) | ||
.param("nickname", nickname)) | ||
.andExpect(status().isBadRequest()) | ||
.andDo(document("validate_nickname/fail/invalid", | ||
preprocessRequest(prettyPrint()), | ||
preprocessResponse(prettyPrint()), | ||
requestHeaders( | ||
headerWithName(AuthenticationHeader.ACCESS_TOKEN.getName()).description("서버로부터 전달받은 액세스 토큰") | ||
), | ||
queryParameters( | ||
parameterWithName("nickname").description("검증할 닉네임") | ||
), | ||
responseFields( | ||
fieldWithPath("code").description("응답에 대한 결과 코드"), | ||
fieldWithPath("message").description("응답에 대한 메시지") | ||
))); | ||
} | ||
|
||
@Test | ||
@WithMockUser | ||
@DisplayName("[실패] 닉네임이 중복되면 검증에 실패합니다.") | ||
void duplicateNicknameTest() throws Exception { | ||
String nickname = member.getName(); | ||
mockMvc.perform(get(path) | ||
.header(AuthenticationHeader.ACCESS_TOKEN.getName(), TokenStub.getMockAccessToken()) | ||
.param("nickname", nickname)) | ||
.andExpect(status().isBadRequest()) | ||
.andDo(document("validate_nickname/fail/duplicate", | ||
preprocessRequest(prettyPrint()), | ||
preprocessResponse(prettyPrint()), | ||
requestHeaders( | ||
headerWithName(AuthenticationHeader.ACCESS_TOKEN.getName()).description("서버로부터 전달받은 액세스 토큰") | ||
), | ||
queryParameters( | ||
parameterWithName("nickname").description("검증할 닉네임") | ||
), | ||
responseFields( | ||
fieldWithPath("code").description("응답에 대한 결과 코드"), | ||
fieldWithPath("message").description("응답에 대한 메시지") | ||
))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters