FIRInstanceIDIntegrationTests.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. // macOS requests a user password when accessing the Keychain for the first time,
  17. // so the tests may fail. Disable integration tests on macOS so far.
  18. // TODO: Configure the tests to run on macOS without requesting the keychain password.
  19. #if !TARGET_OS_OSX
  20. #import <XCTest/XCTest.h>
  21. #import <FirebaseCore/FirebaseCore.h>
  22. #import <FirebaseInstanceID/FirebaseInstanceID.h>
  23. static BOOL sFIRInstanceIDFirebaseDefaultAppConfigured = NO;
  24. @interface FIRInstanceIDIntegrationTests : XCTestCase
  25. @property(nonatomic, strong) FIRInstanceID *instanceID;
  26. @end
  27. @implementation FIRInstanceIDIntegrationTests
  28. - (void)setUp {
  29. [self configureFirebaseDefaultAppIfCan];
  30. if (![self isDefaultAppConfigured]) {
  31. return;
  32. }
  33. self.instanceID = [FIRInstanceID instanceID];
  34. }
  35. - (void)tearDown {
  36. self.instanceID = nil;
  37. }
  38. - (void)testGetID {
  39. if (![self isDefaultAppConfigured]) {
  40. return;
  41. }
  42. XCTestExpectation *expectation = [self expectationWithDescription:@"getID"];
  43. [self.instanceID getIDWithHandler:^(NSString *_Nullable identity, NSError *_Nullable error) {
  44. XCTAssertNil(error);
  45. XCTAssertEqual(identity.length, 22);
  46. [expectation fulfill];
  47. }];
  48. [self waitForExpectations:@[ expectation ] timeout:5];
  49. }
  50. - (void)testInstanceIDWithHandler {
  51. if (![self isDefaultAppConfigured]) {
  52. return;
  53. }
  54. XCTestExpectation *expectation = [self expectationWithDescription:@"instanceIDWithHandler"];
  55. [self.instanceID
  56. instanceIDWithHandler:^(FIRInstanceIDResult *_Nullable result, NSError *_Nullable error) {
  57. XCTAssertNil(error);
  58. XCTAssertNotNil(result);
  59. XCTAssert(result.instanceID.length > 0);
  60. XCTAssert(result.token.length > 0);
  61. [expectation fulfill];
  62. }];
  63. [self waitForExpectations:@[ expectation ] timeout:5];
  64. }
  65. - (void)testTokenWithAuthorizedEntity {
  66. if (![self isDefaultAppConfigured]) {
  67. return;
  68. }
  69. [self assertTokenWithAuthorizedEntity];
  70. }
  71. - (void)testDeleteToken {
  72. if (![self isDefaultAppConfigured]) {
  73. return;
  74. }
  75. [self assertTokenWithAuthorizedEntity];
  76. XCTestExpectation *expectation = [self expectationWithDescription:@"testDeleteToken"];
  77. [self.instanceID deleteTokenWithAuthorizedEntity:[self tokenAuthorizedEntity]
  78. scope:@"*"
  79. handler:^(NSError *_Nonnull error) {
  80. XCTAssertNil(error);
  81. [expectation fulfill];
  82. }];
  83. [self waitForExpectations:@[ expectation ] timeout:5];
  84. }
  85. // TODO: b/147102327 - re-enable the test once the bug fixed.
  86. - (void)disabled_testDeleteID {
  87. if (![self isDefaultAppConfigured]) {
  88. return;
  89. }
  90. XCTestExpectation *expectation = [self expectationWithDescription:@"deleteID"];
  91. [self.instanceID deleteIDWithHandler:^(NSError *_Nullable error) {
  92. XCTAssertNil(error);
  93. [expectation fulfill];
  94. }];
  95. [self waitForExpectations:@[ expectation ] timeout:5];
  96. }
  97. #pragma mark - Helpers
  98. - (void)assertTokenWithAuthorizedEntity {
  99. XCTestExpectation *expectation = [self expectationWithDescription:@"tokenWithAuthorizedEntity"];
  100. [self.instanceID
  101. tokenWithAuthorizedEntity:[self tokenAuthorizedEntity]
  102. scope:@"*"
  103. options:nil
  104. handler:^(NSString *_Nullable token, NSError *_Nullable error) {
  105. XCTAssertNil(error);
  106. XCTAssert(token > 0);
  107. [expectation fulfill];
  108. }];
  109. [self waitForExpectations:@[ expectation ] timeout:5];
  110. }
  111. - (NSString *)tokenAuthorizedEntity {
  112. if (!sFIRInstanceIDFirebaseDefaultAppConfigured) {
  113. return @"";
  114. }
  115. return [FIRApp defaultApp].options.GCMSenderID;
  116. }
  117. - (void)configureFirebaseDefaultAppIfCan {
  118. if (sFIRInstanceIDFirebaseDefaultAppConfigured) {
  119. return;
  120. }
  121. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  122. NSString *plistPath = [bundle pathForResource:@"GoogleService-Info" ofType:@"plist"];
  123. if (plistPath == nil) {
  124. return;
  125. }
  126. FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:plistPath];
  127. [FIRApp configureWithOptions:options];
  128. sFIRInstanceIDFirebaseDefaultAppConfigured = YES;
  129. }
  130. - (BOOL)isDefaultAppConfigured {
  131. if (!sFIRInstanceIDFirebaseDefaultAppConfigured) {
  132. // Fail tests requiring GoogleService-Info.plist only if it is required.
  133. #if FIR_IID_INTEGRATION_TESTS_REQUIRED
  134. XCTFail(@"GoogleService-Info.plist for integration tests was not found. Please add the file to "
  135. @"your project.");
  136. #else
  137. NSLog(@"GoogleService-Info.plist for integration tests was not found. Skipping the test %@",
  138. self.name);
  139. #endif // FIR_IID_INTEGRATION_TESTS_REQUIRED
  140. }
  141. return sFIRInstanceIDFirebaseDefaultAppConfigured;
  142. }
  143. @end
  144. #endif // !TARGET_OS_OSX