SwiftAPI.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. try auth.getStoredUser(forAccessGroup: "def")
  86. }
  87. #if compiler(>=5.5.2) && canImport(_Concurrency)
  88. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  89. func FIRAuth_hAsync() async throws {
  90. let auth = FirebaseAuth.Auth.auth()
  91. let user = auth.currentUser!
  92. try await auth.updateCurrentUser(user)
  93. _ = try await auth.fetchSignInMethods(forEmail: "abc@abc.com")
  94. _ = try await auth.signIn(withEmail: "abc@abc.com", password: "password")
  95. _ = try await auth.signIn(withEmail: "abc@abc.com", link: "link")
  96. let provider = OAuthProvider(providerID: "abc")
  97. let credential = try await provider.credential(with: nil)
  98. _ = try await auth.signIn(with: OAuthProvider(providerID: "abc"), uiDelegate: nil)
  99. _ = try await auth.signIn(with: credential)
  100. _ = try await auth.signInAnonymously()
  101. _ = try await auth.signIn(withCustomToken: "abc")
  102. _ = try await auth.createUser(withEmail: "email", password: "password")
  103. _ = try await auth.confirmPasswordReset(withCode: "code", newPassword: "password")
  104. _ = try await auth.checkActionCode("abc")
  105. _ = try await auth.verifyPasswordResetCode("code")
  106. _ = try await auth.applyActionCode("code")
  107. _ = try await auth.sendPasswordReset(withEmail: "email")
  108. let actionCodeSettings = ActionCodeSettings()
  109. _ = try await auth.sendPasswordReset(
  110. withEmail: "email",
  111. actionCodeSettings: actionCodeSettings
  112. )
  113. _ = try await auth.sendSignInLink(toEmail: "email", actionCodeSettings: actionCodeSettings)
  114. }
  115. #endif
  116. func FIRAuthAPNSTokenType_h() {
  117. _ = AuthAPNSTokenType.unknown
  118. _ = AuthAPNSTokenType.sandbox
  119. _ = AuthAPNSTokenType.prod
  120. }
  121. func FIRAuthErrors_h() {
  122. _ = AuthErrorCode.invalidCustomToken
  123. _ = AuthErrorCode.customTokenMismatch
  124. _ = AuthErrorCode.invalidCredential
  125. _ = AuthErrorCode.userDisabled
  126. _ = AuthErrorCode.operationNotAllowed
  127. _ = AuthErrorCode.emailAlreadyInUse
  128. _ = AuthErrorCode.invalidEmail
  129. _ = AuthErrorCode.wrongPassword
  130. _ = AuthErrorCode.tooManyRequests
  131. _ = AuthErrorCode.userNotFound
  132. _ = AuthErrorCode.accountExistsWithDifferentCredential
  133. _ = AuthErrorCode.requiresRecentLogin
  134. _ = AuthErrorCode.providerAlreadyLinked
  135. _ = AuthErrorCode.noSuchProvider
  136. _ = AuthErrorCode.invalidUserToken
  137. _ = AuthErrorCode.networkError
  138. _ = AuthErrorCode.userTokenExpired
  139. _ = AuthErrorCode.invalidAPIKey
  140. _ = AuthErrorCode.userMismatch
  141. _ = AuthErrorCode.credentialAlreadyInUse
  142. _ = AuthErrorCode.weakPassword
  143. _ = AuthErrorCode.appNotAuthorized
  144. _ = AuthErrorCode.expiredActionCode
  145. _ = AuthErrorCode.invalidActionCode
  146. _ = AuthErrorCode.invalidMessagePayload
  147. _ = AuthErrorCode.invalidSender
  148. _ = AuthErrorCode.invalidRecipientEmail
  149. _ = AuthErrorCode.missingEmail
  150. _ = AuthErrorCode.missingIosBundleID
  151. _ = AuthErrorCode.missingAndroidPackageName
  152. _ = AuthErrorCode.unauthorizedDomain
  153. _ = AuthErrorCode.invalidContinueURI
  154. _ = AuthErrorCode.missingContinueURI
  155. _ = AuthErrorCode.missingPhoneNumber
  156. _ = AuthErrorCode.invalidPhoneNumber
  157. _ = AuthErrorCode.missingVerificationCode
  158. _ = AuthErrorCode.invalidVerificationCode
  159. _ = AuthErrorCode.missingVerificationID
  160. _ = AuthErrorCode.invalidVerificationID
  161. _ = AuthErrorCode.missingAppCredential
  162. _ = AuthErrorCode.invalidAppCredential
  163. _ = AuthErrorCode.sessionExpired
  164. _ = AuthErrorCode.quotaExceeded
  165. _ = AuthErrorCode.missingAppToken
  166. _ = AuthErrorCode.notificationNotForwarded
  167. _ = AuthErrorCode.appNotVerified
  168. _ = AuthErrorCode.captchaCheckFailed
  169. _ = AuthErrorCode.webContextAlreadyPresented
  170. _ = AuthErrorCode.webContextCancelled
  171. _ = AuthErrorCode.appVerificationUserInteractionFailure
  172. _ = AuthErrorCode.invalidClientID
  173. _ = AuthErrorCode.webNetworkRequestFailed
  174. _ = AuthErrorCode.webInternalError
  175. _ = AuthErrorCode.webSignInUserInteractionFailure
  176. _ = AuthErrorCode.localPlayerNotAuthenticated
  177. _ = AuthErrorCode.nullUser
  178. _ = AuthErrorCode.dynamicLinkNotActivated
  179. _ = AuthErrorCode.invalidProviderID
  180. _ = AuthErrorCode.tenantIDMismatch
  181. _ = AuthErrorCode.unsupportedTenantOperation
  182. _ = AuthErrorCode.invalidDynamicLinkDomain
  183. _ = AuthErrorCode.rejectedCredential
  184. _ = AuthErrorCode.gameKitNotLinked
  185. _ = AuthErrorCode.secondFactorRequired
  186. _ = AuthErrorCode.missingMultiFactorSession
  187. _ = AuthErrorCode.missingMultiFactorInfo
  188. _ = AuthErrorCode.invalidMultiFactorSession
  189. _ = AuthErrorCode.multiFactorInfoNotFound
  190. _ = AuthErrorCode.adminRestrictedOperation
  191. _ = AuthErrorCode.unverifiedEmail
  192. _ = AuthErrorCode.secondFactorAlreadyEnrolled
  193. _ = AuthErrorCode.maximumSecondFactorCountExceeded
  194. _ = AuthErrorCode.unsupportedFirstFactor
  195. _ = AuthErrorCode.emailChangeNeedsVerification
  196. _ = AuthErrorCode.missingOrInvalidNonce
  197. _ = AuthErrorCode.missingClientIdentifier
  198. _ = AuthErrorCode.keychainError
  199. _ = AuthErrorCode.internalError
  200. _ = AuthErrorCode.malformedJWT
  201. }
  202. func FIRAuthUIDelegate_h() {
  203. class AuthUIImpl: NSObject, AuthUIDelegate {
  204. func present(_ viewControllerToPresent: UIViewController, animated flag: Bool,
  205. completion: (() -> Void)? = nil) {}
  206. func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {}
  207. }
  208. let obj = AuthUIImpl()
  209. obj.present(UIViewController(), animated: true) {}
  210. obj.dismiss(animated: false) {}
  211. }
  212. #if compiler(>=5.5.2) && canImport(_Concurrency)
  213. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  214. func FIRAuthUIDelegate_hAsync() async {
  215. class AuthUIImpl: NSObject, AuthUIDelegate {
  216. func present(_ viewControllerToPresent: UIViewController, animated flag: Bool) async {}
  217. func dismiss(animated flag: Bool) async {}
  218. }
  219. let obj = AuthUIImpl()
  220. await obj.present(UIViewController(), animated: true)
  221. await obj.dismiss(animated: false)
  222. }
  223. #endif
  224. func FIREmailAuthProvider_h() {
  225. _ = EmailAuthProvider.credential(withEmail: "e@email.com", password: "password")
  226. _ = EmailAuthProvider.credential(withEmail: "e@email.com", link: "link")
  227. }
  228. func FIRFacebookAuthProvider_h() {
  229. _ = FacebookAuthProvider.credential(withAccessToken: "token")
  230. }
  231. func FIRFdederatedAuthProvider_h() {
  232. class FederatedAuthImplementation: NSObject, FederatedAuthProvider {
  233. func getCredentialWith(_ UIDelegate: AuthUIDelegate?,
  234. completion: ((AuthCredential?, Error?) -> Void)? = nil) {}
  235. }
  236. let obj = FederatedAuthImplementation()
  237. obj.getCredentialWith(nil) { _, _ in
  238. }
  239. }
  240. #if compiler(>=5.5.2) && canImport(_Concurrency)
  241. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  242. func FIRFedederatedAuthProvider_hAsync() async throws {
  243. class FederatedAuthImplementation: NSObject, FederatedAuthProvider {
  244. func credential(with UIDelegate: AuthUIDelegate?) async throws -> AuthCredential {
  245. return FacebookAuthProvider.credential(withAccessToken: "token")
  246. }
  247. }
  248. let obj = FederatedAuthImplementation()
  249. try await _ = obj.credential(with: nil)
  250. }
  251. #endif
  252. func FIRGameCenterAuthProvider_h() {
  253. GameCenterAuthProvider.getCredential { _, _ in
  254. }
  255. }
  256. #if compiler(>=5.5.2) && canImport(_Concurrency)
  257. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  258. func FIRGameCenterAuthProvider_hAsync() async throws {
  259. _ = try await GameCenterAuthProvider.getCredential()
  260. }
  261. #endif
  262. func FIRGitHubAuthProvider_h() {
  263. _ = GitHubAuthProvider.credential(withToken: "token")
  264. }
  265. func FIRGoogleAuthProvider_h() {
  266. _ = GoogleAuthProvider.credential(withIDToken: "token", accessToken: "aToken")
  267. }
  268. func FIRMultiFactor_h() {
  269. let obj = MultiFactor()
  270. obj.getSessionWithCompletion { _, _ in
  271. }
  272. obj.enroll(with: MultiFactorAssertion(), displayName: "name") { _ in
  273. }
  274. obj.unenroll(with: MultiFactorInfo()) { _ in
  275. }
  276. obj.unenroll(withFactorUID: "uid") { _ in
  277. }
  278. }
  279. #if compiler(>=5.5.2) && canImport(_Concurrency)
  280. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  281. func FIRMultiFactor_hAsync() async throws {
  282. let obj = MultiFactor()
  283. try await obj.session()
  284. try await obj.enroll(with: MultiFactorAssertion(), displayName: "name")
  285. try await obj.unenroll(with: MultiFactorInfo())
  286. try await obj.unenroll(withFactorUID: "uid")
  287. }
  288. #endif
  289. func FIRMultiFactorResolver_h() {
  290. let obj = MultiFactorResolver()
  291. obj.resolveSignIn(with: MultiFactorAssertion()) { _, _ in
  292. }
  293. }
  294. #if compiler(>=5.5.2) && canImport(_Concurrency)
  295. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  296. func FIRMultiFactorResolver_hAsync() async throws {
  297. let obj = MultiFactorResolver()
  298. try await obj.resolveSignIn(with: MultiFactorAssertion())
  299. }
  300. #endif
  301. func FIROAuthProvider_h() {
  302. let provider = OAuthProvider(providerID: "id", auth: FirebaseAuth.Auth.auth())
  303. _ = provider.providerID
  304. _ = OAuthProvider.credential(withProviderID: "id", idToken: "idToden", accessToken: "token")
  305. _ = OAuthProvider.credential(withProviderID: "id", accessToken: "token")
  306. _ = OAuthProvider.credential(withProviderID: "id", idToken: "idToken", rawNonce: "nonce",
  307. accessToken: "token")
  308. _ = OAuthProvider.credential(withProviderID: "id", idToken: "idToken", rawNonce: "nonce")
  309. }
  310. func FIRPhoneAuthProvider_h() {
  311. _ = PhoneAuthProvider.provider()
  312. let provider = PhoneAuthProvider.provider(auth: FirebaseAuth.Auth.auth())
  313. provider.verifyPhoneNumber("123", uiDelegate: nil) { _, _ in
  314. }
  315. provider.verifyPhoneNumber("123", uiDelegate: nil, multiFactorSession: nil) { _, _ in
  316. }
  317. provider.verifyPhoneNumber(
  318. with: PhoneMultiFactorInfo(),
  319. uiDelegate: nil,
  320. multiFactorSession: nil
  321. ) { _, _ in
  322. }
  323. provider.verifyPhoneNumber("123", uiDelegate: nil, multiFactorSession: nil) { _, _ in
  324. }
  325. provider.credential(withVerificationID: "id", verificationCode: "code")
  326. }
  327. #if compiler(>=5.5.2) && canImport(_Concurrency)
  328. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  329. func FIRPhoneAuthProvider_hAsync() async throws {
  330. _ = PhoneAuthProvider.provider()
  331. let provider = PhoneAuthProvider.provider(auth: FirebaseAuth.Auth.auth())
  332. try await provider.verifyPhoneNumber("123", uiDelegate: nil)
  333. try await provider.verifyPhoneNumber("123", uiDelegate: nil, multiFactorSession: nil)
  334. try await provider.verifyPhoneNumber(with: PhoneMultiFactorInfo(), uiDelegate: nil,
  335. multiFactorSession: nil)
  336. try await provider.verifyPhoneNumber("123", uiDelegate: nil, multiFactorSession: nil)
  337. }
  338. #endif
  339. func FIRPhoneMultiFactorGenerator_h() {
  340. let credential = PhoneAuthProvider.provider().credential(withVerificationID: "id",
  341. verificationCode: "code")
  342. PhoneMultiFactorGenerator.assertion(with: credential)
  343. }
  344. func FIRTwitterAuthProvider_h() {
  345. _ = TwitterAuthProvider.credential(withToken: "token", secret: "secret")
  346. }
  347. func FIRUser_h() {
  348. let auth = FirebaseAuth.Auth.auth()
  349. let user = auth.currentUser!
  350. let credential = PhoneAuthProvider.provider().credential(withVerificationID: "id",
  351. verificationCode: "code")
  352. let provider = PhoneAuthProvider.provider(auth: FirebaseAuth.Auth.auth())
  353. user.updateEmail(to: "email") { _ in
  354. }
  355. user.updatePassword(to: "password") { _ in
  356. }
  357. user.updatePhoneNumber(credential) { _ in
  358. }
  359. let changeRequest = user.createProfileChangeRequest()
  360. user.reload { _ in
  361. }
  362. user.reauthenticate(with: credential) { _, _ in
  363. }
  364. user.reauthenticate(with: provider as! FederatedAuthProvider, uiDelegate: nil)
  365. user.getIDTokenResult { _, _ in
  366. }
  367. user.getIDTokenResult(forcingRefresh: true) { _, _ in
  368. }
  369. user.getIDTokenResult { _, _ in
  370. }
  371. user.getIDTokenForcingRefresh(true) { _, _ in
  372. }
  373. user.link(with: credential) { _, _ in
  374. }
  375. user.link(with: provider as! FederatedAuthProvider, uiDelegate: nil) { _, _ in
  376. }
  377. user.unlink(fromProvider: "abc") { _, _ in
  378. }
  379. user.sendEmailVerification { _ in
  380. }
  381. user.sendEmailVerification(with: ActionCodeSettings()) { _ in
  382. }
  383. user.delete { _ in
  384. }
  385. user.sendEmailVerification(beforeUpdatingEmail: "email") { _ in
  386. }
  387. user.sendEmailVerification(
  388. beforeUpdatingEmail: "email",
  389. actionCodeSettings: ActionCodeSettings()
  390. ) { _ in
  391. }
  392. changeRequest.commitChanges { _ in
  393. }
  394. }
  395. #if compiler(>=5.5.2) && canImport(_Concurrency)
  396. @available(iOS 13, tvOS 13, macOS 10.15, macCatalyst 13, watchOS 7, *)
  397. func FIRUser_hAsync() async throws {
  398. let auth = FirebaseAuth.Auth.auth()
  399. let user = auth.currentUser!
  400. let credential = PhoneAuthProvider.provider().credential(withVerificationID: "id",
  401. verificationCode: "code")
  402. let provider = PhoneAuthProvider.provider(auth: FirebaseAuth.Auth.auth())
  403. try await user.updateEmail(to: "email")
  404. try await user.updatePassword(to: "password")
  405. try await user.updatePhoneNumber(credential)
  406. let changeRequest = user.createProfileChangeRequest()
  407. try await user.reload()
  408. try await user.reauthenticate(with: credential)
  409. try await user.reauthenticate(with: provider as! FederatedAuthProvider, uiDelegate: nil)
  410. try await user.getIDTokenResult()
  411. try await user.getIDTokenResult(forcingRefresh: true)
  412. try await user.getIDTokenResult()
  413. try await user.link(with: credential)
  414. try await user.link(with: provider as! FederatedAuthProvider, uiDelegate: nil)
  415. try await user.unlink(fromProvider: "abc")
  416. try await user.sendEmailVerification()
  417. try await user.sendEmailVerification(with: ActionCodeSettings())
  418. try await user.delete()
  419. try await user.sendEmailVerification(beforeUpdatingEmail: "email")
  420. try await user.sendEmailVerification(
  421. beforeUpdatingEmail: "email",
  422. actionCodeSettings: ActionCodeSettings()
  423. )
  424. try await changeRequest.commitChanges()
  425. }
  426. #endif
  427. }