1 package de.juplo.facebook;
6 import javax.annotation.Resource;
7 import static org.junit.Assert.*;
8 import org.junit.Before;
10 import org.junit.runner.RunWith;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 import org.springframework.http.HttpStatus;
14 import org.springframework.http.converter.HttpMessageNotReadableException;
15 import org.springframework.security.access.AccessDeniedException;
16 import org.springframework.security.oauth2.client.OAuth2RestTemplate;
17 import org.springframework.security.oauth2.client.http.OAuth2ErrorHandler;
18 import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
19 import org.springframework.security.oauth2.client.resource.UserApprovalRequiredException;
20 import org.springframework.security.oauth2.client.resource.UserRedirectRequiredException;
21 import org.springframework.security.oauth2.client.token.AccessTokenProvider;
22 import org.springframework.security.oauth2.client.token.AccessTokenRequest;
23 import org.springframework.security.oauth2.common.OAuth2AccessToken;
24 import static org.springframework.security.oauth2.common.OAuth2AccessToken.OAUTH2_TYPE;
25 import org.springframework.security.oauth2.common.OAuth2RefreshToken;
26 import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
27 import org.springframework.test.context.ContextConfiguration;
28 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
36 @RunWith(SpringJUnit4ClassRunner.class)
37 @ContextConfiguration(
39 "classpath:/spring/test-facebook-error-handler.xml"
41 public class GraphApiErrorHandlerTest
43 private static final Logger log =
44 LoggerFactory.getLogger(GraphApiErrorHandlerTest.class);
47 private OAuth2RestTemplate clientTemplate;
49 private MockClientHttpRequestFactory requestFactory;
53 public void testError1()
55 log.info("testError1");
58 requestFactory.setBody(
62 " \"message\": \"An unknown error has occurred.\",\n" +
63 " \"type\": \"OAuthException\",\n" +
70 clientTemplate.getForObject("ANY", SOME.class);
71 fail("The expected exception was not thrown");
73 catch(UnknownErrorException e)
75 log.debug("{}", e.toString());
76 assertEquals("invalid_request", e.getOAuth2ErrorCode());
77 assertEquals(1, e.getCode());
78 assertEquals("An unknown error has occurred.", e.getMessage());
79 assertEquals("OAuthException", e.getType());
84 public void testError2()
86 log.info("testError2");
89 requestFactory.setBody(
93 " \"message\": \"An unexpected error has occurred. Please retry your request later.\",\n" +
94 " \"type\": \"OAuthException\",\n" +
101 clientTemplate.getForObject("ANY", SOME.class);
102 fail("The expected exception was not thrown");
104 catch(UnexpectedErrorException e)
106 log.debug("{}", e.toString());
107 assertEquals("invalid_request", e.getOAuth2ErrorCode());
108 assertEquals(2, e.getCode());
109 assertEquals("An unexpected error has occurred. Please retry your request later.", e.getMessage());
110 assertEquals("OAuthException", e.getType());
115 public void testError21()
117 log.info("testError21");
120 requestFactory.setBody(
124 " \"message\": \"(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603. Please update your API calls to the new ID\",\n" +
125 " \"type\": \"OAuthException\",\n" +
132 clientTemplate.getForObject("ANY", SOME.class);
133 fail("The expected exception was not thrown");
135 catch(PageMigratedException e)
137 log.debug("{}", e.toString());
138 assertEquals("invalid_request", e.getOAuth2ErrorCode());
139 assertEquals(21, e.getCode());
140 assertEquals("(#21) Page ID 590408587650316 was migrated to page ID 1421620791415603. Please update your API calls to the new ID", e.getMessage());
141 assertEquals("OAuthException", e.getType());
146 public void testError100()
148 log.info("testError100");
151 requestFactory.setBody(
155 " \"message\": \"Unsupported get request.\",\n" +
156 " \"type\": \"GraphMethodException\",\n" +
163 clientTemplate.getForObject("ANY", SOME.class);
164 fail("The expected exception was not thrown");
166 catch(UnsupportedGetRequestException e)
168 log.debug("{}", e.toString());
169 assertEquals("invalid_request", e.getOAuth2ErrorCode());
170 assertEquals(100, e.getCode());
171 assertEquals("Unsupported get request.", e.getMessage());
172 assertEquals("GraphMethodException", e.getType());
177 public void testUnmappedError()
179 log.info("testUnmappedError");
182 requestFactory.setBody(
186 " \"message\": \"This error does not exist.\",\n" +
187 " \"type\": \"NonexistentException\",\n" +
188 " \"code\": 999999999\n" +
194 clientTemplate.getForObject("ANY", SOME.class);
195 fail("The expected exception was not thrown");
197 catch(GraphApiException e)
199 log.debug("{}", e.toString());
200 assertEquals("invalid_request", e.getOAuth2ErrorCode());
201 assertEquals(999999999, e.getCode());
202 assertEquals("This error does not exist.", e.getMessage());
203 assertEquals("NonexistentException", e.getType());
208 public void testInvlalidErrors()
210 log.info("testInvalidErrors");
213 requestFactory.setBody(
217 " \"message\": null,\n" +
218 " \"type\": \"Whatever\",\n" +
219 " \"code\": 999999999\n" +
225 clientTemplate.getForObject("ANY", SOME.class);
226 fail("The expected exception was not thrown");
228 catch(OAuth2Exception e)
230 log.debug("{}", e.toString());
231 assertEquals("invalid_request", e.getOAuth2ErrorCode());
232 assertFalse(e instanceof GraphApiException);
236 requestFactory.setBody(
240 " \"type\": \"Whatever\",\n" +
241 " \"code\": 999999999\n" +
247 clientTemplate.getForObject("ANY", SOME.class);
248 fail("The expected exception was not thrown");
250 catch(OAuth2Exception e)
252 log.debug("{}", e.toString());
253 assertEquals("invalid_request", e.getOAuth2ErrorCode());
254 assertFalse(e instanceof GraphApiException);
258 requestFactory.setBody(
262 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
263 " \"type\": null,\n" +
264 " \"code\": 999999999\n" +
270 clientTemplate.getForObject("ANY", SOME.class);
271 fail("The expected exception was not thrown");
273 catch(OAuth2Exception e)
275 log.debug("{}", e.toString());
276 assertEquals("invalid_request", e.getOAuth2ErrorCode());
277 assertFalse(e instanceof GraphApiException);
281 requestFactory.setBody(
285 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
286 " \"code\": 999999999\n" +
292 clientTemplate.getForObject("ANY", SOME.class);
293 fail("The expected exception was not thrown");
295 catch(OAuth2Exception e)
297 log.debug("{}", e.toString());
298 assertEquals("invalid_request", e.getOAuth2ErrorCode());
299 assertFalse(e instanceof GraphApiException);
303 requestFactory.setBody(
307 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
308 " \"type\": \"Whatever\",\n" +
309 " \"code\": \"some string\"\n" +
315 clientTemplate.getForObject("ANY", SOME.class);
316 fail("The expected exception was not thrown");
318 catch(OAuth2Exception e)
320 log.debug("{}", e.toString());
321 assertEquals("invalid_request", e.getOAuth2ErrorCode());
322 assertFalse(e instanceof GraphApiException);
326 requestFactory.setBody(
330 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
331 " \"type\": \"Whatever\",\n" +
338 clientTemplate.getForObject("ANY", SOME.class);
339 fail("The expected exception was not thrown");
341 catch(OAuth2Exception e)
343 log.debug("{}", e.toString());
344 assertEquals("invalid_request", e.getOAuth2ErrorCode());
345 assertFalse(e instanceof GraphApiException);
349 requestFactory.setBody(
353 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
354 " \"type\": \"Whatever\",\n" +
355 " \"code\": null\n" +
361 clientTemplate.getForObject("ANY", SOME.class);
362 fail("The expected exception was not thrown");
364 catch(OAuth2Exception e)
366 log.debug("{}", e.toString());
367 assertEquals("invalid_request", e.getOAuth2ErrorCode());
368 assertFalse(e instanceof GraphApiException);
372 requestFactory.setBody(
376 " \"message\": \"Not a Graph-Api-Exception.\",\n" +
377 " \"type\": \"Whatever\"\n" +
383 clientTemplate.getForObject("ANY", SOME.class);
384 fail("The expected exception was not thrown");
386 catch(OAuth2Exception e)
388 log.debug("{}", e.toString());
389 assertEquals("invalid_request", e.getOAuth2ErrorCode());
390 assertFalse(e instanceof GraphApiException);
394 requestFactory.setBody("{\"error\":{\"message\":null}}");
398 clientTemplate.getForObject("ANY", SOME.class);
399 fail("The expected exception was not thrown");
401 catch(OAuth2Exception e)
403 log.debug("{}", e.toString());
404 assertEquals("invalid_request", e.getOAuth2ErrorCode());
405 assertFalse(e instanceof GraphApiException);
409 requestFactory.setBody("{\"error\":{\"type\":null}}");
413 clientTemplate.getForObject("ANY", SOME.class);
414 fail("The expected exception was not thrown");
416 catch(OAuth2Exception e)
418 log.debug("{}", e.toString());
419 assertEquals("invalid_request", e.getOAuth2ErrorCode());
420 assertFalse(e instanceof GraphApiException);
424 requestFactory.setBody("{\"error\":{\"code\":null}}");
428 clientTemplate.getForObject("ANY", SOME.class);
429 fail("The expected exception was not thrown");
431 catch(OAuth2Exception e)
433 log.debug("{}", e.toString());
434 assertEquals("invalid_request", e.getOAuth2ErrorCode());
435 assertFalse(e instanceof GraphApiException);
439 requestFactory.setBody("{\"error\":{}}");
443 clientTemplate.getForObject("ANY", SOME.class);
444 fail("The expected exception was not thrown");
446 catch(OAuth2Exception e)
448 log.debug("{}", e.toString());
449 assertEquals("invalid_request", e.getOAuth2ErrorCode());
450 assertFalse(e instanceof GraphApiException);
454 requestFactory.setBody("{\"error\":\"some message\"}");
458 clientTemplate.getForObject("ANY", SOME.class);
459 fail("The expected exception was not thrown");
461 catch(OAuth2Exception e)
463 log.debug("{}", e.toString());
464 assertEquals("invalid_request", e.getOAuth2ErrorCode());
465 assertFalse(e instanceof GraphApiException);
469 requestFactory.setBody("{\"error\":null}");
473 clientTemplate.getForObject("ANY", SOME.class);
474 fail("The expected exception was not thrown");
476 catch(OAuth2Exception e)
478 log.debug("{}", e.toString());
479 assertEquals("invalid_request", e.getOAuth2ErrorCode());
480 assertFalse(e instanceof GraphApiException);
484 requestFactory.setBody("{\"some filed\":\"some message\"}");
488 clientTemplate.getForObject("ANY", SOME.class);
489 fail("The expected exception was not thrown");
491 catch(OAuth2Exception e)
493 log.debug("{}", e.toString());
494 assertEquals("invalid_request", e.getOAuth2ErrorCode());
495 assertFalse(e instanceof GraphApiException);
499 requestFactory.setBody("{}");
503 clientTemplate.getForObject("ANY", SOME.class);
504 fail("The expected exception was not thrown");
506 catch(OAuth2Exception e)
508 log.debug("{}", e.toString());
509 assertEquals("invalid_request", e.getOAuth2ErrorCode());
510 assertFalse(e instanceof GraphApiException);
514 requestFactory.setBody("");
518 clientTemplate.getForObject("ANY", SOME.class);
519 fail("The expected exception was not thrown");
521 catch(HttpMessageNotReadableException e)
523 // TODO: OAuth2ErrorHandler fails, if body contains no valid JSON!
524 log.debug("{}", e.toString());
532 requestFactory = new MockClientHttpRequestFactory();
533 requestFactory.setStatus(HttpStatus.BAD_REQUEST);
534 requestFactory.addHeader("Content-Type", "application/json");
535 clientTemplate.setRequestFactory(requestFactory);
537 clientTemplate.setErrorHandler(
538 new GraphApiErrorHandler(
539 (OAuth2ErrorHandler)clientTemplate.getErrorHandler()
543 clientTemplate.setAccessTokenProvider(new AccessTokenProvider()
546 public OAuth2AccessToken obtainAccessToken(
547 OAuth2ProtectedResourceDetails details,
548 AccessTokenRequest parameters
551 UserRedirectRequiredException,
552 UserApprovalRequiredException,
553 AccessDeniedException
555 return new OAuth2AccessToken() {
558 public Map<String, Object> getAdditionalInformation()
560 throw new UnsupportedOperationException("Not supported yet.");
564 public Set<String> getScope()
566 throw new UnsupportedOperationException("Not supported yet.");
570 public OAuth2RefreshToken getRefreshToken()
572 throw new UnsupportedOperationException("Not supported yet.");
576 public String getTokenType()
582 public boolean isExpired()
588 public Date getExpiration()
590 throw new UnsupportedOperationException("Not supported yet.");
594 public int getExpiresIn()
596 throw new UnsupportedOperationException("Not supported yet.");
600 public String getValue()
608 public boolean supportsResource(OAuth2ProtectedResourceDetails resource)
614 public OAuth2AccessToken refreshAccessToken(
615 OAuth2ProtectedResourceDetails resource,
616 OAuth2RefreshToken refreshToken,
617 AccessTokenRequest request
620 UserRedirectRequiredException
622 throw new UnsupportedOperationException("Not supported yet.");
626 public boolean supportsRefresh(OAuth2ProtectedResourceDetails resource)