Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions Example/Auth/ApiTests/PhoneMultiFactorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright 2019 Google
*
* 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 XCTest

let kNoSecondFactorUserEmail = "iosapitests+no_second_factor@gmail.com"
let kNoSecondFactorUserPassword = "aaaaaa"

let kPhoneSecondFactorPhoneNumber = "+16509999999"
let kPhoneSecondFactorVerificationCode = "123456"
let kPhoneSecondFactorDisplayName = "phone1"

let kOneSecondFactorUserEmail = "iosapitests+one_phone_second_factor@gmail.com"
let kOneSecondFactorUserPassword = "aaaaaa"

class PhoneMultiFactorTests: FIRAuthApiTestsBase {

func testEnrollUnenroll() {
let enrollExpectation = self.expectation(description: "Enroll phone multi factor finished.")
let unenrollExpectation = self.expectation(description: "Unenroll phone multi factor finished.")
Auth.auth().signIn(withEmail: kNoSecondFactorUserEmail, password: kNoSecondFactorUserPassword) { (result, error) in
XCTAssertNil(error, "User normal sign in failed. Error: \(error!.localizedDescription)")

// Enroll
guard let user = result?.user else {
XCTFail("No valid user after attempted sign-in.")
}
user.multiFactor.getSessionWithCompletion({ (session, error) in
XCTAssertNil(error, "Get multi factor session failed. Error: \(error!.localizedDescription)")
PhoneAuthProvider.provider().verifyPhoneNumber(
kPhoneSecondFactorPhoneNumber,
uiDelegate: nil,
multiFactorSession: session) { (verificationId, error) in
XCTAssertNil(error, "Verify phone number failed. Error: \(error!.localizedDescription)")
let credential = PhoneAuthProvider.provider().credential(
withVerificationID: verificationId!,
verificationCode: kPhoneSecondFactorVerificationCode)
let assertion = PhoneMultiFactorGenerator.assertion(with: credential);
user?.multiFactor.enroll(with: assertion, displayName: kPhoneSecondFactorDisplayName) { (error) in
XCTAssertNil(error, "Phone multi factor enroll failed. Error: \(error!.localizedDescription)")
XCTAssertEqual(Auth.auth().currentUser?.multiFactor.enrolledFactors.first?.displayName, kPhoneSecondFactorDisplayName)
enrollExpectation.fulfill()

// Unenroll
user = Auth.auth().currentUser
user?.multiFactor.unenroll(with: (user?.multiFactor.enrolledFactors.first)!, completion: { (error) in
XCTAssertNil(error, "Phone multi factor unenroll failed. Error: \(error!.localizedDescription)")
XCTAssertEqual(Auth.auth().currentUser?.multiFactor.enrolledFactors.count, 0)
unenrollExpectation.fulfill()
})
}
}
})
}

self.waitForExpectations(timeout: 30) { error in
XCTAssertNil(error, "Failed to wait for enroll and unenroll phone multi factor finished. Error: \(error!.localizedDescription)")
}
}

func testSignInWithSecondFactor() {
let signInExpectation = self.expectation(description: "Sign in with phone multi factor finished.")
Auth.auth().signIn(withEmail: kOneSecondFactorUserEmail, password: kOneSecondFactorUserPassword) { (result, error) in
// SignIn
guard let error = error, error.code == AuthErrorCode.secondFactorRequired.rawValue else {
XCTFail("User sign in returns wrong error. Error: \(error!.localizedDescription)")
}
let resolver = error.userInfo["FIRAuthErrorUserInfoMultiFactorResolverKey"] as! MultiFactorResolver
let hint = resolver.hints.first as! PhoneMultiFactorInfo
PhoneAuthProvider.provider().verifyPhoneNumber(
with: hint,
uiDelegate: nil,
multiFactorSession: resolver.session) { (verificationId, error) in
XCTAssertNil(error, "Failed to verify phone number. Error: \(error!.localizedDescription)")
let credential = PhoneAuthProvider.provider().credential(
withVerificationID: verificationId!,
verificationCode: kPhoneSecondFactorVerificationCode)
let assertion = PhoneMultiFactorGenerator.assertion(with: credential);
resolver.resolveSignIn(with: assertion) { (authResult, error) in
XCTAssertNil(error, "Failed to sign in with phone multi factor. Error: \(error!.localizedDescription)")
signInExpectation.fulfill()
}
}
}

self.waitForExpectations(timeout: 300) { error in
XCTAssertNil(error, "Failed to wait for enroll and unenroll phone multi factor finished. Error: \(error!.localizedDescription)")
}
}
}
104 changes: 55 additions & 49 deletions Example/Auth/AuthSample/AuthSample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
400283EA23EA254B0006A298 /* MainViewController+MultiFactor.m in Sources */ = {isa = PBXBuildFile; fileRef = 400283E923EA254A0006A298 /* MainViewController+MultiFactor.m */; };
DE800B4722A2F8AF00AC9A23 /* MainViewController+Custom.m in Sources */ = {isa = PBXBuildFile; fileRef = DE800B1122A2F8AF00AC9A23 /* MainViewController+Custom.m */; };
DE800B4822A2F8AF00AC9A23 /* SettingsViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = DE800B1222A2F8AF00AC9A23 /* SettingsViewController.m */; };
DE800B4922A2F8AF00AC9A23 /* MainViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = DE800B1322A2F8AF00AC9A23 /* MainViewController.xib */; };
Expand Down Expand Up @@ -69,6 +70,8 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
400283E823EA254A0006A298 /* MainViewController+MultiFactor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "MainViewController+MultiFactor.h"; sourceTree = "<group>"; };
400283E923EA254A0006A298 /* MainViewController+MultiFactor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "MainViewController+MultiFactor.m"; sourceTree = "<group>"; };
DE800AF422A2F87E00AC9A23 /* AuthSample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AuthSample.app; sourceTree = BUILT_PRODUCTS_DIR; };
DE800B0E22A2F8AF00AC9A23 /* AppManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppManager.h; sourceTree = "<group>"; };
DE800B0F22A2F8AF00AC9A23 /* MainViewController+AutoTests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "MainViewController+AutoTests.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -210,68 +213,70 @@
DE800B0D22A2F8AF00AC9A23 /* Sample */ = {
isa = PBXGroup;
children = (
DE800B6C22A2FFFF00AC9A23 /* AuthCredentials.h */,
DE800B6922A2FF8700AC9A23 /* Application.plist */,
DE800B6A22A2FF8700AC9A23 /* Sample.entitlements */,
DE800B1F22A2F8AF00AC9A23 /* ApplicationDelegate.h */,
DE800B3C22A2F8AF00AC9A23 /* ApplicationDelegate.m */,
DE800B2722A2F8AF00AC9A23 /* ApplicationTemplate.plist */,
DE800B0E22A2F8AF00AC9A23 /* AppManager.h */,
DE800B2D22A2F8AF00AC9A23 /* AppManager.m */,
DE800B6C22A2FFFF00AC9A23 /* AuthCredentials.h */,
DE800B4622A2F8AF00AC9A23 /* AuthCredentialsTemplate.h */,
DE800B2622A2F8AF00AC9A23 /* AuthProviders.h */,
DE800B4522A2F8AF00AC9A23 /* AuthProviders.m */,
DE800B1D22A2F8AF00AC9A23 /* CustomTokenDataEntryViewController.h */,
DE800B3D22A2F8AF00AC9A23 /* CustomTokenDataEntryViewController.m */,
DE800B1E22A2F8AF00AC9A23 /* FacebookAuthProvider.h */,
DE800B3B22A2F8AF00AC9A23 /* FacebookAuthProvider.m */,
DE800B3322A2F8AF00AC9A23 /* GoogleAuthProvider.h */,
DE800B1422A2F8AF00AC9A23 /* GoogleAuthProvider.m */,
DE800B6522A2FF7300AC9A23 /* GoogleService-Info_multi.plist */,
DE800B6622A2FF7300AC9A23 /* GoogleService-Info.plist */,
DE800B0E22A2F8AF00AC9A23 /* AppManager.h */,
DE800B0F22A2F8AF00AC9A23 /* MainViewController+AutoTests.h */,
DE800B1022A2F8AF00AC9A23 /* MainViewController+OOB.h */,
DE800B1122A2F8AF00AC9A23 /* MainViewController+Custom.m */,
DE800B1222A2F8AF00AC9A23 /* SettingsViewController.m */,
DE800B3222A2F8AF00AC9A23 /* Images.xcassets */,
DE800B2E22A2F8AF00AC9A23 /* main.m */,
DE800B4422A2F8AF00AC9A23 /* MainViewController.h */,
DE800B2922A2F8AF00AC9A23 /* MainViewController.m */,
DE800B1322A2F8AF00AC9A23 /* MainViewController.xib */,
DE800B1422A2F8AF00AC9A23 /* GoogleAuthProvider.m */,
DE800B1522A2F8AF00AC9A23 /* UserInfoViewController.h */,
DE800B1622A2F8AF00AC9A23 /* MainViewController+OAuth.h */,
DE800B1722A2F8AF00AC9A23 /* MainViewController+App.h */,
DE800B1822A2F8AF00AC9A23 /* MainViewController+User.m */,
DE800B1922A2F8AF00AC9A23 /* MainViewController+GameCenter.m */,
DE800B1A22A2F8AF00AC9A23 /* MainViewController+Phone.m */,
DE800B1B22A2F8AF00AC9A23 /* UserTableViewCell.m */,
DE800B1C22A2F8AF00AC9A23 /* MainViewController+Facebook.m */,
DE800B1D22A2F8AF00AC9A23 /* CustomTokenDataEntryViewController.h */,
DE800B1E22A2F8AF00AC9A23 /* FacebookAuthProvider.h */,
DE800B1F22A2F8AF00AC9A23 /* ApplicationDelegate.h */,
DE800B2022A2F8AF00AC9A23 /* UIViewController+Alerts.m */,
DE800B2122A2F8AF00AC9A23 /* MainViewController+Google.h */,
DE800B2222A2F8AF00AC9A23 /* MainViewController+Email.m */,
DE800B2F22A2F8AF00AC9A23 /* MainViewController+App.m */,
DE800B3822A2F8AF00AC9A23 /* MainViewController+Auth.h */,
DE800B2322A2F8AF00AC9A23 /* MainViewController+Auth.m */,
DE800B2422A2F8AF00AC9A23 /* StaticContentTableViewManager.h */,
DE800B2522A2F8AF00AC9A23 /* SampleTemplate.entitlements */,
DE800B2622A2F8AF00AC9A23 /* AuthProviders.h */,
DE800B2722A2F8AF00AC9A23 /* ApplicationTemplate.plist */,
DE800B2822A2F8AF00AC9A23 /* UserInfoViewController.xib */,
DE800B2922A2F8AF00AC9A23 /* MainViewController.m */,
DE800B2A22A2F8AF00AC9A23 /* MainViewController+Custom.h */,
DE800B0F22A2F8AF00AC9A23 /* MainViewController+AutoTests.h */,
DE800B2B22A2F8AF00AC9A23 /* MainViewController+AutoTests.m */,
DE800B2C22A2F8AF00AC9A23 /* MainViewController+OOB.m */,
DE800B2D22A2F8AF00AC9A23 /* AppManager.m */,
DE800B2E22A2F8AF00AC9A23 /* main.m */,
DE800B2F22A2F8AF00AC9A23 /* MainViewController+App.m */,
DE800B2A22A2F8AF00AC9A23 /* MainViewController+Custom.h */,
DE800B1122A2F8AF00AC9A23 /* MainViewController+Custom.m */,
DE800B3722A2F8AF00AC9A23 /* MainViewController+Email.h */,
DE800B2222A2F8AF00AC9A23 /* MainViewController+Email.m */,
DE800B3E22A2F8AF00AC9A23 /* MainViewController+Facebook.h */,
DE800B1C22A2F8AF00AC9A23 /* MainViewController+Facebook.m */,
DE800B4222A2F8AF00AC9A23 /* MainViewController+GameCenter.h */,
DE800B1922A2F8AF00AC9A23 /* MainViewController+GameCenter.m */,
DE800B2122A2F8AF00AC9A23 /* MainViewController+Google.h */,
DE800B3922A2F8AF00AC9A23 /* MainViewController+Google.m */,
DE800B3022A2F8AF00AC9A23 /* MainViewController+Internal.h */,
400283E823EA254A0006A298 /* MainViewController+MultiFactor.h */,
400283E923EA254A0006A298 /* MainViewController+MultiFactor.m */,
DE800B1622A2F8AF00AC9A23 /* MainViewController+OAuth.h */,
DE800B3122A2F8AF00AC9A23 /* MainViewController+OAuth.m */,
DE800B3222A2F8AF00AC9A23 /* Images.xcassets */,
DE800B3322A2F8AF00AC9A23 /* GoogleAuthProvider.h */,
DE800B3422A2F8AF00AC9A23 /* UserInfoViewController.m */,
DE800B1022A2F8AF00AC9A23 /* MainViewController+OOB.h */,
DE800B2C22A2F8AF00AC9A23 /* MainViewController+OOB.m */,
DE800B3F22A2F8AF00AC9A23 /* MainViewController+Phone.h */,
DE800B1A22A2F8AF00AC9A23 /* MainViewController+Phone.m */,
DE800B4322A2F8AF00AC9A23 /* MainViewController+User.h */,
DE800B1822A2F8AF00AC9A23 /* MainViewController+User.m */,
DE800B6A22A2FF8700AC9A23 /* Sample.entitlements */,
DE800B2522A2F8AF00AC9A23 /* SampleTemplate.entitlements */,
DE800B3522A2F8AF00AC9A23 /* SettingsViewController.h */,
DE800B1222A2F8AF00AC9A23 /* SettingsViewController.m */,
DE800B4122A2F8AF00AC9A23 /* SettingsViewController.xib */,
DE800B2422A2F8AF00AC9A23 /* StaticContentTableViewManager.h */,
DE800B3622A2F8AF00AC9A23 /* StaticContentTableViewManager.m */,
DE800B3722A2F8AF00AC9A23 /* MainViewController+Email.h */,
DE800B3822A2F8AF00AC9A23 /* MainViewController+Auth.h */,
DE800B3922A2F8AF00AC9A23 /* MainViewController+Google.m */,
DE800B3A22A2F8AF00AC9A23 /* UIViewController+Alerts.h */,
DE800B3B22A2F8AF00AC9A23 /* FacebookAuthProvider.m */,
DE800B3C22A2F8AF00AC9A23 /* ApplicationDelegate.m */,
DE800B3D22A2F8AF00AC9A23 /* CustomTokenDataEntryViewController.m */,
DE800B3E22A2F8AF00AC9A23 /* MainViewController+Facebook.h */,
DE800B3F22A2F8AF00AC9A23 /* MainViewController+Phone.h */,
DE800B2022A2F8AF00AC9A23 /* UIViewController+Alerts.m */,
DE800B1522A2F8AF00AC9A23 /* UserInfoViewController.h */,
DE800B3422A2F8AF00AC9A23 /* UserInfoViewController.m */,
DE800B2822A2F8AF00AC9A23 /* UserInfoViewController.xib */,
DE800B4022A2F8AF00AC9A23 /* UserTableViewCell.h */,
DE800B4122A2F8AF00AC9A23 /* SettingsViewController.xib */,
DE800B4222A2F8AF00AC9A23 /* MainViewController+GameCenter.h */,
DE800B4322A2F8AF00AC9A23 /* MainViewController+User.h */,
DE800B4422A2F8AF00AC9A23 /* MainViewController.h */,
DE800B4522A2F8AF00AC9A23 /* AuthProviders.m */,
DE800B4622A2F8AF00AC9A23 /* AuthCredentialsTemplate.h */,
DE800B1B22A2F8AF00AC9A23 /* UserTableViewCell.m */,
);
name = Sample;
path = ../Sample;
Expand Down Expand Up @@ -449,6 +454,7 @@
DE800B4F22A2F8AF00AC9A23 /* MainViewController+Facebook.m in Sources */,
DE800B4D22A2F8AF00AC9A23 /* MainViewController+Phone.m in Sources */,
DE800B4722A2F8AF00AC9A23 /* MainViewController+Custom.m in Sources */,
400283EA23EA254B0006A298 /* MainViewController+MultiFactor.m in Sources */,
DE800B5A22A2F8AF00AC9A23 /* MainViewController+App.m in Sources */,
DE800B6222A2F8AF00AC9A23 /* CustomTokenDataEntryViewController.m in Sources */,
DE800B6122A2F8AF00AC9A23 /* ApplicationDelegate.m in Sources */,
Expand Down
2 changes: 1 addition & 1 deletion Example/Auth/E2eTests/FIRAuthE2eTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ - (void)testSignInExistingUser {
grey_allOf(grey_kindOfClass([UILabel class]), grey_accessibilityLabel(@"OK"), nil);

[[EarlGrey selectElementWithMatcher:
#warning TODO Add accessibilityIdentifiers for the elements.
// TODO: Add accessibilityIdentifiers for the elements.
grey_kindOfClass(NSClassFromString(@"_UIAlertControllerView"))]
performAction:grey_typeText(email)];

Expand Down
1 change: 1 addition & 0 deletions Example/Auth/Sample/MainViewController+App.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#import <Foundation/Foundation.h>

#import "MainViewController.h"

#import "StaticContentTableViewManager.h"

NS_ASSUME_NONNULL_BEGIN
Expand Down
2 changes: 2 additions & 0 deletions Example/Auth/Sample/MainViewController+Auth.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "MainViewController.h"

#import "FIRAuth.h"
Expand Down
2 changes: 2 additions & 0 deletions Example/Auth/Sample/MainViewController+AutoTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "MainViewController.h"

#import "StaticContentTableViewManager.h"
Expand Down
2 changes: 2 additions & 0 deletions Example/Auth/Sample/MainViewController+Custom.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

#import <Foundation/Foundation.h>

#import "MainViewController.h"

#import "StaticContentTableViewManager.h"
Expand Down
3 changes: 2 additions & 1 deletion Example/Auth/Sample/MainViewController+Email.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

#import <Foundation/Foundation.h>

#import "FIRAuthCredential.h"
#import "MainViewController.h"

#import "FIRAuth.h"
#import "FIRAuthCredential.h"
#import "StaticContentTableViewManager.h"

NS_ASSUME_NONNULL_BEGIN
Expand Down
Loading