FIRIntegrationTests.m 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Copyright 2017 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <XCTest/XCTest.h>
  15. #import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
  16. #import "FirebaseFunctions/Sources/FIRFunctions+Internal.h"
  17. #import "FirebaseFunctions/Sources/Public/FirebaseFunctions/FIRError.h"
  18. #import "FirebaseFunctions/Sources/Public/FirebaseFunctions/FIRFunctions.h"
  19. #import "FirebaseFunctions/Sources/Public/FirebaseFunctions/FIRHTTPSCallable.h"
  20. #import "SharedTestUtilities/FIRAuthInteropFake.h"
  21. #import "SharedTestUtilities/FIRMessagingInteropFake.h"
  22. // Project ID used by these tests.
  23. static NSString *const kDefaultProjectID = @"functions-integration-test";
  24. @interface FIRIntegrationTests : XCTestCase {
  25. FIRFunctions *_functions;
  26. NSString *_projectID;
  27. BOOL _useLocalhost;
  28. FIRMessagingInteropFake *_messagingFake;
  29. }
  30. @end
  31. @implementation FIRIntegrationTests
  32. - (void)setUp {
  33. [super setUp];
  34. _messagingFake = [[FIRMessagingInteropFake alloc] init];
  35. _projectID = kDefaultProjectID;
  36. _useLocalhost = YES;
  37. // Check for configuration of a prod project via GoogleServices-Info.plist.
  38. FIROptions *options = [FIROptions defaultOptions];
  39. if (options && ![options.projectID isEqualToString:@"abc-xyz-123"]) {
  40. _projectID = options.projectID;
  41. _useLocalhost = NO;
  42. }
  43. _functions = [[FIRFunctions alloc]
  44. initWithProjectID:_projectID
  45. region:@"us-central1"
  46. customDomain:nil
  47. auth:[[FIRAuthInteropFake alloc] initWithToken:nil userID:nil error:nil]
  48. messaging:_messagingFake
  49. appCheck:nil];
  50. if (_useLocalhost) {
  51. [_functions useLocalhost];
  52. }
  53. }
  54. - (void)tearDown {
  55. [super tearDown];
  56. }
  57. - (void)testData {
  58. NSDictionary *data = @{
  59. @"bool" : @YES,
  60. @"int" : @2,
  61. @"long" : @9876543210L,
  62. @"string" : @"four",
  63. @"array" : @[ @5, @6 ],
  64. @"null" : [NSNull null],
  65. };
  66. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  67. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"dataTest"];
  68. [function callWithObject:data
  69. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  70. XCTAssertNil(error);
  71. XCTAssertEqualObjects(@"stub response", result.data[@"message"]);
  72. XCTAssertEqualObjects(@42, result.data[@"code"]);
  73. XCTAssertEqualObjects(@420L, result.data[@"long"]);
  74. [expectation fulfill];
  75. }];
  76. [self waitForExpectations:@[ expectation ] timeout:10];
  77. }
  78. - (void)testScalar {
  79. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  80. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"scalarTest"];
  81. [function callWithObject:@17
  82. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  83. XCTAssertNil(error);
  84. XCTAssertEqualObjects(@76, result.data);
  85. [expectation fulfill];
  86. }];
  87. [self waitForExpectations:@[ expectation ] timeout:10];
  88. }
  89. - (void)testToken {
  90. // Recreate _functions with a token.
  91. FIRFunctions *functions = [[FIRFunctions alloc]
  92. initWithProjectID:_projectID
  93. region:@"us-central1"
  94. customDomain:nil
  95. auth:[[FIRAuthInteropFake alloc] initWithToken:@"token" userID:nil error:nil]
  96. messaging:_messagingFake
  97. appCheck:nil];
  98. if (_useLocalhost) {
  99. [functions useLocalhost];
  100. }
  101. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  102. FIRHTTPSCallable *function = [functions HTTPSCallableWithName:@"tokenTest"];
  103. [function callWithObject:@{}
  104. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  105. XCTAssertNil(error);
  106. XCTAssertEqualObjects(@{}, result.data);
  107. [expectation fulfill];
  108. }];
  109. [self waitForExpectations:@[ expectation ] timeout:10];
  110. }
  111. - (void)testFCMToken {
  112. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  113. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"FCMTokenTest"];
  114. [function callWithObject:@{}
  115. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  116. XCTAssertNil(error);
  117. XCTAssertEqualObjects(@{}, result.data);
  118. [expectation fulfill];
  119. }];
  120. [self waitForExpectations:@[ expectation ] timeout:10];
  121. }
  122. - (void)testNull {
  123. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  124. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"nullTest"];
  125. [function callWithObject:[NSNull null]
  126. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  127. XCTAssertEqualObjects([NSNull null], result.data);
  128. XCTAssertNil(error);
  129. [expectation fulfill];
  130. }];
  131. [self waitForExpectations:@[ expectation ] timeout:10];
  132. // Test the version with no arguments.
  133. expectation = [[XCTestExpectation alloc] init];
  134. [function
  135. callWithCompletion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  136. XCTAssertEqualObjects([NSNull null], result.data);
  137. XCTAssertNil(error);
  138. [expectation fulfill];
  139. }];
  140. [self waitForExpectations:@[ expectation ] timeout:10];
  141. }
  142. - (void)testMissingResult {
  143. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  144. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"missingResultTest"];
  145. [function
  146. callWithCompletion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  147. XCTAssertNotNil(error);
  148. XCTAssertEqual(FIRFunctionsErrorCodeInternal, error.code);
  149. XCTAssertEqualObjects(@"Response is missing data field.", error.localizedDescription);
  150. [expectation fulfill];
  151. }];
  152. [self waitForExpectations:@[ expectation ] timeout:10];
  153. }
  154. - (void)testUnhandledError {
  155. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  156. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"unhandledErrorTest"];
  157. [function callWithObject:@{}
  158. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  159. XCTAssertNotNil(error);
  160. XCTAssertEqual(FIRFunctionsErrorCodeInternal, error.code);
  161. [expectation fulfill];
  162. }];
  163. [self waitForExpectations:@[ expectation ] timeout:10];
  164. }
  165. - (void)testUnknownError {
  166. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  167. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"unknownErrorTest"];
  168. [function callWithObject:@{}
  169. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  170. XCTAssertNotNil(error);
  171. XCTAssertEqual(FIRFunctionsErrorCodeInternal, error.code);
  172. XCTAssertEqualObjects(@"INTERNAL", error.localizedDescription);
  173. [expectation fulfill];
  174. }];
  175. [self waitForExpectations:@[ expectation ] timeout:10];
  176. }
  177. - (void)testExplicitError {
  178. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  179. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"explicitErrorTest"];
  180. [function
  181. callWithObject:@{}
  182. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  183. XCTAssertNotNil(error);
  184. XCTAssertEqual(FIRFunctionsErrorCodeOutOfRange, error.code);
  185. XCTAssertEqualObjects(@"explicit nope", error.userInfo[NSLocalizedDescriptionKey]);
  186. NSDictionary *expectedDetails = @{@"start" : @10, @"end" : @20, @"long" : @30L};
  187. XCTAssertEqualObjects(expectedDetails, error.userInfo[FIRFunctionsErrorDetailsKey]);
  188. [expectation fulfill];
  189. }];
  190. [self waitForExpectations:@[ expectation ] timeout:10];
  191. }
  192. - (void)testHttpError {
  193. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  194. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"httpErrorTest"];
  195. [function callWithObject:@{}
  196. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  197. XCTAssertNotNil(error);
  198. XCTAssertEqual(FIRFunctionsErrorCodeInvalidArgument, error.code);
  199. [expectation fulfill];
  200. }];
  201. [self waitForExpectations:@[ expectation ] timeout:10];
  202. }
  203. - (void)testTimeout {
  204. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  205. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"timeoutTest"];
  206. function.timeoutInterval = 0.05;
  207. [function
  208. callWithCompletion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  209. XCTAssertNotNil(error);
  210. XCTAssertEqual(FIRFunctionsErrorCodeDeadlineExceeded, error.code);
  211. XCTAssertEqualObjects(@"DEADLINE EXCEEDED", error.userInfo[NSLocalizedDescriptionKey]);
  212. XCTAssertNil(error.userInfo[FIRFunctionsErrorDetailsKey]);
  213. [expectation fulfill];
  214. }];
  215. [self waitForExpectations:@[ expectation ] timeout:10];
  216. }
  217. @end