Reputation: 467
I am building a native iOS app that will allow the user to sign into Facebook and 'like' Open Graph objects. I'm trying to write unit tests for my code that does the built-in likes. I created test users in the admin web page of the Facebook app, and what I would like to do in a unit test is sign in as a test user, 'like' an open graph object, check to make sure the number of 'likes' changed, then 'unlike' it.
I'm using the Facebook iOS SDK, and it comes with FBTestSession which I thought would be perfect for my needs. My understanding is that it will randomly grab a test user to sign in as (or create a temporary one if you desire). However, there have been a couple of issues and I don't see any documentation explaining it.
The first issue: it tries to load FacebookSDK-UnitTestConfig.plist from the Documents directory to read the AppID and AppSecret. To fix this issue, I made such a file and I copy it over to the Documents directory at the start of the test. Is this the correct thing to do?
The second issue is that it throws an error when I try to open the test session, with a 400 response code and no helpful message. Looking at the request it tries to make, it seems it is using fql to query for all the test accounts. When I enter the appropriate URL in a web browser
https://graph.facebook.com/fql?access_token=ACCESS_TOKEN&q=SELECT id,access_token FROM test_account WHERE app_id = APP_ID)
I get the error message "(#15) This method is not supported for native apps". But clearly FBTestSession is meant for native apps, so what is going on?
Upvotes: 3
Views: 662
Reputation: 11774
Using FBTestSession is simple, it will create a test user on open, and destroy it on close. After you can use your session like a classic session (internally FBTestSession inherit from FBSession):
FBSession *fbSession = [FBTestSession sessionWithPrivateUserWithPermissions:@[@"publish_stream", @"email"]];
[fbSession openWithCompletionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
// Test here, you can use the session.accessToken attribute
}
I got some trouble with the FacebookSDK-UnitTestConfig.plist
, I think there is something weird in the Facebook SDK, I looked into the code and to build the path to this plist they are looking for the document directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *configFilename = [documentsDirectory stringByAppendingPathComponent:@"FacebookSDK-UnitTestConfig.plist"];
When testing, the document directory is not the same as when we execute an App in the simulator, I print it and it is:
/Users/{user}/Library/Application Support/iPhone Simulator/{iOS_version}/Documents/
If you put your plist there that should work. You plist file have a dictionary structure and contains 2 keys: FacebookAppID
and FacebookAppSecret
.
Upvotes: 1
Reputation: 3236
As long as it is true, and thus doesn't present a security risk to your application, change the App Secret in Client value in the Advanced settings for your Facebook app to No. That should let you use your app access token to make requests for test user information.
If your app does include the app secret in the distributed native code, it could be found out and the security of your app compromised as a result. I think that's why FB toggles this setting on by default when you have a native app.
Upvotes: 0
Reputation: 551
According to their documentation it is not intended for use in application code:
http://developers.facebook.com/docs/reference/ios/3.0/class/FBTestSession
This method should not be used in application code -- but is useful for creating unit tests that use the Facebook SDK.
Upvotes: 1