FIRInstallationsTests.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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.appOptions.APIKey = @"APIKey";
  41. self.appOptions.projectID = @"ProjectID";
  42. self.mockIDController = OCMClassMock([FIRInstallationsIDController class]);
  43. self.installations = [[FIRInstallations alloc] initWithAppOptions:self.appOptions
  44. appName:@"appName"
  45. installationsIDController:self.mockIDController
  46. prefetchAuthToken:NO];
  47. }
  48. - (void)tearDown {
  49. self.installations = nil;
  50. self.mockIDController = nil;
  51. [super tearDown];
  52. }
  53. - (void)testDefaultInstallationWhenNoDefaultAppThenIsNil {
  54. XCTAssertThrows([FIRInstallations installations]);
  55. }
  56. - (void)testInstallationIDSuccess {
  57. // Stub get installation.
  58. FIRInstallationsItem *installation = [FIRInstallationsItem createUnregisteredInstallationItem];
  59. OCMExpect([self.mockIDController getInstallationItem])
  60. .andReturn([FBLPromise resolvedWith:installation]);
  61. XCTestExpectation *idExpectation = [self expectationWithDescription:@"InstallationIDSuccess"];
  62. [self.installations
  63. installationIDWithCompletion:^(NSString *_Nullable identifier, NSError *_Nullable error) {
  64. XCTAssertNil(error);
  65. XCTAssertNotNil(identifier);
  66. XCTAssertEqualObjects(identifier, installation.firebaseInstallationID);
  67. [idExpectation fulfill];
  68. }];
  69. [self waitForExpectations:@[ idExpectation ] timeout:0.5];
  70. OCMVerifyAll(self.mockIDController);
  71. }
  72. - (void)testInstallationIDError {
  73. // Stub get installation.
  74. FBLPromise *errorPromise = [FBLPromise pendingPromise];
  75. NSError *privateError = [NSError errorWithDomain:@"TestsError" code:-1 userInfo:nil];
  76. [errorPromise reject:privateError];
  77. OCMExpect([self.mockIDController getInstallationItem]).andReturn(errorPromise);
  78. XCTestExpectation *idExpectation = [self expectationWithDescription:@"InstallationIDSuccess"];
  79. [self.installations
  80. installationIDWithCompletion:^(NSString *_Nullable identifier, NSError *_Nullable error) {
  81. XCTAssertNil(identifier);
  82. XCTAssertNotNil(error);
  83. XCTAssertEqualObjects(error.domain, kFirebaseInstallationsErrorDomain);
  84. XCTAssertEqualObjects(error.userInfo[NSUnderlyingErrorKey], errorPromise.error);
  85. [idExpectation fulfill];
  86. }];
  87. [self waitForExpectations:@[ idExpectation ] timeout:0.5];
  88. OCMVerifyAll(self.mockIDController);
  89. }
  90. - (void)testAuthTokenSuccess {
  91. FIRInstallationsItem *installationWithToken =
  92. [FIRInstallationsItem createRegisteredInstallationItemWithAppID:self.appOptions.googleAppID
  93. appName:@"appName"];
  94. installationWithToken.authToken.token = @"token";
  95. installationWithToken.authToken.expirationDate = [NSDate dateWithTimeIntervalSinceNow:1000];
  96. OCMExpect([self.mockIDController getAuthTokenForcingRefresh:NO])
  97. .andReturn([FBLPromise resolvedWith:installationWithToken]);
  98. XCTestExpectation *tokenExpectation = [self expectationWithDescription:@"AuthTokenSuccess"];
  99. [self.installations
  100. authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  101. NSError *_Nullable error) {
  102. XCTAssertNotNil(tokenResult);
  103. XCTAssertGreaterThan(tokenResult.authToken.length, 0);
  104. XCTAssertTrue([tokenResult.expirationDate laterDate:[NSDate date]]);
  105. XCTAssertNil(error);
  106. [tokenExpectation fulfill];
  107. }];
  108. [self waitForExpectations:@[ tokenExpectation ] timeout:0.5];
  109. OCMVerifyAll(self.mockIDController);
  110. }
  111. - (void)testAuthTokenError {
  112. FBLPromise *errorPromise = [FBLPromise pendingPromise];
  113. [errorPromise reject:[FIRInstallationsErrorUtil
  114. APIErrorWithHTTPCode:FIRInstallationsHTTPCodesServerInternalError]];
  115. OCMExpect([self.mockIDController getAuthTokenForcingRefresh:NO]).andReturn(errorPromise);
  116. XCTestExpectation *tokenExpectation = [self expectationWithDescription:@"AuthTokenSuccess"];
  117. [self.installations
  118. authTokenWithCompletion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  119. NSError *_Nullable error) {
  120. XCTAssertNil(tokenResult);
  121. XCTAssertEqualObjects(error, errorPromise.error);
  122. [tokenExpectation fulfill];
  123. }];
  124. [self waitForExpectations:@[ tokenExpectation ] timeout:0.5];
  125. OCMVerifyAll(self.mockIDController);
  126. }
  127. - (void)testAuthTokenForcingRefreshSuccess {
  128. FIRInstallationsItem *installationWithToken =
  129. [FIRInstallationsItem createRegisteredInstallationItemWithAppID:self.appOptions.googleAppID
  130. appName:@"appName"];
  131. installationWithToken.authToken.token = @"token";
  132. installationWithToken.authToken.expirationDate = [NSDate dateWithTimeIntervalSinceNow:1000];
  133. OCMExpect([self.mockIDController getAuthTokenForcingRefresh:YES])
  134. .andReturn([FBLPromise resolvedWith:installationWithToken]);
  135. XCTestExpectation *tokenExpectation = [self expectationWithDescription:@"AuthTokenSuccess"];
  136. [self.installations
  137. authTokenForcingRefresh:YES
  138. completion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  139. NSError *_Nullable error) {
  140. XCTAssertNil(error);
  141. XCTAssertNotNil(tokenResult);
  142. XCTAssertEqualObjects(tokenResult.authToken,
  143. installationWithToken.authToken.token);
  144. XCTAssertEqualObjects(tokenResult.expirationDate,
  145. installationWithToken.authToken.expirationDate);
  146. [tokenExpectation fulfill];
  147. }];
  148. [self waitForExpectations:@[ tokenExpectation ] timeout:0.5];
  149. OCMVerifyAll(self.mockIDController);
  150. }
  151. - (void)testAuthTokenForcingRefreshError {
  152. FBLPromise *errorPromise = [FBLPromise pendingPromise];
  153. [errorPromise reject:[FIRInstallationsErrorUtil
  154. APIErrorWithHTTPCode:FIRInstallationsHTTPCodesServerInternalError]];
  155. OCMExpect([self.mockIDController getAuthTokenForcingRefresh:YES]).andReturn(errorPromise);
  156. XCTestExpectation *tokenExpectation = [self expectationWithDescription:@"AuthTokenSuccess"];
  157. [self.installations
  158. authTokenForcingRefresh:YES
  159. completion:^(FIRInstallationsAuthTokenResult *_Nullable tokenResult,
  160. NSError *_Nullable error) {
  161. XCTAssertNil(tokenResult);
  162. XCTAssertEqualObjects(error, errorPromise.error);
  163. [tokenExpectation fulfill];
  164. }];
  165. [self waitForExpectations:@[ tokenExpectation ] timeout:0.5];
  166. OCMVerifyAll(self.mockIDController);
  167. }
  168. - (void)testDeleteSuccess {
  169. OCMExpect([self.mockIDController deleteInstallation])
  170. .andReturn([FBLPromise resolvedWith:[NSNull null]]);
  171. XCTestExpectation *deleteExpectation = [self expectationWithDescription:@"DeleteSuccess"];
  172. [self.installations deleteWithCompletion:^(NSError *_Nullable error) {
  173. XCTAssertNil(error);
  174. [deleteExpectation fulfill];
  175. }];
  176. [self waitForExpectations:@[ deleteExpectation ] timeout:0.5];
  177. }
  178. - (void)testDeleteError {
  179. FBLPromise *errorPromise = [FBLPromise pendingPromise];
  180. NSError *APIError =
  181. [FIRInstallationsErrorUtil APIErrorWithHTTPCode:FIRInstallationsHTTPCodesServerInternalError];
  182. [errorPromise reject:APIError];
  183. OCMExpect([self.mockIDController deleteInstallation]).andReturn(errorPromise);
  184. XCTestExpectation *deleteExpectation = [self expectationWithDescription:@"deleteExpectation"];
  185. [self.installations deleteWithCompletion:^(NSError *_Nullable error) {
  186. XCTAssertEqualObjects(error, APIError);
  187. [deleteExpectation fulfill];
  188. }];
  189. [self waitForExpectations:@[ deleteExpectation ] timeout:0.5];
  190. }
  191. #pragma mark - Invalid Firebase configuration
  192. - (void)testInitWhenProjectIDMissingThenNoThrow {
  193. FIROptions *options = [self.appOptions copy];
  194. options.projectID = nil;
  195. XCTAssertNoThrow([self createInstallationsWithAppOptions:options appName:@"missingProjectID"]);
  196. options.projectID = @"";
  197. XCTAssertNoThrow([self createInstallationsWithAppOptions:options appName:@"emptyProjectID"]);
  198. }
  199. - (void)testInitWhenAPIKeyMissingThenThrows {
  200. FIROptions *options = [self.appOptions copy];
  201. options.APIKey = nil;
  202. XCTAssertThrows([self createInstallationsWithAppOptions:options appName:@"missingAPIKey"]);
  203. options.APIKey = @"";
  204. XCTAssertThrows([self createInstallationsWithAppOptions:options appName:@"emptyAPIKey"]);
  205. }
  206. - (void)testInitWhenGoogleAppIDMissingThenThrows {
  207. FIROptions *options = [self.appOptions copy];
  208. options.googleAppID = @"";
  209. XCTAssertThrows([self createInstallationsWithAppOptions:options appName:@"emptyGoogleAppID"]);
  210. }
  211. - (void)testInitWhenGCMSenderIDMissingThenThrows {
  212. FIROptions *options = [self.appOptions copy];
  213. options.GCMSenderID = @"";
  214. XCTAssertNoThrow([self createInstallationsWithAppOptions:options appName:@"emptyGCMSenderID"]);
  215. }
  216. - (void)testInitWhenProjectIDAndGCMSenderIDMissingThenNoThrow {
  217. FIROptions *options = [self.appOptions copy];
  218. options.GCMSenderID = @"";
  219. options.projectID = nil;
  220. XCTAssertThrows([self createInstallationsWithAppOptions:options appName:@"missingProjectID"]);
  221. options.projectID = @"";
  222. XCTAssertThrows([self createInstallationsWithAppOptions:options appName:@"emptyProjectID"]);
  223. }
  224. - (void)testInitWhenAppNameMissingThenThrows {
  225. FIROptions *options = [self.appOptions copy];
  226. XCTAssertThrows([self createInstallationsWithAppOptions:options appName:@""]);
  227. XCTAssertThrows([self createInstallationsWithAppOptions:options appName:nil]);
  228. }
  229. - (void)testInitWhenAppOptionsMissingThenThrows {
  230. XCTAssertThrows([self createInstallationsWithAppOptions:nil appName:@"missingOptions"]);
  231. }
  232. #pragma mark - Helpers
  233. - (FIRInstallations *)createInstallationsWithAppOptions:(FIROptions *)options
  234. appName:(NSString *)appName {
  235. id mockIDController = OCMClassMock([FIRInstallationsIDController class]);
  236. return [[FIRInstallations alloc] initWithAppOptions:options
  237. appName:appName
  238. installationsIDController:mockIDController
  239. prefetchAuthToken:NO];
  240. }
  241. @end