FIRFinalizePasskeyEnrollmentRequestTests.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 || TARGET_OS_TV || TARGET_OS_OSX || TARGET_OS_MACCATALYST
  18. #import <XCTest/XCTest.h>
  19. #import "FirebaseAuth/Sources/Backend/RPC/FIRFinalizePasskeyEnrollmentRequest.h"
  20. #import "FirebaseAuth/Sources/Backend/RPC/FIRFinalizePasskeyEnrollmentResponse.h"
  21. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuthErrors.h"
  22. #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  23. /**
  24. @var kTestAPIKey
  25. @brief Fake API key used for testing.
  26. */
  27. static NSString *const kTestAPIKey = @"APIKey";
  28. /**
  29. @var kTestFirebaseAppID
  30. @brief Fake Firebase app ID used for testing.
  31. */
  32. static NSString *const kTestFirebaseAppID = @"appID";
  33. /**
  34. @var kExpectedAPIURL
  35. @brief The expected URL for the test calls.
  36. */
  37. static NSString *const kExpectedAPIURL =
  38. @"https://identitytoolkit.googleapis.com/v2/accounts/passkeyEnrollment:finalize?key=APIKey";
  39. /**
  40. @var kIDToken
  41. @brief Token representing the user's identity.
  42. */
  43. static NSString *const kIDToken = @"testIDToken";
  44. /**
  45. @var kIDTokenKey
  46. @brief ID Token field.
  47. */
  48. static NSString *const kIDTokenKey = @"idToken";
  49. /**
  50. @var kName
  51. @brief Passkey name.
  52. */
  53. static NSString *const kName = @"testName";
  54. /**
  55. @var kNameKey
  56. @brief Passkey name field
  57. */
  58. static NSString *const kNameKey = @"name";
  59. /**
  60. @var kCredentialID
  61. @brief credential ID.
  62. */
  63. static NSString *const kCredentialID = @"testCredentialID";
  64. /**
  65. @var kCredentialIDKey
  66. @brief credential ID field.
  67. */
  68. static NSString *const kCredentialIDKey = @"id";
  69. /**
  70. @var kRawAttestationObject
  71. @brief Passkey attestation object.
  72. */
  73. static NSString *const kRawAttestationObject = @"testRawAttestationObject";
  74. /**
  75. @var kRawAttestationObjectKey
  76. @brief The key for the attestation object from the authenticator.
  77. */
  78. static NSString *const kRawAttestationObjectKey = @"attestationObject";
  79. /**
  80. @var kRawClientDataJSON
  81. @brief CollectedClientData object from the authenticator.
  82. */
  83. static NSString *const kRawClientDataJSON = @"testRawClientDataJSON";
  84. /**
  85. @var kRawClientDataJSONKey
  86. @brief The key for the attestation object from the authenticator.
  87. */
  88. static NSString *const kRawClientDataJSONKey = @"clientDataJSON";
  89. /**
  90. @var kAuthRegistrationRespKey
  91. @brief The registration object from the authenticator.
  92. */
  93. static NSString *const kAuthRegistrationRespKey = @"authenticatorRegistrationResponse";
  94. /**
  95. @var kAuthAttestationRespKey
  96. @brief The key for attestation response from a FIDO authenticator.
  97. */
  98. static NSString *const kAuthAttestationRespKey = @"response";
  99. /**
  100. @class FIRFinalizePasskeyEnrollmentRequestTests
  101. @brief Tests for @c FIRFinalizePasskeyEnrollmentRequest.
  102. */
  103. @interface FIRFinalizePasskeyEnrollmentRequestTests : XCTestCase
  104. @end
  105. @implementation FIRFinalizePasskeyEnrollmentRequestTests {
  106. /**
  107. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  108. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  109. */
  110. FIRFakeBackendRPCIssuer *_RPCIssuer;
  111. /**
  112. @brief This is the request configuration used for testing.
  113. */
  114. FIRAuthRequestConfiguration *_requestConfiguration;
  115. }
  116. - (void)setUp {
  117. [super setUp];
  118. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  119. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  120. _RPCIssuer = RPCIssuer;
  121. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey
  122. appID:kTestFirebaseAppID];
  123. }
  124. - (void)tearDown {
  125. _RPCIssuer = nil;
  126. _requestConfiguration = nil;
  127. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  128. [super tearDown];
  129. }
  130. - (void)testFinalizePasskeyEnrollmentRequest {
  131. if (@available(iOS 15.0, *)) {
  132. FIRFinalizePasskeyEnrollmentRequest *request =
  133. [[FIRFinalizePasskeyEnrollmentRequest alloc] initWithIDToken:kIDToken
  134. name:kName
  135. credentialID:kCredentialID
  136. clientDataJson:kRawClientDataJSON
  137. attestationObject:kRawAttestationObject
  138. requestConfiguration:_requestConfiguration];
  139. [FIRAuthBackend
  140. finalizePasskeyEnrollment:request
  141. callback:^(FIRFinalizePasskeyEnrollmentResponse *_Nullable response,
  142. NSError *_Nullable error){
  143. }];
  144. XCTAssertEqualObjects(_RPCIssuer.requestURL.absoluteString, kExpectedAPIURL);
  145. XCTAssertNotNil(_RPCIssuer.decodedRequest);
  146. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kIDTokenKey], kIDToken);
  147. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kNameKey], kName);
  148. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kAuthRegistrationRespKey]
  149. [kAuthAttestationRespKey][kRawClientDataJSONKey],
  150. kRawClientDataJSON);
  151. XCTAssertEqualObjects(
  152. _RPCIssuer.decodedRequest[kAuthRegistrationRespKey][kAuthAttestationRespKey]
  153. [kRawAttestationObjectKey],
  154. kRawAttestationObject);
  155. XCTAssertEqualObjects(_RPCIssuer.decodedRequest[kAuthRegistrationRespKey][kCredentialIDKey],
  156. kCredentialID);
  157. }
  158. }
  159. @end
  160. #endif