FacebookAuthTests.m 8.8 KB

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