FIRIntegrationTests.m 9.5 KB

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