FIRIntegrationTests.m 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 "FIRAuthInteropFake.h"
  17. #import "Functions/Example/TestUtils/FUNFakeInstanceID.h"
  18. #import "Functions/FirebaseFunctions/FIRFunctions+Internal.h"
  19. #import "Functions/FirebaseFunctions/Public/FIRError.h"
  20. #import "Functions/FirebaseFunctions/Public/FIRFunctions.h"
  21. #import "Functions/FirebaseFunctions/Public/FIRHTTPSCallable.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. }
  29. @end
  30. @implementation FIRIntegrationTests
  31. - (void)setUp {
  32. [super setUp];
  33. _projectID = kDefaultProjectID;
  34. _useLocalhost = YES;
  35. // Check for configuration of a prod project via GoogleServices-Info.plist.
  36. FIROptions *options = [FIROptions defaultOptions];
  37. if (options && ![options.projectID isEqualToString:@"abc-xyz-123"]) {
  38. _projectID = options.projectID;
  39. _useLocalhost = NO;
  40. }
  41. _functions = [[FIRFunctions alloc]
  42. initWithProjectID:_projectID
  43. region:@"us-central1"
  44. auth:[[FIRAuthInteropFake alloc] initWithToken:nil userID:nil error:nil]];
  45. if (_useLocalhost) {
  46. [_functions useLocalhost];
  47. }
  48. }
  49. - (void)tearDown {
  50. [super tearDown];
  51. }
  52. - (void)testData {
  53. NSDictionary *data = @{
  54. @"bool" : @YES,
  55. @"int" : @2,
  56. @"long" : @9876543210L,
  57. @"string" : @"four",
  58. @"array" : @[ @5, @6 ],
  59. @"null" : [NSNull null],
  60. };
  61. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  62. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"dataTest"];
  63. [function callWithObject:data
  64. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  65. XCTAssertNil(error);
  66. XCTAssertEqualObjects(@"stub response", result.data[@"message"]);
  67. XCTAssertEqualObjects(@42, result.data[@"code"]);
  68. XCTAssertEqualObjects(@420L, result.data[@"long"]);
  69. [expectation fulfill];
  70. }];
  71. [self waitForExpectations:@[ expectation ] timeout:10];
  72. }
  73. - (void)testScalar {
  74. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  75. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"scalarTest"];
  76. [function callWithObject:@17
  77. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  78. XCTAssertNil(error);
  79. XCTAssertEqualObjects(@76, result.data);
  80. [expectation fulfill];
  81. }];
  82. [self waitForExpectations:@[ expectation ] timeout:10];
  83. }
  84. - (void)testToken {
  85. // Recreate _functions with a token.
  86. FIRFunctions *functions = [[FIRFunctions alloc]
  87. initWithProjectID:_projectID
  88. region:@"us-central1"
  89. auth:[[FIRAuthInteropFake alloc] initWithToken:@"token" userID:nil error:nil]];
  90. if (_useLocalhost) {
  91. [functions useLocalhost];
  92. }
  93. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  94. FIRHTTPSCallable *function = [functions HTTPSCallableWithName:@"tokenTest"];
  95. [function callWithObject:@{}
  96. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  97. XCTAssertNil(error);
  98. XCTAssertEqualObjects(@{}, result.data);
  99. [expectation fulfill];
  100. }];
  101. [self waitForExpectations:@[ expectation ] timeout:10];
  102. }
  103. - (void)testInstanceID {
  104. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  105. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"instanceIdTest"];
  106. [function callWithObject:@{}
  107. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  108. XCTAssertNil(error);
  109. XCTAssertEqualObjects(@{}, result.data);
  110. [expectation fulfill];
  111. }];
  112. [self waitForExpectations:@[ expectation ] timeout:10];
  113. }
  114. - (void)testNull {
  115. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  116. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"nullTest"];
  117. [function callWithObject:[NSNull null]
  118. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  119. XCTAssertEqualObjects([NSNull null], result.data);
  120. XCTAssertNil(error);
  121. [expectation fulfill];
  122. }];
  123. [self waitForExpectations:@[ expectation ] timeout:10];
  124. // Test the version with no arguments.
  125. expectation = [[XCTestExpectation alloc] init];
  126. [function
  127. callWithCompletion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  128. XCTAssertEqualObjects([NSNull null], result.data);
  129. XCTAssertNil(error);
  130. [expectation fulfill];
  131. }];
  132. [self waitForExpectations:@[ expectation ] timeout:10];
  133. }
  134. - (void)testMissingResult {
  135. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  136. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"missingResultTest"];
  137. [function
  138. callWithCompletion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  139. XCTAssertNotNil(error);
  140. XCTAssertEqual(FIRFunctionsErrorCodeInternal, error.code);
  141. XCTAssertEqualObjects(@"Response is missing data field.", error.localizedDescription);
  142. [expectation fulfill];
  143. }];
  144. [self waitForExpectations:@[ expectation ] timeout:10];
  145. }
  146. - (void)testUnhandledError {
  147. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  148. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"unhandledErrorTest"];
  149. [function callWithObject:@{}
  150. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  151. XCTAssertNotNil(error);
  152. XCTAssertEqual(FIRFunctionsErrorCodeInternal, error.code);
  153. [expectation fulfill];
  154. }];
  155. [self waitForExpectations:@[ expectation ] timeout:10];
  156. }
  157. - (void)testUnknownError {
  158. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  159. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"unknownErrorTest"];
  160. [function callWithObject:@{}
  161. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  162. XCTAssertNotNil(error);
  163. XCTAssertEqual(FIRFunctionsErrorCodeInternal, error.code);
  164. XCTAssertEqualObjects(@"INTERNAL", error.localizedDescription);
  165. [expectation fulfill];
  166. }];
  167. [self waitForExpectations:@[ expectation ] timeout:10];
  168. }
  169. - (void)testExplicitError {
  170. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  171. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"explicitErrorTest"];
  172. [function
  173. callWithObject:@{}
  174. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  175. XCTAssertNotNil(error);
  176. XCTAssertEqual(FIRFunctionsErrorCodeOutOfRange, error.code);
  177. XCTAssertEqualObjects(@"explicit nope", error.userInfo[NSLocalizedDescriptionKey]);
  178. NSDictionary *expectedDetails = @{@"start" : @10, @"end" : @20, @"long" : @30L};
  179. XCTAssertEqualObjects(expectedDetails, error.userInfo[FIRFunctionsErrorDetailsKey]);
  180. [expectation fulfill];
  181. }];
  182. [self waitForExpectations:@[ expectation ] timeout:10];
  183. }
  184. - (void)testHttpError {
  185. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  186. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"httpErrorTest"];
  187. [function callWithObject:@{}
  188. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  189. XCTAssertNotNil(error);
  190. XCTAssertEqual(FIRFunctionsErrorCodeInvalidArgument, error.code);
  191. [expectation fulfill];
  192. }];
  193. [self waitForExpectations:@[ expectation ] timeout:10];
  194. }
  195. - (void)testTimeout {
  196. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  197. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"timeoutTest"];
  198. function.timeoutInterval = 0.05;
  199. [function
  200. callWithCompletion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  201. XCTAssertNotNil(error);
  202. XCTAssertEqual(FIRFunctionsErrorCodeDeadlineExceeded, error.code);
  203. XCTAssertEqualObjects(@"DEADLINE EXCEEDED", error.userInfo[NSLocalizedDescriptionKey]);
  204. XCTAssertNil(error.userInfo[FIRFunctionsErrorDetailsKey]);
  205. [expectation fulfill];
  206. }];
  207. [self waitForExpectations:@[ expectation ] timeout:10];
  208. }
  209. @end