SwiftAPI.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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.missingClientIdentifier
  204. _ = AuthErrorCode.keychainError
  205. _ = AuthErrorCode.internalError
  206. _ = AuthErrorCode.malformedJWT
  207. }
  208. func FIRAuthUIDelegate_h() {
  209. class AuthUIImpl: NSObject, AuthUIDelegate {
  210. func present(_ viewControllerToPresent: UIViewController, animated flag: Bool,
  211. completion: (() -> Void)? = nil) {}
  212. func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {}
  213. }
  214. let obj = AuthUIImpl()
  215. obj.present(UIViewController(), animated: true) {}
  216. obj.dismiss(animated: false) {}
  217. }
  218. #if compiler(>=5.5.2) && canImport(_Concurrency)
  219. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  220. func FIRAuthUIDelegate_hAsync() async {
  221. class AuthUIImpl: NSObject, AuthUIDelegate {
  222. func present(_ viewControllerToPresent: UIViewController, animated flag: Bool) async {}
  223. func dismiss(animated flag: Bool) async {}
  224. }
  225. let obj = AuthUIImpl()
  226. await obj.present(UIViewController(), animated: true)
  227. await obj.dismiss(animated: false)
  228. }
  229. #endif
  230. func FIREmailAuthProvider_h() {
  231. _ = EmailAuthProvider.credential(withEmail: "e@email.com", password: "password")
  232. _ = EmailAuthProvider.credential(withEmail: "e@email.com", link: "link")
  233. }
  234. func FIRFacebookAuthProvider_h() {
  235. _ = FacebookAuthProvider.credential(withAccessToken: "token")
  236. }
  237. func FIRFdederatedAuthProvider_h() {
  238. class FederatedAuthImplementation: NSObject, FederatedAuthProvider {
  239. func getCredentialWith(_ UIDelegate: AuthUIDelegate?,
  240. completion: ((AuthCredential?, Error?) -> Void)? = nil) {}
  241. }
  242. let obj = FederatedAuthImplementation()
  243. obj.getCredentialWith(nil) { _, _ in
  244. }
  245. }
  246. #if compiler(>=5.5.2) && canImport(_Concurrency)
  247. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  248. func FIRFedederatedAuthProvider_hAsync() async throws {
  249. class FederatedAuthImplementation: NSObject, FederatedAuthProvider {
  250. func credential(with UIDelegate: AuthUIDelegate?) async throws -> AuthCredential {
  251. return FacebookAuthProvider.credential(withAccessToken: "token")
  252. }
  253. }
  254. let obj = FederatedAuthImplementation()
  255. try await _ = obj.credential(with: nil)
  256. }
  257. #endif
  258. func FIRGameCenterAuthProvider_h() {
  259. GameCenterAuthProvider.getCredential { _, _ in
  260. }
  261. }
  262. #if compiler(>=5.5.2) && canImport(_Concurrency)
  263. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  264. func FIRGameCenterAuthProvider_hAsync() async throws {
  265. _ = try await GameCenterAuthProvider.getCredential()
  266. }
  267. #endif
  268. func FIRGitHubAuthProvider_h() {
  269. _ = GitHubAuthProvider.credential(withToken: "token")
  270. }
  271. func FIRGoogleAuthProvider_h() {
  272. _ = GoogleAuthProvider.credential(withIDToken: "token", accessToken: "aToken")
  273. }
  274. func FIRMultiFactor_h() {
  275. let obj = MultiFactor()
  276. obj.getSessionWithCompletion { _, _ in
  277. }
  278. obj.enroll(with: MultiFactorAssertion(), displayName: "name") { _ in
  279. }
  280. obj.unenroll(with: MultiFactorInfo()) { _ in
  281. }
  282. obj.unenroll(withFactorUID: "uid") { _ in
  283. }
  284. }
  285. #if compiler(>=5.5.2) && canImport(_Concurrency)
  286. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  287. func FIRMultiFactor_hAsync() async throws {
  288. let obj = MultiFactor()
  289. try await obj.session()
  290. try await obj.enroll(with: MultiFactorAssertion(), displayName: "name")
  291. try await obj.unenroll(with: MultiFactorInfo())
  292. try await obj.unenroll(withFactorUID: "uid")
  293. }
  294. #endif
  295. func FIRMultiFactorResolver_h() {
  296. let obj = MultiFactorResolver()
  297. obj.resolveSignIn(with: MultiFactorAssertion()) { _, _ in
  298. }
  299. }
  300. #if compiler(>=5.5.2) && canImport(_Concurrency)
  301. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  302. func FIRMultiFactorResolver_hAsync() async throws {
  303. let obj = MultiFactorResolver()
  304. try await obj.resolveSignIn(with: MultiFactorAssertion())
  305. }
  306. #endif
  307. func FIROAuthProvider_h() {
  308. let provider = OAuthProvider(providerID: "id", auth: FirebaseAuth.Auth.auth())
  309. _ = provider.providerID
  310. _ = OAuthProvider.credential(withProviderID: "id", idToken: "idToden", accessToken: "token")
  311. _ = OAuthProvider.credential(withProviderID: "id", accessToken: "token")
  312. _ = OAuthProvider.credential(withProviderID: "id", idToken: "idToken", rawNonce: "nonce",
  313. accessToken: "token")
  314. _ = OAuthProvider.credential(withProviderID: "id", idToken: "idToken", rawNonce: "nonce")
  315. }
  316. func FIRPhoneAuthProvider_h() {
  317. _ = PhoneAuthProvider.provider()
  318. let provider = PhoneAuthProvider.provider(auth: FirebaseAuth.Auth.auth())
  319. provider.verifyPhoneNumber("123", uiDelegate: nil) { _, _ in
  320. }
  321. provider.verifyPhoneNumber("123", uiDelegate: nil, multiFactorSession: nil) { _, _ in
  322. }
  323. provider.verifyPhoneNumber(
  324. with: PhoneMultiFactorInfo(),
  325. uiDelegate: nil,
  326. multiFactorSession: nil
  327. ) { _, _ in
  328. }
  329. provider.verifyPhoneNumber("123", uiDelegate: nil, multiFactorSession: nil) { _, _ in
  330. }
  331. provider.credential(withVerificationID: "id", verificationCode: "code")
  332. }
  333. #if compiler(>=5.5.2) && canImport(_Concurrency)
  334. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  335. func FIRPhoneAuthProvider_hAsync() async throws {
  336. _ = PhoneAuthProvider.provider()
  337. let provider = PhoneAuthProvider.provider(auth: FirebaseAuth.Auth.auth())
  338. try await provider.verifyPhoneNumber("123", uiDelegate: nil)
  339. try await provider.verifyPhoneNumber("123", uiDelegate: nil, multiFactorSession: nil)
  340. try await provider.verifyPhoneNumber(with: PhoneMultiFactorInfo(), uiDelegate: nil,
  341. multiFactorSession: nil)
  342. try await provider.verifyPhoneNumber("123", uiDelegate: nil, multiFactorSession: nil)
  343. }
  344. #endif
  345. func FIRPhoneMultiFactorGenerator_h() {
  346. let credential = PhoneAuthProvider.provider().credential(withVerificationID: "id",
  347. verificationCode: "code")
  348. PhoneMultiFactorGenerator.assertion(with: credential)
  349. }
  350. func FIRTwitterAuthProvider_h() {
  351. _ = TwitterAuthProvider.credential(withToken: "token", secret: "secret")
  352. }
  353. func FIRUser_h() {
  354. let auth = FirebaseAuth.Auth.auth()
  355. let user = auth.currentUser!
  356. let credential = PhoneAuthProvider.provider().credential(withVerificationID: "id",
  357. verificationCode: "code")
  358. let provider = PhoneAuthProvider.provider(auth: FirebaseAuth.Auth.auth())
  359. user.updateEmail(to: "email") { _ in
  360. }
  361. user.updatePassword(to: "password") { _ in
  362. }
  363. user.updatePhoneNumber(credential) { _ in
  364. }
  365. let changeRequest = user.createProfileChangeRequest()
  366. user.reload { _ in
  367. }
  368. user.reauthenticate(with: credential) { _, _ in
  369. }
  370. user.reauthenticate(with: provider as! FederatedAuthProvider, uiDelegate: nil)
  371. user.getIDTokenResult { _, _ in
  372. }
  373. user.getIDTokenResult(forcingRefresh: true) { _, _ in
  374. }
  375. user.getIDTokenResult { _, _ in
  376. }
  377. user.getIDTokenForcingRefresh(true) { _, _ in
  378. }
  379. user.link(with: credential) { _, _ in
  380. }
  381. user.link(with: provider as! FederatedAuthProvider, uiDelegate: nil) { _, _ in
  382. }
  383. user.unlink(fromProvider: "abc") { _, _ in
  384. }
  385. user.sendEmailVerification { _ in
  386. }
  387. user.sendEmailVerification(with: ActionCodeSettings()) { _ in
  388. }
  389. user.delete { _ in
  390. }
  391. user.sendEmailVerification(beforeUpdatingEmail: "email") { _ in
  392. }
  393. user.sendEmailVerification(
  394. beforeUpdatingEmail: "email",
  395. actionCodeSettings: ActionCodeSettings()
  396. ) { _ in
  397. }
  398. changeRequest.commitChanges { _ in
  399. }
  400. }
  401. #if compiler(>=5.5.2) && canImport(_Concurrency)
  402. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  403. func FIRUser_hAsync() async throws {
  404. let auth = FirebaseAuth.Auth.auth()
  405. let user = auth.currentUser!
  406. let credential = PhoneAuthProvider.provider().credential(withVerificationID: "id",
  407. verificationCode: "code")
  408. let provider = PhoneAuthProvider.provider(auth: FirebaseAuth.Auth.auth())
  409. try await user.updateEmail(to: "email")
  410. try await user.updatePassword(to: "password")
  411. try await user.updatePhoneNumber(credential)
  412. let changeRequest = user.createProfileChangeRequest()
  413. try await user.reload()
  414. try await user.reauthenticate(with: credential)
  415. try await user.reauthenticate(with: provider as! FederatedAuthProvider, uiDelegate: nil)
  416. try await user.getIDTokenResult()
  417. try await user.getIDTokenResult(forcingRefresh: true)
  418. try await user.getIDTokenResult()
  419. try await user.link(with: credential)
  420. try await user.link(with: provider as! FederatedAuthProvider, uiDelegate: nil)
  421. try await user.unlink(fromProvider: "abc")
  422. try await user.sendEmailVerification()
  423. try await user.sendEmailVerification(with: ActionCodeSettings())
  424. try await user.delete()
  425. try await user.sendEmailVerification(beforeUpdatingEmail: "email")
  426. try await user.sendEmailVerification(
  427. beforeUpdatingEmail: "email",
  428. actionCodeSettings: ActionCodeSettings()
  429. )
  430. try await changeRequest.commitChanges()
  431. }
  432. #endif
  433. }