FIRInstallationsTests.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 <FirebaseCore/FIRAppInternal.h>
  19. #import <FirebaseCore/FIROptionsInternal.h>
  20. #import "FBLPromise+Testing.h"
  21. #import "FIRInstallations+Tests.h"
  22. #import "FIRInstallationsErrorUtil+Tests.h"
  23. #import "FIRInstallationsItem+Tests.h"
  24. #import "FIRInstallations.h"
  25. #import "FIRInstallationsAuthTokenResultInternal.h"
  26. #import "FIRInstallationsErrorUtil.h"
  27. #import "FIRInstallationsHTTPError.h"
  28. #import "FIRInstallationsIDController.h"
  29. #import "FIRInstallationsStoredAuthToken.h"
  30. @interface FIRInstallationsTests : XCTestCase
  31. @property(nonatomic) FIRInstallations *installations;
  32. @property(nonatomic) id mockIDController;
  33. @property(nonatomic) FIROptions *appOptions;
  34. @end
  35. @implementation FIRInstallationsTests
  36. - (void)setUp {
  37. [super setUp];
  38. self.appOptions = [[FIROptions alloc] initWithGoogleAppID:@"GoogleAppID"
  39. GCMSenderID:@"GCMSenderID"];
  40. self.mockIDController = OCMClassMock([FIRInstallationsIDController class]);
  41. self.installations = [[FIRInstallations alloc] initWithAppOptions:self.appOptions
  42. appName:@"appName"
  43. installationsIDController:self.mockIDController
  44. prefetchAuthToken:NO];
  45. }
  46. - (void)tearDown {
  47. self.installations = nil;
  48. self.mockIDController = nil;
  49. [super tearDown];
  50. }
  51. - (void)testDefaultInstallationWhenNoDefaultAppThenIsNil {
  52. XCTAssertThrows([FIRInstallations installations]);
  53. }
  54. - (void)testInstallationIDSuccess {
  55. // Stub get installation.
  56. FIRInstallationsItem *installation = [FIRInstallationsItem createUnregisteredInstallationItem];
  57. OCMExpect([self.mockIDController getInstallationItem])
  58. .andReturn([FBLPromise resolvedWith:installation]);
  59. XCTestExpectation *idExpectation = [self expectationWithDescription:@"InstallationIDSuccess"];
  60. [self.installations
  61. installationIDWithCompletion:^(NSString *_Nullable identifier, NSError *_Nullable error) {
  62. XCTAssertNil(error);
  63. XCTAssertNotNil(identifier);
  64. XCTAssertEqualObjects(identifier, installation.firebaseInstallationID);
  65. [idExpectation fulfill];
  66. }];
  67. [self waitForExpectations:@[ idExpectation ] timeout:0.5];
  68. OCMVerifyAll(self.mockIDController);
  69. }
  70. - (void)testInstallationIDError {
  71. // Stub get installation.
  72. FBLPromise *errorPromise = [FBLPromise pendingPromise];
  73. NSError *privateError = [NSError errorWithDomain:@"TestsError" code:-1 userInfo:nil];
  74. [errorPromise reject:privateError];
  75. OCMExpect([self.mockIDController getInstallationItem]).andReturn(errorPromise);
  76. XCTestExpectation *idExpectation = [self expectationWithDescription:@"InstallationIDSuccess"];
  77. [self.installations
  78. installationIDWithCompletion:^(NSString *_Nullable identifier, NSError *_Nullable error) {
  79. XCTAssertNil(identifier);
  80. XCTAssertNotNil(error);
  81. XCTAssertEqualObjects(error.domain, kFirebaseInstallationsErrorDomain);
  82. XCTAssertEqualObjects(error.userInfo[NSUnderlyingErrorKey], errorPromise.error);
  83. [idExpectation fulfill];
  84. }];
  85. [self waitForExpectations:@[ idExpectation ] timeout:0.5];
  86. OCMVerifyAll(self.mockIDController);
  87. }
  88. - (void)testAuthTokenSuccess {
  89. FIRInstallationsItem *installationWithToken =
  90. [FIRInstallationsItem createRegisteredInstallationItemWithAppID:self.appOptions.googleAppID
  91. appName:@"appName"];
  92. installationWithToken.authToken.token = @"token";
  93. installationWithToken.authToken.expirationDate = [NSDate dateWithTimeIntervalSinceNow:1000];
  94. OCMExpect([self.mockIDController getAuthTokenForcingRefresh:NO])
  95. .andReturn([FBLPromise resolvedWith:installationWithToken]);
  96. XCTestExpectation *tokenExpectation = [self expectationWithDescription:@"AuthTokenSuccess"];
  97. [self.installations
  98. authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  99. NSError *_Nullable error) {
  100. XCTAssertNotNil(tokenResult);
  101. XCTAssertGreaterThan(tokenResult.authToken.length, 0);
  102. XCTAssertTrue([tokenResult.expirationDate laterDate:[NSDate date]]);
  103. XCTAssertNil(error);
  104. [tokenExpectation fulfill];
  105. }];
  106. [self waitForExpectations:@[ tokenExpectation ] timeout:0.5];
  107. OCMVerifyAll(self.mockIDController);
  108. }
  109. - (void)testAuthTokenError {
  110. FBLPromise *errorPromise = [FBLPromise pendingPromise];
  111. [errorPromise reject:[FIRInstallationsErrorUtil
  112. APIErrorWithHTTPCode:FIRInstallationsHTTPCodesServerInternalError]];
  113. OCMExpect([self.mockIDController getAuthTokenForcingRefresh:NO]).andReturn(errorPromise);
  114. XCTestExpectation *tokenExpectation = [self expectationWithDescription:@"AuthTokenSuccess"];
  115. [self.installations
  116. authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  117. NSError *_Nullable error) {
  118. XCTAssertNil(tokenResult);
  119. XCTAssertEqualObjects(error, errorPromise.error);
  120. [tokenExpectation fulfill];
  121. }];
  122. [self waitForExpectations:@[ tokenExpectation ] timeout:0.5];
  123. OCMVerifyAll(self.mockIDController);
  124. }
  125. - (void)testAuthTokenForcingRefreshSuccess {
  126. FIRInstallationsItem *installationWithToken =
  127. [FIRInstallationsItem createRegisteredInstallationItemWithAppID:self.appOptions.googleAppID
  128. appName:@"appName"];
  129. installationWithToken.authToken.token = @"token";
  130. installationWithToken.authToken.expirationDate = [NSDate dateWithTimeIntervalSinceNow:1000];
  131. OCMExpect([self.mockIDController getAuthTokenForcingRefresh:YES])
  132. .andReturn([FBLPromise resolvedWith:installationWithToken]);
  133. XCTestExpectation *tokenExpectation = [self expectationWithDescription:@"AuthTokenSuccess"];
  134. [self.installations
  135. authTokenForcingRefresh:YES
  136. completion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  137. NSError *_Nullable error) {
  138. XCTAssertNil(error);
  139. XCTAssertNotNil(tokenResult);
  140. XCTAssertEqualObjects(tokenResult.authToken,
  141. installationWithToken.authToken.token);
  142. XCTAssertEqualObjects(tokenResult.expirationDate,
  143. installationWithToken.authToken.expirationDate);
  144. [tokenExpectation fulfill];
  145. }];
  146. [self waitForExpectations:@[ tokenExpectation ] timeout:0.5];
  147. OCMVerifyAll(self.mockIDController);
  148. }
  149. - (void)testAuthTokenForcingRefreshError {
  150. FBLPromise *errorPromise = [FBLPromise pendingPromise];
  151. [errorPromise reject:[FIRInstallationsErrorUtil
  152. APIErrorWithHTTPCode:FIRInstallationsHTTPCodesServerInternalError]];
  153. OCMExpect([self.mockIDController getAuthTokenForcingRefresh:YES]).andReturn(errorPromise);
  154. XCTestExpectation *tokenExpectation = [self expectationWithDescription:@"AuthTokenSuccess"];
  155. [self.installations
  156. authTokenForcingRefresh:YES
  157. completion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  158. NSError *_Nullable error) {
  159. XCTAssertNil(tokenResult);
  160. XCTAssertEqualObjects(error, errorPromise.error);
  161. [tokenExpectation fulfill];
  162. }];
  163. [self waitForExpectations:@[ tokenExpectation ] timeout:0.5];
  164. OCMVerifyAll(self.mockIDController);
  165. }
  166. - (void)testDeleteSuccess {
  167. OCMExpect([self.mockIDController deleteInstallation])
  168. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  169. XCTestExpectation *deleteExpectation = [self expectationWithDescription:@"DeleteSuccess"];
  170. [self.installations deleteWithCompletion:^(NSError *_Nullable error) {
  171. XCTAssertNil(error);
  172. [deleteExpectation fulfill];
  173. }];
  174. [self waitForExpectations:@[ deleteExpectation ] timeout:0.5];
  175. }
  176. - (void)testDeleteError {
  177. FBLPromise *errorPromise = [FBLPromise pendingPromise];
  178. NSError *APIError =
  179. [FIRInstallationsErrorUtil APIErrorWithHTTPCode:FIRInstallationsHTTPCodesServerInternalError];
  180. [errorPromise reject:APIError];
  181. OCMExpect([self.mockIDController deleteInstallation]).andReturn(errorPromise);
  182. XCTestExpectation *deleteExpectation = [self expectationWithDescription:@"deleteExpectation"];
  183. [self.installations deleteWithCompletion:^(NSError *_Nullable error) {
  184. XCTAssertEqualObjects(error, APIError);
  185. [deleteExpectation fulfill];
  186. }];
  187. [self waitForExpectations:@[ deleteExpectation ] timeout:0.5];
  188. }
  189. @end