FIRInstanceIDCheckinServiceTest.m 7.5 KB

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