FIRInstallationsIntegrationTests.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #import <XCTest/XCTest.h>
  19. #import <FirebaseCore/FIRAppInternal.h>
  20. #import <FirebaseCore/FIROptionsInternal.h>
  21. #import "FBLPromise+Testing.h"
  22. #import "FIRInstallations+Tests.h"
  23. #import "FIRInstallationsItem+Tests.h"
  24. #import <FirebaseInstallations/FIRInstallations.h>
  25. #import <FirebaseInstallations/FIRInstallationsAuthTokenResult.h>
  26. static BOOL sFIRInstallationsFirebaseDefaultAppConfigured = NO;
  27. @interface FIRInstallationsIntegrationTests : XCTestCase
  28. @property(nonatomic) FIRInstallations *installations;
  29. @end
  30. @implementation FIRInstallationsIntegrationTests
  31. - (void)setUp {
  32. [self configureFirebaseDefaultAppIfCan];
  33. if (![self isDefaultAppConfigured]) {
  34. return;
  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. [FIRApp resetApps];
  46. }
  47. // TODO: Enable the test once Travis configured.
  48. // Need to configure the GoogleService-Info.plist copying from the encrypted archive.
  49. // So far, let's run the tests locally.
  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. // TODO: Configure the tests to run on macOS without requesting the keychain password.
  94. #if !TARGET_OS_OSX
  95. - (void)testInstallationsWithApp {
  96. [self assertInstallationsWithAppNamed:@"testInstallationsWithApp1"];
  97. [self assertInstallationsWithAppNamed:@"testInstallationsWithApp2"];
  98. // Wait for finishing all background operations.
  99. FBLWaitForPromisesWithTimeout(10);
  100. }
  101. - (void)testDefaultAppInstallation {
  102. if (![self isDefaultAppConfigured]) {
  103. return;
  104. }
  105. XCTAssertNotNil(self.installations);
  106. XCTAssertEqualObjects(self.installations.appOptions.googleAppID,
  107. [FIRApp defaultApp].options.googleAppID);
  108. XCTAssertEqualObjects(self.installations.appName, [FIRApp defaultApp].name);
  109. // Wait for finishing all background operations.
  110. FBLWaitForPromisesWithTimeout(10);
  111. }
  112. #endif // !TARGET_OS_OSX
  113. #pragma mark - Helpers
  114. - (NSString *)getFID {
  115. XCTestExpectation *expectation =
  116. [self expectationWithDescription:[NSString stringWithFormat:@"FID %@", self.name]];
  117. __block NSString *retrievedID;
  118. [self.installations
  119. installationIDWithCompletion:^(NSString *_Nullable identifier, NSError *_Nullable error) {
  120. XCTAssertNotNil(identifier);
  121. XCTAssertNil(error);
  122. XCTAssertEqual(identifier.length, 22);
  123. retrievedID = identifier;
  124. [expectation fulfill];
  125. }];
  126. [self waitForExpectations:@[ expectation ] timeout:2];
  127. return retrievedID;
  128. }
  129. - (FIRInstallationsAuthTokenResult *)getAuthToken {
  130. XCTestExpectation *authTokenExpectation =
  131. [self expectationWithDescription:@"authTokenExpectation"];
  132. __block FIRInstallationsAuthTokenResult *retrievedTokenResult;
  133. [self.installations
  134. authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  135. NSError *_Nullable error) {
  136. XCTAssertNil(error);
  137. XCTAssertNotNil(tokenResult);
  138. XCTAssertGreaterThanOrEqual(tokenResult.authToken.length, 10);
  139. XCTAssertGreaterThanOrEqual([tokenResult.expirationDate timeIntervalSinceNow], 50 * 60);
  140. retrievedTokenResult = tokenResult;
  141. [authTokenExpectation fulfill];
  142. }];
  143. [self waitForExpectations:@[ authTokenExpectation ] timeout:2];
  144. return retrievedTokenResult;
  145. }
  146. - (FIRInstallations *)assertInstallationsWithAppNamed:(NSString *)appName {
  147. FIRApp *app = [self createAndConfigureAppWithName:appName];
  148. FIRInstallations *installations = [FIRInstallations installationsWithApp:app];
  149. XCTAssertNotNil(installations);
  150. XCTAssertEqualObjects(installations.appOptions.googleAppID, app.options.googleAppID);
  151. XCTAssertEqualObjects(installations.appName, app.name);
  152. return installations;
  153. }
  154. #pragma mark - Helpers
  155. - (FIRApp *)createAndConfigureAppWithName:(NSString *)name {
  156. FIROptions *options =
  157. [[FIROptions alloc] initWithGoogleAppID:@"1:100000000000:ios:aaaaaaaaaaaaaaaaaaaaaaaa"
  158. GCMSenderID:@"valid_sender_id"];
  159. [FIRApp configureWithName:name options:options];
  160. return [FIRApp appNamed:name];
  161. }
  162. - (void)configureFirebaseDefaultAppIfCan {
  163. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  164. NSString *plistPath = [bundle pathForResource:@"GoogleService-Info" ofType:@"plist"];
  165. if (plistPath == nil) {
  166. return;
  167. }
  168. FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:plistPath];
  169. [FIRApp configureWithOptions:options];
  170. sFIRInstallationsFirebaseDefaultAppConfigured = YES;
  171. }
  172. - (BOOL)isDefaultAppConfigured {
  173. if (!sFIRInstallationsFirebaseDefaultAppConfigured) {
  174. #if FIR_INSTALLATIONS_INTEGRATION_TESTS_REQUIRED
  175. XCTFail(@"GoogleService-Info.plist for integration tests was not found. Please add the file to "
  176. @"your project.");
  177. #else
  178. NSLog(@"GoogleService-Info.plist for integration tests was not found. Skipping the test %@",
  179. self.name);
  180. #endif // FIR_INSTALLATIONS_INTEGRATION_TESTS_REQUIRED
  181. }
  182. return sFIRInstallationsFirebaseDefaultAppConfigured;
  183. }
  184. @end