@SuppressWarnings("unchecked")
final OAuth1ConnectionFactory<Object> factory = mock(OAuth1ConnectionFactory.class);
final OAuth1Operations operations = mock(OAuth1Operations.class);
final String serverName = "example.com";
final String serviceUrl = "http://twitter.com/auth";
final OAuthToken oAuthToken = new OAuthToken("my_token", "my_secret");
final String verifier = "my_verifier";
final Connection<Object> connection = DummyConnection.dummy("provider", "user");
final OAuth1AuthenticationService<Object> authSvc = new OAuth1AuthenticationService<Object>(factory);
authSvc.getReturnToUrlParameters().add("param");
authSvc.afterPropertiesSet();
final MockServletContext context = new MockServletContext();
final MockHttpSession session = new MockHttpSession(context);
// mock definitions
when(factory.getProviderId()).thenReturn(connection.getKey().getProviderId());
when(factory.getOAuthOperations()).thenReturn(operations);
when(factory.createConnection(ArgMatchers.oAuthToken(oAuthToken))).thenReturn(connection);
when(operations.getVersion()).thenReturn(OAuth1Version.CORE_10_REVISION_A);
when(operations.fetchRequestToken("http://"+serverName+"/auth/foo?param=param_value", null)).thenReturn(oAuthToken);
when(operations.exchangeForAccessToken(ArgMatchers.authorizedRequestToken(oAuthToken, verifier), Matchers.same((MultiValueMap<String, String>) null))).thenReturn(oAuthToken);
when(operations.buildAuthenticateUrl(oAuthToken.getValue(), OAuth1Parameters.NONE)).thenReturn(serviceUrl + "?oauth_token=" + oAuthToken.getValue());
// first phase
MockHttpServletRequest request = new MockHttpServletRequest(context, "GET", "/auth/foo");
request.setServerName(serverName);
request.setSession(session);
request.addParameter("param", "param_value");
MockHttpServletResponse response = new MockHttpServletResponse();
try {
SocialAuthenticationToken token = authSvc.getAuthToken(request, response);
fail("redirect expected, was token " + token);
} catch (SocialAuthenticationRedirectException e) {
// expect redirect to service url including token
assertEquals(serviceUrl + "?oauth_token=" + oAuthToken.getValue(), e.getRedirectUrl());
}
// second phase
request = new MockHttpServletRequest(context, "GET", "/auth/foo");
request.setServerName(serverName);