FIRInstanceIDCheckinServiceTest.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #import <XCTest/XCTest.h>
  17. #import <FirebaseCore/FIRAppInternal.h>
  18. #import <OCMock/OCMock.h>
  19. #import "Firebase/InstanceID/FIRInstanceIDCheckinPreferences+Internal.h"
  20. #import "Firebase/InstanceID/FIRInstanceIDCheckinPreferences.h"
  21. #import "Firebase/InstanceID/FIRInstanceIDCheckinService.h"
  22. #import "Firebase/InstanceID/FIRInstanceIDUtilities.h"
  23. #import "Firebase/InstanceID/NSError+FIRInstanceID.h"
  24. static NSString *const kDeviceAuthId = @"1234";
  25. static NSString *const kSecretToken = @"567890";
  26. static NSString *const kDigest = @"com.google.digest";
  27. static NSString *const kVersionInfo = @"1.0";
  28. @interface FIRInstanceIDCheckinServiceTest : XCTestCase
  29. @property(nonatomic, readwrite, strong) FIRInstanceIDCheckinService *checkinService;
  30. @end
  31. @implementation FIRInstanceIDCheckinServiceTest
  32. - (void)setUp {
  33. [super setUp];
  34. self.checkinService = [[FIRInstanceIDCheckinService alloc] init];
  35. }
  36. - (void)tearDown {
  37. self.checkinService = nil;
  38. [super tearDown];
  39. }
  40. - (void)testCheckinWithSuccessfulCompletion {
  41. FIRInstanceIDCheckinPreferences *existingCheckin = [self stubCheckinCacheWithValidData];
  42. [FIRInstanceIDCheckinService setCheckinTestBlock:[self successfulCheckinCompletionHandler]];
  43. XCTestExpectation *checkinCompletionExpectation =
  44. [self expectationWithDescription:@"Checkin Completion"];
  45. [self.checkinService
  46. checkinWithExistingCheckin:existingCheckin
  47. completion:^(FIRInstanceIDCheckinPreferences *checkinPreferences,
  48. NSError *error) {
  49. XCTAssertNil(error);
  50. XCTAssertEqualObjects(checkinPreferences.deviceID, kDeviceAuthId);
  51. XCTAssertEqualObjects(checkinPreferences.versionInfo, kVersionInfo);
  52. // For accuracy purposes it's better to compare seconds since the test
  53. // should never run for more than 1 second.
  54. NSInteger expectedTimestampInSeconds =
  55. FIRInstanceIDCurrentTimestampInSeconds();
  56. NSInteger actualTimestampInSeconds =
  57. checkinPreferences.lastCheckinTimestampMillis / 1000.0;
  58. XCTAssertEqual(expectedTimestampInSeconds, actualTimestampInSeconds);
  59. XCTAssertTrue([checkinPreferences hasValidCheckinInfo]);
  60. [checkinCompletionExpectation fulfill];
  61. }];
  62. [self waitForExpectationsWithTimeout:5
  63. handler:^(NSError *error) {
  64. XCTAssertNil(error);
  65. }];
  66. }
  67. - (void)testFailedCheckinService {
  68. [FIRInstanceIDCheckinService setCheckinTestBlock:[self failCheckinCompletionHandler]];
  69. XCTestExpectation *checkinCompletionExpectation =
  70. [self expectationWithDescription:@"Checkin Completion"];
  71. [self.checkinService
  72. checkinWithExistingCheckin:nil
  73. completion:^(FIRInstanceIDCheckinPreferences *preferences, NSError *error) {
  74. XCTAssertNotNil(error);
  75. XCTAssertNil(preferences.deviceID);
  76. XCTAssertNil(preferences.secretToken);
  77. XCTAssertFalse([preferences hasValidCheckinInfo]);
  78. [checkinCompletionExpectation fulfill];
  79. }];
  80. [self waitForExpectationsWithTimeout:5
  81. handler:^(NSError *error) {
  82. if (error) {
  83. XCTFail(@"Checkin Timeout Error: %@", error);
  84. }
  85. }];
  86. }
  87. - (void)testCheckinServiceAddsFirebaseUserAgentToHTTPHeader {
  88. NSString *expectedFirebaseUserAgent = [FIRApp firebaseUserAgent];
  89. FIRInstanceIDURLRequestTestBlock successHandler = [self successfulCheckinCompletionHandler];
  90. [FIRInstanceIDCheckinService
  91. setCheckinTestBlock:^(NSURLRequest *request,
  92. FIRInstanceIDURLRequestTestResponseBlock response) {
  93. NSString *requestFirebaseUserAgentValue =
  94. request.allHTTPHeaderFields[kFIRInstanceIDFirebaseUserAgentKey];
  95. XCTAssertEqualObjects(requestFirebaseUserAgentValue, expectedFirebaseUserAgent);
  96. successHandler(request, response);
  97. }];
  98. XCTestExpectation *checkinCompletionExpectation =
  99. [self expectationWithDescription:@"Checkin Completion"];
  100. [self.checkinService
  101. checkinWithExistingCheckin:nil
  102. completion:^(FIRInstanceIDCheckinPreferences *preferences, NSError *error) {
  103. [checkinCompletionExpectation fulfill];
  104. }];
  105. [self waitForExpectationsWithTimeout:5
  106. handler:^(NSError *error) {
  107. if (error) {
  108. XCTFail(@"Checkin Timeout Error: %@", error);
  109. }
  110. }];
  111. }
  112. - (void)testCheckinServiceFailsWithErrorAfterStopFetching {
  113. [self.checkinService stopFetching];
  114. XCTestExpectation *checkinCompletionExpectation =
  115. [self expectationWithDescription:@"Checkin Completion"];
  116. [self.checkinService
  117. checkinWithExistingCheckin:nil
  118. completion:^(FIRInstanceIDCheckinPreferences *preferences, NSError *error) {
  119. [checkinCompletionExpectation fulfill];
  120. XCTAssertNil(preferences);
  121. XCTAssertNotNil(error);
  122. XCTAssertEqual(error.code, kFIRInstanceIDErrorCodeRegistrarFailedToCheckIn);
  123. }];
  124. [self waitForExpectationsWithTimeout:5
  125. handler:^(NSError *error) {
  126. if (error) {
  127. XCTFail(@"Checkin Timeout Error: %@", error);
  128. }
  129. }];
  130. }
  131. #pragma mark - Stub
  132. - (FIRInstanceIDCheckinPreferences *)stubCheckinCacheWithValidData {
  133. NSDictionary *gservicesData = @{
  134. @"FIRInstanceIDVersionInfo" : kVersionInfo,
  135. @"FIRInstanceIDLastCheckinTimestampKey" : @(FIRInstanceIDCurrentTimestampInMilliseconds())
  136. };
  137. FIRInstanceIDCheckinPreferences *checkinPreferences =
  138. [[FIRInstanceIDCheckinPreferences alloc] initWithDeviceID:kDeviceAuthId
  139. secretToken:kSecretToken];
  140. [checkinPreferences updateWithCheckinPlistContents:gservicesData];
  141. return checkinPreferences;
  142. }
  143. #pragma mark - Swizzle
  144. - (FIRInstanceIDURLRequestTestBlock)successfulCheckinCompletionHandler {
  145. return ^(NSURLRequest *request, FIRInstanceIDURLRequestTestResponseBlock testResponse) {
  146. NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:request.URL
  147. statusCode:200
  148. HTTPVersion:@"HTTP/1.1"
  149. headerFields:nil];
  150. NSMutableDictionary *dataResponse = [NSMutableDictionary dictionary];
  151. dataResponse[@"android_id"] = @([kDeviceAuthId longLongValue]);
  152. dataResponse[@"security_token"] = @([kSecretToken longLongValue]);
  153. dataResponse[@"time_msec"] = @(FIRInstanceIDCurrentTimestampInMilliseconds());
  154. dataResponse[@"version_info"] = kVersionInfo;
  155. dataResponse[@"digest"] = kDigest;
  156. NSData *data = [NSJSONSerialization dataWithJSONObject:dataResponse
  157. options:NSJSONWritingPrettyPrinted
  158. error:nil];
  159. testResponse(data, response, nil);
  160. };
  161. }
  162. - (FIRInstanceIDURLRequestTestBlock)failCheckinCompletionHandler {
  163. return ^(NSURLRequest *request, FIRInstanceIDURLRequestTestResponseBlock testResponse) {
  164. NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:request.URL
  165. statusCode:200
  166. HTTPVersion:@"HTTP/1.1"
  167. headerFields:nil];
  168. NSError *error =
  169. [NSError errorWithFIRInstanceIDErrorCode:kFIRInstanceIDErrorCodeInvalidRequest];
  170. testResponse(nil, response, error);
  171. };
  172. }
  173. @end