FIRInstallationsIntegrationTests.m 7.6 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. // 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/Sources/Private/FirebaseCoreInternal.h"
  24. #import "FBLPromise+Testing.h"
  25. #import "FirebaseInstallations/Source/Tests/Utils/FIRInstallations+Tests.h"
  26. #import "FirebaseInstallations/Source/Tests/Utils/FIRInstallationsItem+Tests.h"
  27. #import "FirebaseInstallations/Source/Library/Public/FIRInstallations.h"
  28. #import "FirebaseInstallations/Source/Library/Public/FIRInstallationsAuthTokenResult.h"
  29. static BOOL sFIRInstallationsFirebaseDefaultAppConfigured = NO;
  30. @interface FIRInstallationsIntegrationTests : XCTestCase
  31. @property(nonatomic) FIRInstallations *installations;
  32. @end
  33. @implementation FIRInstallationsIntegrationTests
  34. - (void)setUp {
  35. [self configureFirebaseDefaultAppIfCan];
  36. if (![self isDefaultAppConfigured]) {
  37. return;
  38. }
  39. self.installations = [FIRInstallations installationsWithApp:[FIRApp defaultApp]];
  40. }
  41. - (void)tearDown {
  42. // Delete the installation.
  43. [self.installations deleteWithCompletion:^(NSError *_Nullable error){
  44. }];
  45. // Wait for any pending background job to be completed.
  46. FBLWaitForPromisesWithTimeout(10);
  47. [FIRApp resetApps];
  48. }
  49. - (void)testGetFID {
  50. if (![self isDefaultAppConfigured]) {
  51. return;
  52. }
  53. NSString *FID1 = [self getFID];
  54. NSString *FID2 = [self getFID];
  55. XCTAssertEqualObjects(FID1, FID2);
  56. }
  57. - (void)testAuthToken {
  58. if (![self isDefaultAppConfigured]) {
  59. return;
  60. }
  61. XCTestExpectation *authTokenExpectation =
  62. [self expectationWithDescription:@"authTokenExpectation"];
  63. [self.installations
  64. authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  65. NSError *_Nullable error) {
  66. XCTAssertNil(error);
  67. XCTAssertNotNil(tokenResult);
  68. XCTAssertGreaterThanOrEqual(tokenResult.authToken.length, 10);
  69. XCTAssertGreaterThanOrEqual([tokenResult.expirationDate timeIntervalSinceNow], 50 * 60);
  70. [authTokenExpectation fulfill];
  71. }];
  72. [self waitForExpectations:@[ authTokenExpectation ] timeout:2];
  73. }
  74. - (void)testDeleteInstallation {
  75. if (![self isDefaultAppConfigured]) {
  76. return;
  77. }
  78. NSString *FIDBefore = [self getFID];
  79. FIRInstallationsAuthTokenResult *authTokenBefore = [self getAuthToken];
  80. XCTestExpectation *deleteExpectation = [self expectationWithDescription:@"Delete Installation"];
  81. [self.installations deleteWithCompletion:^(NSError *_Nullable error) {
  82. XCTAssertNil(error);
  83. [deleteExpectation fulfill];
  84. }];
  85. [self waitForExpectations:@[ deleteExpectation ] timeout:2];
  86. NSString *FIDAfter = [self getFID];
  87. FIRInstallationsAuthTokenResult *authTokenAfter = [self getAuthToken];
  88. XCTAssertNotEqualObjects(FIDBefore, FIDAfter);
  89. XCTAssertNotEqualObjects(authTokenBefore.authToken, authTokenAfter.authToken);
  90. XCTAssertNotEqualObjects(authTokenBefore.expirationDate, authTokenAfter.expirationDate);
  91. }
  92. - (void)testInstallationsWithApp {
  93. [self assertInstallationsWithAppNamed:@"testInstallationsWithApp1"];
  94. [self assertInstallationsWithAppNamed:@"testInstallationsWithApp2"];
  95. // Wait for finishing all background operations.
  96. FBLWaitForPromisesWithTimeout(10);
  97. }
  98. - (void)testDefaultAppInstallation {
  99. if (![self isDefaultAppConfigured]) {
  100. return;
  101. }
  102. XCTAssertNotNil(self.installations);
  103. XCTAssertEqualObjects(self.installations.appOptions.googleAppID,
  104. [FIRApp defaultApp].options.googleAppID);
  105. XCTAssertEqualObjects(self.installations.appName, [FIRApp defaultApp].name);
  106. // Wait for finishing all background operations.
  107. FBLWaitForPromisesWithTimeout(10);
  108. }
  109. #pragma mark - Helpers
  110. - (NSString *)getFID {
  111. XCTestExpectation *expectation =
  112. [self expectationWithDescription:[NSString stringWithFormat:@"FID %@", self.name]];
  113. __block NSString *retrievedID;
  114. [self.installations
  115. installationIDWithCompletion:^(NSString *_Nullable identifier, NSError *_Nullable error) {
  116. XCTAssertNotNil(identifier);
  117. XCTAssertNil(error);
  118. XCTAssertEqual(identifier.length, 22);
  119. retrievedID = identifier;
  120. [expectation fulfill];
  121. }];
  122. [self waitForExpectations:@[ expectation ] timeout:2];
  123. return retrievedID;
  124. }
  125. - (FIRInstallationsAuthTokenResult *)getAuthToken {
  126. XCTestExpectation *authTokenExpectation =
  127. [self expectationWithDescription:@"authTokenExpectation"];
  128. __block FIRInstallationsAuthTokenResult *retrievedTokenResult;
  129. [self.installations
  130. authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  131. NSError *_Nullable error) {
  132. XCTAssertNil(error);
  133. XCTAssertNotNil(tokenResult);
  134. XCTAssertGreaterThanOrEqual(tokenResult.authToken.length, 10);
  135. XCTAssertGreaterThanOrEqual([tokenResult.expirationDate timeIntervalSinceNow], 50 * 60);
  136. retrievedTokenResult = tokenResult;
  137. [authTokenExpectation fulfill];
  138. }];
  139. [self waitForExpectations:@[ authTokenExpectation ] timeout:2];
  140. return retrievedTokenResult;
  141. }
  142. - (FIRInstallations *)assertInstallationsWithAppNamed:(NSString *)appName {
  143. FIRApp *app = [self createAndConfigureAppWithName:appName];
  144. FIRInstallations *installations = [FIRInstallations installationsWithApp:app];
  145. XCTAssertNotNil(installations);
  146. XCTAssertEqualObjects(installations.appOptions.googleAppID, app.options.googleAppID);
  147. XCTAssertEqualObjects(installations.appName, app.name);
  148. return installations;
  149. }
  150. #pragma mark - Helpers
  151. - (FIRApp *)createAndConfigureAppWithName:(NSString *)name {
  152. FIROptions *options =
  153. [[FIROptions alloc] initWithGoogleAppID:@"1:100000000000:ios:aaaaaaaaaaaaaaaaaaaaaaaa"
  154. GCMSenderID:@"valid_sender_id"];
  155. options.APIKey = @"some_api_key";
  156. options.projectID = @"project_id";
  157. [FIRApp configureWithName:name options:options];
  158. return [FIRApp appNamed:name];
  159. }
  160. - (void)configureFirebaseDefaultAppIfCan {
  161. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  162. NSString *plistPath = [bundle pathForResource:@"GoogleService-Info" ofType:@"plist"];
  163. if (plistPath == nil) {
  164. return;
  165. }
  166. FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:plistPath];
  167. [FIRApp configureWithOptions:options];
  168. sFIRInstallationsFirebaseDefaultAppConfigured = YES;
  169. }
  170. - (BOOL)isDefaultAppConfigured {
  171. if (!sFIRInstallationsFirebaseDefaultAppConfigured) {
  172. #if FIR_INSTALLATIONS_INTEGRATION_TESTS_REQUIRED
  173. XCTFail(@"GoogleService-Info.plist for integration tests was not found. Please add the file to "
  174. @"your project.");
  175. #else
  176. NSLog(@"GoogleService-Info.plist for integration tests was not found. Skipping the test %@",
  177. self.name);
  178. #endif // FIR_INSTALLATIONS_INTEGRATION_TESTS_REQUIRED
  179. }
  180. return sFIRInstallationsFirebaseDefaultAppConfigured;
  181. }
  182. @end
  183. #endif // !TARGET_OS_OSX