FacebookAuthTests.m 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright 2019 Google
  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 <XCTest/XCTest.h>
  17. @import FirebaseAuth;
  18. #import "FIRAuthApiTestsBase.h"
  19. /** Facebook app access token that will be used for Facebook Graph API, which is different from
  20. * account access token.
  21. */
  22. static NSString *const kFacebookAppAccessToken = KFACEBOOK_APP_ACCESS_TOKEN;
  23. /** Facebook app ID that will be used for Facebook Graph API. */
  24. static NSString *const kFacebookAppID = KFACEBOOK_APP_ID;
  25. static NSString *const kFacebookGraphApiAuthority = @"graph.facebook.com";
  26. static NSString *const kFacebookTestAccountName = KFACEBOOK_USER_NAME;
  27. @interface FacebookAuthTests : FIRAuthApiTestsBase
  28. @end
  29. @implementation FacebookAuthTests
  30. // TODO(#10752) - Update and fix the Facebook login Sample app and tests.
  31. - (void)SKIPtestSignInWithFacebook {
  32. FIRAuth *auth = [FIRAuth auth];
  33. if (!auth) {
  34. XCTFail(@"Could not obtain auth object.");
  35. }
  36. NSDictionary *userInfoDict = [self createFacebookTestingAccount];
  37. NSString *facebookAccessToken = userInfoDict[@"access_token"];
  38. NSLog(@"Facebook testing account access token is: %@", facebookAccessToken);
  39. NSString *facebookAccountId = userInfoDict[@"id"];
  40. NSLog(@"Facebook testing account id is: %@", facebookAccountId);
  41. FIRAuthCredential *credential =
  42. [FIRFacebookAuthProvider credentialWithAccessToken:facebookAccessToken];
  43. XCTestExpectation *expectation = [self expectationWithDescription:@"Facebook sign-in finished."];
  44. [auth signInWithCredential:credential
  45. completion:^(FIRAuthDataResult *result, NSError *error) {
  46. if (error) {
  47. NSLog(@"Facebook sign in error: %@", error);
  48. }
  49. [expectation fulfill];
  50. }];
  51. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  52. handler:^(NSError *error) {
  53. if (error != nil) {
  54. XCTFail(@"Failed to wait for expectations "
  55. @"in Facebook sign in. Error: %@",
  56. error.localizedDescription);
  57. }
  58. }];
  59. XCTAssertEqualObjects(auth.currentUser.displayName, kFacebookTestAccountName);
  60. // Clean up the created Firebase/Facebook user for future runs.
  61. [self deleteCurrentUser];
  62. [self deleteFacebookTestingAccountbyId:facebookAccountId];
  63. }
  64. // TODO(#10752) - Update and fix the Facebook login Sample app and tests.
  65. - (void)SKIPtestLinkAnonymousAccountToFacebookAccount {
  66. FIRAuth *auth = [FIRAuth auth];
  67. if (!auth) {
  68. XCTFail(@"Could not obtain auth object.");
  69. }
  70. [self signInAnonymously];
  71. NSDictionary *userInfoDict = [self createFacebookTestingAccount];
  72. NSString *facebookAccessToken = userInfoDict[@"access_token"];
  73. NSLog(@"Facebook testing account access token is: %@", facebookAccessToken);
  74. NSString *facebookAccountId = userInfoDict[@"id"];
  75. NSLog(@"Facebook testing account id is: %@", facebookAccountId);
  76. FIRAuthCredential *credential =
  77. [FIRFacebookAuthProvider credentialWithAccessToken:facebookAccessToken];
  78. XCTestExpectation *expectation = [self expectationWithDescription:@"Facebook linking finished."];
  79. [auth.currentUser linkWithCredential:credential
  80. completion:^(FIRAuthDataResult *result, NSError *error) {
  81. if (error) {
  82. NSLog(@"Link to Facebook error: %@", error);
  83. }
  84. [expectation fulfill];
  85. }];
  86. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  87. handler:^(NSError *error) {
  88. if (error != nil) {
  89. XCTFail(@"Failed to wait for expectations "
  90. @"in linking to Facebook. Error: %@",
  91. error.localizedDescription);
  92. }
  93. }];
  94. NSArray<id<FIRUserInfo>> *providerData = auth.currentUser.providerData;
  95. XCTAssertEqual([providerData count], 1);
  96. XCTAssertEqualObjects([providerData[0] providerID], @"facebook.com");
  97. // Clean up the created Firebase/Facebook user for future runs.
  98. [self deleteCurrentUser];
  99. [self deleteFacebookTestingAccountbyId:facebookAccountId];
  100. }
  101. /** Creates a Facebook testing account using Facebook Graph API and return a dictionary that
  102. * constains "id", "access_token", "login_url", "email" and "password" of the created account.
  103. */
  104. - (NSDictionary *)createFacebookTestingAccount {
  105. // Build the URL.
  106. NSString *urltoCreateTestUser =
  107. [NSString stringWithFormat:@"https://%@/%@/accounts/test-users", kFacebookGraphApiAuthority,
  108. kFacebookAppID];
  109. // Build the POST request.
  110. NSString *bodyString =
  111. [NSString stringWithFormat:@"installed=true&name=%@&permissions=read_stream&access_token=%@",
  112. kFacebookTestAccountName, kFacebookAppAccessToken];
  113. NSData *postData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
  114. GTMSessionFetcherService *service = [[GTMSessionFetcherService alloc] init];
  115. GTMSessionFetcher *fetcher = [service fetcherWithURLString:urltoCreateTestUser];
  116. fetcher.bodyData = postData;
  117. [fetcher setRequestValue:@"text/plain" forHTTPHeaderField:@"Content-Type"];
  118. XCTestExpectation *expectation =
  119. [self expectationWithDescription:@"Creating Facebook account finished."];
  120. __block NSData *data = nil;
  121. [fetcher beginFetchWithCompletionHandler:^(NSData *receivedData, NSError *error) {
  122. if (error) {
  123. NSLog(@"Creating Facebook account finished with error: %@", error);
  124. return;
  125. }
  126. data = receivedData;
  127. [expectation fulfill];
  128. }];
  129. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  130. handler:^(NSError *error) {
  131. if (error != nil) {
  132. XCTFail(@"Failed to wait for expectations "
  133. @"in creating Facebook account. Error: %@",
  134. error.localizedDescription);
  135. }
  136. }];
  137. // Parses the access token from the JSON data.
  138. NSDictionary *userInfoDict = [NSJSONSerialization JSONObjectWithData:data
  139. options:kNilOptions
  140. error:nil];
  141. return userInfoDict;
  142. }
  143. /** Delete a Facebook testing account by account Id using Facebook Graph API. */
  144. - (void)deleteFacebookTestingAccountbyId:(NSString *)accountId {
  145. // Build the URL.
  146. NSString *urltoDeleteTestUser =
  147. [NSString stringWithFormat:@"https://%@/%@", kFacebookGraphApiAuthority, accountId];
  148. // Build the POST request.
  149. NSString *bodyString =
  150. [NSString stringWithFormat:@"method=delete&access_token=%@", kFacebookAppAccessToken];
  151. NSData *postData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
  152. GTMSessionFetcherService *service = [[GTMSessionFetcherService alloc] init];
  153. GTMSessionFetcher *fetcher = [service fetcherWithURLString:urltoDeleteTestUser];
  154. fetcher.bodyData = postData;
  155. [fetcher setRequestValue:@"text/plain" forHTTPHeaderField:@"Content-Type"];
  156. XCTestExpectation *expectation =
  157. [self expectationWithDescription:@"Deleting Facebook account finished."];
  158. [fetcher beginFetchWithCompletionHandler:^(NSData *receivedData, NSError *error) {
  159. NSString *deleteResult = [[NSString alloc] initWithData:receivedData
  160. encoding:NSUTF8StringEncoding];
  161. NSLog(@"The result of deleting Facebook account is: %@", deleteResult);
  162. if (error) {
  163. NSLog(@"Deleting Facebook account finished with error: %@", error);
  164. }
  165. [expectation fulfill];
  166. }];
  167. [self waitForExpectationsWithTimeout:kExpectationsTimeout
  168. handler:^(NSError *error) {
  169. if (error != nil) {
  170. XCTFail(@"Failed to wait for expectations "
  171. @"in deleting Facebook account. Error: %@",
  172. error.localizedDescription);
  173. }
  174. }];
  175. }
  176. @end