FBTestSession.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2010-present Facebook.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "FBSession.h"
  17. #import "FBSDKMacros.h"
  18. #if defined(DEBUG) && !defined(SAFE_TO_USE_FBTESTSESSION)
  19. #define SAFE_TO_USE_FBTESTSESSION
  20. #endif
  21. #if !defined(SAFE_TO_USE_FBTESTSESSION)
  22. #pragma message ("warning: using FBTestSession, which is designed for unit-testing uses only, in non-DEBUG code -- ensure this is what you really want")
  23. #endif
  24. /*!
  25. Consider using this tag to pass to sessionWithSharedUserWithPermissions:uniqueUserTag: when
  26. you need a second unique test user in a test case. Using the same tag each time reduces
  27. the proliferation of test users.
  28. */
  29. FBSDK_EXTERN NSString *kSecondTestUserTag;
  30. /*!
  31. Consider using this tag to pass to sessionWithSharedUserWithPermissions:uniqueUserTag: when
  32. you need a third unique test user in a test case. Using the same tag each time reduces
  33. the proliferation of test users.
  34. */
  35. FBSDK_EXTERN NSString *kThirdTestUserTag;
  36. /*!
  37. @class FBTestSession
  38. @abstract
  39. Implements an FBSession subclass that knows about test users for a particular
  40. application. This should never be used from a real application, but may be useful
  41. for writing unit tests, etc.
  42. @discussion
  43. Facebook allows developers to create test accounts for testing their applications'
  44. Facebook integration (see https://developers.facebook.com/docs/test_users/). This class
  45. simplifies use of these accounts for writing unit tests. It is not designed for use in
  46. production application code.
  47. The main use case for this class is using sessionForUnitTestingWithPermissions:mode:
  48. to create a session for a test user. Two modes are supported. In "shared" mode, an attempt
  49. is made to find an existing test user that has the required permissions and, if it is not
  50. currently in use by another FBTestSession, just use that user. If no such user is available,
  51. a new one is created with the required permissions. In "private" mode, designed for
  52. scenarios which require a new user in a known clean state, a new test user will always be
  53. created, and it will be automatically deleted when the FBTestSession is closed.
  54. Note that the shared test user functionality depends on a naming convention for the test users.
  55. It is important that any testing of functionality which will mutate the permissions for a
  56. test user NOT use a shared test user, or this scheme will break down. If a shared test user
  57. seems to be in an invalid state, it can be deleted manually via the Web interface at
  58. https://developers.facebook.com/apps/APP_ID/permissions?role=test+users.
  59. */
  60. @interface FBTestSession : FBSession
  61. /// The app access token (composed of app ID and secret) to use for accessing test users.
  62. @property (readonly, copy) NSString *appAccessToken;
  63. /// The ID of the test user associated with this session.
  64. @property (readonly, copy) NSString *testUserID;
  65. /// The name of the test user associated with this session.
  66. @property (readonly, copy) NSString *testUserName;
  67. /// The App ID of the test app as configured in the plist.
  68. @property (readonly, copy) NSString *testAppID;
  69. /// The App Secret of the test app as configured in the plist.
  70. @property (readonly, copy) NSString *testAppSecret;
  71. // Defaults to NO. If set to YES, reauthorize calls will fail with a nil token
  72. // as if the user had cancelled it reauthorize.
  73. @property (assign) BOOL disableReauthorize;
  74. /*!
  75. @abstract
  76. Constructor helper to create a session for use in unit tests
  77. @discussion
  78. This method creates a session object which uses a shared test user with the right permissions,
  79. creating one if necessary on open (but not deleting it on close, so it can be re-used in later
  80. tests). Calling this method multiple times may return sessions with the same user. If this is not
  81. desired, use the variant sessionWithSharedUserWithPermissions:uniqueUserTag:.
  82. This method should not be used in application code -- but is useful for creating unit tests
  83. that use the Facebook SDK.
  84. @param permissions array of strings naming permissions to authorize; nil indicates
  85. a common default set of permissions should be used for unit testing
  86. */
  87. + (instancetype)sessionWithSharedUserWithPermissions:(NSArray *)permissions;
  88. /*!
  89. @abstract
  90. Constructor helper to create a session for use in unit tests
  91. @discussion
  92. This method creates a session object which uses a shared test user with the right permissions,
  93. creating one if necessary on open (but not deleting it on close, so it can be re-used in later
  94. tests).
  95. This method should not be used in application code -- but is useful for creating unit tests
  96. that use the Facebook SDK.
  97. @param permissions array of strings naming permissions to authorize; nil indicates
  98. a common default set of permissions should be used for unit testing
  99. @param uniqueUserTag a string which will be used to make this user unique among other
  100. users with the same permissions. Useful for tests which require two or more users to interact
  101. with each other, and which therefore must have sessions associated with different users. For
  102. this case, consider using kSecondTestUserTag and kThirdTestUserTag so these users can be shared
  103. with other, similar, tests.
  104. */
  105. + (instancetype)sessionWithSharedUserWithPermissions:(NSArray *)permissions
  106. uniqueUserTag:(NSString *)uniqueUserTag;
  107. /*!
  108. @abstract
  109. Constructor helper to create a session for use in unit tests
  110. @discussion
  111. This method creates a session object which creates a test user on open, and destroys the user on
  112. close; This method should not be used in application code -- but is useful for creating unit tests
  113. that use the Facebook SDK.
  114. @param permissions array of strings naming permissions to authorize; nil indicates
  115. a common default set of permissions should be used for unit testing
  116. */
  117. + (instancetype)sessionWithPrivateUserWithPermissions:(NSArray *)permissions;
  118. @end