FIRGetRecaptchaConfigRequestTests.m 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright 2022 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <XCTest/XCTest.h>
  17. #import "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
  18. #import "FirebaseAuth/Sources/Backend/RPC/FIRGetRecaptchaConfigRequest.h"
  19. #import "FirebaseAuth/Sources/Backend/RPC/FIRGetRecaptchaConfigResponse.h"
  20. #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  21. /** @var kGetRecaptchaConfigEndPoint
  22. @brief The "getRecaptchaConfig" endpoint.
  23. */
  24. static NSString *const kGetRecaptchaConfigEndPoint = @"recaptchaConfig";
  25. /** @var kTestAPIKey
  26. @brief Fake API key used for testing.
  27. */
  28. static NSString *const kTestAPIKey = @"APIKey";
  29. /** @var kTestFirebaseAppID
  30. @brief Fake Firebase app ID used for testing.
  31. */
  32. static NSString *const kTestFirebaseAppID = @"appID";
  33. /** @var kAPIURLFormat
  34. @brief URL format for server API calls.
  35. */
  36. static NSString *const kAPIURLFormat = @"https://identitytoolkit.googleapis.com/v2/%@?key=%@";
  37. /** @var gAPIHost
  38. @brief Host for server API calls.
  39. */
  40. static NSString *gAPIHost = @"www.googleapis.com";
  41. @interface FIRGetRecaptchaConfigRequestTests : XCTestCase
  42. @end
  43. @implementation FIRGetRecaptchaConfigRequestTests {
  44. /** @var _RPCIssuer
  45. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  46. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  47. */
  48. FIRFakeBackendRPCIssuer *_RPCIssuer;
  49. }
  50. - (void)setUp {
  51. [super setUp];
  52. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  53. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  54. _RPCIssuer = RPCIssuer;
  55. }
  56. - (void)tearDown {
  57. _RPCIssuer = nil;
  58. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  59. [super tearDown];
  60. }
  61. /** @fn testGetRecaptchaConfigRequest
  62. @brief Tests get Recaptcha config request.
  63. */
  64. - (void)testGetRecaptchaConfigRequest {
  65. FIRAuthRequestConfiguration *requestConfiguration =
  66. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey appID:kTestFirebaseAppID];
  67. FIRGetRecaptchaConfigRequest *request =
  68. [[FIRGetRecaptchaConfigRequest alloc] initWithClientType:@"CLIENT_TYPE_IOS"
  69. version:@"RECAPTCHA_ENTERPRISE"
  70. requestConfiguration:requestConfiguration];
  71. [FIRAuthBackend getRecaptchaConfig:request
  72. callback:^(FIRGetRecaptchaConfigResponse *_Nullable response,
  73. NSError *_Nullable error){
  74. }];
  75. XCTAssertFalse([request containsPostBody]);
  76. // Confirm that the quest has no decoded body as it is get request.
  77. XCTAssertNil(_RPCIssuer.decodedRequest);
  78. NSMutableString *URLString =
  79. [NSMutableString stringWithFormat:kAPIURLFormat, kGetRecaptchaConfigEndPoint, kTestAPIKey];
  80. [URLString appendFormat:@"&%@=%@&%@=%@", @"clientType", @"CLIENT_TYPE_IOS", @"version",
  81. @"RECAPTCHA_ENTERPRISE"];
  82. XCTAssertEqualObjects(URLString, [request requestURL].absoluteString);
  83. }
  84. @end