FIRIAMClientInfoFetcherTests.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright 2020 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 <OCMock/OCMock.h>
  17. #import <XCTest/XCTest.h>
  18. #import "FIRIAMClientInfoFetcher.h"
  19. #import "FIRIAMSDKRuntimeErrorCodes.h"
  20. #import <FirebaseInstallations/FIRInstallations.h>
  21. #import <FirebaseInstallations/FIRInstallationsAuthTokenResult.h>
  22. @interface FIRInstallationsAuthTokenResult (Tests)
  23. - (instancetype)initWithToken:(NSString *)token expirationDate:(NSDate *)expirationDate;
  24. @end
  25. @interface FIRIAMClientInfoFetcherTests : XCTestCase
  26. @property(nonatomic, strong) FIRIAMClientInfoFetcher *clientInfoFetcher;
  27. @property(nonatomic) id mockInstallations;
  28. @end
  29. @implementation FIRIAMClientInfoFetcherTests
  30. - (void)setUp {
  31. self.mockInstallations = OCMClassMock([FIRInstallations class]);
  32. self.clientInfoFetcher =
  33. [[FIRIAMClientInfoFetcher alloc] initWithFirebaseInstallations:self.mockInstallations];
  34. }
  35. - (void)tearDown {
  36. [self.mockInstallations stopMocking];
  37. }
  38. - (void)testNoInstallations {
  39. FIRIAMClientInfoFetcher *fetcherWithoutInstallations =
  40. [[FIRIAMClientInfoFetcher alloc] initWithFirebaseInstallations:nil];
  41. [fetcherWithoutInstallations
  42. fetchFirebaseInstallationDataWithProjectNumber:@"my project number"
  43. withCompletion:^(NSString *_Nullable FID,
  44. NSString *_Nullable FISToken,
  45. NSError *_Nullable error) {
  46. XCTAssertEqual(
  47. error.code,
  48. FIRIAMSDKRuntimeErrorNoFirebaseInstallationsObject);
  49. }];
  50. }
  51. - (void)testReturnsBothFIDAndToken {
  52. FIRInstallationsAuthTokenResult *tokenResult =
  53. [[FIRInstallationsAuthTokenResult alloc] initWithToken:@"mock_token"
  54. expirationDate:[NSDate distantFuture]];
  55. [self mockInstanceIDMethodForTokenAndIdentity:tokenResult
  56. tokenError:nil
  57. identity:@"mock_id"
  58. identityError:nil];
  59. [self.clientInfoFetcher
  60. fetchFirebaseInstallationDataWithProjectNumber:@"my project number"
  61. withCompletion:^(NSString *_Nullable FID,
  62. NSString *_Nullable FISToken,
  63. NSError *_Nullable error) {
  64. XCTAssertEqualObjects(FID, @"mock_id");
  65. XCTAssertEqualObjects(FISToken, @"mock_token");
  66. XCTAssertNil(error);
  67. }];
  68. }
  69. - (void)testReturnsTokenButNotFID {
  70. // Mock error where no installations ID was fetched.
  71. NSError *error =
  72. [NSError errorWithDomain:@"com.mock.installations"
  73. code:0
  74. userInfo:@{NSLocalizedDescriptionKey : @"Installations couldn't return FID"}];
  75. FIRInstallationsAuthTokenResult *tokenResult =
  76. [[FIRInstallationsAuthTokenResult alloc] initWithToken:@"mock_token"
  77. expirationDate:[NSDate distantFuture]];
  78. [self mockInstanceIDMethodForTokenAndIdentity:tokenResult
  79. tokenError:nil
  80. identity:nil
  81. identityError:error];
  82. [self.clientInfoFetcher
  83. fetchFirebaseInstallationDataWithProjectNumber:@"my project number"
  84. withCompletion:^(NSString *_Nullable FID,
  85. NSString *_Nullable FISToken,
  86. NSError *_Nullable error) {
  87. // FID should be nil.
  88. XCTAssertNil(FID);
  89. // FIS token is still passed.
  90. XCTAssertEqualObjects(FISToken, @"mock_token");
  91. // Validate error gets propagated.
  92. XCTAssertEqualObjects(error.localizedDescription,
  93. @"Installations couldn't return FID");
  94. }];
  95. }
  96. - (void)testDoesntReturnToken {
  97. // Mock error where no auth token was fetched.
  98. NSError *error = [NSError
  99. errorWithDomain:@"com.mock.installations"
  100. code:0
  101. userInfo:@{NSLocalizedDescriptionKey : @"Installations couldn't return FIS token"}];
  102. [self mockInstanceIDMethodForTokenAndIdentity:nil
  103. tokenError:error
  104. identity:nil
  105. identityError:nil];
  106. [self.clientInfoFetcher
  107. fetchFirebaseInstallationDataWithProjectNumber:@"my project number"
  108. withCompletion:^(NSString *_Nullable FID,
  109. NSString *_Nullable FISToken,
  110. NSError *_Nullable error) {
  111. XCTAssertNil(FID);
  112. XCTAssertNil(FISToken);
  113. // Validate error gets propagated.
  114. XCTAssertEqualObjects(
  115. error.localizedDescription,
  116. @"Installations couldn't return FIS token");
  117. }];
  118. }
  119. // Mock FIRInstallations methods.
  120. - (void)mockInstanceIDMethodForTokenAndIdentity:
  121. (nullable FIRInstallationsAuthTokenResult *)tokenResult
  122. tokenError:(nullable NSError *)tokenError
  123. identity:(nullable NSString *)identity
  124. identityError:(nullable NSError *)identityError {
  125. OCMStub([self.mockInstallations
  126. installationIDWithCompletion:([OCMArg
  127. invokeBlockWithArgs:(identity ? identity : [NSNull null]),
  128. (identityError ? identityError
  129. : [NSNull null]),
  130. nil])]);
  131. OCMStub([self.mockInstallations
  132. authTokenWithCompletion:([OCMArg
  133. invokeBlockWithArgs:(tokenResult ? tokenResult : [NSNull null]),
  134. (tokenError ? tokenError : [NSNull null]),
  135. nil])]);
  136. }
  137. @end