FIRInstanceIDIntegrationTests.m 5.4 KB

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