FIROAuthProviderTests.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Copyright 2017 Google
  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 <TargetConditionals.h>
  17. #if TARGET_OS_IOS
  18. #import <XCTest/XCTest.h>
  19. @import FirebaseAuth;
  20. @import FirebaseCore;
  21. /** @var kExpectationTimeout
  22. @brief The maximum time waiting for expectations to fulfill.
  23. */
  24. static const NSTimeInterval kExpectationTimeout = 1;
  25. /** @var kFakeAuthorizedDomain
  26. @brief A fake authorized domain for the app.
  27. */
  28. static NSString *const kFakeAuthorizedDomain = @"test.firebaseapp.com";
  29. /** @var kFakeBundleID
  30. @brief A fake bundle ID.
  31. */
  32. static NSString *const kFakeBundleID = @"com.firebaseapp.example";
  33. /** @var kFakeAccessToken
  34. @brief A fake access token for testing.
  35. */
  36. static NSString *const kFakeAccessToken = @"fakeAccessToken";
  37. /** @var kFakeIDToken
  38. @brief A fake ID token for testing.
  39. */
  40. static NSString *const kFakeIDToken = @"fakeIDToken";
  41. /** @var kFakeProviderID
  42. @brief A fake provider ID for testing.
  43. */
  44. static NSString *const kFakeProviderID = @"fakeProviderID";
  45. /** @var kFakeGivenName
  46. @brief A fake given name for testing.
  47. */
  48. static NSString *const kFakeGivenName = @"fakeGivenName";
  49. /** @var kFakeFamilyName
  50. @brief A fake family name for testing.
  51. */
  52. static NSString *const kFakeFamilyName = @"fakeFamilyName";
  53. /** @var kFakeAPIKey
  54. @brief A fake API key.
  55. */
  56. static NSString *const kFakeAPIKey = @"asdfghjkl";
  57. /** @var kFakeEmulatorHost
  58. @brief A fake emulator host.
  59. */
  60. static NSString *const kFakeEmulatorHost = @"emulatorhost";
  61. /** @var kFakeEmulatorPort
  62. @brief A fake emulator port.
  63. */
  64. static NSString *const kFakeEmulatorPort = @"12345";
  65. /** @var kFakeClientID
  66. @brief A fake client ID.
  67. */
  68. static NSString *const kFakeClientID = @"123456.apps.googleusercontent.com";
  69. /** @var kFakeReverseClientID
  70. @brief The dot-reversed version of the fake client ID.
  71. */
  72. static NSString *const kFakeReverseClientID = @"com.googleusercontent.apps.123456";
  73. /** @var kFakeFirebaseAppID
  74. @brief A fake Firebase app ID.
  75. */
  76. static NSString *const kFakeFirebaseAppID = @"1:123456789:ios:123abc456def";
  77. /** @var kFakeEncodedFirebaseAppID
  78. @brief A fake encoded Firebase app ID to be used as a custom URL scheme.
  79. */
  80. static NSString *const kFakeEncodedFirebaseAppID = @"app-1-123456789-ios-123abc456def";
  81. /** @var kFakeTenantID
  82. @brief A fake tenant ID.
  83. */
  84. static NSString *const kFakeTenantID = @"tenantID";
  85. /** @var kFakeOAuthResponseURL
  86. @brief A fake OAuth response URL used in test.
  87. */
  88. static NSString *const kFakeOAuthResponseURL = @"fakeOAuthResponseURL";
  89. /** @var kFakeRedirectURLResponseURL
  90. @brief A fake callback URL (minus the scheme) containing a fake response URL.
  91. */
  92. @interface FIROAuthProviderTests : XCTestCase
  93. @end
  94. @implementation FIROAuthProviderTests
  95. /** @fn testObtainingOAuthCredentialNoIDToken
  96. @brief Tests the correct creation of an OAuthCredential without an IDToken.
  97. */
  98. - (void)testObtainingOAuthCredentialNoIDToken {
  99. FIRAuthCredential *credential = [FIROAuthProvider credentialWithProviderID:kFakeProviderID
  100. accessToken:kFakeAccessToken];
  101. XCTAssertTrue([credential isKindOfClass:[FIROAuthCredential class]]);
  102. FIROAuthCredential *OAuthCredential = (FIROAuthCredential *)credential;
  103. XCTAssertEqualObjects(OAuthCredential.accessToken, kFakeAccessToken);
  104. XCTAssertEqualObjects(OAuthCredential.provider, kFakeProviderID);
  105. XCTAssertNil(OAuthCredential.IDToken);
  106. }
  107. /** @fn testObtainingOAuthCredentialWithFullName
  108. @brief Tests the correct creation of an OAuthCredential with a fullName.
  109. */
  110. - (void)testObtainingOAuthCredentialWithFullName {
  111. NSPersonNameComponents *fullName = [[NSPersonNameComponents alloc] init];
  112. fullName.givenName = kFakeGivenName;
  113. fullName.familyName = kFakeFamilyName;
  114. FIRAuthCredential *credential = [FIROAuthProvider appleCredentialWithIDToken:kFakeIDToken
  115. rawNonce:nil
  116. fullName:fullName];
  117. XCTAssertTrue([credential isKindOfClass:[FIROAuthCredential class]]);
  118. FIROAuthCredential *OAuthCredential = (FIROAuthCredential *)credential;
  119. XCTAssertEqualObjects(OAuthCredential.provider, @"apple.com");
  120. XCTAssertEqualObjects(OAuthCredential.IDToken, kFakeIDToken);
  121. XCTAssertNil(OAuthCredential.accessToken);
  122. }
  123. /** @fn testObtainingOAuthCredentialWithIDToken
  124. @brief Tests the correct creation of an OAuthCredential with an IDToken
  125. */
  126. - (void)testObtainingOAuthCredentialWithIDToken {
  127. FIRAuthCredential *credential = [FIROAuthProvider credentialWithProviderID:kFakeProviderID
  128. IDToken:kFakeIDToken
  129. accessToken:kFakeAccessToken];
  130. XCTAssertTrue([credential isKindOfClass:[FIROAuthCredential class]]);
  131. FIROAuthCredential *OAuthCredential = (FIROAuthCredential *)credential;
  132. XCTAssertEqualObjects(OAuthCredential.accessToken, kFakeAccessToken);
  133. XCTAssertEqualObjects(OAuthCredential.provider, kFakeProviderID);
  134. XCTAssertEqualObjects(OAuthCredential.IDToken, kFakeIDToken);
  135. }
  136. /** @fn testGetCredentialWithUIDelegateWithClientIDOnMainThread
  137. @brief Verifies @c getCredentialWithUIDelegate:completion: calls its completion handler on the
  138. main thread. Regression test for firebase/FirebaseUI-iOS#1199.
  139. */
  140. - (void)testGetCredentialWithUIDelegateWithClientIDOnMainThread {
  141. XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
  142. FIROptions *options =
  143. [[FIROptions alloc] initWithGoogleAppID:@"0:0000000000000:ios:0000000000000000"
  144. GCMSenderID:@"00000000000000000-00000000000-000000000"];
  145. options.APIKey = kFakeAPIKey;
  146. options.projectID = @"myProjectID";
  147. options.clientID = kFakeClientID;
  148. [FIRApp configureWithName:@"objAppName" options:options];
  149. FIRAuth *auth = [FIRAuth authWithApp:[FIRApp appNamed:@"objAppName"]];
  150. [auth setMainBundleUrlTypes:@[ @{@"CFBundleURLSchemes" : @[ kFakeReverseClientID ]} ]];
  151. FIROAuthProvider *provider = [FIROAuthProvider providerWithProviderID:kFakeProviderID auth:auth];
  152. [provider getCredentialWithUIDelegate:nil
  153. completion:^(FIRAuthCredential *_Nullable credential,
  154. NSError *_Nullable error) {
  155. XCTAssertTrue([NSThread isMainThread]);
  156. [expectation fulfill];
  157. }];
  158. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  159. }
  160. @end
  161. #endif // TARGET_OS_IOS