| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971 |
- // Copyright 2021 Google LLC
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- import Foundation
- import Combine
- import XCTest
- import FirebaseAuth
- class UserTests: XCTestCase {
- override class func setUp() {
- FirebaseApp.configureForTests()
- }
- override class func tearDown() {
- FirebaseApp.app()?.delete { success in
- if success {
- print("Shut down app successfully.")
- } else {
- print("💥 There was a problem when shutting down the app..")
- }
- }
- }
- override func setUp() {
- do {
- try Auth.auth().signOut()
- } catch {}
- }
- fileprivate struct ProviderCredentials {
- var providerID: String
- var federatedID: String
- var displayName: String
- var idToken: String?
- var accessToken: String
- var email: String
- var localID: String
- var phoneNumber: String? = nil
- var userInfo: [String: String]? = [:]
- }
- fileprivate static let continueURL = "continueURL"
- fileprivate static let fakeOOBCode = "testoobcode"
- fileprivate static let phoneNumber = "12345658"
- fileprivate static let verificationID = "55432"
- fileprivate static let verificationCode = "12345678"
- fileprivate static let accessTokenTimeToLive: TimeInterval = 60 * 60
- fileprivate static let approximateExpirationDate: TimeInterval = 60 * 60
- fileprivate static let refreshToken = "REFRESH_TOKEN"
- fileprivate static let accessToken = "ACCESS_TOKEN"
- fileprivate static let newAccessToken = "NewAccessToken"
- fileprivate static let email = "user@company.com"
- fileprivate static let password = "!@#$%^"
- fileprivate static let passwordHash = "UkVEQUNURUQ="
- fileprivate static let userName = "User Doe"
- fileprivate static let localID = "localId"
- fileprivate static let googleEmail = "user@gmail.com"
- fileprivate static let googleProfile: [String: String] = {
- [
- "iss": "https://accounts.google.com\\",
- "email": googleEmail,
- "given_name": "User",
- "family_name": "Doe",
- ]
- }()
- class MockGetAccountInfoResponse: FIRGetAccountInfoResponse {
- fileprivate var providerCredentials: ProviderCredentials!
- override var users: [FIRGetAccountInfoResponseUser] {
- let response = MockGetAccountInfoResponseUser(dictionary: [:])
- response.providerCredentials = providerCredentials
- return [response]
- }
- }
- class MockSetAccountInfoResponse: FIRSetAccountInfoResponse {}
- class MockSecureAccessResponse: FIRSecureTokenResponse {
- override var accessToken: String { return "ACCESS_TOKEN" }
- }
- class MockVerifyPhoneNumberResponse: FIRVerifyPhoneNumberResponse {
- fileprivate var providerCredentials: ProviderCredentials!
- override var phoneNumber: String? { providerCredentials.phoneNumber }
- }
- class MockGetAccountInfoResponseUser: FIRGetAccountInfoResponseUser {
- fileprivate var providerCredentials: ProviderCredentials!
- override var localID: String { providerCredentials.localID }
- override var email: String { providerCredentials.email }
- override var displayName: String { providerCredentials.displayName }
- override var passwordHash: String? { UserTests.passwordHash }
- override var phoneNumber: String? { providerCredentials.phoneNumber }
- override var providerUserInfo: [FIRGetAccountInfoResponseProviderUserInfo]? {
- guard let userInfo = providerCredentials.userInfo else {
- return nil
- }
- let response = MockGetAccountInfoResponseProviderUserInfo(dictionary: userInfo)
- response.providerCredentials = providerCredentials
- return [response]
- }
- }
- class MockVerifyAssertionResponse: FIRVerifyAssertionResponse {
- fileprivate var providerCredentials: ProviderCredentials!
- override var localID: String? { providerCredentials.localID }
- override var federatedID: String? { providerCredentials.federatedID }
- override var providerID: String? { providerCredentials.providerID }
- override var displayName: String? { providerCredentials.displayName }
- override var profile: [String: NSObject]? { googleProfile as [String: NSString] }
- override var username: String? { userName }
- override var idToken: String? { providerCredentials.accessToken }
- override var refreshToken: String? { UserTests.refreshToken }
- override var approximateExpirationDate: Date? {
- Date(timeIntervalSinceNow: accessTokenTimeToLive)
- }
- }
- class MockVerifyPasswordResponse: FIRVerifyPasswordResponse {
- fileprivate var providerCredentials: ProviderCredentials!
- override var refreshToken: String { UserTests.refreshToken }
- override var email: String { providerCredentials.email }
- override var idToken: String { providerCredentials.accessToken }
- override var approximateExpirationDate: Date {
- Date(timeIntervalSinceNow: UserTests.accessTokenTimeToLive)
- }
- }
- class MockGetAccountInfoResponseProviderUserInfo: FIRGetAccountInfoResponseProviderUserInfo {
- fileprivate var providerCredentials: ProviderCredentials!
- override var providerID: String? { providerCredentials.providerID }
- override var displayName: String? { providerCredentials.displayName }
- override var federatedID: String? { providerCredentials.federatedID }
- override var email: String? { googleEmail }
- }
- class MockAuthBackend: AuthBackendImplementationMock {
- fileprivate var providerCredentials: ProviderCredentials!
- var verifyAssertionCallback: Result<MockVerifyAssertionResponse, Error> =
- .success(MockVerifyAssertionResponse())
- override func verifyAssertion(_ request: FIRVerifyAssertionRequest,
- callback: @escaping FIRVerifyAssertionResponseCallback) {
- XCTAssertEqual(request.apiKey, Credentials.apiKey)
- XCTAssertEqual(request.providerID, providerCredentials.providerID)
- XCTAssertEqual(request.providerIDToken, providerCredentials.idToken)
- XCTAssertEqual(request.providerAccessToken, providerCredentials.accessToken)
- XCTAssertTrue(request.returnSecureToken)
- switch verifyAssertionCallback {
- case let .success(response):
- response.providerCredentials = providerCredentials
- callback(response, nil)
- case let .failure(error):
- callback(nil, error)
- }
- }
- override func verifyPassword(_ request: FIRVerifyPasswordRequest,
- callback: @escaping FIRVerifyPasswordResponseCallback) {
- let response = MockVerifyPasswordResponse()
- response.providerCredentials = providerCredentials
- callback(response, nil)
- }
- override func getAccountInfo(_ request: FIRGetAccountInfoRequest,
- callback: @escaping FIRGetAccountInfoResponseCallback) {
- XCTAssertEqual(request.apiKey, Credentials.apiKey)
- XCTAssertEqual(request.accessToken, providerCredentials.accessToken)
- let response = MockGetAccountInfoResponse()
- response.providerCredentials = providerCredentials
- callback(response, nil)
- }
- override func setAccountInfo(_ request: FIRSetAccountInfoRequest,
- callback: @escaping FIRSetAccountInfoResponseCallback) {
- XCTAssertEqual(request.apiKey, Credentials.apiKey)
- XCTAssertEqual(request.accessToken, providerCredentials.accessToken)
- XCTAssertNotNil(request.deleteProviders)
- XCTAssertNil(request.email)
- XCTAssertNil(request.localID)
- XCTAssertNil(request.displayName)
- XCTAssertNil(request.photoURL)
- XCTAssertNil(request.password)
- XCTAssertNil(request.providers)
- XCTAssertNil(request.deleteAttributes)
- callback(MockSetAccountInfoResponse(), nil)
- }
- override func verifyPhoneNumber(_ request: FIRVerifyPhoneNumberRequest,
- callback: @escaping FIRVerifyPhoneNumberResponseCallback) {
- XCTAssertEqual(request.verificationID, UserTests.verificationID)
- XCTAssertEqual(request.verificationCode, UserTests.verificationCode)
- XCTAssertEqual(request.operation, FIRAuthOperationType.link)
- XCTAssertEqual(request.accessToken, providerCredentials.accessToken)
- let response = MockVerifyPhoneNumberResponse()
- response.providerCredentials = providerCredentials
- callback(response, nil)
- }
- var actionCodeSettings: ActionCodeSettings?
- var getOOBConfirmationCodeError: Error?
- override func getOOBConfirmationCode(_ request: FIRGetOOBConfirmationCodeRequest,
- callback: @escaping FIRGetOOBConfirmationCodeResponseCallback) {
- XCTAssertEqual(request.accessToken, UserTests.accessToken)
- XCTAssertEqual(request.continueURL, actionCodeSettings?.url?.absoluteString)
- XCTAssertEqual(request.dynamicLinkDomain, actionCodeSettings?.dynamicLinkDomain)
- if let error = getOOBConfirmationCodeError {
- callback(nil, error)
- } else {
- callback(MockGetOOBConfirmationCodeResponse(), nil)
- }
- }
- override func secureToken(_ request: FIRSecureTokenRequest,
- callback: @escaping FIRSecureTokenResponseCallback) {
- callback(MockSecureAccessResponse(), nil)
- }
- }
- class MockGetOOBConfirmationCodeResponse: FIRGetOOBConfirmationCodeResponse {
- override var oobCode: String { return UserTests.fakeOOBCode }
- }
- func testlinkAndRetrieveDataSuccess() {
- let facebookCredentials = ProviderCredentials(
- providerID: FacebookAuthProviderID,
- federatedID: "FACEBOOK_ID",
- displayName: "Facebook Doe",
- idToken: nil,
- accessToken: "FACEBOOK_ACCESS_TOKEN",
- email: UserTests.email,
- localID: UserTests.localID
- )
- let authBackend = MockAuthBackend()
- authBackend.providerCredentials = facebookCredentials
- FIRAuthBackend.setBackendImplementation(authBackend)
- var cancellables = Set<AnyCancellable>()
- let userSignInExpectation = expectation(description: "User associated from a third-party")
- let facebookCredential = FacebookAuthProvider
- .credential(withAccessToken: facebookCredentials.accessToken)
- // when
- Auth.auth()
- .signIn(with: facebookCredential)
- .flatMap { [weak authBackend] authResult -> Future<AuthDataResult, Error> in
- XCTAssertEqual(authResult.additionalUserInfo?.profile,
- UserTests.googleProfile as [String: NSString])
- XCTAssertEqual(authResult.additionalUserInfo?.username,
- UserTests.userName)
- XCTAssertEqual(authResult.additionalUserInfo?.providerID,
- FacebookAuthProviderID)
- let googleCredentials = ProviderCredentials(
- providerID: GoogleAuthProviderID,
- federatedID: "GOOGLE_ID",
- displayName: "Google Doe",
- idToken: "GOOGLE_ID_TOKEN",
- accessToken: "GOOGLE_ACCESS_TOKEN",
- email: UserTests.googleEmail,
- localID: UserTests.localID,
- phoneNumber: nil,
- userInfo: [:]
- )
- authBackend?.providerCredentials = googleCredentials
- let linkGoogleCredential = GoogleAuthProvider.credential(
- withIDToken: googleCredentials.idToken!,
- accessToken: googleCredentials.accessToken
- )
- return authResult.user
- .link(with: linkGoogleCredential)
- }
- .sink { completion in
- switch completion {
- case .finished:
- print("Finished")
- case let .failure(error):
- XCTFail("💥 Something went wrong: \(error)")
- }
- } receiveValue: { linkAuthResult in
- let user = linkAuthResult.user
- // Verify that the current user and reauthenticated user are same pointers.
- XCTAssertEqual(Auth.auth().currentUser, user)
- XCTAssertEqual(linkAuthResult.additionalUserInfo?.profile,
- UserTests.googleProfile as [String: NSString])
- XCTAssertEqual(linkAuthResult.additionalUserInfo?.username,
- UserTests.userName)
- XCTAssertEqual(linkAuthResult.additionalUserInfo?.providerID,
- GoogleAuthProviderID)
- userSignInExpectation.fulfill()
- }
- .store(in: &cancellables)
- // then
- wait(for: [userSignInExpectation], timeout: expectationTimeout)
- }
- func testLinkAndRetrieveDataError() {
- let facebookCredentials = ProviderCredentials(
- providerID: FacebookAuthProviderID,
- federatedID: "FACEBOOK_ID",
- displayName: "Facebook Doe",
- idToken: nil,
- accessToken: "FACEBOOK_ACCESS_TOKEN",
- email: UserTests.email,
- localID: UserTests.localID
- )
- let authBackend = MockAuthBackend()
- authBackend.providerCredentials = facebookCredentials
- FIRAuthBackend.setBackendImplementation(authBackend)
- var cancellables = Set<AnyCancellable>()
- let userSignInExpectation = expectation(description: "User associated from a third-party")
- let facebookCredential = FacebookAuthProvider
- .credential(withAccessToken: facebookCredentials.accessToken)
- // when
- Auth.auth()
- .signIn(with: facebookCredential)
- .flatMap { [weak authBackend] authResult -> Future<AuthDataResult, Error> in
- XCTAssertEqual(authResult.additionalUserInfo?.profile,
- UserTests.googleProfile as [String: NSString])
- XCTAssertEqual(authResult.additionalUserInfo?.username,
- UserTests.userName)
- XCTAssertEqual(authResult.additionalUserInfo?.providerID,
- FacebookAuthProviderID)
- XCTAssertEqual(Auth.auth().currentUser, authResult.user)
- let googleCredentials = ProviderCredentials(
- providerID: GoogleAuthProviderID,
- federatedID: "GOOGLE_ID",
- displayName: "Google Doe",
- idToken: "GOOGLE_ID_TOKEN",
- accessToken: "GOOGLE_ACCESS_TOKEN",
- email: UserTests.googleEmail,
- localID: UserTests.localID,
- phoneNumber: nil,
- userInfo: [:]
- )
- authBackend?.providerCredentials = googleCredentials
- authBackend?
- .verifyAssertionCallback = .failure(FIRAuthErrorUtils
- .accountExistsWithDifferentCredentialError(
- withEmail: UserTests.userName,
- updatedCredential: nil
- ))
- let linkGoogleCredential = GoogleAuthProvider.credential(
- withIDToken: googleCredentials.idToken!,
- accessToken: googleCredentials.accessToken
- )
- return authResult.user
- .link(with: linkGoogleCredential)
- }
- .sink { completion in
- if case let .failure(error as NSError) = completion {
- XCTAssertEqual(
- error.code,
- AuthErrorCode.accountExistsWithDifferentCredential.rawValue
- )
- userSignInExpectation.fulfill()
- }
- } receiveValue: { linkAuthResult in
- XCTFail("💥 result unexpected")
- }
- .store(in: &cancellables)
- // then
- wait(for: [userSignInExpectation], timeout: expectationTimeout)
- }
- func testLinkAndRetrieveDataProviderAlreadyLinked() {
- let facebookCredentials = ProviderCredentials(
- providerID: FacebookAuthProviderID,
- federatedID: "FACEBOOK_ID",
- displayName: "Facebook Doe",
- idToken: nil,
- accessToken: "FACEBOOK_ACCESS_TOKEN",
- email: UserTests.email,
- localID: UserTests.localID
- )
- let authBackend = MockAuthBackend()
- authBackend.providerCredentials = facebookCredentials
- FIRAuthBackend.setBackendImplementation(authBackend)
- var cancellables = Set<AnyCancellable>()
- let userSignInExpectation = expectation(description: "User associated from a third-party")
- let facebookCredential = FacebookAuthProvider
- .credential(withAccessToken: facebookCredentials.accessToken)
- // when
- Auth.auth()
- .signIn(with: facebookCredential)
- .flatMap { authResult -> Future<AuthDataResult, Error> in
- XCTAssertEqual(authResult.additionalUserInfo?.profile,
- UserTests.googleProfile as [String: NSString])
- XCTAssertEqual(authResult.additionalUserInfo?.username,
- UserTests.userName)
- XCTAssertEqual(authResult.additionalUserInfo?.providerID,
- FacebookAuthProviderID)
- XCTAssertEqual(Auth.auth().currentUser, authResult.user)
- authBackend.providerCredentials = facebookCredentials
- authBackend
- .verifyAssertionCallback = .failure(FIRAuthErrorUtils
- .accountExistsWithDifferentCredentialError(
- withEmail: UserTests.userName,
- updatedCredential: nil
- ))
- let linkFacebookCredential = FacebookAuthProvider
- .credential(withAccessToken: facebookCredentials.accessToken)
- return authResult.user
- .link(with: linkFacebookCredential)
- }
- .sink { completion in
- if case let .failure(error as NSError) = completion {
- XCTAssertEqual(error.code, AuthErrorCode.providerAlreadyLinked.rawValue)
- userSignInExpectation.fulfill()
- }
- } receiveValue: { linkAuthResult in
- XCTFail("💥 result unexpected")
- }
- .store(in: &cancellables)
- // then
- wait(for: [userSignInExpectation], timeout: expectationTimeout)
- }
- func testLinkAndRetrieveDataErrorAutoSignOut() {
- let facebookCredentials = ProviderCredentials(
- providerID: FacebookAuthProviderID,
- federatedID: "FACEBOOK_ID",
- displayName: "Facebook Doe",
- idToken: nil,
- accessToken: "FACEBOOK_ACCESS_TOKEN",
- email: UserTests.email,
- localID: UserTests.localID
- )
- let authBackend = MockAuthBackend()
- authBackend.providerCredentials = facebookCredentials
- FIRAuthBackend.setBackendImplementation(authBackend)
- var cancellables = Set<AnyCancellable>()
- let userSignInExpectation = expectation(description: "User associated from a third-party")
- let facebookCredential = FacebookAuthProvider
- .credential(withAccessToken: facebookCredentials.accessToken)
- // when
- Auth.auth()
- .signIn(with: facebookCredential)
- .flatMap { [weak authBackend] authResult -> Future<AuthDataResult, Error> in
- XCTAssertEqual(authResult.additionalUserInfo?.profile,
- UserTests.googleProfile as [String: NSString])
- XCTAssertEqual(authResult.additionalUserInfo?.username,
- UserTests.userName)
- XCTAssertEqual(authResult.additionalUserInfo?.providerID,
- FacebookAuthProviderID)
- XCTAssertEqual(Auth.auth().currentUser, authResult.user)
- let googleCredentials = ProviderCredentials(
- providerID: GoogleAuthProviderID,
- federatedID: "GOOGLE_ID",
- displayName: "Google Doe",
- idToken: "GOOGLE_ID_TOKEN",
- accessToken: "GOOGLE_ACCESS_TOKEN",
- email: UserTests.googleEmail,
- localID: UserTests.localID,
- phoneNumber: nil,
- userInfo: [:]
- )
- authBackend?.providerCredentials = googleCredentials
- authBackend?
- .verifyAssertionCallback = .failure(FIRAuthErrorUtils
- .userDisabledError(withMessage: nil))
- let linkGoogleCredential = GoogleAuthProvider.credential(
- withIDToken: googleCredentials.idToken!,
- accessToken: googleCredentials.accessToken
- )
- return authResult.user
- .link(with: linkGoogleCredential)
- }
- .sink { completion in
- if case let .failure(error as NSError) = completion {
- XCTAssertEqual(error.code, AuthErrorCode.userDisabled.rawValue)
- userSignInExpectation.fulfill()
- }
- } receiveValue: { linkAuthResult in
- XCTFail("💥 result unexpected")
- }
- .store(in: &cancellables)
- // then
- wait(for: [userSignInExpectation], timeout: expectationTimeout)
- }
- func testReauthenticateSuccess() {
- // given
- let emailCredentials = ProviderCredentials(
- providerID: EmailAuthProviderID,
- federatedID: "EMAIL_ID",
- displayName: "Google Doe",
- idToken: nil,
- accessToken: UserTests.accessToken,
- email: UserTests.email,
- localID: UserTests.localID
- )
- let authBackend = MockAuthBackend()
- authBackend.providerCredentials = emailCredentials
- FIRAuthBackend.setBackendImplementation(authBackend)
- var cancellables = Set<AnyCancellable>()
- let userReauthenticateExpectation = expectation(description: "User reauthenticated")
- // when
- Auth.auth()
- .signIn(withEmail: UserTests.email, password: UserTests.password)
- .flatMap { authResult -> Future<AuthDataResult, Error> in
- let credential = EmailAuthProvider.credential(
- withEmail: UserTests.email,
- password: UserTests.password
- )
- authBackend.providerCredentials.accessToken = UserTests.newAccessToken
- return authResult.user
- .reauthenticate(with: credential)
- }
- .sink { completion in
- switch completion {
- case .finished:
- print("Finished")
- case let .failure(error):
- XCTFail("💥 Something went wrong: \(error)")
- }
- } receiveValue: { authResult in
- let user = authResult.user
- XCTAssertEqual(user.displayName, emailCredentials.displayName)
- XCTAssertEqual(user.email, emailCredentials.email)
- userReauthenticateExpectation.fulfill()
- }
- .store(in: &cancellables)
- // then
- wait(for: [userReauthenticateExpectation], timeout: expectationTimeout)
- }
- func testReauthenticateWithCredentialSuccess() {
- // given
- let googleCredentials = ProviderCredentials(
- providerID: GoogleAuthProviderID,
- federatedID: "GOOGLE_ID",
- displayName: "Google Doe",
- idToken: "GOOGLE_ID_TOKEN",
- accessToken: "GOOGLE_ACCESS_TOKEN",
- email: UserTests.googleEmail,
- localID: UserTests.localID
- )
- let authBackend = MockAuthBackend()
- authBackend.providerCredentials = googleCredentials
- FIRAuthBackend.setBackendImplementation(authBackend)
- var cancellables = Set<AnyCancellable>()
- let userReauthenticateExpectation = expectation(description: "User reauthenticated")
- let googleCredential = GoogleAuthProvider.credential(
- withIDToken: googleCredentials.idToken!,
- accessToken: googleCredentials.accessToken
- )
- // when
- Auth.auth()
- .signIn(with: googleCredential)
- .flatMap { authResult -> Future<AuthDataResult, Error> in
- XCTAssertEqual(authResult.additionalUserInfo?.profile,
- UserTests.googleProfile as [String: NSString])
- XCTAssertEqual(authResult.additionalUserInfo?.username,
- UserTests.userName)
- XCTAssertEqual(authResult.additionalUserInfo?.providerID,
- GoogleAuthProviderID)
- return authResult.user
- .reauthenticate(with: googleCredential)
- }
- .sink { completion in
- switch completion {
- case .finished:
- print("Finished")
- case let .failure(error):
- XCTFail("💥 Something went wrong: \(error)")
- }
- } receiveValue: { authResult in
- XCTAssertEqual(authResult.additionalUserInfo?.profile,
- UserTests.googleProfile as [String: NSString])
- XCTAssertEqual(authResult.additionalUserInfo?.username,
- UserTests.userName)
- XCTAssertEqual(authResult.additionalUserInfo?.providerID,
- GoogleAuthProviderID)
- userReauthenticateExpectation.fulfill()
- }
- .store(in: &cancellables)
- // then
- wait(for: [userReauthenticateExpectation], timeout: expectationTimeout)
- }
- func testReauthenticateFailure() {
- // given
- let emailCredentials = ProviderCredentials(
- providerID: EmailAuthProviderID,
- federatedID: "EMAIL_ID",
- displayName: "Google Doe",
- idToken: nil,
- accessToken: UserTests.accessToken,
- email: UserTests.email,
- localID: UserTests.localID
- )
- let authBackend = MockAuthBackend()
- authBackend.providerCredentials = emailCredentials
- FIRAuthBackend.setBackendImplementation(authBackend)
- var cancellables = Set<AnyCancellable>()
- let userReauthenticateExpectation = expectation(description: "User reauthenticated")
- // when
- Auth.auth()
- .signIn(withEmail: UserTests.email, password: UserTests.password)
- .flatMap { authResult -> Future<AuthDataResult, Error> in
- authBackend.providerCredentials.displayName = "New User Doe"
- authBackend.providerCredentials.email = "newEmail"
- authBackend.providerCredentials.localID = "ANOTHER_LOCAL_ID"
- authBackend.providerCredentials.accessToken = "NewAccessToken"
- let credential = EmailAuthProvider.credential(
- withEmail: UserTests.email,
- password: UserTests.password
- )
- return authResult.user
- .reauthenticate(with: credential)
- }
- .sink { completion in
- if case let .failure(error as NSError) = completion {
- // Verify user mismatch error.
- XCTAssertEqual(error.code, AuthErrorCode.userMismatch.rawValue)
- userReauthenticateExpectation.fulfill()
- }
- } receiveValue: { authResult in
- XCTFail("💥 result unexpected")
- }
- .store(in: &cancellables)
- // then
- wait(for: [userReauthenticateExpectation], timeout: expectationTimeout)
- }
- func testReauthenticateUserMismatchFailure() {
- // given
- let emailCredentials = ProviderCredentials(
- providerID: EmailAuthProviderID,
- federatedID: "EMAIL_ID",
- displayName: "Google Doe",
- idToken: nil,
- accessToken: UserTests.accessToken,
- email: UserTests.email,
- localID: UserTests.localID
- )
- let authBackend = MockAuthBackend()
- authBackend.providerCredentials = emailCredentials
- FIRAuthBackend.setBackendImplementation(authBackend)
- var cancellables = Set<AnyCancellable>()
- let userReauthenticateExpectation = expectation(description: "User reauthenticated")
- // when
- Auth.auth()
- .signIn(withEmail: UserTests.email, password: UserTests.password)
- .flatMap { authResult -> Future<AuthDataResult, Error> in
- let googleCredentials = ProviderCredentials(
- providerID: GoogleAuthProviderID,
- federatedID: "GOOGLE_ID",
- displayName: "Google Doe",
- idToken: "GOOGLE_ID_TOKEN",
- accessToken: "GOOGLE_ACCESS_TOKEN",
- email: UserTests.googleEmail,
- localID: UserTests.localID,
- phoneNumber: nil,
- userInfo: [:]
- )
- authBackend.providerCredentials = googleCredentials
- authBackend
- .verifyAssertionCallback = .failure(FIRAuthErrorUtils
- .userNotFoundError(withMessage: nil))
- let googleCredential = GoogleAuthProvider.credential(
- withIDToken: "GOOGLE_ID_TOKEN",
- accessToken: "GOOGLE_ACCESS_TOKEN"
- )
- return authResult.user
- .reauthenticate(with: googleCredential)
- }
- .sink { completion in
- if case let .failure(error as NSError) = completion {
- // Verify user mismatch error.
- XCTAssertEqual(error.code, AuthErrorCode.userMismatch.rawValue)
- userReauthenticateExpectation.fulfill()
- }
- } receiveValue: { authResult in
- XCTFail("💥 result unexpected")
- }
- .store(in: &cancellables)
- // then
- wait(for: [userReauthenticateExpectation], timeout: expectationTimeout)
- }
- func testUnlinkPhoneAuthCredentialSuccess() {
- // given
- let emailCredentials = ProviderCredentials(
- providerID: PhoneAuthProviderID,
- federatedID: "EMAIL_ID",
- displayName: "Google Doe",
- idToken: nil,
- accessToken: UserTests.accessToken,
- email: UserTests.email,
- localID: UserTests.localID,
- userInfo: nil
- )
- let authBackend = MockAuthBackend()
- authBackend.providerCredentials = emailCredentials
- FIRAuthBackend.setBackendImplementation(authBackend)
- var cancellables = Set<AnyCancellable>()
- let userUnlinkedExpectation = expectation(description: "User Unlinked")
- // when
- Auth.auth()
- .signIn(withEmail: UserTests.email, password: UserTests.password)
- .flatMap { authResult -> Future<AuthDataResult, Error> in
- authBackend.providerCredentials.phoneNumber = Self.phoneNumber
- authBackend.providerCredentials.userInfo = ["providerId": PhoneAuthProviderID]
- let credential = PhoneAuthProvider.provider()
- .credential(withVerificationID: Self.verificationID,
- verificationCode: Self.verificationCode)
- return authResult.user
- .link(with: credential)
- }
- .flatMap { authResult -> AnyPublisher<User, Error> in
- XCTAssertEqual(
- Auth.auth().currentUser?.providerData.first?.providerID,
- PhoneAuthProviderID
- )
- XCTAssertEqual(Auth.auth().currentUser?.phoneNumber, Self.phoneNumber)
- return authResult.user
- .unlink(fromProvider: PhoneAuthProviderID)
- .eraseToAnyPublisher()
- }
- .sink { completion in
- switch completion {
- case .finished:
- print("Finished")
- case let .failure(error):
- XCTFail("💥 Something went wrong: \(error)")
- }
- } receiveValue: { user in
- XCTAssertNil(Auth.auth().currentUser?.phoneNumber)
- userUnlinkedExpectation.fulfill()
- }
- .store(in: &cancellables)
- // then
- wait(for: [userUnlinkedExpectation], timeout: expectationTimeout)
- }
- func testSendVerificationEmailSuccess() {
- // given
- let emailCredentials = ProviderCredentials(
- providerID: PhoneAuthProviderID,
- federatedID: "EMAIL_ID",
- displayName: "Google Doe",
- idToken: nil,
- accessToken: UserTests.accessToken,
- email: UserTests.email,
- localID: UserTests.localID
- )
- let authBackend = MockAuthBackend()
- authBackend.providerCredentials = emailCredentials
- FIRAuthBackend.setBackendImplementation(authBackend)
- var cancellables = Set<AnyCancellable>()
- let sentVerificationEmailExpectation = expectation(description: "Sent verification email")
- // when
- Auth.auth()
- .signIn(withEmail: UserTests.email, password: UserTests.password)
- .flatMap { authResult -> Future<Void, Error> in
- XCTAssertNotNil(authResult.user)
- return authResult.user
- .sendEmailVerification()
- }
- .sink { completion in
- switch completion {
- case .finished:
- print("Finished")
- case let .failure(error):
- XCTFail("💥 Something went wrong: \(error)")
- }
- } receiveValue: { _ in
- sentVerificationEmailExpectation.fulfill()
- }
- .store(in: &cancellables)
- // then
- wait(for: [sentVerificationEmailExpectation], timeout: expectationTimeout)
- }
- func testSendVerificationEmailWithActionCodeSettingsSuccess() {
- // given
- let emailCredentials = ProviderCredentials(
- providerID: PhoneAuthProviderID,
- federatedID: "EMAIL_ID",
- displayName: "Google Doe",
- idToken: nil,
- accessToken: UserTests.accessToken,
- email: UserTests.email,
- localID: UserTests.localID
- )
- let authBackend = MockAuthBackend()
- authBackend.providerCredentials = emailCredentials
- FIRAuthBackend.setBackendImplementation(authBackend)
- var cancellables = Set<AnyCancellable>()
- let sentVerificationEmailExpectation = expectation(description: "Sent verification email")
- // when
- Auth.auth()
- .signIn(withEmail: UserTests.email, password: UserTests.password)
- .flatMap { authResult -> Future<Void, Error> in
- XCTAssertNotNil(authResult.user)
- let actionCodeSettings = ActionCodeSettings()
- actionCodeSettings.url = URL(string: UserTests.continueURL)
- actionCodeSettings.dynamicLinkDomain = "example.page.link"
- authBackend.actionCodeSettings = actionCodeSettings
- return authResult.user
- .sendEmailVerification(with: actionCodeSettings)
- }
- .sink { completion in
- switch completion {
- case .finished:
- print("Finished")
- case let .failure(error):
- XCTFail("💥 Something went wrong: \(error)")
- }
- } receiveValue: { _ in
- sentVerificationEmailExpectation.fulfill()
- }
- .store(in: &cancellables)
- // then
- wait(for: [sentVerificationEmailExpectation], timeout: expectationTimeout)
- }
- func testSendVerificationEmailInvalidRecipientEmail() {
- // given
- let emailCredentials = ProviderCredentials(
- providerID: PhoneAuthProviderID,
- federatedID: "EMAIL_ID",
- displayName: "Google Doe",
- idToken: nil,
- accessToken: UserTests.accessToken,
- email: UserTests.email,
- localID: UserTests.localID
- )
- let authBackend = MockAuthBackend()
- authBackend.providerCredentials = emailCredentials
- authBackend.getOOBConfirmationCodeError = FIRAuthErrorUtils
- .invalidRecipientEmailError(withMessage: nil)
- FIRAuthBackend.setBackendImplementation(authBackend)
- var cancellables = Set<AnyCancellable>()
- let sentVerificationEmailExpectation = expectation(description: "Sent verification email")
- // when
- Auth.auth()
- .signIn(withEmail: UserTests.email, password: UserTests.password)
- .flatMap { authResult -> Future<Void, Error> in
- XCTAssertNotNil(authResult.user)
- return authResult.user
- .sendEmailVerification()
- }
- .sink { completion in
- if case let .failure(error as NSError) = completion {
- // Verify user mismatch error.
- XCTAssertEqual(error.code, AuthErrorCode.invalidRecipientEmail.rawValue)
- sentVerificationEmailExpectation.fulfill()
- }
- } receiveValue: { authResult in
- XCTFail("💥 result unexpected")
- }
- .store(in: &cancellables)
- // then
- wait(for: [sentVerificationEmailExpectation], timeout: expectationTimeout)
- }
- }
|