SwiftAPI.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * Copyright 2022 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 XCTest
  18. import FirebaseAuth
  19. import FirebaseCore
  20. /// This file tests public methods and enums. Properties are not included.
  21. /// Each function maps to a public header file.
  22. class AuthAPI_hOnlyTests: XCTestCase {
  23. // Each function corresponds with a public header.
  24. func FIRActionCodeSettings_h() {
  25. let codeSettings = FirebaseAuth.ActionCodeSettings()
  26. codeSettings.setIOSBundleID("abc")
  27. codeSettings.setAndroidPackageName("name", installIfNotAvailable: true, minimumVersion: "10.0")
  28. }
  29. func FIRAuth_h() throws {
  30. let auth = FirebaseAuth.Auth.auth()
  31. let authApp = FirebaseAuth.Auth.auth(app: FirebaseApp.app()!)
  32. let user = auth.currentUser!
  33. auth.updateCurrentUser(user) { _ in
  34. }
  35. authApp.fetchSignInMethods(forEmail: "abc@abc.com") { string, error in
  36. }
  37. auth.signIn(withEmail: "abc@abc.com", password: "password") { result, error in
  38. }
  39. auth.signIn(withEmail: "abc@abc.com", link: "link") { result, error in
  40. }
  41. let provider = OAuthProvider(providerID: "abc")
  42. auth.signIn(with: OAuthProvider(providerID: "abc"), uiDelegate: nil) { result, error in
  43. }
  44. provider.getCredentialWith(nil) { credential, error in
  45. auth.signIn(with: credential!) { result, error in
  46. }
  47. }
  48. auth.signIn(with: OAuthProvider(providerID: "abc"), uiDelegate: nil) { result, error in
  49. }
  50. auth.signInAnonymously { result, error in
  51. }
  52. auth.signIn(withCustomToken: "abc") { result, error in
  53. }
  54. auth.createUser(withEmail: "email", password: "password") { result, error in
  55. }
  56. auth.confirmPasswordReset(withCode: "code", newPassword: "password") { error in
  57. }
  58. auth.checkActionCode("abc") { codeInfo, error in
  59. }
  60. auth.verifyPasswordResetCode("code") { email, error in
  61. }
  62. auth.applyActionCode("code") { error in
  63. }
  64. auth.sendPasswordReset(withEmail: "email") { error in
  65. }
  66. let actionCodeSettings = ActionCodeSettings()
  67. auth.sendPasswordReset(withEmail: "email", actionCodeSettings: actionCodeSettings) { error in
  68. }
  69. auth.sendSignInLink(toEmail: "email", actionCodeSettings: actionCodeSettings) { error in
  70. }
  71. try auth.signOut()
  72. auth.isSignIn(withEmailLink: "link")
  73. let handle = auth.addStateDidChangeListener { auth, user in
  74. }
  75. auth.removeStateDidChangeListener(handle)
  76. auth.addIDTokenDidChangeListener { auth, user in
  77. }
  78. auth.removeIDTokenDidChangeListener(handle)
  79. auth.useAppLanguage()
  80. auth.useEmulator(withHost: "myHost", port: 123)
  81. auth.canHandle(URL(fileURLWithPath: "/my/path"))
  82. auth.setAPNSToken(Data(), type: AuthAPNSTokenType(rawValue: 2)!)
  83. auth.canHandleNotification([:])
  84. try auth.useUserAccessGroup("abc")
  85. let nilUser = try auth.getStoredUser(forAccessGroup: "def")
  86. // If nilUser is not optional, this will raise a compiler error.
  87. // This condition does not need to execute, and may not if prior
  88. // functions throw.
  89. if let value = nilUser {
  90. XCTAssert(true)
  91. }
  92. }
  93. #if compiler(>=5.5.2) && canImport(_Concurrency)
  94. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  95. func FIRAuth_hAsync() async throws {
  96. let auth = FirebaseAuth.Auth.auth()
  97. let user = auth.currentUser!
  98. try await auth.updateCurrentUser(user)
  99. _ = try await auth.fetchSignInMethods(forEmail: "abc@abc.com")
  100. _ = try await auth.signIn(withEmail: "abc@abc.com", password: "password")
  101. _ = try await auth.signIn(withEmail: "abc@abc.com", link: "link")
  102. let provider = OAuthProvider(providerID: "abc")
  103. let credential = try await provider.credential(with: nil)
  104. _ = try await auth.signIn(with: OAuthProvider(providerID: "abc"), uiDelegate: nil)
  105. _ = try await auth.signIn(with: credential)
  106. _ = try await auth.signInAnonymously()
  107. _ = try await auth.signIn(withCustomToken: "abc")
  108. _ = try await auth.createUser(withEmail: "email", password: "password")
  109. _ = try await auth.confirmPasswordReset(withCode: "code", newPassword: "password")
  110. _ = try await auth.checkActionCode("abc")
  111. _ = try await auth.verifyPasswordResetCode("code")
  112. _ = try await auth.applyActionCode("code")
  113. _ = try await auth.sendPasswordReset(withEmail: "email")
  114. let actionCodeSettings = ActionCodeSettings()
  115. _ = try await auth.sendPasswordReset(
  116. withEmail: "email",
  117. actionCodeSettings: actionCodeSettings
  118. )
  119. _ = try await auth.sendSignInLink(toEmail: "email", actionCodeSettings: actionCodeSettings)
  120. }
  121. #endif
  122. func FIRAuthAPNSTokenType_h() {
  123. _ = AuthAPNSTokenType.unknown
  124. _ = AuthAPNSTokenType.sandbox
  125. _ = AuthAPNSTokenType.prod
  126. }
  127. func FIRAuthErrors_h() {
  128. _ = AuthErrorCode.invalidCustomToken
  129. _ = AuthErrorCode.customTokenMismatch
  130. _ = AuthErrorCode.invalidCredential
  131. _ = AuthErrorCode.userDisabled
  132. _ = AuthErrorCode.operationNotAllowed
  133. _ = AuthErrorCode.emailAlreadyInUse
  134. _ = AuthErrorCode.invalidEmail
  135. _ = AuthErrorCode.wrongPassword
  136. _ = AuthErrorCode.tooManyRequests
  137. _ = AuthErrorCode.userNotFound
  138. _ = AuthErrorCode.accountExistsWithDifferentCredential
  139. _ = AuthErrorCode.requiresRecentLogin
  140. _ = AuthErrorCode.providerAlreadyLinked
  141. _ = AuthErrorCode.noSuchProvider
  142. _ = AuthErrorCode.invalidUserToken
  143. _ = AuthErrorCode.networkError
  144. _ = AuthErrorCode.userTokenExpired
  145. _ = AuthErrorCode.invalidAPIKey
  146. _ = AuthErrorCode.userMismatch
  147. _ = AuthErrorCode.credentialAlreadyInUse
  148. _ = AuthErrorCode.weakPassword
  149. _ = AuthErrorCode.appNotAuthorized
  150. _ = AuthErrorCode.expiredActionCode
  151. _ = AuthErrorCode.invalidActionCode
  152. _ = AuthErrorCode.invalidMessagePayload
  153. _ = AuthErrorCode.invalidSender
  154. _ = AuthErrorCode.invalidRecipientEmail
  155. _ = AuthErrorCode.missingEmail
  156. _ = AuthErrorCode.missingIosBundleID
  157. _ = AuthErrorCode.missingAndroidPackageName
  158. _ = AuthErrorCode.unauthorizedDomain
  159. _ = AuthErrorCode.invalidContinueURI
  160. _ = AuthErrorCode.missingContinueURI
  161. _ = AuthErrorCode.missingPhoneNumber
  162. _ = AuthErrorCode.invalidPhoneNumber
  163. _ = AuthErrorCode.missingVerificationCode
  164. _ = AuthErrorCode.invalidVerificationCode
  165. _ = AuthErrorCode.missingVerificationID
  166. _ = AuthErrorCode.invalidVerificationID
  167. _ = AuthErrorCode.missingAppCredential
  168. _ = AuthErrorCode.invalidAppCredential
  169. _ = AuthErrorCode.sessionExpired
  170. _ = AuthErrorCode.quotaExceeded
  171. _ = AuthErrorCode.missingAppToken
  172. _ = AuthErrorCode.notificationNotForwarded
  173. _ = AuthErrorCode.appNotVerified
  174. _ = AuthErrorCode.captchaCheckFailed
  175. _ = AuthErrorCode.webContextAlreadyPresented
  176. _ = AuthErrorCode.webContextCancelled
  177. _ = AuthErrorCode.appVerificationUserInteractionFailure
  178. _ = AuthErrorCode.invalidClientID
  179. _ = AuthErrorCode.webNetworkRequestFailed
  180. _ = AuthErrorCode.webInternalError
  181. _ = AuthErrorCode.webSignInUserInteractionFailure
  182. _ = AuthErrorCode.localPlayerNotAuthenticated
  183. _ = AuthErrorCode.nullUser
  184. _ = AuthErrorCode.dynamicLinkNotActivated
  185. _ = AuthErrorCode.invalidProviderID
  186. _ = AuthErrorCode.tenantIDMismatch
  187. _ = AuthErrorCode.unsupportedTenantOperation
  188. _ = AuthErrorCode.invalidDynamicLinkDomain
  189. _ = AuthErrorCode.rejectedCredential
  190. _ = AuthErrorCode.gameKitNotLinked
  191. _ = AuthErrorCode.secondFactorRequired
  192. _ = AuthErrorCode.missingMultiFactorSession
  193. _ = AuthErrorCode.missingMultiFactorInfo
  194. _ = AuthErrorCode.invalidMultiFactorSession
  195. _ = AuthErrorCode.multiFactorInfoNotFound
  196. _ = AuthErrorCode.adminRestrictedOperation
  197. _ = AuthErrorCode.unverifiedEmail
  198. _ = AuthErrorCode.secondFactorAlreadyEnrolled
  199. _ = AuthErrorCode.maximumSecondFactorCountExceeded
  200. _ = AuthErrorCode.unsupportedFirstFactor
  201. _ = AuthErrorCode.emailChangeNeedsVerification
  202. _ = AuthErrorCode.missingOrInvalidNonce
  203. _ = AuthErrorCode.recaptchaNotEnabled
  204. _ = AuthErrorCode.missingClientIdentifier
  205. _ = AuthErrorCode.keychainError
  206. _ = AuthErrorCode.internalError
  207. _ = AuthErrorCode.malformedJWT
  208. }
  209. func FIRAuthUIDelegate_h() {
  210. class AuthUIImpl: NSObject, AuthUIDelegate {
  211. func present(_ viewControllerToPresent: UIViewController, animated flag: Bool,
  212. completion: (() -> Void)? = nil) {}
  213. func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {}
  214. }
  215. let obj = AuthUIImpl()
  216. obj.present(UIViewController(), animated: true) {}
  217. obj.dismiss(animated: false) {}
  218. }
  219. #if compiler(>=5.5.2) && canImport(_Concurrency)
  220. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  221. func FIRAuthUIDelegate_hAsync() async {
  222. class AuthUIImpl: NSObject, AuthUIDelegate {
  223. func present(_ viewControllerToPresent: UIViewController, animated flag: Bool) async {}
  224. func dismiss(animated flag: Bool) async {}
  225. }
  226. let obj = AuthUIImpl()
  227. await obj.present(UIViewController(), animated: true)
  228. await obj.dismiss(animated: false)
  229. }
  230. #endif
  231. func FIREmailAuthProvider_h() {
  232. _ = EmailAuthProvider.credential(withEmail: "e@email.com", password: "password")
  233. _ = EmailAuthProvider.credential(withEmail: "e@email.com", link: "link")
  234. }
  235. func FIRFacebookAuthProvider_h() {
  236. _ = FacebookAuthProvider.credential(withAccessToken: "token")
  237. }
  238. func FIRFdederatedAuthProvider_h() {
  239. class FederatedAuthImplementation: NSObject, FederatedAuthProvider {
  240. func getCredentialWith(_ UIDelegate: AuthUIDelegate?,
  241. completion: ((AuthCredential?, Error?) -> Void)? = nil) {}
  242. }
  243. let obj = FederatedAuthImplementation()
  244. obj.getCredentialWith(nil) { _, _ in
  245. }
  246. }
  247. #if compiler(>=5.5.2) && canImport(_Concurrency)
  248. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  249. func FIRFedederatedAuthProvider_hAsync() async throws {
  250. class FederatedAuthImplementation: NSObject, FederatedAuthProvider {
  251. func credential(with UIDelegate: AuthUIDelegate?) async throws -> AuthCredential {
  252. return FacebookAuthProvider.credential(withAccessToken: "token")
  253. }
  254. }
  255. let obj = FederatedAuthImplementation()
  256. try await _ = obj.credential(with: nil)
  257. }
  258. #endif
  259. func FIRGameCenterAuthProvider_h() {
  260. GameCenterAuthProvider.getCredential { _, _ in
  261. }
  262. }
  263. #if compiler(>=5.5.2) && canImport(_Concurrency)
  264. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  265. func FIRGameCenterAuthProvider_hAsync() async throws {
  266. _ = try await GameCenterAuthProvider.getCredential()
  267. }
  268. #endif
  269. func FIRGitHubAuthProvider_h() {
  270. _ = GitHubAuthProvider.credential(withToken: "token")
  271. }
  272. func FIRGoogleAuthProvider_h() {
  273. _ = GoogleAuthProvider.credential(withIDToken: "token", accessToken: "aToken")
  274. }
  275. func FIRMultiFactor_h() {
  276. let obj = MultiFactor()
  277. obj.getSessionWithCompletion { _, _ in
  278. }
  279. obj.enroll(with: MultiFactorAssertion(), displayName: "name") { _ in
  280. }
  281. obj.unenroll(with: MultiFactorInfo()) { _ in
  282. }
  283. obj.unenroll(withFactorUID: "uid") { _ in
  284. }
  285. }
  286. #if compiler(>=5.5.2) && canImport(_Concurrency)
  287. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  288. func FIRMultiFactor_hAsync() async throws {
  289. let obj = MultiFactor()
  290. try await obj.session()
  291. try await obj.enroll(with: MultiFactorAssertion(), displayName: "name")
  292. try await obj.unenroll(with: MultiFactorInfo())
  293. try await obj.unenroll(withFactorUID: "uid")
  294. }
  295. #endif
  296. func FIRMultiFactorResolver_h() {
  297. let obj = MultiFactorResolver()
  298. obj.resolveSignIn(with: MultiFactorAssertion()) { _, _ in
  299. }
  300. }
  301. #if compiler(>=5.5.2) && canImport(_Concurrency)
  302. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  303. func FIRMultiFactorResolver_hAsync() async throws {
  304. let obj = MultiFactorResolver()
  305. try await obj.resolveSignIn(with: MultiFactorAssertion())
  306. }
  307. #endif
  308. func FIROAuthProvider_h() {
  309. let provider = OAuthProvider(providerID: "id", auth: FirebaseAuth.Auth.auth())
  310. _ = provider.providerID
  311. _ = OAuthProvider.credential(withProviderID: "id", idToken: "idToden", accessToken: "token")
  312. _ = OAuthProvider.credential(withProviderID: "id", accessToken: "token")
  313. _ = OAuthProvider.credential(withProviderID: "id", idToken: "idToken", rawNonce: "nonce",
  314. accessToken: "token")
  315. _ = OAuthProvider.credential(withProviderID: "id", idToken: "idToken", rawNonce: "nonce")
  316. }
  317. func FIRPhoneAuthProvider_h() {
  318. _ = PhoneAuthProvider.provider()
  319. let provider = PhoneAuthProvider.provider(auth: FirebaseAuth.Auth.auth())
  320. provider.verifyPhoneNumber("123", uiDelegate: nil) { _, _ in
  321. }
  322. provider.verifyPhoneNumber("123", uiDelegate: nil, multiFactorSession: nil) { _, _ in
  323. }
  324. provider.verifyPhoneNumber(
  325. with: PhoneMultiFactorInfo(),
  326. uiDelegate: nil,
  327. multiFactorSession: nil
  328. ) { _, _ in
  329. }
  330. provider.verifyPhoneNumber("123", uiDelegate: nil, multiFactorSession: nil) { _, _ in
  331. }
  332. provider.credential(withVerificationID: "id", verificationCode: "code")
  333. }
  334. #if compiler(>=5.5.2) && canImport(_Concurrency)
  335. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  336. func FIRPhoneAuthProvider_hAsync() async throws {
  337. _ = PhoneAuthProvider.provider()
  338. let provider = PhoneAuthProvider.provider(auth: FirebaseAuth.Auth.auth())
  339. try await provider.verifyPhoneNumber("123", uiDelegate: nil)
  340. try await provider.verifyPhoneNumber("123", uiDelegate: nil, multiFactorSession: nil)
  341. try await provider.verifyPhoneNumber(with: PhoneMultiFactorInfo(), uiDelegate: nil,
  342. multiFactorSession: nil)
  343. try await provider.verifyPhoneNumber("123", uiDelegate: nil, multiFactorSession: nil)
  344. }
  345. #endif
  346. func FIRPhoneMultiFactorGenerator_h() {
  347. let credential = PhoneAuthProvider.provider().credential(withVerificationID: "id",
  348. verificationCode: "code")
  349. PhoneMultiFactorGenerator.assertion(with: credential)
  350. }
  351. func FIRTwitterAuthProvider_h() {
  352. _ = TwitterAuthProvider.credential(withToken: "token", secret: "secret")
  353. }
  354. func FIRUser_h() {
  355. let auth = FirebaseAuth.Auth.auth()
  356. let user = auth.currentUser!
  357. let credential = PhoneAuthProvider.provider().credential(withVerificationID: "id",
  358. verificationCode: "code")
  359. let provider = PhoneAuthProvider.provider(auth: FirebaseAuth.Auth.auth())
  360. user.updateEmail(to: "email") { _ in
  361. }
  362. user.updatePassword(to: "password") { _ in
  363. }
  364. user.updatePhoneNumber(credential) { _ in
  365. }
  366. let changeRequest = user.createProfileChangeRequest()
  367. user.reload { _ in
  368. }
  369. user.reauthenticate(with: credential) { _, _ in
  370. }
  371. user.reauthenticate(with: provider as! FederatedAuthProvider, uiDelegate: nil)
  372. user.getIDTokenResult { _, _ in
  373. }
  374. user.getIDTokenResult(forcingRefresh: true) { _, _ in
  375. }
  376. user.getIDTokenResult { _, _ in
  377. }
  378. user.getIDTokenForcingRefresh(true) { _, _ in
  379. }
  380. user.link(with: credential) { _, _ in
  381. }
  382. user.link(with: provider as! FederatedAuthProvider, uiDelegate: nil) { _, _ in
  383. }
  384. user.unlink(fromProvider: "abc") { _, _ in
  385. }
  386. user.sendEmailVerification { _ in
  387. }
  388. user.sendEmailVerification(with: ActionCodeSettings()) { _ in
  389. }
  390. user.delete { _ in
  391. }
  392. user.sendEmailVerification(beforeUpdatingEmail: "email") { _ in
  393. }
  394. user.sendEmailVerification(
  395. beforeUpdatingEmail: "email",
  396. actionCodeSettings: ActionCodeSettings()
  397. ) { _ in
  398. }
  399. changeRequest.commitChanges { _ in
  400. }
  401. }
  402. #if compiler(>=5.5.2) && canImport(_Concurrency)
  403. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  404. func FIRUser_hAsync() async throws {
  405. let auth = FirebaseAuth.Auth.auth()
  406. let user = auth.currentUser!
  407. let credential = PhoneAuthProvider.provider().credential(withVerificationID: "id",
  408. verificationCode: "code")
  409. let provider = PhoneAuthProvider.provider(auth: FirebaseAuth.Auth.auth())
  410. try await user.updateEmail(to: "email")
  411. try await user.updatePassword(to: "password")
  412. try await user.updatePhoneNumber(credential)
  413. let changeRequest = user.createProfileChangeRequest()
  414. try await user.reload()
  415. try await user.reauthenticate(with: credential)
  416. try await user.reauthenticate(with: provider as! FederatedAuthProvider, uiDelegate: nil)
  417. try await user.getIDTokenResult()
  418. try await user.getIDTokenResult(forcingRefresh: true)
  419. try await user.getIDTokenResult()
  420. try await user.link(with: credential)
  421. try await user.link(with: provider as! FederatedAuthProvider, uiDelegate: nil)
  422. try await user.unlink(fromProvider: "abc")
  423. try await user.sendEmailVerification()
  424. try await user.sendEmailVerification(with: ActionCodeSettings())
  425. try await user.delete()
  426. try await user.sendEmailVerification(beforeUpdatingEmail: "email")
  427. try await user.sendEmailVerification(
  428. beforeUpdatingEmail: "email",
  429. actionCodeSettings: ActionCodeSettings()
  430. )
  431. try await changeRequest.commitChanges()
  432. }
  433. #endif
  434. }