FIRInstanceIDResultTest.m 4.7 KB

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