FIRInstallationsIntegrationTests.m 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 FirebaseCoreInternal;
  25. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  26. #import "FBLPromise+Testing.h"
  27. #import "FirebaseInstallations/Source/Tests/Utils/FIRInstallations+Tests.h"
  28. #import "FirebaseInstallations/Source/Tests/Utils/FIRInstallationsItem+Tests.h"
  29. #import "FirebaseInstallations/Source/Library/Public/FirebaseInstallations/FIRInstallations.h"
  30. #import "FirebaseInstallations/Source/Library/Public/FirebaseInstallations/FIRInstallationsAuthTokenResult.h"
  31. static BOOL sFIRInstallationsFirebaseDefaultAppConfigured = NO;
  32. @interface FIRInstallationsIntegrationTests : XCTestCase
  33. @property(nonatomic) FIRInstallations *installations;
  34. @end
  35. @implementation FIRInstallationsIntegrationTests
  36. - (void)setUp {
  37. [self configureFirebaseDefaultAppIfCan];
  38. if (![self isDefaultAppConfigured]) {
  39. return;
  40. }
  41. FIRApp *installationsApp = [FIRApp defaultApp];
  42. self.installations = [FIRInstallations installationsWithApp:installationsApp];
  43. // Remove the underlying heartbeat storage container to reset heartbeat data.
  44. [FIRHeartbeatLoggingTestUtils removeUnderlyingHeartbeatStorageContainersAndReturnError:nil];
  45. // Log a heartbeat with the Firebase app associated with `self.installations`.
  46. // This ensures that each test starts with the heartbeat logger having a
  47. // non-empty storage (this means there are heartbeats to send to the server).
  48. //
  49. // For each test that sends a network request, it is expected that the
  50. // heartbeat logger's storage will be flushed into a payload that is included
  51. // in the request. To confirm this occurs, those tests will assert that the
  52. // heartbeat logger's storage is empty after calling API that performs
  53. // the network request.
  54. [installationsApp.heartbeatLogger log];
  55. }
  56. - (void)tearDown {
  57. // Delete the installation.
  58. [self.installations deleteWithCompletion:^(NSError *_Nullable error){
  59. }];
  60. // Wait for any pending background job to be completed.
  61. FBLWaitForPromisesWithTimeout(20);
  62. [FIRApp resetApps];
  63. }
  64. - (void)testGetFID {
  65. if (![self isDefaultAppConfigured]) {
  66. return;
  67. }
  68. NSString *FID1 = [self getFID];
  69. NSString *FID2 = [self getFID];
  70. XCTAssertEqualObjects(FID1, FID2);
  71. // The heartbeat logged during `-[FIRInstallationsIntegrationTests setUp]`
  72. // should have been flushed and added to the resulting network request
  73. // from the above Installations API call.
  74. [self addTeardownBlock:^{
  75. FBLWaitForPromisesWithTimeout(20);
  76. XCTAssertNil([FIRApp.defaultApp.heartbeatLogger headerValue]);
  77. }];
  78. }
  79. - (void)testAuthToken {
  80. if (![self isDefaultAppConfigured]) {
  81. return;
  82. }
  83. XCTestExpectation *authTokenExpectation =
  84. [self expectationWithDescription:@"authTokenExpectation"];
  85. [self.installations
  86. authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  87. NSError *_Nullable error) {
  88. XCTAssertNil(error);
  89. XCTAssertNotNil(tokenResult);
  90. XCTAssertGreaterThanOrEqual(tokenResult.authToken.length, 10);
  91. XCTAssertGreaterThanOrEqual([tokenResult.expirationDate timeIntervalSinceNow], 50 * 60);
  92. [authTokenExpectation fulfill];
  93. }];
  94. [self waitForExpectations:@[ authTokenExpectation ] timeout:2];
  95. // The heartbeat logged during `-[FIRInstallationsIntegrationTests setUp]`
  96. // should have been flushed and added to the resulting network request
  97. // from the above Installations API call.
  98. [self addTeardownBlock:^{
  99. FBLWaitForPromisesWithTimeout(20);
  100. XCTAssertNil([FIRApp.defaultApp.heartbeatLogger headerValue]);
  101. }];
  102. }
  103. - (void)testDeleteInstallation {
  104. if (![self isDefaultAppConfigured]) {
  105. return;
  106. }
  107. NSString *FIDBefore = [self getFID];
  108. FIRInstallationsAuthTokenResult *authTokenBefore = [self getAuthToken];
  109. XCTestExpectation *deleteExpectation = [self expectationWithDescription:@"Delete Installation"];
  110. [self.installations deleteWithCompletion:^(NSError *_Nullable error) {
  111. XCTAssertNil(error);
  112. [deleteExpectation fulfill];
  113. }];
  114. [self waitForExpectations:@[ deleteExpectation ] timeout:2];
  115. NSString *FIDAfter = [self getFID];
  116. FIRInstallationsAuthTokenResult *authTokenAfter = [self getAuthToken];
  117. XCTAssertNotEqualObjects(FIDBefore, FIDAfter);
  118. XCTAssertNotEqualObjects(authTokenBefore.authToken, authTokenAfter.authToken);
  119. XCTAssertNotEqualObjects(authTokenBefore.expirationDate, authTokenAfter.expirationDate);
  120. // The heartbeat logged during `-[FIRInstallationsIntegrationTests setUp]`
  121. // should have been flushed and added to the resulting network request
  122. // from the above Installations API call.
  123. [self addTeardownBlock:^{
  124. FBLWaitForPromisesWithTimeout(20);
  125. XCTAssertNil([FIRApp.defaultApp.heartbeatLogger headerValue]);
  126. }];
  127. }
  128. - (void)testInstallationsWithApp {
  129. [self assertInstallationsWithAppNamed:@"testInstallationsWithApp1"];
  130. [self assertInstallationsWithAppNamed:@"testInstallationsWithApp2"];
  131. // Wait for finishing all background operations.
  132. FBLWaitForPromisesWithTimeout(10);
  133. }
  134. - (void)testDefaultAppInstallation {
  135. if (![self isDefaultAppConfigured]) {
  136. return;
  137. }
  138. XCTAssertNotNil(self.installations);
  139. XCTAssertEqualObjects(self.installations.appOptions.googleAppID,
  140. [FIRApp defaultApp].options.googleAppID);
  141. XCTAssertEqualObjects(self.installations.appName, [FIRApp defaultApp].name);
  142. // Wait for finishing all background operations.
  143. FBLWaitForPromisesWithTimeout(10);
  144. }
  145. #pragma mark - Helpers
  146. - (NSString *)getFID {
  147. XCTestExpectation *expectation =
  148. [self expectationWithDescription:[NSString stringWithFormat:@"FID %@", self.name]];
  149. __block NSString *retrievedID;
  150. [self.installations
  151. installationIDWithCompletion:^(NSString *_Nullable identifier, NSError *_Nullable error) {
  152. XCTAssertNotNil(identifier);
  153. XCTAssertNil(error);
  154. XCTAssertEqual(identifier.length, 22);
  155. retrievedID = identifier;
  156. [expectation fulfill];
  157. }];
  158. [self waitForExpectations:@[ expectation ] timeout:2];
  159. return retrievedID;
  160. }
  161. - (FIRInstallationsAuthTokenResult *)getAuthToken {
  162. XCTestExpectation *authTokenExpectation =
  163. [self expectationWithDescription:@"authTokenExpectation"];
  164. __block FIRInstallationsAuthTokenResult *retrievedTokenResult;
  165. [self.installations
  166. authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  167. NSError *_Nullable error) {
  168. XCTAssertNil(error);
  169. XCTAssertNotNil(tokenResult);
  170. XCTAssertGreaterThanOrEqual(tokenResult.authToken.length, 10);
  171. XCTAssertGreaterThanOrEqual([tokenResult.expirationDate timeIntervalSinceNow], 50 * 60);
  172. retrievedTokenResult = tokenResult;
  173. [authTokenExpectation fulfill];
  174. }];
  175. [self waitForExpectations:@[ authTokenExpectation ] timeout:2];
  176. return retrievedTokenResult;
  177. }
  178. - (FIRInstallations *)assertInstallationsWithAppNamed:(NSString *)appName {
  179. FIRApp *app = [self createAndConfigureAppWithName:appName];
  180. FIRInstallations *installations = [FIRInstallations installationsWithApp:app];
  181. XCTAssertNotNil(installations);
  182. XCTAssertEqualObjects(installations.appOptions.googleAppID, app.options.googleAppID);
  183. XCTAssertEqualObjects(installations.appName, app.name);
  184. return installations;
  185. }
  186. #pragma mark - Helpers
  187. - (FIRApp *)createAndConfigureAppWithName:(NSString *)name {
  188. FIROptions *options =
  189. [[FIROptions alloc] initWithGoogleAppID:@"1:100000000000:ios:aaaaaaaaaaaaaaaaaaaaaaaa"
  190. GCMSenderID:@"valid_sender_id"];
  191. options.APIKey = @"AIzaSy-ApiKeyWithValidFormat_0123456789";
  192. options.projectID = @"project_id";
  193. [FIRApp configureWithName:name options:options];
  194. return [FIRApp appNamed:name];
  195. }
  196. - (void)configureFirebaseDefaultAppIfCan {
  197. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  198. NSString *plistPath = [bundle pathForResource:@"GoogleService-Info" ofType:@"plist"];
  199. if (plistPath == nil) {
  200. return;
  201. }
  202. FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:plistPath];
  203. [FIRApp configureWithOptions:options];
  204. sFIRInstallationsFirebaseDefaultAppConfigured = YES;
  205. }
  206. - (BOOL)isDefaultAppConfigured {
  207. if (!sFIRInstallationsFirebaseDefaultAppConfigured) {
  208. #if FIR_INSTALLATIONS_INTEGRATION_TESTS_REQUIRED
  209. XCTFail(@"GoogleService-Info.plist for integration tests was not found. Please add the file to "
  210. @"your project.");
  211. #else
  212. NSLog(@"GoogleService-Info.plist for integration tests was not found. Skipping the test %@",
  213. self.name);
  214. #endif // FIR_INSTALLATIONS_INTEGRATION_TESTS_REQUIRED
  215. }
  216. return sFIRInstallationsFirebaseDefaultAppConfigured;
  217. }
  218. @end
  219. #endif // !TARGET_OS_OSX