FIRInstanceIDResultTest.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/FIRInstanceID+Testing.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. @end
  29. @interface FIRInstanceIDResultTest : XCTestCase {
  30. FIRInstanceID *_instanceID;
  31. id _mockInstanceID;
  32. }
  33. @end
  34. @implementation FIRInstanceIDResultTest
  35. - (void)setUp {
  36. [super setUp];
  37. _instanceID = [[FIRInstanceID alloc] initPrivately];
  38. [_instanceID start];
  39. _mockInstanceID = OCMPartialMock(_instanceID);
  40. }
  41. - (void)tearDown {
  42. [_mockInstanceID stopMocking];
  43. [super tearDown];
  44. }
  45. - (void)testResultWithFailedIID {
  46. // mocking getting iid failed with error.
  47. OCMStub([_mockInstanceID
  48. getIDWithHandler:([OCMArg invokeBlockWithArgs:[NSNull null],
  49. [NSError errorWithFIRInstanceIDErrorCode:100],
  50. nil])]);
  51. [_instanceID
  52. instanceIDWithHandler:^(FIRInstanceIDResult *_Nullable result, NSError *_Nullable error) {
  53. XCTAssertNil(result);
  54. XCTAssertNotNil(error);
  55. XCTAssertEqual(error.code, 100);
  56. }];
  57. }
  58. - (void)testResultWithCacheToken {
  59. // mocking getting iid succeed and a cache token exists.
  60. OCMStub([_mockInstanceID
  61. getIDWithHandler:([OCMArg invokeBlockWithArgs:kFakeIID, [NSNull null], nil])]);
  62. OCMStub([_mockInstanceID cachedTokenIfAvailable]).andReturn(kFakeToken);
  63. [_instanceID
  64. instanceIDWithHandler:^(FIRInstanceIDResult *_Nullable result, NSError *_Nullable error) {
  65. XCTAssertNotNil(result);
  66. XCTAssertNil(error);
  67. XCTAssertEqualObjects(result.instanceID, kFakeIID);
  68. XCTAssertEqualObjects(result.token, kFakeToken);
  69. }];
  70. }
  71. - (void)testResultWithNewToken {
  72. // mocking getting iid succeed and a new token is generated.
  73. OCMStub([_mockInstanceID
  74. getIDWithHandler:([OCMArg invokeBlockWithArgs:kFakeIID, [NSNull null], nil])]);
  75. OCMStub([_mockInstanceID cachedTokenIfAvailable]).andReturn(nil);
  76. OCMStub([_mockInstanceID
  77. defaultTokenWithHandler:([OCMArg invokeBlockWithArgs:kFakeToken, [NSNull null], nil])]);
  78. [_instanceID
  79. instanceIDWithHandler:^(FIRInstanceIDResult *_Nullable result, NSError *_Nullable error) {
  80. XCTAssertNotNil(result);
  81. XCTAssertNil(error);
  82. XCTAssertEqualObjects(result.instanceID, kFakeIID);
  83. XCTAssertEqualObjects(result.token, kFakeToken);
  84. }];
  85. }
  86. - (void)testResultWithFailedFetchingToken {
  87. // mock getting iid succeed and token fails
  88. OCMStub([_mockInstanceID
  89. getIDWithHandler:([OCMArg invokeBlockWithArgs:kFakeIID, [NSNull null], nil])]);
  90. OCMStub([_mockInstanceID cachedTokenIfAvailable]).andReturn(nil);
  91. OCMStub([_mockInstanceID
  92. defaultTokenWithHandler:([OCMArg
  93. invokeBlockWithArgs:[NSNull null],
  94. [NSError errorWithFIRInstanceIDErrorCode:200],
  95. nil])]);
  96. [_instanceID
  97. instanceIDWithHandler:^(FIRInstanceIDResult *_Nullable result, NSError *_Nullable error) {
  98. XCTAssertNil(result);
  99. XCTAssertNotNil(error);
  100. XCTAssertEqual(error.code, 200);
  101. }];
  102. }
  103. - (void)testResultCanBeCoplied {
  104. // mocking getting iid succeed and a cache token exists.
  105. OCMStub([_mockInstanceID
  106. getIDWithHandler:([OCMArg invokeBlockWithArgs:kFakeIID, [NSNull null], nil])]);
  107. OCMStub([_mockInstanceID cachedTokenIfAvailable]).andReturn(kFakeToken);
  108. [_instanceID
  109. instanceIDWithHandler:^(FIRInstanceIDResult *_Nullable result, NSError *_Nullable error) {
  110. FIRInstanceIDResult *resultCopy = [result copy];
  111. XCTAssertEqualObjects(resultCopy.instanceID, kFakeIID);
  112. XCTAssertEqualObjects(resultCopy.token, kFakeToken);
  113. }];
  114. }
  115. @end