|
@@ -22,9 +22,11 @@ import org.powermock.modules.junit4.PowerMockRunner;
|
|
import org.springframework.mock.web.MockHttpServletRequest;
|
|
import org.springframework.mock.web.MockHttpServletRequest;
|
|
import org.springframework.mock.web.MockHttpServletResponse;
|
|
import org.springframework.mock.web.MockHttpServletResponse;
|
|
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
|
|
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
|
|
|
|
+import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
import static org.mockito.Mockito.mock;
|
|
import static org.mockito.Mockito.mock;
|
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
|
|
|
/**
|
|
/**
|
|
* Tests for {@link HttpSessionOAuth2AuthorizationRequestRepository}.
|
|
* Tests for {@link HttpSessionOAuth2AuthorizationRequestRepository}.
|
|
@@ -44,8 +46,10 @@ public class HttpSessionOAuth2AuthorizationRequestRepositoryTests {
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void loadAuthorizationRequestWhenNotSavedThenReturnNull() {
|
|
public void loadAuthorizationRequestWhenNotSavedThenReturnNull() {
|
|
|
|
+ MockHttpServletRequest request = new MockHttpServletRequest();
|
|
|
|
+ request.addParameter(OAuth2ParameterNames.STATE, "state-1234");
|
|
OAuth2AuthorizationRequest authorizationRequest =
|
|
OAuth2AuthorizationRequest authorizationRequest =
|
|
- this.authorizationRequestRepository.loadAuthorizationRequest(new MockHttpServletRequest());
|
|
|
|
|
|
+ this.authorizationRequestRepository.loadAuthorizationRequest(request);
|
|
|
|
|
|
assertThat(authorizationRequest).isNull();
|
|
assertThat(authorizationRequest).isNull();
|
|
}
|
|
}
|
|
@@ -54,15 +58,69 @@ public class HttpSessionOAuth2AuthorizationRequestRepositoryTests {
|
|
public void loadAuthorizationRequestWhenSavedThenReturnAuthorizationRequest() {
|
|
public void loadAuthorizationRequestWhenSavedThenReturnAuthorizationRequest() {
|
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
|
|
|
+
|
|
OAuth2AuthorizationRequest authorizationRequest = mock(OAuth2AuthorizationRequest.class);
|
|
OAuth2AuthorizationRequest authorizationRequest = mock(OAuth2AuthorizationRequest.class);
|
|
|
|
+ when(authorizationRequest.getState()).thenReturn("state-1234");
|
|
|
|
|
|
this.authorizationRequestRepository.saveAuthorizationRequest(authorizationRequest, request, response);
|
|
this.authorizationRequestRepository.saveAuthorizationRequest(authorizationRequest, request, response);
|
|
|
|
+ request.addParameter(OAuth2ParameterNames.STATE, "state-1234");
|
|
OAuth2AuthorizationRequest loadedAuthorizationRequest =
|
|
OAuth2AuthorizationRequest loadedAuthorizationRequest =
|
|
this.authorizationRequestRepository.loadAuthorizationRequest(request);
|
|
this.authorizationRequestRepository.loadAuthorizationRequest(request);
|
|
|
|
|
|
assertThat(loadedAuthorizationRequest).isEqualTo(authorizationRequest);
|
|
assertThat(loadedAuthorizationRequest).isEqualTo(authorizationRequest);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // gh-5110
|
|
|
|
+ @Test
|
|
|
|
+ public void loadAuthorizationRequestWhenMultipleSavedThenReturnMatchingAuthorizationRequest() {
|
|
|
|
+ MockHttpServletRequest request = new MockHttpServletRequest();
|
|
|
|
+ MockHttpServletResponse response = new MockHttpServletResponse();
|
|
|
|
+
|
|
|
|
+ String state1 = "state-1122";
|
|
|
|
+ OAuth2AuthorizationRequest authorizationRequest1 = mock(OAuth2AuthorizationRequest.class);
|
|
|
|
+ when(authorizationRequest1.getState()).thenReturn(state1);
|
|
|
|
+ this.authorizationRequestRepository.saveAuthorizationRequest(authorizationRequest1, request, response);
|
|
|
|
+
|
|
|
|
+ String state2 = "state-3344";
|
|
|
|
+ OAuth2AuthorizationRequest authorizationRequest2 = mock(OAuth2AuthorizationRequest.class);
|
|
|
|
+ when(authorizationRequest2.getState()).thenReturn(state2);
|
|
|
|
+ this.authorizationRequestRepository.saveAuthorizationRequest(authorizationRequest2, request, response);
|
|
|
|
+
|
|
|
|
+ String state3 = "state-5566";
|
|
|
|
+ OAuth2AuthorizationRequest authorizationRequest3 = mock(OAuth2AuthorizationRequest.class);
|
|
|
|
+ when(authorizationRequest3.getState()).thenReturn(state3);
|
|
|
|
+ this.authorizationRequestRepository.saveAuthorizationRequest(authorizationRequest3, request, response);
|
|
|
|
+
|
|
|
|
+ request.addParameter(OAuth2ParameterNames.STATE, state1);
|
|
|
|
+ OAuth2AuthorizationRequest loadedAuthorizationRequest1 =
|
|
|
|
+ this.authorizationRequestRepository.loadAuthorizationRequest(request);
|
|
|
|
+ assertThat(loadedAuthorizationRequest1).isEqualTo(authorizationRequest1);
|
|
|
|
+
|
|
|
|
+ request.removeParameter(OAuth2ParameterNames.STATE);
|
|
|
|
+ request.addParameter(OAuth2ParameterNames.STATE, state2);
|
|
|
|
+ OAuth2AuthorizationRequest loadedAuthorizationRequest2 =
|
|
|
|
+ this.authorizationRequestRepository.loadAuthorizationRequest(request);
|
|
|
|
+ assertThat(loadedAuthorizationRequest2).isEqualTo(authorizationRequest2);
|
|
|
|
+
|
|
|
|
+ request.removeParameter(OAuth2ParameterNames.STATE);
|
|
|
|
+ request.addParameter(OAuth2ParameterNames.STATE, state3);
|
|
|
|
+ OAuth2AuthorizationRequest loadedAuthorizationRequest3 =
|
|
|
|
+ this.authorizationRequestRepository.loadAuthorizationRequest(request);
|
|
|
|
+ assertThat(loadedAuthorizationRequest3).isEqualTo(authorizationRequest3);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test(expected = IllegalArgumentException.class)
|
|
|
|
+ public void loadAuthorizationRequestWhenSavedAndStateParameterNullThenThrowIllegalArgumentException() {
|
|
|
|
+ MockHttpServletRequest request = new MockHttpServletRequest();
|
|
|
|
+
|
|
|
|
+ OAuth2AuthorizationRequest authorizationRequest = mock(OAuth2AuthorizationRequest.class);
|
|
|
|
+ when(authorizationRequest.getState()).thenReturn("state-1234");
|
|
|
|
+ this.authorizationRequestRepository.saveAuthorizationRequest(
|
|
|
|
+ authorizationRequest, request, new MockHttpServletResponse());
|
|
|
|
+
|
|
|
|
+ this.authorizationRequestRepository.loadAuthorizationRequest(request);
|
|
|
|
+ }
|
|
|
|
+
|
|
@Test(expected = IllegalArgumentException.class)
|
|
@Test(expected = IllegalArgumentException.class)
|
|
public void saveAuthorizationRequestWhenHttpServletRequestIsNullThenThrowIllegalArgumentException() {
|
|
public void saveAuthorizationRequestWhenHttpServletRequestIsNullThenThrowIllegalArgumentException() {
|
|
this.authorizationRequestRepository.saveAuthorizationRequest(
|
|
this.authorizationRequestRepository.saveAuthorizationRequest(
|
|
@@ -75,13 +133,22 @@ public class HttpSessionOAuth2AuthorizationRequestRepositoryTests {
|
|
mock(OAuth2AuthorizationRequest.class), new MockHttpServletRequest(), null);
|
|
mock(OAuth2AuthorizationRequest.class), new MockHttpServletRequest(), null);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Test(expected = IllegalArgumentException.class)
|
|
|
|
+ public void saveAuthorizationRequestWhenStateNullThenThrowIllegalArgumentException() {
|
|
|
|
+ this.authorizationRequestRepository.saveAuthorizationRequest(
|
|
|
|
+ mock(OAuth2AuthorizationRequest.class), new MockHttpServletRequest(), new MockHttpServletResponse());
|
|
|
|
+ }
|
|
|
|
+
|
|
@Test
|
|
@Test
|
|
public void saveAuthorizationRequestWhenNotNullThenSaved() {
|
|
public void saveAuthorizationRequestWhenNotNullThenSaved() {
|
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
|
- OAuth2AuthorizationRequest authorizationRequest = mock(OAuth2AuthorizationRequest.class);
|
|
|
|
|
|
|
|
|
|
+ OAuth2AuthorizationRequest authorizationRequest = mock(OAuth2AuthorizationRequest.class);
|
|
|
|
+ when(authorizationRequest.getState()).thenReturn("state-1234");
|
|
this.authorizationRequestRepository.saveAuthorizationRequest(
|
|
this.authorizationRequestRepository.saveAuthorizationRequest(
|
|
authorizationRequest, request, new MockHttpServletResponse());
|
|
authorizationRequest, request, new MockHttpServletResponse());
|
|
|
|
+
|
|
|
|
+ request.addParameter(OAuth2ParameterNames.STATE, "state-1234");
|
|
OAuth2AuthorizationRequest loadedAuthorizationRequest =
|
|
OAuth2AuthorizationRequest loadedAuthorizationRequest =
|
|
this.authorizationRequestRepository.loadAuthorizationRequest(request);
|
|
this.authorizationRequestRepository.loadAuthorizationRequest(request);
|
|
|
|
|
|
@@ -92,12 +159,17 @@ public class HttpSessionOAuth2AuthorizationRequestRepositoryTests {
|
|
public void saveAuthorizationRequestWhenNullThenRemoved() {
|
|
public void saveAuthorizationRequestWhenNullThenRemoved() {
|
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
|
|
|
+
|
|
OAuth2AuthorizationRequest authorizationRequest = mock(OAuth2AuthorizationRequest.class);
|
|
OAuth2AuthorizationRequest authorizationRequest = mock(OAuth2AuthorizationRequest.class);
|
|
|
|
+ when(authorizationRequest.getState()).thenReturn("state-1234");
|
|
|
|
|
|
this.authorizationRequestRepository.saveAuthorizationRequest( // Save
|
|
this.authorizationRequestRepository.saveAuthorizationRequest( // Save
|
|
authorizationRequest, request, response);
|
|
authorizationRequest, request, response);
|
|
|
|
+
|
|
|
|
+ request.addParameter(OAuth2ParameterNames.STATE, "state-1234");
|
|
this.authorizationRequestRepository.saveAuthorizationRequest( // Null value removes
|
|
this.authorizationRequestRepository.saveAuthorizationRequest( // Null value removes
|
|
null, request, response);
|
|
null, request, response);
|
|
|
|
+
|
|
OAuth2AuthorizationRequest loadedAuthorizationRequest =
|
|
OAuth2AuthorizationRequest loadedAuthorizationRequest =
|
|
this.authorizationRequestRepository.loadAuthorizationRequest(request);
|
|
this.authorizationRequestRepository.loadAuthorizationRequest(request);
|
|
|
|
|
|
@@ -113,10 +185,14 @@ public class HttpSessionOAuth2AuthorizationRequestRepositoryTests {
|
|
public void removeAuthorizationRequestWhenSavedThenRemoved() {
|
|
public void removeAuthorizationRequestWhenSavedThenRemoved() {
|
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
|
|
|
+
|
|
OAuth2AuthorizationRequest authorizationRequest = mock(OAuth2AuthorizationRequest.class);
|
|
OAuth2AuthorizationRequest authorizationRequest = mock(OAuth2AuthorizationRequest.class);
|
|
|
|
+ when(authorizationRequest.getState()).thenReturn("state-1234");
|
|
|
|
|
|
this.authorizationRequestRepository.saveAuthorizationRequest(
|
|
this.authorizationRequestRepository.saveAuthorizationRequest(
|
|
authorizationRequest, request, response);
|
|
authorizationRequest, request, response);
|
|
|
|
+
|
|
|
|
+ request.addParameter(OAuth2ParameterNames.STATE, "state-1234");
|
|
OAuth2AuthorizationRequest removedAuthorizationRequest =
|
|
OAuth2AuthorizationRequest removedAuthorizationRequest =
|
|
this.authorizationRequestRepository.removeAuthorizationRequest(request);
|
|
this.authorizationRequestRepository.removeAuthorizationRequest(request);
|
|
OAuth2AuthorizationRequest loadedAuthorizationRequest =
|
|
OAuth2AuthorizationRequest loadedAuthorizationRequest =
|
|
@@ -129,6 +205,7 @@ public class HttpSessionOAuth2AuthorizationRequestRepositoryTests {
|
|
@Test
|
|
@Test
|
|
public void removeAuthorizationRequestWhenNotSavedThenNotRemoved() {
|
|
public void removeAuthorizationRequestWhenNotSavedThenNotRemoved() {
|
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
|
|
|
+ request.addParameter(OAuth2ParameterNames.STATE, "state-1234");
|
|
|
|
|
|
OAuth2AuthorizationRequest removedAuthorizationRequest =
|
|
OAuth2AuthorizationRequest removedAuthorizationRequest =
|
|
this.authorizationRequestRepository.removeAuthorizationRequest(request);
|
|
this.authorizationRequestRepository.removeAuthorizationRequest(request);
|