FIRIAMClientInfoFetcherTests.m 7.4 KB

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