FIRInstallationsIntegrationTests.m 7.6 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. #if !TARGET_OS_OSX
  22. #import <XCTest/XCTest.h>
  23. #import <FirebaseCore/FIRAppInternal.h>
  24. #import <FirebaseCore/FIROptionsInternal.h>
  25. #import "FBLPromise+Testing.h"
  26. #import "FIRInstallations+Tests.h"
  27. #import "FIRInstallationsItem+Tests.h"
  28. #import <FirebaseInstallations/FIRInstallations.h>
  29. #import <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. XCTAssertNil(error);
  46. }];
  47. // Wait for any pending background job to be completed.
  48. FBLWaitForPromisesWithTimeout(10);
  49. [FIRApp resetApps];
  50. }
  51. - (void)testGetFID {
  52. if (![self isDefaultAppConfigured]) {
  53. return;
  54. }
  55. NSString *FID1 = [self getFID];
  56. NSString *FID2 = [self getFID];
  57. XCTAssertEqualObjects(FID1, FID2);
  58. }
  59. - (void)testAuthToken {
  60. if (![self isDefaultAppConfigured]) {
  61. return;
  62. }
  63. XCTestExpectation *authTokenExpectation =
  64. [self expectationWithDescription:@"authTokenExpectation"];
  65. [self.installations
  66. authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  67. NSError *_Nullable error) {
  68. XCTAssertNil(error);
  69. XCTAssertNotNil(tokenResult);
  70. XCTAssertGreaterThanOrEqual(tokenResult.authToken.length, 10);
  71. XCTAssertGreaterThanOrEqual([tokenResult.expirationDate timeIntervalSinceNow], 50 * 60);
  72. [authTokenExpectation fulfill];
  73. }];
  74. [self waitForExpectations:@[ authTokenExpectation ] timeout:2];
  75. }
  76. - (void)testDeleteInstallation {
  77. if (![self isDefaultAppConfigured]) {
  78. return;
  79. }
  80. NSString *FIDBefore = [self getFID];
  81. FIRInstallationsAuthTokenResult *authTokenBefore = [self getAuthToken];
  82. XCTestExpectation *deleteExpectation = [self expectationWithDescription:@"Delete Installation"];
  83. [self.installations deleteWithCompletion:^(NSError *_Nullable error) {
  84. XCTAssertNil(error);
  85. [deleteExpectation fulfill];
  86. }];
  87. [self waitForExpectations:@[ deleteExpectation ] timeout:2];
  88. NSString *FIDAfter = [self getFID];
  89. FIRInstallationsAuthTokenResult *authTokenAfter = [self getAuthToken];
  90. XCTAssertNotEqualObjects(FIDBefore, FIDAfter);
  91. XCTAssertNotEqualObjects(authTokenBefore.authToken, authTokenAfter.authToken);
  92. XCTAssertNotEqualObjects(authTokenBefore.expirationDate, authTokenAfter.expirationDate);
  93. }
  94. - (void)testInstallationsWithApp {
  95. [self assertInstallationsWithAppNamed:@"testInstallationsWithApp1"];
  96. [self assertInstallationsWithAppNamed:@"testInstallationsWithApp2"];
  97. // Wait for finishing all background operations.
  98. FBLWaitForPromisesWithTimeout(10);
  99. }
  100. - (void)testDefaultAppInstallation {
  101. if (![self isDefaultAppConfigured]) {
  102. return;
  103. }
  104. XCTAssertNotNil(self.installations);
  105. XCTAssertEqualObjects(self.installations.appOptions.googleAppID,
  106. [FIRApp defaultApp].options.googleAppID);
  107. XCTAssertEqualObjects(self.installations.appName, [FIRApp defaultApp].name);
  108. // Wait for finishing all background operations.
  109. FBLWaitForPromisesWithTimeout(10);
  110. }
  111. #pragma mark - Helpers
  112. - (NSString *)getFID {
  113. XCTestExpectation *expectation =
  114. [self expectationWithDescription:[NSString stringWithFormat:@"FID %@", self.name]];
  115. __block NSString *retrievedID;
  116. [self.installations
  117. installationIDWithCompletion:^(NSString *_Nullable identifier, NSError *_Nullable error) {
  118. XCTAssertNotNil(identifier);
  119. XCTAssertNil(error);
  120. XCTAssertEqual(identifier.length, 22);
  121. retrievedID = identifier;
  122. [expectation fulfill];
  123. }];
  124. [self waitForExpectations:@[ expectation ] timeout:2];
  125. return retrievedID;
  126. }
  127. - (FIRInstallationsAuthTokenResult *)getAuthToken {
  128. XCTestExpectation *authTokenExpectation =
  129. [self expectationWithDescription:@"authTokenExpectation"];
  130. __block FIRInstallationsAuthTokenResult *retrievedTokenResult;
  131. [self.installations
  132. authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  133. NSError *_Nullable error) {
  134. XCTAssertNil(error);
  135. XCTAssertNotNil(tokenResult);
  136. XCTAssertGreaterThanOrEqual(tokenResult.authToken.length, 10);
  137. XCTAssertGreaterThanOrEqual([tokenResult.expirationDate timeIntervalSinceNow], 50 * 60);
  138. retrievedTokenResult = tokenResult;
  139. [authTokenExpectation fulfill];
  140. }];
  141. [self waitForExpectations:@[ authTokenExpectation ] timeout:2];
  142. return retrievedTokenResult;
  143. }
  144. - (FIRInstallations *)assertInstallationsWithAppNamed:(NSString *)appName {
  145. FIRApp *app = [self createAndConfigureAppWithName:appName];
  146. FIRInstallations *installations = [FIRInstallations installationsWithApp:app];
  147. XCTAssertNotNil(installations);
  148. XCTAssertEqualObjects(installations.appOptions.googleAppID, app.options.googleAppID);
  149. XCTAssertEqualObjects(installations.appName, app.name);
  150. return installations;
  151. }
  152. #pragma mark - Helpers
  153. - (FIRApp *)createAndConfigureAppWithName:(NSString *)name {
  154. FIROptions *options =
  155. [[FIROptions alloc] initWithGoogleAppID:@"1:100000000000:ios:aaaaaaaaaaaaaaaaaaaaaaaa"
  156. GCMSenderID:@"valid_sender_id"];
  157. options.APIKey = @"some_api_key";
  158. options.projectID = @"project_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
  185. #endif // !TARGET_OS_OSX