FIRIntegrationTests.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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/Extension/FirebaseCoreInternal.h"
  16. #import "FirebaseFunctions/Tests/ObjCIntegration/FIRFunctions+Internal.h"
  17. @import FirebaseFunctions;
  18. @import FirebaseAuthInterop;
  19. @import FirebaseMessagingInterop;
  20. @import GTMSessionFetcherCore;
  21. // Project ID used by these tests.
  22. static NSString *const kDefaultProjectID = @"functions-integration-test";
  23. @interface MessagingTokenProvider : NSObject <FIRMessagingInterop>
  24. @end
  25. @implementation MessagingTokenProvider
  26. @synthesize FCMToken;
  27. - (instancetype)init {
  28. if (self = [super init]) {
  29. FCMToken = @"fakeFCMToken";
  30. }
  31. return self;
  32. }
  33. @end
  34. @interface AuthTokenProvider : NSObject <FIRAuthInterop>
  35. @property NSString *token;
  36. @end
  37. @implementation AuthTokenProvider
  38. - (instancetype)initWithToken:(NSString *)token {
  39. if (self = [super init]) {
  40. _token = token;
  41. }
  42. return self;
  43. }
  44. - (void)getTokenForcingRefresh:(BOOL)forceRefresh withCallback:(nonnull FIRTokenCallback)callback {
  45. callback(_token, nil);
  46. }
  47. - (nullable NSString *)getUserID {
  48. return @"dummy user id";
  49. }
  50. @end
  51. @interface FIRIntegrationTests : XCTestCase {
  52. FIRFunctions *_functions;
  53. NSString *_projectID;
  54. BOOL _useLocalhost;
  55. MessagingTokenProvider *_messagingFake;
  56. }
  57. @end
  58. @implementation FIRIntegrationTests
  59. - (void)setUp {
  60. [super setUp];
  61. _messagingFake = [[MessagingTokenProvider alloc] init];
  62. _projectID = kDefaultProjectID;
  63. _useLocalhost = YES;
  64. // Check for configuration of a prod project via GoogleServices-Info.plist.
  65. FIROptions *options = [FIROptions defaultOptions];
  66. if (options && ![options.projectID isEqualToString:@"abc-xyz-123"]) {
  67. _projectID = options.projectID;
  68. _useLocalhost = NO;
  69. }
  70. _functions = [[FIRFunctions alloc] initWithProjectID:_projectID
  71. region:@"us-central1"
  72. customDomain:nil
  73. auth:nil
  74. messaging:_messagingFake
  75. appCheck:nil
  76. fetcherService:[[GTMSessionFetcherService alloc] init]];
  77. if (_useLocalhost) {
  78. [_functions useEmulatorWithHost:@"localhost" port:5005];
  79. }
  80. }
  81. - (void)tearDown {
  82. [super tearDown];
  83. }
  84. - (void)testData {
  85. NSDictionary *data = @{
  86. @"bool" : @YES,
  87. @"int" : @2,
  88. @"long" : @9876543210L,
  89. @"string" : @"four",
  90. @"array" : @[ @5, @6 ],
  91. @"null" : [NSNull null],
  92. };
  93. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  94. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"dataTest"];
  95. [function callWithObject:data
  96. completion:^(FIRHTTPSCallableResult *result, NSError *_Nullable error) {
  97. XCTAssertNil(error);
  98. XCTAssertEqualObjects(@"stub response", result.data[@"message"]);
  99. XCTAssertEqualObjects(@42, result.data[@"code"]);
  100. XCTAssertEqualObjects(@420L, result.data[@"long"]);
  101. [expectation fulfill];
  102. }];
  103. [self waitForExpectations:@[ expectation ] timeout:10];
  104. }
  105. - (void)testScalar {
  106. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  107. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"scalarTest"];
  108. [function callWithObject:@17
  109. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  110. XCTAssertNil(error);
  111. XCTAssertEqualObjects(@76, result.data);
  112. [expectation fulfill];
  113. }];
  114. [self waitForExpectations:@[ expectation ] timeout:10];
  115. }
  116. - (void)testToken {
  117. // Recreate _functions with a token.
  118. FIRFunctions *functions =
  119. [[FIRFunctions alloc] initWithProjectID:_projectID
  120. region:@"us-central1"
  121. customDomain:nil
  122. auth:[[AuthTokenProvider alloc] initWithToken:@"token"]
  123. messaging:_messagingFake
  124. appCheck:nil
  125. fetcherService:[[GTMSessionFetcherService alloc] init]];
  126. if (_useLocalhost) {
  127. [functions useEmulatorWithHost:@"localhost" port:5005];
  128. }
  129. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  130. FIRHTTPSCallable *function = [functions HTTPSCallableWithName:@"tokenTest"];
  131. [function callWithObject:@{}
  132. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  133. XCTAssertNil(error);
  134. XCTAssertEqualObjects(@{}, result.data);
  135. [expectation fulfill];
  136. }];
  137. [self waitForExpectations:@[ expectation ] timeout:10];
  138. }
  139. - (void)testFCMToken {
  140. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  141. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"FCMTokenTest"];
  142. [function callWithObject:@{}
  143. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  144. XCTAssertNil(error);
  145. XCTAssertEqualObjects(@{}, result.data);
  146. [expectation fulfill];
  147. }];
  148. [self waitForExpectations:@[ expectation ] timeout:10];
  149. }
  150. - (void)testNull {
  151. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  152. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"nullTest"];
  153. [function callWithObject:[NSNull null]
  154. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  155. XCTAssertEqualObjects([NSNull null], result.data);
  156. XCTAssertNil(error);
  157. [expectation fulfill];
  158. }];
  159. [self waitForExpectations:@[ expectation ] timeout:10];
  160. // Test the version with no arguments.
  161. expectation = [[XCTestExpectation alloc] init];
  162. [function
  163. callWithCompletion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  164. XCTAssertEqualObjects([NSNull null], result.data);
  165. XCTAssertNil(error);
  166. [expectation fulfill];
  167. }];
  168. [self waitForExpectations:@[ expectation ] timeout:10];
  169. }
  170. - (void)testMissingResult {
  171. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  172. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"missingResultTest"];
  173. [function
  174. callWithCompletion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  175. XCTAssertNotNil(error);
  176. XCTAssertEqual(FIRFunctionsErrorCodeInternal, error.code);
  177. XCTAssertEqualObjects(@"Response is missing data field.", error.localizedDescription);
  178. [expectation fulfill];
  179. }];
  180. [self waitForExpectations:@[ expectation ] timeout:10];
  181. }
  182. - (void)testUnhandledError {
  183. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  184. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"unhandledErrorTest"];
  185. [function callWithObject:@{}
  186. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  187. XCTAssertNotNil(error);
  188. XCTAssertEqual(FIRFunctionsErrorCodeInternal, error.code);
  189. [expectation fulfill];
  190. }];
  191. [self waitForExpectations:@[ expectation ] timeout:10];
  192. }
  193. - (void)testUnknownError {
  194. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  195. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"unknownErrorTest"];
  196. [function callWithObject:@{}
  197. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  198. XCTAssertNotNil(error);
  199. XCTAssertEqual(FIRFunctionsErrorCodeInternal, error.code);
  200. XCTAssertEqualObjects(@"INTERNAL", error.localizedDescription);
  201. [expectation fulfill];
  202. }];
  203. [self waitForExpectations:@[ expectation ] timeout:10];
  204. }
  205. - (void)testExplicitError {
  206. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  207. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"explicitErrorTest"];
  208. [function
  209. callWithObject:@{}
  210. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  211. XCTAssertNotNil(error);
  212. XCTAssertEqual(FIRFunctionsErrorCodeOutOfRange, error.code);
  213. XCTAssertEqualObjects(@"explicit nope", error.userInfo[NSLocalizedDescriptionKey]);
  214. NSDictionary *expectedDetails = @{@"start" : @10, @"end" : @20, @"long" : @30L};
  215. XCTAssertEqualObjects(expectedDetails, error.userInfo[@"details"]);
  216. [expectation fulfill];
  217. }];
  218. [self waitForExpectations:@[ expectation ] timeout:10];
  219. }
  220. - (void)testHttpError {
  221. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  222. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"httpErrorTest"];
  223. [function callWithObject:@{}
  224. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  225. XCTAssertNotNil(error);
  226. XCTAssertEqual(FIRFunctionsErrorCodeInvalidArgument, error.code);
  227. XCTAssertEqualObjects(error.localizedDescription, @"INVALID ARGUMENT");
  228. [expectation fulfill];
  229. }];
  230. [self waitForExpectations:@[ expectation ] timeout:10];
  231. }
  232. // Regression test for https://github.com/firebase/firebase-ios-sdk/issues/9855
  233. - (void)testThrowTest {
  234. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  235. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"throwTest"];
  236. [function callWithObject:@{}
  237. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  238. XCTAssertNotNil(error);
  239. XCTAssertEqual(FIRFunctionsErrorCodeInvalidArgument, error.code);
  240. XCTAssertEqualObjects(error.localizedDescription, @"Invalid test requested.");
  241. [expectation fulfill];
  242. }];
  243. [self waitForExpectations:@[ expectation ] timeout:10];
  244. }
  245. - (void)testTimeout {
  246. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  247. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"timeoutTest"];
  248. function.timeoutInterval = 0.05;
  249. [function
  250. callWithCompletion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  251. XCTAssertNotNil(error);
  252. XCTAssertEqual(FIRFunctionsErrorCodeDeadlineExceeded, error.code);
  253. XCTAssertEqualObjects(@"DEADLINE EXCEEDED", error.userInfo[NSLocalizedDescriptionKey]);
  254. XCTAssertNil(error.userInfo[@"details"]);
  255. [expectation fulfill];
  256. }];
  257. [self waitForExpectations:@[ expectation ] timeout:10];
  258. }
  259. - (void)testFunctionsReturnsOnMainThread {
  260. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  261. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"scalarTest"];
  262. [function callWithObject:@17
  263. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  264. XCTAssert(NSThread.isMainThread);
  265. XCTAssertNotNil(result);
  266. [expectation fulfill];
  267. }];
  268. [self waitForExpectations:@[ expectation ] timeout:10];
  269. }
  270. - (void)testFunctionErrorsOnMainThread {
  271. XCTestExpectation *expectation = [[XCTestExpectation alloc] init];
  272. FIRHTTPSCallable *function = [_functions HTTPSCallableWithName:@"httpErrorTest"];
  273. [function callWithObject:@{}
  274. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  275. XCTAssert(NSThread.isMainThread);
  276. XCTAssertNotNil(error);
  277. [expectation fulfill];
  278. }];
  279. [self waitForExpectations:@[ expectation ] timeout:10];
  280. }
  281. @end