FIRInstallationsIntegrationTests.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright 2019 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. // Uncomment or set the flag in GCC_PREPROCESSOR_DEFINITIONS to enable integration tests.
  17. //#define FIR_INSTALLATIONS_INTEGRATION_TESTS_REQUIRED 1
  18. // macOS requests a user password when accessing the Keychain for the first time,
  19. // so the tests may fail. Disable integration tests on macOS so far.
  20. // TODO: Configure the tests to run on macOS without requesting the keychain password.
  21. #import <TargetConditionals.h>
  22. #if !TARGET_OS_OSX
  23. #import <XCTest/XCTest.h>
  24. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  25. #import "FBLPromise+Testing.h"
  26. #import "FirebaseInstallations/Source/Tests/Utils/FIRInstallations+Tests.h"
  27. #import "FirebaseInstallations/Source/Tests/Utils/FIRInstallationsItem+Tests.h"
  28. #import "FirebaseInstallations/Source/Library/Public/FirebaseInstallations/FIRInstallations.h"
  29. #import "FirebaseInstallations/Source/Library/Public/FirebaseInstallations/FIRInstallationsAuthTokenResult.h"
  30. static BOOL sFIRInstallationsFirebaseDefaultAppConfigured = NO;
  31. @interface FIRInstallationsIntegrationTests : XCTestCase
  32. @property(nonatomic) FIRInstallations *installations;
  33. @end
  34. @implementation FIRInstallationsIntegrationTests
  35. - (void)setUp {
  36. [self configureFirebaseDefaultAppIfCan];
  37. if (![self isDefaultAppConfigured]) {
  38. return;
  39. }
  40. self.installations = [FIRInstallations installationsWithApp:[FIRApp defaultApp]];
  41. }
  42. - (void)tearDown {
  43. // Delete the installation.
  44. [self.installations deleteWithCompletion:^(NSError *_Nullable error){
  45. }];
  46. // Wait for any pending background job to be completed.
  47. FBLWaitForPromisesWithTimeout(10);
  48. [FIRApp resetApps];
  49. }
  50. - (void)testGetFID {
  51. if (![self isDefaultAppConfigured]) {
  52. return;
  53. }
  54. NSString *FID1 = [self getFID];
  55. NSString *FID2 = [self getFID];
  56. XCTAssertEqualObjects(FID1, FID2);
  57. }
  58. - (void)testAuthToken {
  59. if (![self isDefaultAppConfigured]) {
  60. return;
  61. }
  62. XCTestExpectation *authTokenExpectation =
  63. [self expectationWithDescription:@"authTokenExpectation"];
  64. [self.installations
  65. authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  66. NSError *_Nullable error) {
  67. XCTAssertNil(error);
  68. XCTAssertNotNil(tokenResult);
  69. XCTAssertGreaterThanOrEqual(tokenResult.authToken.length, 10);
  70. XCTAssertGreaterThanOrEqual([tokenResult.expirationDate timeIntervalSinceNow], 50 * 60);
  71. [authTokenExpectation fulfill];
  72. }];
  73. [self waitForExpectations:@[ authTokenExpectation ] timeout:2];
  74. }
  75. - (void)testDeleteInstallation {
  76. if (![self isDefaultAppConfigured]) {
  77. return;
  78. }
  79. NSString *FIDBefore = [self getFID];
  80. FIRInstallationsAuthTokenResult *authTokenBefore = [self getAuthToken];
  81. XCTestExpectation *deleteExpectation = [self expectationWithDescription:@"Delete Installation"];
  82. [self.installations deleteWithCompletion:^(NSError *_Nullable error) {
  83. XCTAssertNil(error);
  84. [deleteExpectation fulfill];
  85. }];
  86. [self waitForExpectations:@[ deleteExpectation ] timeout:2];
  87. NSString *FIDAfter = [self getFID];
  88. FIRInstallationsAuthTokenResult *authTokenAfter = [self getAuthToken];
  89. XCTAssertNotEqualObjects(FIDBefore, FIDAfter);
  90. XCTAssertNotEqualObjects(authTokenBefore.authToken, authTokenAfter.authToken);
  91. XCTAssertNotEqualObjects(authTokenBefore.expirationDate, authTokenAfter.expirationDate);
  92. }
  93. - (void)testInstallationsWithApp {
  94. [self assertInstallationsWithAppNamed:@"testInstallationsWithApp1"];
  95. [self assertInstallationsWithAppNamed:@"testInstallationsWithApp2"];
  96. // Wait for finishing all background operations.
  97. FBLWaitForPromisesWithTimeout(10);
  98. }
  99. - (void)testDefaultAppInstallation {
  100. if (![self isDefaultAppConfigured]) {
  101. return;
  102. }
  103. XCTAssertNotNil(self.installations);
  104. XCTAssertEqualObjects(self.installations.appOptions.googleAppID,
  105. [FIRApp defaultApp].options.googleAppID);
  106. XCTAssertEqualObjects(self.installations.appName, [FIRApp defaultApp].name);
  107. // Wait for finishing all background operations.
  108. FBLWaitForPromisesWithTimeout(10);
  109. }
  110. #pragma mark - Helpers
  111. - (NSString *)getFID {
  112. XCTestExpectation *expectation =
  113. [self expectationWithDescription:[NSString stringWithFormat:@"FID %@", self.name]];
  114. __block NSString *retrievedID;
  115. [self.installations
  116. installationIDWithCompletion:^(NSString *_Nullable identifier, NSError *_Nullable error) {
  117. XCTAssertNotNil(identifier);
  118. XCTAssertNil(error);
  119. XCTAssertEqual(identifier.length, 22);
  120. retrievedID = identifier;
  121. [expectation fulfill];
  122. }];
  123. [self waitForExpectations:@[ expectation ] timeout:2];
  124. return retrievedID;
  125. }
  126. - (FIRInstallationsAuthTokenResult *)getAuthToken {
  127. XCTestExpectation *authTokenExpectation =
  128. [self expectationWithDescription:@"authTokenExpectation"];
  129. __block FIRInstallationsAuthTokenResult *retrievedTokenResult;
  130. [self.installations
  131. authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  132. NSError *_Nullable error) {
  133. XCTAssertNil(error);
  134. XCTAssertNotNil(tokenResult);
  135. XCTAssertGreaterThanOrEqual(tokenResult.authToken.length, 10);
  136. XCTAssertGreaterThanOrEqual([tokenResult.expirationDate timeIntervalSinceNow], 50 * 60);
  137. retrievedTokenResult = tokenResult;
  138. [authTokenExpectation fulfill];
  139. }];
  140. [self waitForExpectations:@[ authTokenExpectation ] timeout:2];
  141. return retrievedTokenResult;
  142. }
  143. - (FIRInstallations *)assertInstallationsWithAppNamed:(NSString *)appName {
  144. FIRApp *app = [self createAndConfigureAppWithName:appName];
  145. FIRInstallations *installations = [FIRInstallations installationsWithApp:app];
  146. XCTAssertNotNil(installations);
  147. XCTAssertEqualObjects(installations.appOptions.googleAppID, app.options.googleAppID);
  148. XCTAssertEqualObjects(installations.appName, app.name);
  149. return installations;
  150. }
  151. #pragma mark - Helpers
  152. - (FIRApp *)createAndConfigureAppWithName:(NSString *)name {
  153. FIROptions *options =
  154. [[FIROptions alloc] initWithGoogleAppID:@"1:100000000000:ios:aaaaaaaaaaaaaaaaaaaaaaaa"
  155. GCMSenderID:@"valid_sender_id"];
  156. options.APIKey = @"AIzaSy-ApiKeyWithValidFormat_0123456789";
  157. options.projectID = @"project_id";
  158. [FIRApp configureWithName:name options:options];
  159. return [FIRApp appNamed:name];
  160. }
  161. - (void)configureFirebaseDefaultAppIfCan {
  162. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  163. NSString *plistPath = [bundle pathForResource:@"GoogleService-Info" ofType:@"plist"];
  164. if (plistPath == nil) {
  165. return;
  166. }
  167. FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:plistPath];
  168. [FIRApp configureWithOptions:options];
  169. sFIRInstallationsFirebaseDefaultAppConfigured = YES;
  170. }
  171. - (BOOL)isDefaultAppConfigured {
  172. if (!sFIRInstallationsFirebaseDefaultAppConfigured) {
  173. #if FIR_INSTALLATIONS_INTEGRATION_TESTS_REQUIRED
  174. XCTFail(@"GoogleService-Info.plist for integration tests was not found. Please add the file to "
  175. @"your project.");
  176. #else
  177. NSLog(@"GoogleService-Info.plist for integration tests was not found. Skipping the test %@",
  178. self.name);
  179. #endif // FIR_INSTALLATIONS_INTEGRATION_TESTS_REQUIRED
  180. }
  181. return sFIRInstallationsFirebaseDefaultAppConfigured;
  182. }
  183. @end
  184. #endif // !TARGET_OS_OSX