FIRInstallationsIntegrationTests.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 to enable integration tests.
  17. //#define FIR_INSTALLATIONS_INTEGRATION_TESTS_ENABLED 1
  18. #ifdef FIR_INSTALLATIONS_INTEGRATION_TESTS_ENABLED
  19. #import <XCTest/XCTest.h>
  20. #import <FirebaseCore/FIRAppInternal.h>
  21. #import <FirebaseCore/FIROptionsInternal.h>
  22. #import "FBLPromise+Testing.h"
  23. #import "FIRInstallations+Tests.h"
  24. #import "FIRInstallationsItem+Tests.h"
  25. #import <FirebaseInstallations/FIRInstallations.h>
  26. #import <FirebaseInstallations/FIRInstallationsAuthTokenResult.h>
  27. @interface FIRInstallationsIntegrationTests : XCTestCase
  28. @property(nonatomic) FIRInstallations *installations;
  29. @end
  30. @implementation FIRInstallationsIntegrationTests
  31. - (void)setUp {
  32. static dispatch_once_t onceToken;
  33. dispatch_once(&onceToken, ^{
  34. [FIRApp configure];
  35. });
  36. self.installations = [FIRInstallations installationsWithApp:[FIRApp defaultApp]];
  37. }
  38. - (void)tearDown {
  39. // Delete the installation.
  40. [self.installations deleteWithCompletion:^(NSError *_Nullable error) {
  41. XCTAssertNil(error);
  42. }];
  43. // Wait for any pending background job to be completed.
  44. FBLWaitForPromisesWithTimeout(10);
  45. }
  46. // TODO: Enable the test once Travis configured.
  47. // Need to configure the GoogleService-Info.plist copying from the encrypted archive.
  48. // So far, let's run the tests locally.
  49. - (void)disabled_testGetFID {
  50. NSString *FID1 = [self getFID];
  51. NSString *FID2 = [self getFID];
  52. XCTAssertEqualObjects(FID1, FID2);
  53. }
  54. - (void)disabled_testAuthToken {
  55. XCTestExpectation *authTokenExpectation =
  56. [self expectationWithDescription:@"authTokenExpectation"];
  57. [self.installations
  58. authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  59. NSError *_Nullable error) {
  60. XCTAssertNil(error);
  61. XCTAssertNotNil(tokenResult);
  62. XCTAssertGreaterThanOrEqual(tokenResult.authToken.length, 10);
  63. XCTAssertGreaterThanOrEqual([tokenResult.expirationDate timeIntervalSinceNow], 50 * 60);
  64. [authTokenExpectation fulfill];
  65. }];
  66. [self waitForExpectations:@[ authTokenExpectation ] timeout:2];
  67. }
  68. - (void)disabled_testDeleteInstallation {
  69. NSString *FIDBefore = [self getFID];
  70. FIRInstallationsAuthTokenResult *authTokenBefore = [self getAuthToken];
  71. XCTestExpectation *deleteExpectation = [self expectationWithDescription:@"Delete Installation"];
  72. [self.installations deleteWithCompletion:^(NSError *_Nullable error) {
  73. XCTAssertNil(error);
  74. [deleteExpectation fulfill];
  75. }];
  76. [self waitForExpectations:@[ deleteExpectation ] timeout:2];
  77. NSString *FIDAfter = [self getFID];
  78. FIRInstallationsAuthTokenResult *authTokenAfter = [self getAuthToken];
  79. XCTAssertNotEqualObjects(FIDBefore, FIDAfter);
  80. XCTAssertNotEqualObjects(authTokenBefore.authToken, authTokenAfter.authToken);
  81. XCTAssertNotEqualObjects(authTokenBefore.expirationDate, authTokenAfter.expirationDate);
  82. }
  83. // TODO: Configure the tests to run on macOS without requesting the keychain password.
  84. #if !TARGET_OS_OSX
  85. - (void)testInstallationsWithApp {
  86. [self assertInstallationsWithAppNamed:@"testInstallationsWithApp1"];
  87. [self assertInstallationsWithAppNamed:@"testInstallationsWithApp2"];
  88. // Wait for finishing all background operations.
  89. FBLWaitForPromisesWithTimeout(10);
  90. [FIRApp resetApps];
  91. }
  92. - (void)testDefaultAppInstallation {
  93. XCTAssertNotNil(self.installations);
  94. XCTAssertEqualObjects(self.installations.appOptions.googleAppID,
  95. [FIRApp defaultApp].options.googleAppID);
  96. XCTAssertEqualObjects(self.installations.appName, [FIRApp defaultApp].name);
  97. // Wait for finishing all background operations.
  98. FBLWaitForPromisesWithTimeout(10);
  99. [FIRApp resetApps];
  100. }
  101. #endif // !TARGET_OS_OSX
  102. #pragma mark - Helpers
  103. - (NSString *)getFID {
  104. XCTestExpectation *expectation =
  105. [self expectationWithDescription:[NSString stringWithFormat:@"FID %@", self.name]];
  106. __block NSString *retrievedID;
  107. [self.installations
  108. installationIDWithCompletion:^(NSString *_Nullable identifier, NSError *_Nullable error) {
  109. XCTAssertNotNil(identifier);
  110. XCTAssertNil(error);
  111. XCTAssertEqual(identifier.length, 22);
  112. retrievedID = identifier;
  113. [expectation fulfill];
  114. }];
  115. [self waitForExpectations:@[ expectation ] timeout:2];
  116. return retrievedID;
  117. }
  118. - (FIRInstallationsAuthTokenResult *)getAuthToken {
  119. XCTestExpectation *authTokenExpectation =
  120. [self expectationWithDescription:@"authTokenExpectation"];
  121. __block FIRInstallationsAuthTokenResult *retrievedTokenResult;
  122. [self.installations
  123. authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  124. NSError *_Nullable error) {
  125. XCTAssertNil(error);
  126. XCTAssertNotNil(tokenResult);
  127. XCTAssertGreaterThanOrEqual(tokenResult.authToken.length, 10);
  128. XCTAssertGreaterThanOrEqual([tokenResult.expirationDate timeIntervalSinceNow], 50 * 60);
  129. retrievedTokenResult = tokenResult;
  130. [authTokenExpectation fulfill];
  131. }];
  132. [self waitForExpectations:@[ authTokenExpectation ] timeout:2];
  133. return retrievedTokenResult;
  134. }
  135. - (FIRInstallations *)assertInstallationsWithAppNamed:(NSString *)appName {
  136. FIRApp *app = [self createAndConfigureAppWithName:appName];
  137. FIRInstallations *installations = [FIRInstallations installationsWithApp:app];
  138. XCTAssertNotNil(installations);
  139. XCTAssertEqualObjects(installations.appOptions.googleAppID, app.options.googleAppID);
  140. XCTAssertEqualObjects(installations.appName, app.name);
  141. return installations;
  142. }
  143. #pragma mark - Helpers
  144. - (FIRApp *)createAndConfigureAppWithName:(NSString *)name {
  145. FIROptions *options =
  146. [[FIROptions alloc] initWithGoogleAppID:@"1:100000000000:ios:aaaaaaaaaaaaaaaaaaaaaaaa"
  147. GCMSenderID:@"valid_sender_id"];
  148. [FIRApp configureWithName:name options:options];
  149. return [FIRApp appNamed:name];
  150. }
  151. @end
  152. #endif // FIR_INSTALLATIONS_INTEGRATION_TESTS_ENABLED