FIRFunctionsTests.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "FirebaseFunctions/Sources/FIRFunctions+Internal.h"
  16. #import "FirebaseFunctions/Sources/Public/FirebaseFunctions/FIRFunctions.h"
  17. #import "SharedTestUtilities/AppCheckFake/FIRAppCheckFake.h"
  18. #import "SharedTestUtilities/AppCheckFake/FIRAppCheckTokenResultFake.h"
  19. #import <FirebaseCore/FirebaseCore.h>
  20. #if SWIFT_PACKAGE
  21. @import GTMSessionFetcherCore;
  22. #else
  23. #import <GTMSessionFetcher/GTMSessionFetcherService.h>
  24. #endif
  25. @interface FIRFunctions (Test)
  26. @property(nonatomic, readonly) NSString *emulatorOrigin;
  27. - (instancetype)initWithProjectID:(NSString *)projectID
  28. region:(NSString *)region
  29. customDomain:(nullable NSString *)customDomain
  30. auth:(nullable id<FIRAuthInterop>)auth
  31. messaging:(nullable id<FIRMessagingInterop>)messaging
  32. appCheck:(nullable id<FIRAppCheckInterop>)appCheck
  33. fetcherService:(GTMSessionFetcherService *)fetcherService;
  34. @end
  35. @interface FIRFunctionsTests : XCTestCase
  36. @end
  37. @implementation FIRFunctionsTests {
  38. FIRFunctions *_functions;
  39. FIRFunctions *_functionsCustomDomain;
  40. GTMSessionFetcherService *_fetcherService;
  41. FIRAppCheckFake *_appCheckFake;
  42. }
  43. - (void)setUp {
  44. [super setUp];
  45. _fetcherService = [[GTMSessionFetcherService alloc] init];
  46. _appCheckFake = [[FIRAppCheckFake alloc] init];
  47. _functions = [[FIRFunctions alloc] initWithProjectID:@"my-project"
  48. region:@"my-region"
  49. customDomain:nil
  50. auth:nil
  51. messaging:nil
  52. appCheck:_appCheckFake
  53. fetcherService:_fetcherService];
  54. _functionsCustomDomain = [[FIRFunctions alloc] initWithProjectID:@"my-project"
  55. region:@"my-region"
  56. customDomain:@"https://mydomain.com"
  57. auth:nil
  58. messaging:nil
  59. appCheck:nil
  60. fetcherService:_fetcherService];
  61. }
  62. - (void)tearDown {
  63. _functionsCustomDomain = nil;
  64. _functions = nil;
  65. _fetcherService = nil;
  66. [super tearDown];
  67. }
  68. - (void)testFunctionsInstanceIsStablePerApp {
  69. FIROptions *options =
  70. [[FIROptions alloc] initWithGoogleAppID:@"0:0000000000000:ios:0000000000000000"
  71. GCMSenderID:@"00000000000000000-00000000000-000000000"];
  72. [FIRApp configureWithOptions:options];
  73. FIRFunctions *functions1 = [FIRFunctions functions];
  74. FIRFunctions *functions2 = [FIRFunctions functionsForApp:[FIRApp defaultApp]];
  75. XCTAssertEqualObjects(functions1, functions2);
  76. [FIRApp configureWithName:@"test" options:options];
  77. FIRApp *app2 = [FIRApp appNamed:@"test"];
  78. functions2 = [FIRFunctions functionsForApp:app2 region:@"us-central2"];
  79. XCTAssertNotEqualObjects(functions1, functions2);
  80. functions1 = [FIRFunctions functionsForApp:app2 region:@"us-central2"];
  81. XCTAssertEqualObjects(functions1, functions2);
  82. functions1 = [FIRFunctions functionsForCustomDomain:@"test_domain"];
  83. functions2 = [FIRFunctions functionsForRegion:@"us-central1"];
  84. XCTAssertNotEqualObjects(functions1, functions2);
  85. functions2 = [FIRFunctions functionsForApp:[FIRApp defaultApp] customDomain:@"test_domain"];
  86. XCTAssertEqualObjects(functions1, functions2);
  87. }
  88. - (void)testURLWithName {
  89. NSString *url = [_functions URLWithName:@"my-endpoint"];
  90. XCTAssertEqualObjects(@"https://my-region-my-project.cloudfunctions.net/my-endpoint", url);
  91. }
  92. - (void)testRegionWithEmulator {
  93. [_functionsCustomDomain useEmulatorWithHost:@"localhost" port:5005];
  94. NSLog(@"%@", _functionsCustomDomain.emulatorOrigin);
  95. NSString *url = [_functionsCustomDomain URLWithName:@"my-endpoint"];
  96. XCTAssertEqualObjects(@"http://localhost:5005/my-project/my-region/my-endpoint", url);
  97. }
  98. - (void)testRegionWithEmulatorWithScheme {
  99. [_functionsCustomDomain useEmulatorWithHost:@"http://localhost" port:5005];
  100. NSLog(@"%@", _functionsCustomDomain.emulatorOrigin);
  101. NSString *url = [_functionsCustomDomain URLWithName:@"my-endpoint"];
  102. XCTAssertEqualObjects(@"http://localhost:5005/my-project/my-region/my-endpoint", url);
  103. }
  104. - (void)testCustomDomain {
  105. NSString *url = [_functionsCustomDomain URLWithName:@"my-endpoint"];
  106. XCTAssertEqualObjects(@"https://mydomain.com/my-endpoint", url);
  107. }
  108. - (void)testCustomDomainWithEmulator {
  109. [_functionsCustomDomain useEmulatorWithHost:@"localhost" port:5005];
  110. NSString *url = [_functionsCustomDomain URLWithName:@"my-endpoint"];
  111. XCTAssertEqualObjects(@"http://localhost:5005/my-project/my-region/my-endpoint", url);
  112. }
  113. - (void)testSetEmulatorSettings {
  114. [_functions useEmulatorWithHost:@"localhost" port:1000];
  115. XCTAssertEqualObjects(@"http://localhost:1000", _functions.emulatorOrigin);
  116. }
  117. #pragma mark - App Check integration
  118. - (void)testCallFunctionWhenAppCheckIsInstalledAndFACTokenSuccess {
  119. _appCheckFake.tokenResult = [[FIRAppCheckTokenResultFake alloc] initWithToken:@"valid_token"
  120. error:nil];
  121. NSError *networkError = [NSError errorWithDomain:@"testCallFunctionWhenAppCheckIsInstalled"
  122. code:-1
  123. userInfo:nil];
  124. XCTestExpectation *httpRequestExpectation =
  125. [self expectationWithDescription:@"HTTPRequestExpectation"];
  126. __weak __auto_type weakSelf = self;
  127. _fetcherService.testBlock = ^(GTMSessionFetcher *_Nonnull fetcherToTest,
  128. GTMSessionFetcherTestResponse _Nonnull testResponse) {
  129. // __unused to avoid warning in Xcode 12+ in g3.
  130. __unused __auto_type self = weakSelf;
  131. [httpRequestExpectation fulfill];
  132. NSString *appCheckTokenHeader =
  133. [fetcherToTest.request valueForHTTPHeaderField:@"X-Firebase-AppCheck"];
  134. XCTAssertEqualObjects(appCheckTokenHeader, @"valid_token");
  135. testResponse(nil, nil, networkError);
  136. };
  137. XCTestExpectation *completionExpectation =
  138. [self expectationWithDescription:@"completionExpectation"];
  139. [_functions callFunction:@"fake_func"
  140. withObject:nil
  141. timeout:10
  142. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  143. XCTAssertEqualObjects(error, networkError);
  144. [completionExpectation fulfill];
  145. }];
  146. [self waitForExpectations:@[ httpRequestExpectation, completionExpectation ] timeout:1.5];
  147. }
  148. - (void)testCallFunctionWhenAppCheckIsInstalledAndFACTokenError {
  149. NSError *appCheckError = [NSError errorWithDomain:self.name code:-1 userInfo:nil];
  150. _appCheckFake.tokenResult = [[FIRAppCheckTokenResultFake alloc] initWithToken:@"dummy_token"
  151. error:appCheckError];
  152. NSError *networkError = [NSError errorWithDomain:self.name code:-2 userInfo:nil];
  153. XCTestExpectation *httpRequestExpectation =
  154. [self expectationWithDescription:@"HTTPRequestExpectation"];
  155. __weak __auto_type weakSelf = self;
  156. _fetcherService.testBlock = ^(GTMSessionFetcher *_Nonnull fetcherToTest,
  157. GTMSessionFetcherTestResponse _Nonnull testResponse) {
  158. // __unused to avoid warning in Xcode 12+ in g3.
  159. __unused __auto_type self = weakSelf;
  160. [httpRequestExpectation fulfill];
  161. NSString *appCheckTokenHeader =
  162. [fetcherToTest.request valueForHTTPHeaderField:@"X-Firebase-AppCheck"];
  163. XCTAssertNil(appCheckTokenHeader);
  164. testResponse(nil, nil, networkError);
  165. };
  166. XCTestExpectation *completionExpectation =
  167. [self expectationWithDescription:@"completionExpectation"];
  168. [_functions callFunction:@"fake_func"
  169. withObject:nil
  170. timeout:10
  171. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  172. XCTAssertEqualObjects(error, networkError);
  173. [completionExpectation fulfill];
  174. }];
  175. [self waitForExpectations:@[ httpRequestExpectation, completionExpectation ] timeout:1.5];
  176. }
  177. - (void)testCallFunctionWhenAppCheckIsNotInstalled {
  178. NSError *networkError = [NSError errorWithDomain:@"testCallFunctionWhenAppCheckIsInstalled"
  179. code:-1
  180. userInfo:nil];
  181. XCTestExpectation *httpRequestExpectation =
  182. [self expectationWithDescription:@"HTTPRequestExpectation"];
  183. __weak __auto_type weakSelf = self;
  184. _fetcherService.testBlock = ^(GTMSessionFetcher *_Nonnull fetcherToTest,
  185. GTMSessionFetcherTestResponse _Nonnull testResponse) {
  186. // __unused to avoid warning in Xcode 12+ in g3.
  187. __unused __auto_type self = weakSelf;
  188. [httpRequestExpectation fulfill];
  189. NSString *appCheckTokenHeader =
  190. [fetcherToTest.request valueForHTTPHeaderField:@"X-Firebase-AppCheck"];
  191. XCTAssertNil(appCheckTokenHeader);
  192. testResponse(nil, nil, networkError);
  193. };
  194. XCTestExpectation *completionExpectation =
  195. [self expectationWithDescription:@"completionExpectation"];
  196. [_functionsCustomDomain
  197. callFunction:@"fake_func"
  198. withObject:nil
  199. timeout:10
  200. completion:^(FIRHTTPSCallableResult *_Nullable result, NSError *_Nullable error) {
  201. XCTAssertEqualObjects(error, networkError);
  202. [completionExpectation fulfill];
  203. }];
  204. [self waitForExpectations:@[ httpRequestExpectation, completionExpectation ] timeout:1.5];
  205. }
  206. @end