FacebookTests.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright 2020 Google LLC
  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 FirebaseAuth
  17. import Foundation
  18. import GTMSessionFetcherCore
  19. import XCTest
  20. // TODO(#10752) - Update and fix the Facebook login Sample app and tests.
  21. //
  22. // class FacebookTests: TestsBase {
  23. // func testSignInWithFacebook() throws {
  24. // let auth = Auth.auth()
  25. // let userInfoDict = createFacebookTestingAccount()
  26. // let facebookAccessToken: String = try XCTUnwrap(userInfoDict["access_token"] as? String)
  27. // let facebookAccountID: String = try XCTUnwrap(userInfoDict["id"] as? String)
  28. // let credential = FacebookAuthProvider.credential(withAccessToken: facebookAccessToken)
  29. // let expectation = self.expectation(description: "Signing in with Facebook finished.")
  30. // auth.signIn(with: credential) { result, error in
  31. // if let error {
  32. // XCTFail("Signing in with Facebook had error: \(error)")
  33. // } else {
  34. // XCTAssertEqual(auth.currentUser?.displayName, Credentials.kFacebookUserName)
  35. // }
  36. // expectation.fulfill()
  37. // }
  38. // waitForExpectations(timeout: TestsBase.kExpectationsTimeout)
  39. //
  40. // // Clean up the created Firebase/Facebook user for future runs.
  41. // deleteCurrentUser()
  42. // deleteFacebookTestingAccountbyID(facebookAccountID)
  43. // }
  44. //
  45. // @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  46. // func testSignInWithFacebookAsync() async throws {
  47. // let auth = Auth.auth()
  48. // let userInfoDict = try await createFacebookTestingAccountAsync()
  49. // let facebookAccessToken: String = try XCTUnwrap(userInfoDict["access_token"] as? String)
  50. // let facebookAccountID: String = try XCTUnwrap(userInfoDict["id"] as? String)
  51. // let credential = FacebookAuthProvider.credential(withAccessToken: facebookAccessToken)
  52. //
  53. // try await auth.signIn(with: credential)
  54. // XCTAssertEqual(auth.currentUser?.displayName, Credentials.kFacebookUserName)
  55. //
  56. // // Clean up the created Firebase/Facebook user for future runs.
  57. // try await deleteCurrentUserAsync()
  58. // try await deleteFacebookTestingAccountbyIDAsync(facebookAccountID)
  59. // }
  60. //
  61. // func testLinkAnonymousAccountToFacebookAccount() throws {
  62. // let auth = Auth.auth()
  63. // signInAnonymously()
  64. // let userInfoDict = createFacebookTestingAccount()
  65. // let facebookAccessToken: String = try XCTUnwrap(userInfoDict["access_token"] as? String)
  66. // let facebookAccountID: String = try XCTUnwrap(userInfoDict["id"] as? String)
  67. // let credential = FacebookAuthProvider.credential(withAccessToken: facebookAccessToken)
  68. // let expectation = self.expectation(description: "Facebook linking finished.")
  69. // auth.currentUser?.link(with: credential, completion: { result, error in
  70. // if let error {
  71. // XCTFail("Link to Firebase error: \(error)")
  72. // } else {
  73. // guard let providers = (auth.currentUser?.providerData) else {
  74. // XCTFail("Failed to get providers")
  75. // return
  76. // }
  77. // XCTAssertEqual(providers.count, 1)
  78. // XCTAssertEqual(providers[0].providerID, "facebook.com")
  79. // }
  80. // expectation.fulfill()
  81. // })
  82. // waitForExpectations(timeout: TestsBase.kExpectationsTimeout)
  83. //
  84. // // Clean up the created Firebase/Facebook user for future runs.
  85. // deleteCurrentUser()
  86. // deleteFacebookTestingAccountbyID(facebookAccountID)
  87. // }
  88. //
  89. // @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  90. // func testLinkAnonymousAccountToFacebookAccountAsync() async throws {
  91. // let auth = Auth.auth()
  92. // try await signInAnonymouslyAsync()
  93. // let userInfoDict = try await createFacebookTestingAccountAsync()
  94. // let facebookAccessToken: String = try XCTUnwrap(userInfoDict["access_token"] as? String)
  95. // let facebookAccountID: String = try XCTUnwrap(userInfoDict["id"] as? String)
  96. // let credential = FacebookAuthProvider.credential(withAccessToken: facebookAccessToken)
  97. // try await auth.currentUser?.link(with: credential)
  98. // guard let providers = (auth.currentUser?.providerData) else {
  99. // XCTFail("Failed to get providers")
  100. // return
  101. // }
  102. // XCTAssertEqual(providers.count, 1)
  103. // XCTAssertEqual(providers[0].providerID, "facebook.com")
  104. //
  105. // // Clean up the created Firebase/Facebook user for future runs.
  106. // try await deleteCurrentUserAsync()
  107. // try await deleteFacebookTestingAccountbyIDAsync(facebookAccountID)
  108. // }
  109. //
  110. // /// Creates a Facebook testing account using Facebook Graph API and return a dictionary that
  111. // /// constrains "id", "access_token", "login_url", "email" and "password" of the created account.
  112. // func createFacebookTestingAccount() -> [String: Any] {
  113. // var returnValue: [String: Any] = [:]
  114. // let urltoCreateTestUser = "https://graph.facebook.com/\(Credentials.kFacebookAppID)" +
  115. // "/accounts/test-users"
  116. // let bodyString = "installed=true&name=\(Credentials.kFacebookUserName)" +
  117. // "&permissions=read_stream&access_token=\(Credentials.kFacebookAppAccessToken)"
  118. // let postData = bodyString.data(using: .utf8)
  119. // let service = GTMSessionFetcherService()
  120. // let fetcher = service.fetcher(withURLString: urltoCreateTestUser)
  121. // fetcher.bodyData = postData
  122. // fetcher.setRequestValue("text/plain", forHTTPHeaderField: "Content-Type")
  123. // let expectation = self.expectation(description: "Creating Facebook account finished.")
  124. // fetcher.beginFetch { data, error in
  125. // if let error {
  126. // let error = error as NSError
  127. // if let message = String(data: error.userInfo["data"] as! Data, encoding: .utf8) {
  128. // // May get transient errors here for too many api calls when tests run frequently.
  129. // XCTFail("Creating Facebook account failed with error: \(message)")
  130. // } else {
  131. // XCTFail("Creating Facebook account failed with error: \(error)")
  132. // }
  133. // } else {
  134. // do {
  135. // let data = try XCTUnwrap(data)
  136. // returnValue = try JSONSerialization.jsonObject(with: data, options: [])
  137. // as! [String: Any]
  138. // } catch {
  139. // XCTFail("Failed to unwrap data \(error)")
  140. // }
  141. // }
  142. // expectation.fulfill()
  143. // }
  144. // waitForExpectations(timeout: TestsBase.kExpectationsTimeout)
  145. // return returnValue
  146. // }
  147. //
  148. // @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  149. // /// Creates a Facebook testing account using Facebook Graph API and return a dictionary that
  150. // /// constains "id", "access_token", "login_url", "email" and "password" of the created
  151. // /// account.
  152. // func createFacebookTestingAccountAsync() async throws -> [String: Any] {
  153. // let urltoCreateTestUser = "https://graph.facebook.com/\(Credentials.kFacebookAppID)" +
  154. // "/accounts/test-users"
  155. // let bodyString = "installed=true&name=\(Credentials.kFacebookUserName)" +
  156. // "&permissions=read_stream&access_token=\(Credentials.kFacebookAppAccessToken)"
  157. // let postData = bodyString.data(using: .utf8)
  158. // let service = GTMSessionFetcherService()
  159. // let fetcher = service.fetcher(withURLString: urltoCreateTestUser)
  160. // fetcher.bodyData = postData
  161. // fetcher.setRequestValue("text/plain", forHTTPHeaderField: "Content-Type")
  162. // let data = try await fetcher.beginFetch()
  163. // guard let returnValue = try JSONSerialization.jsonObject(with: data, options: [])
  164. // as? [String: Any] else {
  165. // XCTFail("Failed to serialize userInfo as a Dictionary")
  166. // fatalError()
  167. // }
  168. // return returnValue
  169. // }
  170. //
  171. // // ** Delete a Facebook testing account by account Id using Facebook Graph API. */
  172. // func deleteFacebookTestingAccountbyID(_ accountID: String) {
  173. // let urltoDeleteTestUser = "https://graph.facebook.com/\(accountID)"
  174. // let bodyString = "method=delete&access_token=\(Credentials.kFacebookAppAccessToken)"
  175. // let postData = bodyString.data(using: .utf8)
  176. // let service = GTMSessionFetcherService()
  177. // let fetcher = service.fetcher(withURLString: urltoDeleteTestUser)
  178. // fetcher.bodyData = postData
  179. // fetcher.setRequestValue("text/plain", forHTTPHeaderField: "Content-Type")
  180. // let expectation = self.expectation(description: "Deleting Facebook account finished.")
  181. // fetcher.beginFetch { data, error in
  182. // if let error {
  183. // XCTFail("Deleting Facebook account failed with error: \(error)")
  184. // }
  185. // expectation.fulfill()
  186. // }
  187. // waitForExpectations(timeout: TestsBase.kExpectationsTimeout)
  188. // }
  189. //
  190. // @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  191. // // ** Delete a Facebook testing account by account Id using Facebook Graph API. */
  192. // func deleteFacebookTestingAccountbyIDAsync(_ accountID: String) async throws {
  193. // let urltoDeleteTestUser = "https://graph.facebook.com/\(accountID)"
  194. // let bodyString = "method=delete&access_token=\(Credentials.kFacebookAppAccessToken)"
  195. // let postData = bodyString.data(using: .utf8)
  196. // let service = GTMSessionFetcherService()
  197. // let fetcher = service.fetcher(withURLString: urltoDeleteTestUser)
  198. // fetcher.bodyData = postData
  199. // fetcher.setRequestValue("text/plain", forHTTPHeaderField: "Content-Type")
  200. // try await fetcher.beginFetch()
  201. // }
  202. // }