FIRInstanceIDResultTest.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/Public/FIRInstanceID.h"
  19. #import "Firebase/InstanceID/NSError+FIRInstanceID.h"
  20. static NSString *const kFakeIID = @"fE1e1PZJFSQ";
  21. static NSString *const kFakeToken =
  22. @"fE1e1PZJFSQ:APA91bFAOjp1ahBWn9rTlbjArwBEm_"
  23. @"yUTTzK6dhIvLqzqqCSabaa4TQVM0pGTmF6r7tmMHPe6VYiGMHuCwJFgj5v97xl78sUNMLwuPPhoci8z_"
  24. @"QGlCrTbxCFGzEUfvA3fGpGgIVQU2W6";
  25. @interface FIRInstanceID (ExposedForTest)
  26. - (NSString *)cachedTokenIfAvailable;
  27. - (void)defaultTokenWithHandler:(FIRInstanceIDTokenHandler)handler;
  28. - (instancetype)initPrivately;
  29. - (void)start;
  30. @end
  31. @interface FIRInstanceIDResultTest : XCTestCase {
  32. #pragma clang diagnostic push
  33. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  34. FIRInstanceID *_instanceID;
  35. #pragma clang diagnostic pop
  36. id _mockInstanceID;
  37. }
  38. @end
  39. @implementation FIRInstanceIDResultTest
  40. - (void)setUp {
  41. [super setUp];
  42. #pragma clang diagnostic push
  43. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  44. _mockInstanceID = OCMClassMock([FIRInstanceID class]);
  45. #pragma clang diagnostic pop
  46. }
  47. - (void)tearDown {
  48. [_mockInstanceID stopMocking];
  49. _mockInstanceID = nil;
  50. [super tearDown];
  51. }
  52. #pragma clang diagnostic push
  53. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  54. - (void)testResultWithFailedIID {
  55. // mocking getting iid failed with error.
  56. OCMStub([_mockInstanceID
  57. getIDWithHandler:([OCMArg invokeBlockWithArgs:[NSNull null],
  58. [NSError errorWithFIRInstanceIDErrorCode:100],
  59. nil])]);
  60. [_instanceID
  61. instanceIDWithHandler:^(FIRInstanceIDResult *_Nullable result, NSError *_Nullable error) {
  62. XCTAssertNil(result);
  63. XCTAssertNotNil(error);
  64. XCTAssertEqual(error.code, 100);
  65. }];
  66. }
  67. - (void)testResultWithCacheToken {
  68. // mocking getting iid succeed and a cache token exists.
  69. OCMStub([_mockInstanceID
  70. getIDWithHandler:([OCMArg invokeBlockWithArgs:kFakeIID, [NSNull null], nil])]);
  71. OCMStub([_mockInstanceID cachedTokenIfAvailable]).andReturn(kFakeToken);
  72. [_instanceID
  73. instanceIDWithHandler:^(FIRInstanceIDResult *_Nullable result, NSError *_Nullable error) {
  74. XCTAssertNotNil(result);
  75. XCTAssertNil(error);
  76. XCTAssertEqualObjects(result.instanceID, kFakeIID);
  77. XCTAssertEqualObjects(result.token, kFakeToken);
  78. }];
  79. }
  80. - (void)testResultWithNewToken {
  81. // mocking getting iid succeed and a new token is generated.
  82. OCMStub([_mockInstanceID
  83. getIDWithHandler:([OCMArg invokeBlockWithArgs:kFakeIID, [NSNull null], nil])]);
  84. OCMStub([_mockInstanceID cachedTokenIfAvailable]).andReturn(nil);
  85. OCMStub([_mockInstanceID
  86. defaultTokenWithHandler:([OCMArg invokeBlockWithArgs:kFakeToken, [NSNull null], nil])]);
  87. [_instanceID
  88. instanceIDWithHandler:^(FIRInstanceIDResult *_Nullable result, NSError *_Nullable error) {
  89. XCTAssertNotNil(result);
  90. XCTAssertNil(error);
  91. XCTAssertEqualObjects(result.instanceID, kFakeIID);
  92. XCTAssertEqualObjects(result.token, kFakeToken);
  93. }];
  94. }
  95. - (void)testResultWithFailedFetchingToken {
  96. // mock getting iid succeed and token fails
  97. OCMStub([_mockInstanceID
  98. getIDWithHandler:([OCMArg invokeBlockWithArgs:kFakeIID, [NSNull null], nil])]);
  99. OCMStub([_mockInstanceID cachedTokenIfAvailable]).andReturn(nil);
  100. OCMStub([_mockInstanceID
  101. defaultTokenWithHandler:([OCMArg
  102. invokeBlockWithArgs:[NSNull null],
  103. [NSError errorWithFIRInstanceIDErrorCode:200],
  104. nil])]);
  105. [_instanceID
  106. instanceIDWithHandler:^(FIRInstanceIDResult *_Nullable result, NSError *_Nullable error) {
  107. XCTAssertNil(result);
  108. XCTAssertNotNil(error);
  109. XCTAssertEqual(error.code, 200);
  110. }];
  111. }
  112. - (void)testResultCanBeCoplied {
  113. // mocking getting iid succeed and a cache token exists.
  114. OCMStub([_mockInstanceID
  115. getIDWithHandler:([OCMArg invokeBlockWithArgs:kFakeIID, [NSNull null], nil])]);
  116. OCMStub([_mockInstanceID cachedTokenIfAvailable]).andReturn(kFakeToken);
  117. [_instanceID
  118. instanceIDWithHandler:^(FIRInstanceIDResult *_Nullable result, NSError *_Nullable error) {
  119. FIRInstanceIDResult *resultCopy = [result copy];
  120. XCTAssertEqualObjects(resultCopy.instanceID, kFakeIID);
  121. XCTAssertEqualObjects(resultCopy.token, kFakeToken);
  122. }];
  123. }
  124. #pragma clang diagnostic pop
  125. @end