FacebookTests.swift 9.9 KB

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