FIRInstanceIDResultTest.m 4.7 KB

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