|
102 | 102 | */
|
103 | 103 | static NSString *const kFakeEncodedFirebaseAppID = @"app-1-123456789-ios-123abc456def";
|
104 | 104 |
|
| 105 | +/** @var kFakeTenantID |
| 106 | + @brief A fake tenant ID. |
| 107 | + */ |
| 108 | +static NSString *const kFakeTenantID = @"tenantID"; |
| 109 | + |
105 | 110 | /** @var kFakeOAuthResponseURL
|
106 | 111 | @brief A fake OAuth response URL used in test.
|
107 | 112 | */
|
@@ -313,6 +318,111 @@ - (void)testGetCredentialWithUIDelegateWithClientID {
|
313 | 318 | XCTAssertEqualObjects(params[@"apiKey"], kFakeAPIKey);
|
314 | 319 | XCTAssertEqualObjects(params[@"authType"], @"signInWithRedirect");
|
315 | 320 | XCTAssertNotNil(params[@"v"]);
|
| 321 | + XCTAssertNil(params[@"tid"]); |
| 322 | + // `callbackMatcher` is at index 4 |
| 323 | + [invocation getArgument:&unretainedArgument atIndex:4]; |
| 324 | + FIRAuthURLCallbackMatcher callbackMatcher = unretainedArgument; |
| 325 | + NSMutableString *redirectURL = [NSMutableString |
| 326 | + stringWithString:[kFakeReverseClientID |
| 327 | + stringByAppendingString:kFakeRedirectURLResponseURL]]; |
| 328 | + // Add fake OAuthResponse to callback. |
| 329 | + [redirectURL appendString:kFakeOAuthResponseURL]; |
| 330 | + // Verify that the URL is rejected by the callback matcher without the event ID. |
| 331 | + XCTAssertFalse(callbackMatcher([NSURL URLWithString:redirectURL])); |
| 332 | + [redirectURL appendString:@"%26eventId%3D"]; |
| 333 | + [redirectURL appendString:params[@"eventId"]]; |
| 334 | + NSURLComponents *originalComponents = [[NSURLComponents alloc] initWithString:redirectURL]; |
| 335 | + // Verify that the URL is accepted by the callback matcher with the matching event ID. |
| 336 | + XCTAssertTrue(callbackMatcher([originalComponents URL])); |
| 337 | + NSURLComponents *components = [originalComponents copy]; |
| 338 | + components.query = @"https"; |
| 339 | + XCTAssertFalse(callbackMatcher([components URL])); |
| 340 | + components = [originalComponents copy]; |
| 341 | + components.host = @"badhost"; |
| 342 | + XCTAssertFalse(callbackMatcher([components URL])); |
| 343 | + components = [originalComponents copy]; |
| 344 | + components.path = @"badpath"; |
| 345 | + XCTAssertFalse(callbackMatcher([components URL])); |
| 346 | + components = [originalComponents copy]; |
| 347 | + components.query = @"badquery"; |
| 348 | + XCTAssertFalse(callbackMatcher([components URL])); |
| 349 | + |
| 350 | + // `completion` is at index 5 |
| 351 | + [invocation getArgument:&unretainedArgument atIndex:5]; |
| 352 | + FIRAuthURLPresentationCompletion completion = unretainedArgument; |
| 353 | + dispatch_async(FIRAuthGlobalWorkQueue(), ^() { |
| 354 | + completion(originalComponents.URL, nil); |
| 355 | + }); |
| 356 | + }); |
| 357 | + |
| 358 | + XCTestExpectation *expectation = [self expectationWithDescription:@"callback"]; |
| 359 | + [_provider |
| 360 | + getCredentialWithUIDelegate:mockUIDelegate |
| 361 | + completion:^(FIRAuthCredential *_Nullable credential, |
| 362 | + NSError *_Nullable error) { |
| 363 | + XCTAssertTrue([NSThread isMainThread]); |
| 364 | + XCTAssertNil(error); |
| 365 | + XCTAssertTrue([credential isKindOfClass:[FIROAuthCredential class]]); |
| 366 | + FIROAuthCredential *OAuthCredential = (FIROAuthCredential *)credential; |
| 367 | + XCTAssertEqualObjects(kFakeOAuthResponseURL, |
| 368 | + OAuthCredential.OAuthResponseURLString); |
| 369 | + [expectation fulfill]; |
| 370 | + }]; |
| 371 | + [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil]; |
| 372 | + OCMVerifyAll(_mockBackend); |
| 373 | +} |
| 374 | + |
| 375 | +/** @fn testGetCredentialWithUIDelegateWithTenantID |
| 376 | + @brief Tests a successful invocation of @c getCredentialWithUIDelegte:completion: |
| 377 | + */ |
| 378 | +- (void)testGetCredentialWithUIDelegateWithTenantID { |
| 379 | + id mockBundle = OCMClassMock([NSBundle class]); |
| 380 | + OCMStub(ClassMethod([mockBundle mainBundle])).andReturn(mockBundle); |
| 381 | + OCMStub([mockBundle objectForInfoDictionaryKey:@"CFBundleURLTypes"]).andReturn(@[ |
| 382 | + @{@"CFBundleURLSchemes" : @[ kFakeReverseClientID ]} |
| 383 | + ]); |
| 384 | + OCMStub([mockBundle bundleIdentifier]).andReturn(kFakeBundleID); |
| 385 | + OCMStub([_mockAuth tenantID]).andReturn(kFakeTenantID); |
| 386 | + |
| 387 | + OCMStub([_mockOptions clientID]).andReturn(kFakeClientID); |
| 388 | + _provider = [FIROAuthProvider providerWithProviderID:kFakeProviderID auth:_mockAuth]; |
| 389 | + |
| 390 | + OCMExpect([_mockBackend getProjectConfig:[OCMArg any] callback:[OCMArg any]]) |
| 391 | + .andCallBlock2( |
| 392 | + ^(FIRGetProjectConfigRequest *request, FIRGetProjectConfigResponseCallback callback) { |
| 393 | + XCTAssertNotNil(request); |
| 394 | + dispatch_async(FIRAuthGlobalWorkQueue(), ^() { |
| 395 | + id mockGetProjectConfigResponse = OCMClassMock([FIRGetProjectConfigResponse class]); |
| 396 | + OCMStub([mockGetProjectConfigResponse authorizedDomains]).andReturn(@[ |
| 397 | + kFakeAuthorizedDomain |
| 398 | + ]); |
| 399 | + callback(mockGetProjectConfigResponse, nil); |
| 400 | + }); |
| 401 | + }); |
| 402 | + |
| 403 | + id mockUIDelegate = OCMProtocolMock(@protocol(FIRAuthUIDelegate)); |
| 404 | + |
| 405 | + // Expect view controller presentation by UIDelegate. |
| 406 | + OCMExpect([_mockURLPresenter presentURL:OCMOCK_ANY |
| 407 | + UIDelegate:mockUIDelegate |
| 408 | + callbackMatcher:OCMOCK_ANY |
| 409 | + completion:OCMOCK_ANY]) |
| 410 | + .andDo(^(NSInvocation *invocation) { |
| 411 | + __unsafe_unretained id unretainedArgument; |
| 412 | + // Indices 0 and 1 indicate the hidden arguments self and _cmd. |
| 413 | + // `presentURL` is at index 2. |
| 414 | + [invocation getArgument:&unretainedArgument atIndex:2]; |
| 415 | + NSURL *presentURL = unretainedArgument; |
| 416 | + XCTAssertEqualObjects(presentURL.scheme, @"https"); |
| 417 | + XCTAssertEqualObjects(presentURL.host, kFakeAuthorizedDomain); |
| 418 | + XCTAssertEqualObjects(presentURL.path, @"/__/auth/handler"); |
| 419 | + NSDictionary *params = [FIRAuthWebUtils dictionaryWithHttpArgumentsString:presentURL.query]; |
| 420 | + XCTAssertEqualObjects(params[@"ibi"], kFakeBundleID); |
| 421 | + XCTAssertEqualObjects(params[@"clientId"], kFakeClientID); |
| 422 | + XCTAssertEqualObjects(params[@"apiKey"], kFakeAPIKey); |
| 423 | + XCTAssertEqualObjects(params[@"authType"], @"signInWithRedirect"); |
| 424 | + XCTAssertEqualObjects(params[@"tid"], kFakeTenantID); |
| 425 | + XCTAssertNotNil(params[@"v"]); |
316 | 426 | // `callbackMatcher` is at index 4
|
317 | 427 | [invocation getArgument:&unretainedArgument atIndex:4];
|
318 | 428 | FIRAuthURLCallbackMatcher callbackMatcher = unretainedArgument;
|
@@ -416,6 +526,7 @@ - (void)testGetCredentialWithUIDelegateUserCancellationWithClientID {
|
416 | 526 | XCTAssertEqualObjects(params[@"apiKey"], kFakeAPIKey);
|
417 | 527 | XCTAssertEqualObjects(params[@"authType"], @"signInWithRedirect");
|
418 | 528 | XCTAssertNotNil(params[@"v"]);
|
| 529 | + XCTAssertNil(params[@"tid"]); |
419 | 530 | // `callbackMatcher` is at index 4
|
420 | 531 | [invocation getArgument:&unretainedArgument atIndex:4];
|
421 | 532 | FIRAuthURLCallbackMatcher callbackMatcher = unretainedArgument;
|
@@ -516,6 +627,7 @@ - (void)testGetCredentialWithUIDelegateNetworkRequestFailedWithClientID {
|
516 | 627 | XCTAssertEqualObjects(params[@"apiKey"], kFakeAPIKey);
|
517 | 628 | XCTAssertEqualObjects(params[@"authType"], @"signInWithRedirect");
|
518 | 629 | XCTAssertNotNil(params[@"v"]);
|
| 630 | + XCTAssertNil(params[@"tid"]); |
519 | 631 | // `callbackMatcher` is at index 4
|
520 | 632 | [invocation getArgument:&unretainedArgument atIndex:4];
|
521 | 633 | FIRAuthURLCallbackMatcher callbackMatcher = unretainedArgument;
|
@@ -614,6 +726,7 @@ - (void)testGetCredentialWithUIDelegateInternalErrorWithClientID {
|
614 | 726 | XCTAssertEqualObjects(params[@"apiKey"], kFakeAPIKey);
|
615 | 727 | XCTAssertEqualObjects(params[@"authType"], @"signInWithRedirect");
|
616 | 728 | XCTAssertNotNil(params[@"v"]);
|
| 729 | + XCTAssertNil(params[@"tid"]); |
617 | 730 | // `callbackMatcher` is at index 4
|
618 | 731 | [invocation getArgument:&unretainedArgument atIndex:4];
|
619 | 732 | FIRAuthURLCallbackMatcher callbackMatcher = unretainedArgument;
|
@@ -713,6 +826,7 @@ - (void)testGetCredentialWithUIDelegateInvalidClientID {
|
713 | 826 | XCTAssertEqualObjects(params[@"apiKey"], kFakeAPIKey);
|
714 | 827 | XCTAssertEqualObjects(params[@"authType"], @"signInWithRedirect");
|
715 | 828 | XCTAssertNotNil(params[@"v"]);
|
| 829 | + XCTAssertNil(params[@"tid"]); |
716 | 830 | // `callbackMatcher` is at index 4
|
717 | 831 | [invocation getArgument:&unretainedArgument atIndex:4];
|
718 | 832 | FIRAuthURLCallbackMatcher callbackMatcher = unretainedArgument;
|
@@ -812,6 +926,7 @@ - (void)testGetCredentialWithUIDelegateUnknownErrorWithClientID {
|
812 | 926 | XCTAssertEqualObjects(params[@"apiKey"], kFakeAPIKey);
|
813 | 927 | XCTAssertEqualObjects(params[@"authType"], @"signInWithRedirect");
|
814 | 928 | XCTAssertNotNil(params[@"v"]);
|
| 929 | + XCTAssertNil(params[@"tid"]); |
815 | 930 | // `callbackMatcher` is at index 4
|
816 | 931 | [invocation getArgument:&unretainedArgument atIndex:4];
|
817 | 932 | FIRAuthURLCallbackMatcher callbackMatcher = unretainedArgument;
|
@@ -910,6 +1025,7 @@ - (void)testGetCredentialWithUIDelegateWithFirebaseAppID {
|
910 | 1025 | XCTAssertEqualObjects(params[@"apiKey"], kFakeAPIKey);
|
911 | 1026 | XCTAssertEqualObjects(params[@"authType"], @"signInWithRedirect");
|
912 | 1027 | XCTAssertNotNil(params[@"v"]);
|
| 1028 | + XCTAssertNil(params[@"tid"]); |
913 | 1029 | // `callbackMatcher` is at index 4
|
914 | 1030 | [invocation getArgument:&unretainedArgument atIndex:4];
|
915 | 1031 | FIRAuthURLCallbackMatcher callbackMatcher = unretainedArgument;
|
@@ -1014,6 +1130,7 @@ - (void)testGetCredentialWithUIDelegateWithFirebaseAppIDWhileClientIdPresent {
|
1014 | 1130 | XCTAssertEqualObjects(params[@"apiKey"], kFakeAPIKey);
|
1015 | 1131 | XCTAssertEqualObjects(params[@"authType"], @"signInWithRedirect");
|
1016 | 1132 | XCTAssertNotNil(params[@"v"]);
|
| 1133 | + XCTAssertNil(params[@"tid"]); |
1017 | 1134 | // `callbackMatcher` is at index 4
|
1018 | 1135 | [invocation getArgument:&unretainedArgument atIndex:4];
|
1019 | 1136 | FIRAuthURLCallbackMatcher callbackMatcher = unretainedArgument;
|
@@ -1108,6 +1225,7 @@ - (void)testGetCredentialWithUIDelegateUseEmulator {
|
1108 | 1225 | XCTAssertEqualObjects(params[@"apiKey"], kFakeAPIKey);
|
1109 | 1226 | XCTAssertEqualObjects(params[@"authType"], @"signInWithRedirect");
|
1110 | 1227 | XCTAssertNotNil(params[@"v"]);
|
| 1228 | + XCTAssertNil(params[@"tid"]); |
1111 | 1229 | // `callbackMatcher` is at index 4
|
1112 | 1230 | [invocation getArgument:&unretainedArgument atIndex:4];
|
1113 | 1231 | FIRAuthURLCallbackMatcher callbackMatcher = unretainedArgument;
|
|
0 commit comments