FIRGetRecaptchaConfigResponseTests.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/Public/FirebaseAuth/FIRAuthErrors.h"
  18. #import "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
  19. #import "FirebaseAuth/Sources/Backend/RPC/FIRGetRecaptchaConfigRequest.h"
  20. #import "FirebaseAuth/Sources/Backend/RPC/FIRGetRecaptchaConfigResponse.h"
  21. #import "FirebaseAuth/Sources/Utilities/FIRAuthInternalErrors.h"
  22. #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  23. /** @var kTestAPIKey
  24. @brief Fake API key used for testing.
  25. */
  26. static NSString *const kTestAPIKey = @"APIKey";
  27. /** @var kTestFirebaseAppID
  28. @brief Fake Firebase app ID used for testing.
  29. */
  30. static NSString *const kTestFirebaseAppID = @"appID";
  31. /** @var kTestRecaptchaID
  32. @brief Fake Recaptcha ID used for testing.
  33. */
  34. static NSString *const kTestRecaptchaKey = @"projects/123/keys/456";
  35. @interface FIRGetRecaptchaConfigResponseTests : XCTestCase
  36. @end
  37. @implementation FIRGetRecaptchaConfigResponseTests {
  38. /** @var _RPCIssuer
  39. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  40. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  41. */
  42. FIRFakeBackendRPCIssuer *_RPCIssuer;
  43. }
  44. - (void)setUp {
  45. [super setUp];
  46. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  47. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  48. _RPCIssuer = RPCIssuer;
  49. }
  50. - (void)tearDown {
  51. _RPCIssuer = nil;
  52. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  53. [super tearDown];
  54. }
  55. /** @fn testSuccessFulGetRecaptchaConfigRequest
  56. @brief This test simulates a successful @c getRecaptchaConfig Flow.
  57. */
  58. - (void)testSuccessFulGetRecaptchaConfigRequest {
  59. FIRAuthRequestConfiguration *requestConfiguration =
  60. [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey appID:kTestFirebaseAppID];
  61. FIRGetRecaptchaConfigRequest *request =
  62. [[FIRGetRecaptchaConfigRequest alloc] initWithClientType:@"CLIENT_TYPE_IOS"
  63. version:@"RECAPTCHA_ENTERPRISE"
  64. requestConfiguration:requestConfiguration];
  65. __block BOOL callbackInvoked;
  66. __block FIRGetRecaptchaConfigResponse *RPCResponse;
  67. __block NSError *RPCError;
  68. [FIRAuthBackend getRecaptchaConfig:request
  69. callback:^(FIRGetRecaptchaConfigResponse *_Nullable response,
  70. NSError *_Nullable error) {
  71. callbackInvoked = YES;
  72. RPCResponse = response;
  73. RPCError = error;
  74. }];
  75. [_RPCIssuer respondWithJSON:@{
  76. @"recaptchaKey" : kTestRecaptchaKey,
  77. }];
  78. XCTAssert(callbackInvoked);
  79. XCTAssertNil(RPCError);
  80. XCTAssertEqualObjects(kTestRecaptchaKey, RPCResponse.recaptchaKey);
  81. }
  82. @end