@Test public void unknownPath() { try { mockServer .expect(requestTo("https://graph.facebook.com/me/boguspath")) .andExpect(method(GET)) .andExpect(header("Authorization", "OAuth someAccessToken")) .andRespond( withBadRequest() .body(jsonResource("testdata/error-unknown-path")) .contentType(MediaType.APPLICATION_JSON)); facebook.fetchConnections("me", "boguspath", String.class); fail(); } catch (ResourceNotFoundException e) { assertEquals("Unknown path components: /boguspath", e.getMessage()); } }
@Test public void notAFriend() { try { mockServer .expect(requestTo("https://graph.facebook.com/119297590579/members/100001387295207")) .andExpect(method(POST)) .andExpect(header("Authorization", "OAuth someAccessToken")) .andRespond( withServerError() .body(jsonResource("testdata/error-not-a-friend")) .contentType(MediaType.APPLICATION_JSON)); facebook.friendOperations().addToFriendList("119297590579", "100001387295207"); fail(); } catch (NotAFriendException e) { assertEquals("The member must be a friend of the current user.", e.getMessage()); } }
@Test public void insufficientPrivileges() { try { mockServer .expect(requestTo("https://graph.facebook.com/193482154020832/declined")) .andExpect(method(POST)) .andExpect(header("Authorization", "OAuth someAccessToken")) .andRespond( withStatus(HttpStatus.FORBIDDEN) .body(jsonResource("testdata/error-insufficient-privilege")) .contentType(MediaType.APPLICATION_JSON)); facebook.eventOperations().declineInvitation("193482154020832"); fail(); } catch (InsufficientPermissionException e) { assertEquals("The operation requires 'rsvp_event' permission.", e.getMessage()); assertEquals("rsvp_event", e.getRequiredPermission()); } }
@Test public void tokenInvalid_signedOutOfFacebook_unauthorized() { MockRestServiceServer mockServer = MockRestServiceServer.createServer(facebook.getRestTemplate()); mockServer .expect(requestTo("https://graph.facebook.com/me")) .andExpect(method(GET)) .andRespond( withStatus(HttpStatus.UNAUTHORIZED) .body(jsonResource("testdata/error-invalid-token-signout")) .contentType(MediaType.APPLICATION_JSON)); try { facebook.userOperations().getUserProfile(); fail("Expected RevokedAuthorizationException"); } catch (RevokedAuthorizationException e) { assertEquals(LOGGED_OUT_REVOKATION, e.getMessage()); } }
@Test public void tokenInvalid_passwordChanged_unauthorized() { MockRestServiceServer mockServer = MockRestServiceServer.createServer(facebook.getRestTemplate()); mockServer .expect(requestTo("https://graph.facebook.com/me")) .andExpect(method(GET)) .andRespond( withBadRequest() .body(jsonResource("testdata/error-invalid-token-password")) .contentType(MediaType.APPLICATION_JSON)); try { facebook.userOperations().getUserProfile(); fail("Expected RevokedAuthorizationException"); } catch (RevokedAuthorizationException e) { assertEquals(CHANGED_PASSWORD_REVOKATION, e.getMessage()); } }
@Test public void notTheOwner() { try { mockServer .expect(requestTo("https://graph.facebook.com/1234567890")) .andExpect(method(POST)) .andExpect(header("Authorization", "OAuth someAccessToken")) .andExpect(content().string("method=delete")) .andRespond( withServerError() .body(jsonResource("testdata/error-not-the-owner")) .contentType(MediaType.APPLICATION_JSON)); facebook.friendOperations().deleteFriendList("1234567890"); fail(); } catch (ResourceOwnershipException e) { assertEquals("User must be an owner of the friendlist", e.getMessage()); } }
@Test @Ignore("This doesn't seem to be true anymore...It looks like FB fixed their status code.") public void unknownAlias_HTTP200() { // yes, Facebook really does return this error as HTTP 200 (probably should be 404) try { mockServer .expect(requestTo("https://graph.facebook.com/dummyalias")) .andExpect(method(GET)) .andExpect(header("Authorization", "OAuth someAccessToken")) .andRespond( withSuccess( jsonResource("testdata/error-unknown-alias"), MediaType.APPLICATION_JSON)); facebook.fetchObject("dummyalias", FacebookProfile.class); fail("Expected GraphAPIException when fetching an unknown object alias"); } catch (ResourceNotFoundException e) { assertEquals( "(#803) Some of the aliases you requested do not exist: dummyalias", e.getMessage()); } }
@Test public void userHasntAuthorized() { try { mockServer .expect(requestTo("https://graph.facebook.com/me/feed")) .andExpect(method(POST)) .andExpect(header("Authorization", "OAuth someAccessToken")) .andRespond( withStatus(HttpStatus.FORBIDDEN) .body(jsonResource("testdata/error-user-hasnt-authorized")) .contentType(MediaType.APPLICATION_JSON)); facebook .feedOperations() .postLink( "Test message", new FacebookLink("http://test.com", "Test", "Test this", "Testing some stuff")); fail(); } catch (InsufficientPermissionException e) { assertEquals("Insufficient permission for this operation.", e.getMessage()); } }
@Test(expected = UncategorizedApiException.class) public void htmlErrorResponse() { try { FacebookTemplate facebook = new FacebookTemplate(); // use anonymous FacebookTemplate in this test MockRestServiceServer mockServer = MockRestServiceServer.createServer(facebook.getRestTemplate()); mockServer .expect(requestTo("https://graph.facebook.com/123456/picture?type=normal")) .andExpect(method(GET)) .andRespond( withBadRequest() .body(new ClassPathResource("testdata/error-not-json.html", getClass())) .contentType(MediaType.TEXT_HTML)); facebook.userOperations().getUserProfileImage("123456"); fail("Expected UncategorizedApiException"); } catch (UncategorizedApiException e) { assertTrue(e.getCause() instanceof HttpClientErrorException); throw e; } }