FIRStartMFAEnrollmentRequestTests.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright 2023 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 <TargetConditionals.h>
  17. #if TARGET_OS_IOS
  18. #import <XCTest/XCTest.h>
  19. #import "FirebaseAuth/Sources/Backend/FIRAuthBackend+MultiFactor.h"
  20. #import "FirebaseAuth/Sources/Backend/RPC/MultiFactor/Enroll/FIRStartMFAEnrollmentRequest.h"
  21. #import "FirebaseAuth/Sources/Backend/RPC/MultiFactor/Enroll/FIRStartMFAEnrollmentResponse.h"
  22. #import "FirebaseAuth/Sources/Backend/RPC/Proto/TOTP/FIRAuthProtoStartMFATOTPEnrollmentRequestInfo.h"
  23. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuthErrors.h"
  24. #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  25. /**
  26. @var kTestAPIKey
  27. @brief Fake API key used for testing.
  28. */
  29. static NSString *const kTestAPIKey = @"APIKey";
  30. /**
  31. @var kTestFirebaseAppID
  32. @brief Fake Firebase app ID used for testing.
  33. */
  34. static NSString *const kTestFirebaseAppID = @"appID";
  35. /**
  36. @var kExpectedAPIURL
  37. @brief The expected URL for the test calls.
  38. */
  39. static NSString *const kExpectedAPIURL =
  40. @"https://identitytoolkit.googleapis.com/v2/accounts/mfaEnrollment:start?key=APIKey";
  41. /**
  42. @var kIDToken
  43. @brief Token representing the user's identity.
  44. */
  45. static NSString *const kIDToken = @"idToken";
  46. /**
  47. @var kTOTPSessionInfo
  48. @brief Information about the TOTP (Time-Based One-Time Password) Enrollment.
  49. */
  50. static NSString *const kTOTPEnrollmentInfo = @"totpEnrollmentInfo";
  51. /**
  52. @var kPhoneSessionInfo
  53. @brief Information about the Phone Enrollment.
  54. */
  55. static NSString *const kPhoneEnrollmentInfo = @"enrollmentInfo";
  56. /**
  57. @class FIRStartMFAEnrollmentRequestTests
  58. @brief Tests for @c FIRStartMFAEnrollmentRequest.
  59. */
  60. @interface FIRStartMFAEnrollmentRequestTests : XCTestCase
  61. @end
  62. @implementation FIRStartMFAEnrollmentRequestTests {
  63. /**
  64. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  65. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  66. */
  67. FIRFakeBackendRPCIssuer *_RPCIssuer;
  68. /**
  69. @brief This is the request configuration used for testing.
  70. */
  71. FIRAuthRequestConfiguration *_requestConfiguration;
  72. }
  73. - (void)setUp {
  74. [super setUp];
  75. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  76. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  77. _RPCIssuer = RPCIssuer;
  78. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey
  79. appID:kTestFirebaseAppID];
  80. }
  81. - (void)tearDown {
  82. _RPCIssuer = nil;
  83. _requestConfiguration = nil;
  84. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  85. [super tearDown];
  86. }
  87. /**
  88. @fn testTOTPStartMFAEnrollmentRequest
  89. @brief Tests the Start MFA Enrollment using TOTP request.
  90. */
  91. - (void)testTOTPStartMFAEnrollmentRequest {
  92. FIRAuthProtoStartMFATOTPEnrollmentRequestInfo *requestInfo =
  93. [[FIRAuthProtoStartMFATOTPEnrollmentRequestInfo alloc] init];
  94. FIRStartMFAEnrollmentRequest *request =
  95. [[FIRStartMFAEnrollmentRequest alloc] initWithIDToken:kIDToken
  96. TOTPEnrollmentInfo:requestInfo
  97. requestConfiguration:_requestConfiguration];
  98. [FIRAuthBackend startMultiFactorEnrollment:request
  99. callback:^(FIRStartMFAEnrollmentResponse *_Nullable response,
  100. NSError *_Nullable error){
  101. }];
  102. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  103. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  104. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kIDToken], kIDToken);
  105. XCTAssertNotNil(_RPCIssuer.decodedRequest[kTOTPEnrollmentInfo]);
  106. XCTAssertNil(_RPCIssuer.decodedRequest[kPhoneEnrollmentInfo]);
  107. }
  108. @end
  109. #endif