FIRInstallationsIntegrationTests.m 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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(FIRHeaderValueFromHeartbeatsPayload(
  77. [FIRApp.defaultApp.heartbeatLogger flushHeartbeatsIntoPayload]));
  78. }];
  79. }
  80. - (void)testAuthToken {
  81. if (![self isDefaultAppConfigured]) {
  82. return;
  83. }
  84. XCTestExpectation *authTokenExpectation =
  85. [self expectationWithDescription:@"authTokenExpectation"];
  86. [self.installations
  87. authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  88. NSError *_Nullable error) {
  89. XCTAssertNil(error);
  90. XCTAssertNotNil(tokenResult);
  91. XCTAssertGreaterThanOrEqual(tokenResult.authToken.length, 10);
  92. XCTAssertGreaterThanOrEqual([tokenResult.expirationDate timeIntervalSinceNow], 50 * 60);
  93. [authTokenExpectation fulfill];
  94. }];
  95. [self waitForExpectations:@[ authTokenExpectation ] timeout:2];
  96. // The heartbeat logged during `-[FIRInstallationsIntegrationTests setUp]`
  97. // should have been flushed and added to the resulting network request
  98. // from the above Installations API call.
  99. [self addTeardownBlock:^{
  100. FBLWaitForPromisesWithTimeout(20);
  101. XCTAssertNil(FIRHeaderValueFromHeartbeatsPayload(
  102. [FIRApp.defaultApp.heartbeatLogger flushHeartbeatsIntoPayload]));
  103. }];
  104. }
  105. - (void)testDeleteInstallation {
  106. if (![self isDefaultAppConfigured]) {
  107. return;
  108. }
  109. NSString *FIDBefore = [self getFID];
  110. FIRInstallationsAuthTokenResult *authTokenBefore = [self getAuthToken];
  111. XCTestExpectation *deleteExpectation = [self expectationWithDescription:@"Delete Installation"];
  112. [self.installations deleteWithCompletion:^(NSError *_Nullable error) {
  113. XCTAssertNil(error);
  114. [deleteExpectation fulfill];
  115. }];
  116. [self waitForExpectations:@[ deleteExpectation ] timeout:2];
  117. NSString *FIDAfter = [self getFID];
  118. FIRInstallationsAuthTokenResult *authTokenAfter = [self getAuthToken];
  119. XCTAssertNotEqualObjects(FIDBefore, FIDAfter);
  120. XCTAssertNotEqualObjects(authTokenBefore.authToken, authTokenAfter.authToken);
  121. XCTAssertNotEqualObjects(authTokenBefore.expirationDate, authTokenAfter.expirationDate);
  122. // The heartbeat logged during `-[FIRInstallationsIntegrationTests setUp]`
  123. // should have been flushed and added to the resulting network request
  124. // from the above Installations API call.
  125. [self addTeardownBlock:^{
  126. FBLWaitForPromisesWithTimeout(20);
  127. XCTAssertNil(FIRHeaderValueFromHeartbeatsPayload(
  128. [FIRApp.defaultApp.heartbeatLogger flushHeartbeatsIntoPayload]));
  129. }];
  130. }
  131. - (void)testInstallationsWithApp {
  132. [self assertInstallationsWithAppNamed:@"testInstallationsWithApp1"];
  133. [self assertInstallationsWithAppNamed:@"testInstallationsWithApp2"];
  134. // Wait for finishing all background operations.
  135. FBLWaitForPromisesWithTimeout(10);
  136. }
  137. - (void)testDefaultAppInstallation {
  138. if (![self isDefaultAppConfigured]) {
  139. return;
  140. }
  141. XCTAssertNotNil(self.installations);
  142. XCTAssertEqualObjects(self.installations.appOptions.googleAppID,
  143. [FIRApp defaultApp].options.googleAppID);
  144. XCTAssertEqualObjects(self.installations.appName, [FIRApp defaultApp].name);
  145. // Wait for finishing all background operations.
  146. FBLWaitForPromisesWithTimeout(10);
  147. }
  148. #pragma mark - Helpers
  149. - (NSString *)getFID {
  150. XCTestExpectation *expectation =
  151. [self expectationWithDescription:[NSString stringWithFormat:@"FID %@", self.name]];
  152. __block NSString *retrievedID;
  153. [self.installations
  154. installationIDWithCompletion:^(NSString *_Nullable identifier, NSError *_Nullable error) {
  155. XCTAssertNotNil(identifier);
  156. XCTAssertNil(error);
  157. XCTAssertEqual(identifier.length, 22);
  158. retrievedID = identifier;
  159. [expectation fulfill];
  160. }];
  161. [self waitForExpectations:@[ expectation ] timeout:2];
  162. return retrievedID;
  163. }
  164. - (FIRInstallationsAuthTokenResult *)getAuthToken {
  165. XCTestExpectation *authTokenExpectation =
  166. [self expectationWithDescription:@"authTokenExpectation"];
  167. __block FIRInstallationsAuthTokenResult *retrievedTokenResult;
  168. [self.installations
  169. authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  170. NSError *_Nullable error) {
  171. XCTAssertNil(error);
  172. XCTAssertNotNil(tokenResult);
  173. XCTAssertGreaterThanOrEqual(tokenResult.authToken.length, 10);
  174. XCTAssertGreaterThanOrEqual([tokenResult.expirationDate timeIntervalSinceNow], 50 * 60);
  175. retrievedTokenResult = tokenResult;
  176. [authTokenExpectation fulfill];
  177. }];
  178. [self waitForExpectations:@[ authTokenExpectation ] timeout:2];
  179. return retrievedTokenResult;
  180. }
  181. - (FIRInstallations *)assertInstallationsWithAppNamed:(NSString *)appName {
  182. FIRApp *app = [self createAndConfigureAppWithName:appName];
  183. FIRInstallations *installations = [FIRInstallations installationsWithApp:app];
  184. XCTAssertNotNil(installations);
  185. XCTAssertEqualObjects(installations.appOptions.googleAppID, app.options.googleAppID);
  186. XCTAssertEqualObjects(installations.appName, app.name);
  187. return installations;
  188. }
  189. #pragma mark - Helpers
  190. - (FIRApp *)createAndConfigureAppWithName:(NSString *)name {
  191. FIROptions *options =
  192. [[FIROptions alloc] initWithGoogleAppID:@"1:100000000000:ios:aaaaaaaaaaaaaaaaaaaaaaaa"
  193. GCMSenderID:@"valid_sender_id"];
  194. options.APIKey = @"AIzaSy-ApiKeyWithValidFormat_0123456789";
  195. options.projectID = @"project_id";
  196. [FIRApp configureWithName:name options:options];
  197. return [FIRApp appNamed:name];
  198. }
  199. - (void)configureFirebaseDefaultAppIfCan {
  200. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  201. NSString *plistPath = [bundle pathForResource:@"GoogleService-Info" ofType:@"plist"];
  202. if (plistPath == nil) {
  203. return;
  204. }
  205. FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:plistPath];
  206. [FIRApp configureWithOptions:options];
  207. sFIRInstallationsFirebaseDefaultAppConfigured = YES;
  208. }
  209. - (BOOL)isDefaultAppConfigured {
  210. if (!sFIRInstallationsFirebaseDefaultAppConfigured) {
  211. #if FIR_INSTALLATIONS_INTEGRATION_TESTS_REQUIRED
  212. XCTFail(@"GoogleService-Info.plist for integration tests was not found. Please add the file to "
  213. @"your project.");
  214. #else
  215. NSLog(@"GoogleService-Info.plist for integration tests was not found. Skipping the test %@",
  216. self.name);
  217. #endif // FIR_INSTALLATIONS_INTEGRATION_TESTS_REQUIRED
  218. }
  219. return sFIRInstallationsFirebaseDefaultAppConfigured;
  220. }
  221. @end
  222. #endif // !TARGET_OS_OSX