FIRInstallationsTests.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 APIErrorWithHTTPCode:500]];
  112. OCMExpect([self.mockIDController getAuthTokenForcingRefresh:NO]).andReturn(errorPromise);
  113. XCTestExpectation *tokenExpectation = [self expectationWithDescription:@"AuthTokenSuccess"];
  114. [self.installations
  115. authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  116. NSError *_Nullable error) {
  117. XCTAssertNil(tokenResult);
  118. XCTAssertEqualObjects(error, errorPromise.error);
  119. [tokenExpectation fulfill];
  120. }];
  121. [self waitForExpectations:@[ tokenExpectation ] timeout:0.5];
  122. OCMVerifyAll(self.mockIDController);
  123. }
  124. - (void)testAuthTokenForcingRefreshSuccess {
  125. FIRInstallationsItem *installationWithToken =
  126. [FIRInstallationsItem createRegisteredInstallationItemWithAppID:self.appOptions.googleAppID
  127. appName:@"appName"];
  128. installationWithToken.authToken.token = @"token";
  129. installationWithToken.authToken.expirationDate = [NSDate dateWithTimeIntervalSinceNow:1000];
  130. OCMExpect([self.mockIDController getAuthTokenForcingRefresh:YES])
  131. .andReturn([FBLPromise resolvedWith:installationWithToken]);
  132. XCTestExpectation *tokenExpectation = [self expectationWithDescription:@"AuthTokenSuccess"];
  133. [self.installations
  134. authTokenForcingRefresh:YES
  135. completion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  136. NSError *_Nullable error) {
  137. XCTAssertNil(error);
  138. XCTAssertNotNil(tokenResult);
  139. XCTAssertEqualObjects(tokenResult.authToken,
  140. installationWithToken.authToken.token);
  141. XCTAssertEqualObjects(tokenResult.expirationDate,
  142. installationWithToken.authToken.expirationDate);
  143. [tokenExpectation fulfill];
  144. }];
  145. [self waitForExpectations:@[ tokenExpectation ] timeout:0.5];
  146. OCMVerifyAll(self.mockIDController);
  147. }
  148. - (void)testAuthTokenForcingRefreshError {
  149. FBLPromise *errorPromise = [FBLPromise pendingPromise];
  150. [errorPromise reject:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:500]];
  151. OCMExpect([self.mockIDController getAuthTokenForcingRefresh:YES]).andReturn(errorPromise);
  152. XCTestExpectation *tokenExpectation = [self expectationWithDescription:@"AuthTokenSuccess"];
  153. [self.installations
  154. authTokenForcingRefresh:YES
  155. completion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  156. NSError *_Nullable error) {
  157. XCTAssertNil(tokenResult);
  158. XCTAssertEqualObjects(error, errorPromise.error);
  159. [tokenExpectation fulfill];
  160. }];
  161. [self waitForExpectations:@[ tokenExpectation ] timeout:0.5];
  162. OCMVerifyAll(self.mockIDController);
  163. }
  164. - (void)testDeleteSuccess {
  165. OCMExpect([self.mockIDController deleteInstallation])
  166. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  167. XCTestExpectation *deleteExpectation = [self expectationWithDescription:@"DeleteSuccess"];
  168. [self.installations deleteWithCompletion:^(NSError *_Nullable error) {
  169. XCTAssertNil(error);
  170. [deleteExpectation fulfill];
  171. }];
  172. [self waitForExpectations:@[ deleteExpectation ] timeout:0.5];
  173. }
  174. - (void)testDeleteError {
  175. FBLPromise *errorPromise = [FBLPromise pendingPromise];
  176. [errorPromise reject:[FIRInstallationsErrorUtil APIErrorWithHTTPCode:500]];
  177. OCMExpect([self.mockIDController deleteInstallation]).andReturn(errorPromise);
  178. XCTestExpectation *deleteExpectation = [self expectationWithDescription:@"DeleteSuccess"];
  179. [self.installations deleteWithCompletion:^(NSError *_Nullable error) {
  180. XCTAssertNotNil(error);
  181. // TODO: Verify the error content.
  182. [deleteExpectation fulfill];
  183. }];
  184. [self waitForExpectations:@[ deleteExpectation ] timeout:0.5];
  185. }
  186. @end