FIRIntegrationTests.m 9.5 KB

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