|
| 1 | +package org.springframework.social.facebook.itest; |
| 2 | + |
| 3 | +import org.junit.After; |
| 4 | +import org.junit.Before; |
| 5 | +import org.springframework.http.ResponseEntity; |
| 6 | +import org.springframework.social.facebook.api.Facebook; |
| 7 | +import org.springframework.social.facebook.api.impl.FacebookTemplate; |
| 8 | +import org.springframework.social.facebook.connect.FacebookServiceProvider; |
| 9 | +import org.springframework.social.oauth2.AccessGrant; |
| 10 | +import org.springframework.util.LinkedMultiValueMap; |
| 11 | +import org.springframework.util.MultiValueMap; |
| 12 | + |
| 13 | +public abstract class AbstractFacebookITest { |
| 14 | + |
| 15 | + public static final String USER_EXTENDED_PERMISSIONS = "user_about_me,user_activities,user_birthday,user_checkins,user_education_history,user_events,user_groups,user_hometown,user_interests,user_likes,user_location,user_notes,user_photos,user_questions,user_relationships,user_relationship_details,user_religion_politics,user_status,user_subscriptions,user_videos,user_website,user_work_history"; |
| 16 | + public static final String FRIENDS_EXTENDED_PERMISSIONS = "friends_about_me,friends_activities,friends_birthday,friends_checkins,friends_education_history,friends_events,friends_groups,friends_hometown,friends_interests,friends_likes,friends_location,friends_notes,friends_photos,friends_questions,friends_relationships,friends_relationship_details,friends_religion_politics,friends_status,friends_subscriptions,friends_videos,friends_website,friends_work_history"; |
| 17 | + public static final String EXTENDED_READ_PERMISSIONS = "read_friendlists,read_insights,read_mailbox,read_requests,read_stream,xmpp_login,user_online_presence,friends_online_presence"; |
| 18 | + public static final String EXTENDED_PUBLISH_PERMISSIONS = "create_event,manage_friendlists,manage_notifications,publish_actions,publish_stream,rsvp_event"; |
| 19 | + public static final String USER_OPEN_GRAPH_PERMISSIONS = "publish_actions,user_actions.music,user_actions.news,user_actions.video,user_games_activity"; |
| 20 | + public static final String FRIENDS_OPEN_GRAPH_PERMISSIONS = "friends_actions.music,friends_actions.news,friends_actions.video,friends_games_activity"; |
| 21 | + public static final String ALL_PERMISSIONS = "email" + "," + USER_EXTENDED_PERMISSIONS + "," + FRIENDS_EXTENDED_PERMISSIONS + "," + EXTENDED_READ_PERMISSIONS + "," + EXTENDED_PUBLISH_PERMISSIONS; |
| 22 | + |
| 23 | + private Facebook CLIENT_FB; |
| 24 | + |
| 25 | + protected abstract AppCredentials getAppCredentials(); |
| 26 | + |
| 27 | + protected TestUser testUser; |
| 28 | + |
| 29 | + protected FacebookTemplate fb; |
| 30 | + |
| 31 | + @Before |
| 32 | + public void setup() { |
| 33 | + AppCredentials app = getAppCredentials(); |
| 34 | + FacebookServiceProvider sp = new FacebookServiceProvider(app.getAppId(), app.getAppSecret(), app.getAppNamespace()); |
| 35 | + AccessGrant clientGrant = sp.getOAuthOperations().authenticateClient(); |
| 36 | + this.CLIENT_FB = new FacebookTemplate(clientGrant.getAccessToken()); |
| 37 | + this.testUser = createTestUser("Jack Diamond", ALL_PERMISSIONS); |
| 38 | + this.fb = new FacebookTemplate(this.testUser.getAccessToken()); |
| 39 | + } |
| 40 | + |
| 41 | + @After |
| 42 | + public void after() { |
| 43 | + deleteTestUser(testUser); |
| 44 | + } |
| 45 | + |
| 46 | + protected TestUser createTestUser(String name, String permissions) { |
| 47 | + MultiValueMap<String, Object> req = new LinkedMultiValueMap<String, Object>(); |
| 48 | + req.set("name", name); |
| 49 | + req.set("installed", "true"); |
| 50 | + req.set("permissions", permissions); |
| 51 | + |
| 52 | + ResponseEntity<TestUser> entity = CLIENT_FB.restOperations().postForEntity("https://graph.facebook.com/{appId}/accounts/test-users", req, TestUser.class, getAppCredentials().getAppId()); |
| 53 | + return entity.getBody(); |
| 54 | + } |
| 55 | + |
| 56 | + protected void deleteTestUser(TestUser testUser) { |
| 57 | + CLIENT_FB.restOperations().delete("https://graph.facebook.com/{userId}", testUser.getId()); |
| 58 | + } |
| 59 | + |
| 60 | + static class AppCredentials { |
| 61 | + private final String appId; |
| 62 | + private final String appSecret; |
| 63 | + private final String appNamespace; |
| 64 | + |
| 65 | + public AppCredentials(String appId, String appSecret, String appNamespace) { |
| 66 | + this.appId = appId; |
| 67 | + this.appSecret = appSecret; |
| 68 | + this.appNamespace = appNamespace; |
| 69 | + } |
| 70 | + |
| 71 | + public String getAppId() { |
| 72 | + return appId; |
| 73 | + } |
| 74 | + |
| 75 | + public String getAppSecret() { |
| 76 | + return appSecret; |
| 77 | + } |
| 78 | + |
| 79 | + public String getAppNamespace() { |
| 80 | + return appNamespace; |
| 81 | + } |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments