FIRInstanceIDIntegrationTests.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. - (void)testDeleteID {
  86. if (![self isDefaultAppConfigured]) {
  87. return;
  88. }
  89. XCTestExpectation *expectation = [self expectationWithDescription:@"deleteID"];
  90. [self.instanceID deleteIDWithHandler:^(NSError *_Nullable error) {
  91. XCTAssertNil(error);
  92. [expectation fulfill];
  93. }];
  94. [self waitForExpectations:@[ expectation ] timeout:5];
  95. }
  96. #pragma mark - Helpers
  97. - (void)assertTokenWithAuthorizedEntity {
  98. XCTestExpectation *expectation = [self expectationWithDescription:@"tokenWithAuthorizedEntity"];
  99. [self.instanceID
  100. tokenWithAuthorizedEntity:[self tokenAuthorizedEntity]
  101. scope:@"*"
  102. options:nil
  103. handler:^(NSString *_Nullable token, NSError *_Nullable error) {
  104. XCTAssertNil(error);
  105. XCTAssert(token > 0);
  106. [expectation fulfill];
  107. }];
  108. [self waitForExpectations:@[ expectation ] timeout:5];
  109. }
  110. - (NSString *)tokenAuthorizedEntity {
  111. if (!sFIRInstanceIDFirebaseDefaultAppConfigured) {
  112. return @"";
  113. }
  114. return [FIRApp defaultApp].options.GCMSenderID;
  115. }
  116. - (void)configureFirebaseDefaultAppIfCan {
  117. if (sFIRInstanceIDFirebaseDefaultAppConfigured) {
  118. return;
  119. }
  120. NSBundle *bundle = [NSBundle bundleForClass:[self class]];
  121. NSString *plistPath = [bundle pathForResource:@"GoogleService-Info" ofType:@"plist"];
  122. if (plistPath == nil) {
  123. return;
  124. }
  125. FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:plistPath];
  126. [FIRApp configureWithOptions:options];
  127. sFIRInstanceIDFirebaseDefaultAppConfigured = YES;
  128. }
  129. - (BOOL)isDefaultAppConfigured {
  130. if (!sFIRInstanceIDFirebaseDefaultAppConfigured) {
  131. // Fail tests requiring GoogleService-Info.plist only if it is required.
  132. #if FIR_IID_INTEGRATION_TESTS_REQUIRED
  133. XCTFail(@"GoogleService-Info.plist for integration tests was not found. Please add the file to "
  134. @"your project.");
  135. #else
  136. NSLog(@"GoogleService-Info.plist for integration tests was not found. Skipping the test %@",
  137. self.name);
  138. #endif // FIR_IID_INTEGRATION_TESTS_REQUIRED
  139. }
  140. return sFIRInstanceIDFirebaseDefaultAppConfigured;
  141. }
  142. @end
  143. #endif // !TARGET_OS_OSX