FIRInstanceIDCheckinServiceTest.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <OCMock/OCMock.h>
  18. #import "Firebase/InstanceID/FIRInstanceIDCheckinPreferences+Internal.h"
  19. #import "Firebase/InstanceID/FIRInstanceIDCheckinPreferences.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. }
  34. - (void)tearDown {
  35. [super tearDown];
  36. }
  37. - (void)testCheckinWithSuccessfulCompletion {
  38. self.checkinService = [[FIRInstanceIDCheckinService alloc] init];
  39. FIRInstanceIDCheckinPreferences *existingCheckin = [self stubCheckinCacheWithValidData];
  40. [FIRInstanceIDCheckinService setCheckinTestBlock:[self successfulCheckinCompletionHandler]];
  41. XCTestExpectation *checkinCompletionExpectation =
  42. [self expectationWithDescription:@"Checkin Completion"];
  43. [self.checkinService
  44. checkinWithExistingCheckin:existingCheckin
  45. completion:^(FIRInstanceIDCheckinPreferences *checkinPreferences,
  46. NSError *error) {
  47. XCTAssertNil(error);
  48. XCTAssertEqualObjects(checkinPreferences.deviceID, kDeviceAuthId);
  49. XCTAssertEqualObjects(checkinPreferences.versionInfo, kVersionInfo);
  50. // For accuracy purposes it's better to compare seconds since the test
  51. // should never run for more than 1 second.
  52. NSInteger expectedTimestampInSeconds =
  53. FIRInstanceIDCurrentTimestampInSeconds();
  54. NSInteger actualTimestampInSeconds =
  55. checkinPreferences.lastCheckinTimestampMillis / 1000.0;
  56. XCTAssertEqual(expectedTimestampInSeconds, actualTimestampInSeconds);
  57. XCTAssertTrue([checkinPreferences hasValidCheckinInfo]);
  58. [checkinCompletionExpectation fulfill];
  59. }];
  60. [self waitForExpectationsWithTimeout:5
  61. handler:^(NSError *error) {
  62. XCTAssertNil(error);
  63. }];
  64. }
  65. - (void)testFailedCheckinService {
  66. self.checkinService = [[FIRInstanceIDCheckinService alloc] init];
  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. #pragma mark - Stub
  87. - (FIRInstanceIDCheckinPreferences *)stubCheckinCacheWithValidData {
  88. NSDictionary *gservicesData = @{
  89. @"FIRInstanceIDVersionInfo" : kVersionInfo,
  90. @"FIRInstanceIDLastCheckinTimestampKey" : @(FIRInstanceIDCurrentTimestampInMilliseconds())
  91. };
  92. FIRInstanceIDCheckinPreferences *checkinPreferences =
  93. [[FIRInstanceIDCheckinPreferences alloc] initWithDeviceID:kDeviceAuthId
  94. secretToken:kSecretToken];
  95. [checkinPreferences updateWithCheckinPlistContents:gservicesData];
  96. return checkinPreferences;
  97. }
  98. #pragma mark - Swizzle
  99. - (FIRInstanceIDURLRequestTestBlock)successfulCheckinCompletionHandler {
  100. return ^(NSURLRequest *request, FIRInstanceIDURLRequestTestResponseBlock testResponse) {
  101. NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:request.URL
  102. statusCode:200
  103. HTTPVersion:@"HTTP/1.1"
  104. headerFields:nil];
  105. NSMutableDictionary *dataResponse = [NSMutableDictionary dictionary];
  106. dataResponse[@"android_id"] = @([kDeviceAuthId longLongValue]);
  107. dataResponse[@"security_token"] = @([kSecretToken longLongValue]);
  108. dataResponse[@"time_msec"] = @(FIRInstanceIDCurrentTimestampInMilliseconds());
  109. dataResponse[@"version_info"] = kVersionInfo;
  110. dataResponse[@"digest"] = kDigest;
  111. NSData *data = [NSJSONSerialization dataWithJSONObject:dataResponse
  112. options:NSJSONWritingPrettyPrinted
  113. error:nil];
  114. testResponse(data, response, nil);
  115. };
  116. }
  117. - (FIRInstanceIDURLRequestTestBlock)failCheckinCompletionHandler {
  118. return ^(NSURLRequest *request, FIRInstanceIDURLRequestTestResponseBlock testResponse) {
  119. NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:request.URL
  120. statusCode:200
  121. HTTPVersion:@"HTTP/1.1"
  122. headerFields:nil];
  123. NSError *error =
  124. [NSError errorWithFIRInstanceIDErrorCode:kFIRInstanceIDErrorCodeInvalidRequest];
  125. testResponse(nil, response, error);
  126. };
  127. }
  128. @end