FIRGetRecaptchaConfigResponseTests.m 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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] initWithRequestConfiguration:requestConfiguration];
  63. __block BOOL callbackInvoked;
  64. __block FIRGetRecaptchaConfigResponse *RPCResponse;
  65. __block NSError *RPCError;
  66. [FIRAuthBackend getRecaptchaConfig:request
  67. callback:^(FIRGetRecaptchaConfigResponse *_Nullable response,
  68. NSError *_Nullable error) {
  69. callbackInvoked = YES;
  70. RPCResponse = response;
  71. RPCError = error;
  72. }];
  73. [_RPCIssuer respondWithJSON:@{
  74. @"recaptchaKey" : kTestRecaptchaKey,
  75. }];
  76. XCTAssert(callbackInvoked);
  77. XCTAssertNil(RPCError);
  78. XCTAssertEqualObjects(kTestRecaptchaKey, RPCResponse.recaptchaKey);
  79. }
  80. @end