-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test(#222) : oauth controller #225
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1696782
Test : oauth controller
Astin01 fb65afe
Fix : μ μ μ΄λ―Έμ§ μμ± νλ μμ
Astin01 9d74c29
Test : μ€ν¨ ν
μ€νΈ
Astin01 4536d1a
Fix : ν΄λ©΄ νμμΌ λ μμλλ μνμ½λ μμ
Astin01 e67f8bb
Merge branch 'develop' of https://github.com/TripInfoWeb/solitour-bacβ¦
Astin01 a445ff8
Test : 리νλ μ ν ν°μΌλ‘ μμΈμ€ ν ν° μ¬λ°κΈ ν
μ€νΈ
Astin01 87bf74a
Test : νμνν΄ ν
μ€νΈ
Astin01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
113 changes: 113 additions & 0 deletions
113
src/test/java/solitour_backend/solitour/oauth/controller/OauthControllerTest.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,113 @@ | ||
package solitour_backend.solitour.oauth.controller; | ||
|
||
import jakarta.servlet.http.Cookie; | ||
import jakarta.transaction.Transactional; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.ArgumentMatchers; | ||
import org.mockito.BDDMockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.restdocs.RestDocumentationContextProvider; | ||
import org.springframework.restdocs.RestDocumentationExtension; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.springframework.web.context.WebApplicationContext; | ||
import solitour_backend.solitour.auth.service.OauthService; | ||
import solitour_backend.solitour.auth.service.dto.response.LoginResponse; | ||
import solitour_backend.solitour.auth.service.dto.response.OauthLinkResponse; | ||
import solitour_backend.solitour.user.user_status.UserStatus; | ||
|
||
import static org.hamcrest.Matchers.notNullValue; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; | ||
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration; | ||
import static org.springframework.restdocs.operation.preprocess.Preprocessors.*; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; | ||
|
||
@ActiveProfiles("test") | ||
@AutoConfigureMockMvc | ||
@ExtendWith({MockitoExtension.class, RestDocumentationExtension.class}) | ||
@AutoConfigureRestDocs | ||
@Transactional | ||
@SpringBootTest | ||
class OauthControllerTest { | ||
|
||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@MockBean | ||
private OauthService oauthService; | ||
|
||
private final String kakaoRedirectUrl = "http://localhost:8080/oauth2/callback/kakao"; | ||
|
||
private final String kakaoOauthLink = "https://kauth.kakao.com/oauth/authorize?response_type=code&redirect_uri=http://localhost:8080/oauth2/callback/kakao&client_id=cleintId"; | ||
|
||
@BeforeEach | ||
void setUp(WebApplicationContext webApplicationContext, | ||
RestDocumentationContextProvider restDocumentation) { | ||
|
||
this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) | ||
.apply(documentationConfiguration(restDocumentation) | ||
.operationPreprocessors() | ||
.withRequestDefaults(modifyUris(), prettyPrint()) | ||
.withResponseDefaults(prettyPrint())) | ||
.build(); | ||
} | ||
|
||
@DisplayName("μ μμ μΌλ‘ Oauth μΈμ¦ λ§ν¬κ° λ°νλλμ§ ν μ€νΈνλ€") | ||
@Test | ||
void access() throws Exception { | ||
BDDMockito.given(oauthService.generateAuthUrl(eq("kakao"), anyString())) | ||
.willReturn(new OauthLinkResponse(kakaoOauthLink)); | ||
|
||
mockMvc.perform(get("/api/auth/oauth2/login") | ||
.queryParam("type", "kakao") | ||
.queryParam("redirectUrl", kakaoRedirectUrl) | ||
) | ||
.andExpectAll( | ||
status().isOk(), | ||
jsonPath("oauthLink", notNullValue())) | ||
.andDo( | ||
document("oauthLink", | ||
preprocessRequest(prettyPrint()), | ||
preprocessResponse(prettyPrint()) | ||
) | ||
); | ||
} | ||
|
||
@DisplayName("μ μμ μΌλ‘ λ‘κ·ΈμΈλ μ ν ν°μ΄ λ°κΈλλ€") | ||
@Test | ||
void login() throws Exception { | ||
Cookie accessCookie = new Cookie("access_token", "eyJhbGciOiJIUzI1NiJ9"); | ||
Cookie refreshCookie = new Cookie("refresh_token", "l9Fp78G5l6RWbG9SMvOVnb0pnrEkWPHMPBmQw8c"); | ||
|
||
BDDMockito.given(oauthService.requestAccessToken(anyString(), ArgumentMatchers.anyString(), ArgumentMatchers.anyString())) | ||
.willReturn(new LoginResponse(accessCookie, refreshCookie, UserStatus.PENDING)); | ||
|
||
String code = "code"; | ||
mockMvc.perform(get("/api/auth/oauth2/login") | ||
.queryParam("code", code) | ||
.queryParam("type", "kakao") | ||
.queryParam("redirectUrl", kakaoRedirectUrl)) | ||
.andExpectAll( | ||
status().isOk(), | ||
header().exists("Set-Cookie"), | ||
jsonPath("$").value("PENDING")) | ||
.andDo( | ||
document("oauthLogin", | ||
preprocessRequest(prettyPrint()), | ||
preprocessResponse(prettyPrint()) | ||
) | ||
); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
μ€ν¨ ν μ€νΈλ μν΄λ λ κΉμ..?