diff --git a/src/test/java/com/server/bbo_gak/domain/auth/controller/AuthControllerTest.java b/src/test/java/com/server/bbo_gak/domain/auth/controller/AuthControllerTest.java index 0c28d78..6a6326b 100644 --- a/src/test/java/com/server/bbo_gak/domain/auth/controller/AuthControllerTest.java +++ b/src/test/java/com/server/bbo_gak/domain/auth/controller/AuthControllerTest.java @@ -1,11 +1,11 @@ package com.server.bbo_gak.domain.auth.controller; -import static com.server.bbo_gak.global.security.jwt.service.JwtTokenService.TOKEN_ROLE_NAME; -import static org.junit.jupiter.api.Assertions.assertTrue; import static com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper.document; import static com.epages.restdocs.apispec.ResourceDocumentation.parameterWithName; import static com.epages.restdocs.apispec.ResourceDocumentation.resource; +import static com.server.bbo_gak.global.security.jwt.service.JwtTokenService.TOKEN_ROLE_NAME; import static org.hamcrest.Matchers.matchesPattern; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.when; import static org.springframework.restdocs.headers.HeaderDocumentation.headerWithName; import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post; @@ -20,10 +20,10 @@ import com.epages.restdocs.apispec.ResourceSnippetParameters; import com.server.bbo_gak.domain.auth.dto.request.LoginRequest; +import com.server.bbo_gak.domain.auth.dto.request.RefreshTokenRequest; import com.server.bbo_gak.domain.auth.dto.response.oauth.OauthUserInfoResponse; import com.server.bbo_gak.domain.auth.service.oauth.GoogleService; import com.server.bbo_gak.domain.user.entity.OauthProvider; -import com.server.bbo_gak.domain.auth.dto.request.RefreshTokenRequest; import com.server.bbo_gak.domain.user.entity.UserRole; import com.server.bbo_gak.global.AbstractRestDocsTests; import com.server.bbo_gak.global.RestDocsFactory; @@ -42,8 +42,8 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; -import org.springframework.test.annotation.Rollback; import org.springframework.http.MediaType; +import org.springframework.test.annotation.Rollback; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.jdbc.Sql; import org.springframework.transaction.annotation.Transactional; @@ -89,6 +89,7 @@ void setUp() { .signWith(Keys.hmacShaKeyFor(jwtRTSecret.getBytes()), SignatureAlgorithm.HS256) .setIssuer(issuer) .compact(); + } @Nested @@ -101,53 +102,53 @@ class 소셜_로그인 { OauthProvider provider = OauthProvider.GOOGLE; //구글 서비스 모킹 OauthUserInfoResponse mockOauthUserInfoResponse = OauthUserInfoResponse.builder() - .oauthId("oauthId") - .email("email") - .name("name") - .provider(provider) - .build(); + .oauthId("oauthId") + .email("email") + .name("name") + .provider(provider) + .build(); when(googleService.getOauthUserInfo(socialAccessToken)).thenReturn(mockOauthUserInfoResponse); //when mockMvc.perform(post("/api/v1/users/social-login") - .header("SOCIAL-AUTH-TOKEN", socialAccessToken) - .queryParam("provider", provider.name()) - .contentType(MediaType.APPLICATION_JSON) - .accept(MediaType.APPLICATION_JSON)) - - //then - // Verifying HTTP request matching - .andExpect(status().isOk()) - // 헤더 검증 - requestBody의 content-type - .andExpect( - header().string(HttpHeaders.CONTENT_TYPE, matchesPattern("application/json(;charset=UTF-8)?"))) - // Verifying output serialization - .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) - .andExpect(jsonPath("$.accessToken").isNotEmpty()) // accessToken 존재 검증 - .andExpect(jsonPath("$.refreshToken").isNotEmpty()) // refreshToken 존재 검증 - - // RestDocs 문서 - .andDo(document("[소셜 로그인] 성공", - preprocessRequest(prettyPrint()), - preprocessResponse(prettyPrint()), - resource( - ResourceSnippetParameters.builder() - .tags("auth") // API 문서의 태그 - .description("소셜로그인 - jwt 토큰 생성") // API 문서의 설명 - .requestHeaders( - headerWithName("SOCIAL-AUTH-TOKEN").description( - "소셜 엑세스 토큰") - ) - .queryParameters( - parameterWithName("provider").description("OAuth provider(ex) GOOGLE)") - ) - .responseFields( // 응답 필드 - fieldWithPath("accessToken").description("accessToken"), - fieldWithPath("refreshToken").description("refreshToken"), - fieldWithPath("isFirstLogin").description("isFirstLogin") - ) - .build() - ))); + .header("SOCIAL-AUTH-TOKEN", socialAccessToken) + .queryParam("provider", provider.name()) + .contentType(MediaType.APPLICATION_JSON) + .accept(MediaType.APPLICATION_JSON)) + + //then + // Verifying HTTP request matching + .andExpect(status().isOk()) + // 헤더 검증 - requestBody의 content-type + .andExpect( + header().string(HttpHeaders.CONTENT_TYPE, matchesPattern("application/json(;charset=UTF-8)?"))) + // Verifying output serialization + .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.accessToken").isNotEmpty()) // accessToken 존재 검증 + .andExpect(jsonPath("$.refreshToken").isNotEmpty()) // refreshToken 존재 검증 + + // RestDocs 문서 + .andDo(document("[소셜 로그인] 성공", + preprocessRequest(prettyPrint()), + preprocessResponse(prettyPrint()), + resource( + ResourceSnippetParameters.builder() + .tags("auth") // API 문서의 태그 + .description("소셜로그인 - jwt 토큰 생성") // API 문서의 설명 + .requestHeaders( + headerWithName("SOCIAL-AUTH-TOKEN").description( + "소셜 엑세스 토큰") + ) + .queryParameters( + parameterWithName("provider").description("OAuth provider(ex) GOOGLE)") + ) + .responseFields( // 응답 필드 + fieldWithPath("accessToken").description("accessToken"), + fieldWithPath("refreshToken").description("refreshToken"), + fieldWithPath("isFirstLogin").description("isFirstLogin") + ) + .build() + ))); }