|
20 | 20 |
|
21 | 21 | #import <FirebaseAuthInterop/FIRAuthInterop.h>
|
22 | 22 | #import <FirebaseCore/FIRAppInternal.h>
|
| 23 | + |
| 24 | +#import <FirebaseAuth/FIREmailAuthProvider.h> |
| 25 | +#import <FirebaseAuth/FIRGoogleAuthProvider.h> |
| 26 | +#import <FirebaseAuth/FIRAdditionalUserInfo.h> |
| 27 | + |
23 | 28 | #import <FirebaseCore/FIRComponent.h>
|
24 | 29 | #import <FirebaseCore/FIRLibrary.h>
|
25 | 30 |
|
|
41 | 46 | #import "FIRGetAccountInfoResponse.h"
|
42 | 47 | #import "FIRGetOOBConfirmationCodeRequest.h"
|
43 | 48 | #import "FIRGetOOBConfirmationCodeResponse.h"
|
44 |
| -#import "FIRGoogleAuthProvider.h" |
| 49 | +#import "FIROAuthProvider.h" |
45 | 50 | #import "FIRSecureTokenRequest.h"
|
46 | 51 | #import "FIRSecureTokenResponse.h"
|
47 | 52 | #import "FIRResetPasswordRequest.h"
|
|
59 | 64 | #import "FIRVerifyPhoneNumberRequest.h"
|
60 | 65 | #import "FIRVerifyPhoneNumberResponse.h"
|
61 | 66 | #import "FIRApp+FIRAuthUnitTests.h"
|
| 67 | +#import "OAuth/FIROAuthCredential_Internal.h" |
62 | 68 | #import "OCMStubRecorder+FIRAuthUnitTests.h"
|
63 | 69 | #import <OCMock/OCMock.h>
|
64 | 70 | #import "FIRActionCodeSettings.h"
|
65 | 71 |
|
66 | 72 | #if TARGET_OS_IOS
|
| 73 | +#import "FIRAuthUIDelegate.h" |
67 | 74 | #import "FIRPhoneAuthCredential.h"
|
68 | 75 | #import "FIRPhoneAuthProvider.h"
|
69 | 76 | #endif
|
|
163 | 170 | */
|
164 | 171 | static NSString *const kVerificationID = @"55432";
|
165 | 172 |
|
| 173 | +/** @var kOAuthRequestURI |
| 174 | + @brief Fake OAuthRequest URI for testing. |
| 175 | + */ |
| 176 | +static NSString *const kOAuthRequestURI = @"requestURI"; |
| 177 | + |
| 178 | +/** @var kOAuthSessionID |
| 179 | + @brief Fake session ID for testing. |
| 180 | + */ |
| 181 | +static NSString *const kOAuthSessionID = @"sessionID"; |
| 182 | + |
| 183 | +/** @var kFakeWebSignInUserInteractionFailureReason |
| 184 | + @brief Fake reason for FIRAuthErrorCodeWebSignInUserInteractionFailure error while testing. |
| 185 | + */ |
| 186 | +static NSString *const kFakeWebSignInUserInteractionFailureReason = @"fake_reason"; |
| 187 | + |
166 | 188 | /** @var kContinueURL
|
167 | 189 | @brief Fake string value of continue url.
|
168 | 190 | */
|
@@ -1116,6 +1138,91 @@ - (void)testSignInWithEmailCredentialEmptyPassword {
|
1116 | 1138 | [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
|
1117 | 1139 | }
|
1118 | 1140 |
|
| 1141 | +#if TARGET_OS_IOS |
| 1142 | +/** @fn testSignInWithProviderSuccess |
| 1143 | + @brief Tests a successful @c signInWithProvider:UIDelegate:completion: call with an OAuth |
| 1144 | + provider configured for Google. |
| 1145 | + */ |
| 1146 | +- (void)testSignInWithProviderSuccess { |
| 1147 | + OCMExpect([_mockBackend verifyAssertion:[OCMArg any] callback:[OCMArg any]]) |
| 1148 | + .andCallBlock2(^(FIRVerifyAssertionRequest *_Nullable request, |
| 1149 | + FIRVerifyAssertionResponseCallback callback) { |
| 1150 | + XCTAssertEqualObjects(request.APIKey, kAPIKey); |
| 1151 | + XCTAssertEqualObjects(request.providerID, FIRGoogleAuthProviderID); |
| 1152 | + XCTAssertTrue(request.returnSecureToken); |
| 1153 | + dispatch_async(FIRAuthGlobalWorkQueue(), ^() { |
| 1154 | + id mockVerifyAssertionResponse = OCMClassMock([FIRVerifyAssertionResponse class]); |
| 1155 | + OCMStub([mockVerifyAssertionResponse federatedID]).andReturn(kGoogleID); |
| 1156 | + OCMStub([mockVerifyAssertionResponse providerID]).andReturn(FIRGoogleAuthProviderID); |
| 1157 | + OCMStub([mockVerifyAssertionResponse localID]).andReturn(kLocalID); |
| 1158 | + OCMStub([mockVerifyAssertionResponse displayName]).andReturn(kGoogleDisplayName); |
| 1159 | + [self stubTokensWithMockResponse:mockVerifyAssertionResponse]; |
| 1160 | + callback(mockVerifyAssertionResponse, nil); |
| 1161 | + }); |
| 1162 | + }); |
| 1163 | + [self expectGetAccountInfoGoogle]; |
| 1164 | + XCTestExpectation *expectation = [self expectationWithDescription:@"callback"]; |
| 1165 | + [[FIRAuth auth] signOut:NULL]; |
| 1166 | + id mockProvider = OCMClassMock([FIROAuthProvider class]); |
| 1167 | + OCMExpect([mockProvider getCredentialWithUIDelegate:[OCMArg any] completion:[OCMArg any]]) |
| 1168 | + .andCallBlock2(^(id<FIRAuthUIDelegate> delegate, FIRAuthCredentialCallback callback) { |
| 1169 | + dispatch_async(FIRAuthGlobalWorkQueue(), ^(){ |
| 1170 | + FIROAuthCredential *credential = |
| 1171 | + [[FIROAuthCredential alloc] initWithProviderID:FIRGoogleAuthProviderID |
| 1172 | + sessionID:kOAuthSessionID |
| 1173 | + OAuthResponseURLString:kOAuthRequestURI]; |
| 1174 | + callback(credential, nil); |
| 1175 | + }); |
| 1176 | + }); |
| 1177 | + [[FIRAuth auth] signInWithProvider:mockProvider |
| 1178 | + UIDelegate:nil |
| 1179 | + completion:^(FIRAuthDataResult *_Nullable authResult, |
| 1180 | + NSError *_Nullable error) { |
| 1181 | + XCTAssertTrue([NSThread isMainThread]); |
| 1182 | + [self assertUserGoogle:authResult.user]; |
| 1183 | + XCTAssertNil(error); |
| 1184 | + [expectation fulfill]; |
| 1185 | + }]; |
| 1186 | + [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; |
| 1187 | + OCMVerifyAll(_mockBackend); |
| 1188 | +} |
| 1189 | + |
| 1190 | +/** @fn testSignInWithProviderFailure |
| 1191 | + @brief Tests a failed @c signInWithProvider:UIDelegate:completion: call with the error code |
| 1192 | + FIRAuthErrorCodeWebSignInUserInteractionFailure. |
| 1193 | + */ |
| 1194 | +- (void)testSignInWithProviderFailure { |
| 1195 | + OCMExpect([_mockBackend verifyAssertion:[OCMArg any] callback:[OCMArg any]]) |
| 1196 | + .andDispatchError2([FIRAuthErrorUtils webSignInUserInteractionFailureWithReason: |
| 1197 | + kFakeWebSignInUserInteractionFailureReason]); |
| 1198 | + [[FIRAuth auth] signOut:NULL]; |
| 1199 | + id mockProvider = OCMClassMock([FIROAuthProvider class]); |
| 1200 | + OCMExpect([mockProvider getCredentialWithUIDelegate:[OCMArg any] completion:[OCMArg any]]) |
| 1201 | + .andCallBlock2(^(id<FIRAuthUIDelegate> delegate, FIRAuthCredentialCallback callback) { |
| 1202 | + dispatch_async(FIRAuthGlobalWorkQueue(), ^(){ |
| 1203 | + FIROAuthCredential *credential = |
| 1204 | + [[FIROAuthCredential alloc] initWithProviderID:FIRGoogleAuthProviderID |
| 1205 | + sessionID:kOAuthSessionID |
| 1206 | + OAuthResponseURLString:kOAuthRequestURI]; |
| 1207 | + callback(credential, nil); |
| 1208 | + }); |
| 1209 | + }); |
| 1210 | + XCTestExpectation *expectation = [self expectationWithDescription:@"callback"]; |
| 1211 | + [[FIRAuth auth] signInWithProvider:mockProvider |
| 1212 | + UIDelegate:nil |
| 1213 | + completion:^(FIRAuthDataResult *_Nullable authResult, |
| 1214 | + NSError *_Nullable error) { |
| 1215 | + XCTAssertTrue([NSThread isMainThread]); |
| 1216 | + XCTAssertNil(authResult); |
| 1217 | + XCTAssertEqual(error.code, FIRAuthErrorCodeWebSignInUserInteractionFailure); |
| 1218 | + XCTAssertEqualObjects(error.userInfo[NSLocalizedFailureReasonErrorKey], |
| 1219 | + kFakeWebSignInUserInteractionFailureReason); |
| 1220 | + [expectation fulfill]; |
| 1221 | + }]; |
| 1222 | + [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; |
| 1223 | + OCMVerifyAll(_mockBackend); |
| 1224 | +} |
| 1225 | + |
1119 | 1226 | /** @fn testSignInWithGoogleAccountExistsError
|
1120 | 1227 | @brief Tests the flow of a failed @c signInWithCredential:completion: with a Google credential
|
1121 | 1228 | where the backend returns a needs @needConfirmation equal to true. An
|
@@ -1195,6 +1302,64 @@ - (void)testSignInWithGoogleCredentialSuccess {
|
1195 | 1302 | OCMVerifyAll(_mockBackend);
|
1196 | 1303 | }
|
1197 | 1304 |
|
| 1305 | +/** @fn testSignInWithOAuthCredentialSuccess |
| 1306 | + @brief Tests the flow of a successful @c signInWithCredential:completion: call with a generic |
| 1307 | + OAuth credential (In this case, configured for the Google IDP). |
| 1308 | + */ |
| 1309 | +- (void)testSignInWithOAuthCredentialSuccess { |
| 1310 | + OCMExpect([_mockBackend verifyAssertion:[OCMArg any] callback:[OCMArg any]]) |
| 1311 | + .andCallBlock2(^(FIRVerifyAssertionRequest *_Nullable request, |
| 1312 | + FIRVerifyAssertionResponseCallback callback) { |
| 1313 | + XCTAssertEqualObjects(request.APIKey, kAPIKey); |
| 1314 | + XCTAssertEqualObjects(request.providerID, FIRGoogleAuthProviderID); |
| 1315 | + XCTAssertEqualObjects(request.requestURI, kOAuthRequestURI); |
| 1316 | + XCTAssertEqualObjects(request.sessionID, kOAuthSessionID); |
| 1317 | + XCTAssertTrue(request.returnSecureToken); |
| 1318 | + dispatch_async(FIRAuthGlobalWorkQueue(), ^() { |
| 1319 | + id mockVeriyAssertionResponse = OCMClassMock([FIRVerifyAssertionResponse class]); |
| 1320 | + OCMStub([mockVeriyAssertionResponse federatedID]).andReturn(kGoogleID); |
| 1321 | + OCMStub([mockVeriyAssertionResponse providerID]).andReturn(FIRGoogleAuthProviderID); |
| 1322 | + OCMStub([mockVeriyAssertionResponse localID]).andReturn(kLocalID); |
| 1323 | + OCMStub([mockVeriyAssertionResponse displayName]).andReturn(kGoogleDisplayName); |
| 1324 | + [self stubTokensWithMockResponse:mockVeriyAssertionResponse]; |
| 1325 | + callback(mockVeriyAssertionResponse, nil); |
| 1326 | + }); |
| 1327 | + }); |
| 1328 | + [self expectGetAccountInfoGoogle]; |
| 1329 | + XCTestExpectation *expectation = [self expectationWithDescription:@"callback"]; |
| 1330 | + [[FIRAuth auth] signOut:NULL]; |
| 1331 | + id mockProvider = OCMClassMock([FIROAuthProvider class]); |
| 1332 | + OCMExpect([mockProvider getCredentialWithUIDelegate:[OCMArg any] completion:[OCMArg any]]) |
| 1333 | + .andCallBlock2(^(id<FIRAuthUIDelegate> delegate, FIRAuthCredentialCallback callback) { |
| 1334 | + dispatch_async(FIRAuthGlobalWorkQueue(), ^(){ |
| 1335 | + FIROAuthCredential *credential = |
| 1336 | + [[FIROAuthCredential alloc] initWithProviderID:FIRGoogleAuthProviderID |
| 1337 | + sessionID:kOAuthSessionID |
| 1338 | + OAuthResponseURLString:kOAuthRequestURI]; |
| 1339 | + callback(credential, nil); |
| 1340 | + }); |
| 1341 | + }); |
| 1342 | + [mockProvider getCredentialWithUIDelegate:nil |
| 1343 | + completion:^(FIRAuthCredential *_Nullable credential, |
| 1344 | + NSError *_Nullable error) { |
| 1345 | + XCTAssertTrue([credential isKindOfClass:[FIROAuthCredential class]]); |
| 1346 | + FIROAuthCredential *OAuthCredential = (FIROAuthCredential *)credential; |
| 1347 | + XCTAssertEqualObjects(OAuthCredential.OAuthResponseURLString, kOAuthRequestURI); |
| 1348 | + XCTAssertEqualObjects(OAuthCredential.sessionID, kOAuthSessionID); |
| 1349 | + [[FIRAuth auth] signInWithCredential:OAuthCredential completion:^(FIRUser *_Nullable user, |
| 1350 | + NSError *_Nullable error) { |
| 1351 | + XCTAssertTrue([NSThread isMainThread]); |
| 1352 | + [self assertUserGoogle:user]; |
| 1353 | + XCTAssertNil(error); |
| 1354 | + [expectation fulfill]; |
| 1355 | + }]; |
| 1356 | + }]; |
| 1357 | + [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; |
| 1358 | + [self assertUserGoogle:[FIRAuth auth].currentUser]; |
| 1359 | + OCMVerifyAll(_mockBackend); |
| 1360 | +} |
| 1361 | +#endif // TARGET_OS_IOS |
| 1362 | + |
1198 | 1363 | /** @fn testSignInAndRetrieveDataWithCredentialSuccess
|
1199 | 1364 | @brief Tests the flow of a successful @c signInAndRetrieveDataWithCredential:completion: call
|
1200 | 1365 | with an Google Sign-In credential.
|
|
0 commit comments