-
Notifications
You must be signed in to change notification settings - Fork 13
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
Feature/refactoring #354
base: master
Are you sure you want to change the base?
Feature/refactoring #354
Changes from all commits
e2b1fee
4392fb7
6b1e54d
1f6732b
f0b611c
fab5f1c
c2ff7ae
2d2a9b6
3255aac
b55ce70
3b3211d
d06d5b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
package com.inhabas.api.auth.domain.oauth2.cookie; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
|
@@ -22,132 +21,132 @@ | |
|
||
public class CookieUtilsTest { | ||
|
||
private static final String COOKIE_NAME = "myCookie"; | ||
private static final String COOKIE_CONTENTS = "hello"; | ||
private static final int COOKIE_MAX_AGE = 180; | ||
|
||
@DisplayName("request 에서 쿠키를 꺼낸다.") | ||
@Test | ||
public void resolveCookieFromRequest() { | ||
// given | ||
MockHttpServletRequest request = new MockHttpServletRequest(); | ||
Cookie cookie = new Cookie("myCookie", "hello"); | ||
cookie.setMaxAge(180); | ||
request.setCookies(cookie); | ||
MockHttpServletRequest request = | ||
createRequestWithCookie(COOKIE_NAME, COOKIE_CONTENTS, COOKIE_MAX_AGE); | ||
|
||
// when | ||
Optional<Cookie> myCookie = CookieUtils.resolveCookie(request, "myCookie"); | ||
Optional<Cookie> myCookie = CookieUtils.resolveCookie(request, COOKIE_NAME); | ||
|
||
// then | ||
assertThat(myCookie.isPresent()).isTrue(); | ||
assertThat(myCookie.get().getValue()).isEqualTo("hello"); | ||
assertThat(myCookie.get().getMaxAge()).isEqualTo(180); | ||
assertThat(myCookie) | ||
.isPresent() | ||
.hasValueSatisfying( | ||
cookie -> { | ||
assertThat(cookie.getValue()).isEqualTo(COOKIE_CONTENTS); | ||
assertThat(cookie.getMaxAge()).isEqualTo(COOKIE_MAX_AGE); | ||
}); | ||
} | ||
|
||
@DisplayName("response 에 쿠키를 저장한다.") | ||
@Test | ||
public void saveCookieToResponse() { | ||
// given | ||
MockHttpServletResponse response = new MockHttpServletResponse(); | ||
String cookieName = "myCookie"; | ||
String cookieContents = "hello"; | ||
|
||
// when | ||
CookieUtils.setCookie(response, cookieName, cookieContents, 180); | ||
CookieUtils.setCookie(response, COOKIE_NAME, COOKIE_CONTENTS, COOKIE_MAX_AGE); | ||
|
||
// then | ||
Cookie resolvedCookie = response.getCookie(cookieName); | ||
assert resolvedCookie != null; | ||
|
||
assertThat(resolvedCookie.getName()).isEqualTo(cookieName); | ||
assertThat(resolvedCookie.getValue()).isEqualTo(cookieContents); | ||
assertThat(resolvedCookie.getMaxAge()).isEqualTo(180); | ||
Cookie resolvedCookie = response.getCookie(COOKIE_NAME); | ||
assertThat(resolvedCookie) | ||
.isNotNull() | ||
.satisfies( | ||
cookie -> { | ||
assertThat(cookie.getName()).isEqualTo(COOKIE_NAME); | ||
assertThat(cookie.getValue()).isEqualTo(COOKIE_CONTENTS); | ||
assertThat(cookie.getMaxAge()).isEqualTo(COOKIE_MAX_AGE); | ||
}); | ||
} | ||
|
||
@DisplayName("request 에서 쿠키를 지운다.") | ||
@Test | ||
public void removeCookieOfRequest() { | ||
// given | ||
MockHttpServletResponse response = new MockHttpServletResponse(); | ||
MockHttpServletRequest request = new MockHttpServletRequest(); | ||
Cookie cookie = new Cookie("myCookie", "hello"); | ||
cookie.setMaxAge(180); | ||
request.setCookies(cookie); | ||
MockHttpServletRequest request = | ||
createRequestWithCookie(COOKIE_NAME, COOKIE_CONTENTS, COOKIE_MAX_AGE); | ||
|
||
// when | ||
CookieUtils.deleteCookie(request, response, "myCookie"); | ||
CookieUtils.deleteCookie(request, response, COOKIE_NAME); | ||
|
||
// then | ||
Cookie deletedCookie = response.getCookie("myCookie"); | ||
assert deletedCookie != null; | ||
assertThat(deletedCookie.getMaxAge()).isEqualTo(0); | ||
assertThat(deletedCookie.getValue()).isEqualTo(""); | ||
Cookie deletedCookie = response.getCookie(COOKIE_NAME); | ||
assertThat(deletedCookie) | ||
.isNotNull() | ||
.satisfies( | ||
cookie -> { | ||
assertThat(cookie.getMaxAge()).isEqualTo(0); | ||
assertThat(cookie.getValue()).isEmpty(); | ||
}); | ||
} | ||
|
||
@DisplayName("성공적으로 serialize 한다.") | ||
@Test | ||
public void serializingTest() | ||
throws InvocationTargetException, InstantiationException, IllegalAccessException, | ||
NoSuchMethodException { | ||
// reflection | ||
Constructor<?> constructor = | ||
OAuth2AuthorizationRequest.Builder.class.getDeclaredConstructor( | ||
AuthorizationGrantType.class); | ||
constructor.setAccessible(true); | ||
|
||
// given | ||
OAuth2AuthorizationRequest.Builder builder = | ||
(OAuth2AuthorizationRequest.Builder) | ||
constructor.newInstance(AuthorizationGrantType.AUTHORIZATION_CODE); | ||
OAuth2AuthorizationRequest request = | ||
builder | ||
.authorizationUri("https://kauth.kakao.com/oauth/authorize") | ||
.clientId("1234") | ||
.redirectUri("http://localhost/api/login/oauth2/code/kakao") | ||
.scopes(Set.of("gender", "profile_image", "account_email", "profile_nickname")) | ||
.state("state1934") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 중복되는 부분 함수로 처리 |
||
.additionalParameters(java.util.Map.of()) | ||
.attributes(java.util.Map.of("registration_id", "kakao")) | ||
.build(); | ||
throws NoSuchMethodException, InvocationTargetException, InstantiationException, | ||
IllegalAccessException { | ||
OAuth2AuthorizationRequest request = createOAuth2AuthorizationRequest(); | ||
|
||
// when | ||
String serializedRequest = CookieUtils.serialize(request); | ||
|
||
// then | ||
assertTrue(Base64.isBase64(serializedRequest)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. assertTrue를 assertThat으로 변경 |
||
assertThat(serializedRequest).matches(Base64::isBase64); | ||
} | ||
|
||
@DisplayName("성공적으로 deserialize 한다.") | ||
@Test | ||
public void deserializingTest() | ||
throws NoSuchMethodException, InvocationTargetException, InstantiationException, | ||
IllegalAccessException { | ||
OAuth2AuthorizationRequest originalRequest = createOAuth2AuthorizationRequest(); | ||
String serializedRequest = CookieUtils.serialize(originalRequest); | ||
Cookie cookie = new Cookie("base64", serializedRequest); | ||
|
||
// when | ||
OAuth2AuthorizationRequest deserializedRequest = | ||
CookieUtils.deserialize(cookie, OAuth2AuthorizationRequest.class); | ||
|
||
// then | ||
assertThat(deserializedRequest).usingRecursiveComparison().isEqualTo(originalRequest); | ||
} | ||
|
||
private MockHttpServletRequest createRequestWithCookie(String name, String value, int maxAge) { | ||
MockHttpServletRequest request = new MockHttpServletRequest(); | ||
Cookie cookie = new Cookie(name, value); | ||
cookie.setMaxAge(maxAge); | ||
request.setCookies(cookie); | ||
return request; | ||
} | ||
|
||
private static OAuth2AuthorizationRequest createOAuth2AuthorizationRequest() | ||
throws NoSuchMethodException, InvocationTargetException, InstantiationException, | ||
IllegalAccessException { | ||
// reflection | ||
Constructor<?> constructor = | ||
OAuth2AuthorizationRequest.Builder.class.getDeclaredConstructor( | ||
AuthorizationGrantType.class); | ||
constructor.setAccessible(true); | ||
|
||
// given | ||
OAuth2AuthorizationRequest.Builder builder = | ||
(OAuth2AuthorizationRequest.Builder) | ||
constructor.newInstance(AuthorizationGrantType.AUTHORIZATION_CODE); | ||
OAuth2AuthorizationRequest originalRequest = | ||
builder | ||
.authorizationUri("https://kauth.kakao.com/oauth/authorize") | ||
.clientId("1234") | ||
.redirectUri("http://localhost/api/login/oauth2/code/kakao") | ||
.scopes(Set.of("gender", "profile_image", "account_email", "profile_nickname")) | ||
.state("state1934") | ||
.additionalParameters(java.util.Map.of()) | ||
.attributes(java.util.Map.of("registration_id", "kakao")) | ||
.build(); | ||
|
||
String serializedRequest = CookieUtils.serialize(originalRequest); | ||
Cookie cookie = new Cookie("base64", serializedRequest); | ||
|
||
// when | ||
OAuth2AuthorizationRequest deserializedRequest = | ||
CookieUtils.deserialize(cookie, OAuth2AuthorizationRequest.class); | ||
|
||
// then | ||
assertThat(deserializedRequest).usingRecursiveComparison().isEqualTo(originalRequest); | ||
return builder | ||
.authorizationUri("https://kauth.kakao.com/oauth/authorize") | ||
.clientId("1234") | ||
.redirectUri("http://localhost/api/login/oauth2/code/kakao") | ||
.scopes(Set.of("gender", "profile_image", "account_email", "profile_nickname")) | ||
.state("state1934") | ||
.additionalParameters(java.util.Map.of()) | ||
.attributes(java.util.Map.of("registration_id", "kakao")) | ||
.build(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ | |
import static com.inhabas.api.auth.domain.oauth2.cookie.HttpCookieOAuth2AuthorizationRequestRepository.OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME; | ||
import static com.inhabas.api.auth.domain.oauth2.cookie.HttpCookieOAuth2AuthorizationRequestRepository.REDIRECT_URL_PARAM_COOKIE_NAME; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
|
@@ -19,11 +18,19 @@ | |
|
||
import org.apache.commons.codec.binary.Base64; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class HttpCookieOAuth2AuthorizationRequestRepositoryTest { | ||
|
||
private HttpCookieOAuth2AuthorizationRequestRepository repository; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
repository = new HttpCookieOAuth2AuthorizationRequestRepository(); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. repository 객체를 각 테스트 메서드에서 개별적으로 초기화하는 대신 @beforeeach 메서드를 사용하여 모든 테스트가 실행되기 전에 한 번만 초기화하도록 한다. |
||
private final HttpCookieOAuth2AuthorizationRequestRepository | ||
httpCookieOAuth2AuthorizationRequestRepository = | ||
new HttpCookieOAuth2AuthorizationRequestRepository(); | ||
|
@@ -62,8 +69,7 @@ public void saveAuthorizationRequestNullTest() { | |
MockHttpServletResponse response = new MockHttpServletResponse(); | ||
|
||
// when | ||
httpCookieOAuth2AuthorizationRequestRepository.saveAuthorizationRequest( | ||
null, request, response); | ||
repository.saveAuthorizationRequest(null, request, response); | ||
|
||
// then | ||
assertThat(response.getCookies()) | ||
|
@@ -83,14 +89,13 @@ public void saveAuthorizationRequestTest() throws NoSuchMethodException { | |
OAuth2AuthorizationRequest oAuth2AuthorizationRequest = this.createOAuth2AuthorizationRequest(); | ||
|
||
// when | ||
httpCookieOAuth2AuthorizationRequestRepository.saveAuthorizationRequest( | ||
oAuth2AuthorizationRequest, request, response); | ||
repository.saveAuthorizationRequest(oAuth2AuthorizationRequest, request, response); | ||
|
||
// then | ||
// 쿠키 한가지 존재하는지 확인. | ||
Cookie savedCookie = response.getCookie(OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME); | ||
assert savedCookie != null; | ||
assertTrue(Base64.isBase64(savedCookie.getValue())); | ||
assertThat(savedCookie).isNotNull(); | ||
assertThat(Base64.isBase64(savedCookie.getValue())).isTrue(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. assertTrue를 assertThat으로 수정 |
||
} | ||
|
||
@DisplayName("OAuth2AuthorizationRequest 를 쿠키로 저장할 때, redirect_url 도 쿠키로 저장한다.") | ||
|
@@ -104,8 +109,7 @@ public void saveAuthorizationRequestWithRedirectUrlTest() throws NoSuchMethodExc | |
request.setParameter(REDIRECT_URL_PARAM_COOKIE_NAME, "/index.html"); | ||
|
||
// when | ||
httpCookieOAuth2AuthorizationRequestRepository.saveAuthorizationRequest( | ||
oAuth2AuthorizationRequest, request, response); | ||
repository.saveAuthorizationRequest(oAuth2AuthorizationRequest, request, response); | ||
|
||
// then | ||
// 쿠키 두가지 존재하는 지 확인 | ||
|
@@ -133,13 +137,13 @@ public void removeAuthorizationRequestTest() throws NoSuchMethodException { | |
|
||
// when | ||
OAuth2AuthorizationRequest returnedRequest = | ||
httpCookieOAuth2AuthorizationRequestRepository.removeAuthorizationRequest( | ||
request, response); | ||
repository.removeAuthorizationRequest(request, response); | ||
|
||
// then | ||
Cookie cookie = response.getCookie(OAUTH2_AUTHORIZATION_REQUEST_COOKIE_NAME); | ||
assert cookie != null; | ||
assertTrue(cookie.getValue().isBlank() && cookie.getMaxAge() == 0); | ||
assertThat(cookie).isNotNull(); | ||
assertThat(cookie.getValue()).isBlank(); | ||
assertThat(cookie.getMaxAge()).isEqualTo(0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. assertTrue를 assertThat으로 수정 |
||
} | ||
|
||
@DisplayName("OAuth2AuthorizationRequest 를 성공적으로 쿠키에서 삭제한다. (redirectUrl 쿠키도 삭제된다.)") | ||
|
@@ -150,7 +154,7 @@ public void removeAuthorizationRequestTotally() { | |
MockHttpServletResponse response = new MockHttpServletResponse(); | ||
|
||
// when | ||
httpCookieOAuth2AuthorizationRequestRepository.clearCookies(request, response); | ||
repository.clearCookies(request, response); | ||
|
||
// then | ||
assertThat(response.getCookies()) | ||
|
@@ -189,8 +193,8 @@ private OAuth2AuthorizationRequest createOAuth2AuthorizationRequest() | |
.attributes(java.util.Map.of("registration_id", "kakao")) | ||
.build(); | ||
|
||
} catch (InvocationTargetException | InstantiationException | IllegalAccessException ignored) { | ||
return null; | ||
} catch (InvocationTargetException | InstantiationException | IllegalAccessException e) { | ||
throw new RuntimeException("Failed to create OAuth2AuthorizationRequest", e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 예외처리시 null을 반환하는 것보다, 해당 정보를 반환하여 문제를 알리도록 변경 |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,53 +38,59 @@ public class Oauth2AuthenticationFailureHandlerTest { | |
|
||
@Mock private AuthProperties.OAuth2 oauth2Utils; | ||
|
||
private static final String VALID_REDIRECT_URL = "https://www.inhabas.com"; | ||
private static final String INVALID_REDIRECT_URL = "https://www.unauthorized_url.com"; | ||
private static final String ERROR_CODE = OAuth2ErrorCodes.INVALID_REQUEST; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 중복 방지와 가독성을 위한 상수 추가 |
||
@BeforeEach | ||
public void setUp() { | ||
given(authProperties.getOauth2()).willReturn(oauth2Utils); | ||
} | ||
|
||
private MockHttpServletRequest createRequestWithCookie(String cookieValue) { | ||
MockHttpServletRequest request = new MockHttpServletRequest(); | ||
Cookie redirectCookie = new Cookie(REDIRECT_URL_PARAM_COOKIE_NAME, cookieValue); | ||
request.setCookies(redirectCookie); | ||
return request; | ||
} | ||
|
||
private AuthenticationException createAuthenticationException(String errorCode) { | ||
return new OAuth2AuthenticationException(errorCode); | ||
} | ||
|
||
@DisplayName("FailureHandler 호출 시, 허락된 defaultURL 로 정상적으로 리다이렉트 된다.") | ||
@Test | ||
public void redirectToDefaultTest() throws IOException { | ||
// given | ||
String errorCode = OAuth2ErrorCodes.INVALID_REQUEST; | ||
MockHttpServletRequest request = new MockHttpServletRequest(); | ||
MockHttpServletRequest request = createRequestWithCookie(VALID_REDIRECT_URL); | ||
MockHttpServletResponse response = new MockHttpServletResponse(); | ||
AuthenticationException authenticationException = new OAuth2AuthenticationException(errorCode); | ||
|
||
Cookie redirectCookie = new Cookie(REDIRECT_URL_PARAM_COOKIE_NAME, "https://www.inhabas.com"); | ||
request.setCookies(redirectCookie); | ||
AuthenticationException authenticationException = new OAuth2AuthenticationException(ERROR_CODE); | ||
|
||
given(oauth2Utils.getDefaultRedirectUri()).willReturn("https://www.inhabas.com"); | ||
given(oauth2Utils.getDefaultRedirectUri()).willReturn(VALID_REDIRECT_URL); | ||
|
||
// when | ||
oauth2AuthenticationFailureHandler.onAuthenticationFailure( | ||
request, response, authenticationException); | ||
|
||
// then | ||
assertThat(response.getRedirectedUrl()).isEqualTo("https://www.inhabas.com?error=" + errorCode); | ||
assertThat(response.getRedirectedUrl()).isEqualTo(VALID_REDIRECT_URL + "?error=" + ERROR_CODE); | ||
} | ||
|
||
@DisplayName("유효하지 않은 redirect_url 은 허용하지 않는다.") | ||
@Test | ||
public void validateRedirectUrlTest() throws IOException { | ||
// given | ||
String errorCode = OAuth2ErrorCodes.INVALID_REQUEST; | ||
MockHttpServletRequest request = new MockHttpServletRequest(); | ||
MockHttpServletRequest request = createRequestWithCookie(INVALID_REDIRECT_URL); | ||
MockHttpServletResponse response = new MockHttpServletResponse(); | ||
AuthenticationException authenticationException = new OAuth2AuthenticationException(errorCode); | ||
|
||
Cookie redirectCookie = | ||
new Cookie(REDIRECT_URL_PARAM_COOKIE_NAME, "https://www.unauthorized_url.com"); | ||
request.setCookies(redirectCookie); | ||
AuthenticationException authenticationException = createAuthenticationException(ERROR_CODE); | ||
|
||
given(oauth2Utils.getDefaultRedirectUri()).willReturn("https://www.inhabas.com"); | ||
given(oauth2Utils.getDefaultRedirectUri()).willReturn(VALID_REDIRECT_URL); | ||
|
||
// when | ||
oauth2AuthenticationFailureHandler.onAuthenticationFailure( | ||
request, response, authenticationException); | ||
|
||
// then | ||
assertThat(response.getRedirectedUrl()).isEqualTo("https://www.inhabas.com?error=" + errorCode); | ||
assertThat(response.getRedirectedUrl()).isEqualTo(VALID_REDIRECT_URL + "?error=" + ERROR_CODE); | ||
} | ||
} |
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.
중복을 제거하고 가독성 향상을 위해 상수 추가