FIRStartPasskeySignInResponseTests.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 <XCTest/XCTest.h>
  17. #if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_OSX || TARGET_OS_MACCATALYST
  18. #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuthErrors.h"
  19. #import "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
  20. #import "FirebaseAuth/Sources/Backend/RPC/FIRStartPasskeySignInRequest.h"
  21. #import "FirebaseAuth/Sources/Backend/RPC/FIRStartPasskeySignInResponse.h"
  22. #import "FirebaseAuth/Sources/Utilities/FIRAuthInternalErrors.h"
  23. #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
  24. /**
  25. @var kTestAPIKey
  26. @brief Fake API key used for testing.
  27. */
  28. static NSString *const kTestAPIKey = @"APIKey";
  29. /**
  30. @var kTestFirebaseAppID
  31. @brief Fake Firebase app ID used for testing.
  32. */
  33. static NSString *const kTestFirebaseAppID = @"appID";
  34. /**
  35. @var kTestRpID
  36. @brief Fake Relying Party ID used for testing.
  37. */
  38. static NSString *const kTestRpID = @"1234567890";
  39. /**
  40. @var kTestChallenge
  41. @brief Fake challenge used for testing.
  42. */
  43. static NSString *const kTestChallenge = @"challengebytes";
  44. /**
  45. @var kTestRpKey
  46. @brief the name of the "rp" property in the response.
  47. */
  48. static NSString *const kRpKey = @"rpId";
  49. /**
  50. @var kTestChallengeKey
  51. @brief the name of the "challenge" property in the response.
  52. */
  53. static NSString *const kChallengeKey = @"challenge";
  54. /**
  55. @class FIRStartPasskeySignInResponseTests
  56. @brief Tests for @c FIRStartPasskeySignInResponse.
  57. */
  58. @interface FIRStartPasskeySignInResponseTests : XCTestCase
  59. @end
  60. @implementation FIRStartPasskeySignInResponseTests {
  61. /**
  62. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  63. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  64. */
  65. FIRFakeBackendRPCIssuer *_RPCIssuer;
  66. /**
  67. @brief This is the request configuration used for testing.
  68. */
  69. FIRAuthRequestConfiguration *_requestConfiguration;
  70. }
  71. - (void)setUp {
  72. [super setUp];
  73. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  74. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  75. _RPCIssuer = RPCIssuer;
  76. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey
  77. appID:kTestFirebaseAppID];
  78. }
  79. - (void)tearDown {
  80. _RPCIssuer = nil;
  81. _requestConfiguration = nil;
  82. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  83. [super tearDown];
  84. }
  85. /** @fn testSuccessfulStartPasskeySignInResponse
  86. @brief This test simulates a successful @c StartPasskeySignIn flow.
  87. */
  88. - (void)testSuccessfulStartPasskeySignInResponse {
  89. FIRStartPasskeySignInRequest *request =
  90. [[FIRStartPasskeySignInRequest alloc] initWithRequestConfiguration:_requestConfiguration];
  91. __block BOOL callbackInvoked;
  92. __block FIRStartPasskeySignInResponse *RPCResponse;
  93. __block NSError *RPCError;
  94. [FIRAuthBackend startPasskeySignIn:request
  95. callback:^(FIRStartPasskeySignInResponse *_Nullable response,
  96. NSError *_Nullable error) {
  97. callbackInvoked = YES;
  98. RPCResponse = response;
  99. RPCError = error;
  100. }];
  101. [_RPCIssuer respondWithJSON:@{
  102. @"credentialRequestOptions" : @{
  103. kChallengeKey : kTestChallenge,
  104. kRpKey : kTestRpID,
  105. },
  106. }];
  107. XCTAssert(callbackInvoked);
  108. XCTAssertNil(RPCError);
  109. XCTAssertNotNil(RPCResponse);
  110. XCTAssertEqualObjects(RPCResponse.rpID, kTestRpID);
  111. XCTAssertEqualObjects(RPCResponse.challenge, kTestChallenge);
  112. }
  113. /** @fn testStartPasskeySignInResponseMissingRequestOptionsError
  114. @brief This test simulates an unexpected response returned from server in @c
  115. StartPasskeySignIn flow.
  116. */
  117. - (void)testStartPasskeySignInResponseMissingRequestOptionsError {
  118. FIRStartPasskeySignInRequest *request =
  119. [[FIRStartPasskeySignInRequest alloc] initWithRequestConfiguration:_requestConfiguration];
  120. __block BOOL callbackInvoked;
  121. __block FIRStartPasskeySignInResponse *RPCResponse;
  122. __block NSError *RPCError;
  123. [FIRAuthBackend startPasskeySignIn:request
  124. callback:^(FIRStartPasskeySignInResponse *_Nullable response,
  125. NSError *_Nullable error) {
  126. callbackInvoked = YES;
  127. RPCResponse = response;
  128. RPCError = error;
  129. }];
  130. [_RPCIssuer respondWithJSON:@{
  131. @"wrongkey" : @{},
  132. }];
  133. [self errorValidationHelperWithCallbackInvoked:callbackInvoked
  134. rpcError:RPCError
  135. rpcResponse:RPCResponse];
  136. }
  137. /** @fn testStartPasskeySignInResponseMissingRpIdError
  138. @brief This test simulates an unexpected response returned from server in @c
  139. StartPasskeySignIn flow.
  140. */
  141. - (void)testStartPasskeySignInResponseMissingRpIdError {
  142. FIRStartPasskeySignInRequest *request =
  143. [[FIRStartPasskeySignInRequest alloc] initWithRequestConfiguration:_requestConfiguration];
  144. __block BOOL callbackInvoked;
  145. __block FIRStartPasskeySignInResponse *RPCResponse;
  146. __block NSError *RPCError;
  147. [FIRAuthBackend startPasskeySignIn:request
  148. callback:^(FIRStartPasskeySignInResponse *_Nullable response,
  149. NSError *_Nullable error) {
  150. callbackInvoked = YES;
  151. RPCResponse = response;
  152. RPCError = error;
  153. }];
  154. [_RPCIssuer respondWithJSON:@{
  155. @"credentialRequestOptions" : @{
  156. kChallengeKey : kTestChallenge,
  157. },
  158. }];
  159. [self errorValidationHelperWithCallbackInvoked:callbackInvoked
  160. rpcError:RPCError
  161. rpcResponse:RPCResponse];
  162. }
  163. /** @fn testStartPasskeySignInResponseMissingChallengeError
  164. @brief This test simulates an unexpected response returned from server in @c
  165. StartPasskeySignIn flow.
  166. */
  167. - (void)testStartPasskeySignInResponseMissingChallengeError {
  168. FIRStartPasskeySignInRequest *request =
  169. [[FIRStartPasskeySignInRequest alloc] initWithRequestConfiguration:_requestConfiguration];
  170. __block BOOL callbackInvoked;
  171. __block FIRStartPasskeySignInResponse *RPCResponse;
  172. __block NSError *RPCError;
  173. [FIRAuthBackend startPasskeySignIn:request
  174. callback:^(FIRStartPasskeySignInResponse *_Nullable response,
  175. NSError *_Nullable error) {
  176. callbackInvoked = YES;
  177. RPCResponse = response;
  178. RPCError = error;
  179. }];
  180. [_RPCIssuer respondWithJSON:@{
  181. @"credentialCreationOptions" : @{
  182. kRpKey : kTestRpID,
  183. },
  184. }];
  185. [self errorValidationHelperWithCallbackInvoked:callbackInvoked
  186. rpcError:RPCError
  187. rpcResponse:RPCResponse];
  188. }
  189. /** @fn errorValidationHelperWithCallbackInvoked:rpcError:rpcResponse:
  190. @brief Helper function to validate the unexpected response returned from server in @c
  191. StartPasskeySignIn flow.
  192. */
  193. - (void)errorValidationHelperWithCallbackInvoked:(BOOL)callbackInvoked
  194. rpcError:(NSError *)RPCError
  195. rpcResponse:(FIRStartPasskeySignInResponse *)RPCResponse {
  196. XCTAssert(callbackInvoked);
  197. XCTAssertNotNil(RPCError);
  198. XCTAssertEqualObjects(RPCError.domain, FIRAuthErrorDomain);
  199. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInternalError);
  200. XCTAssertNotNil(RPCError.userInfo[NSUnderlyingErrorKey]);
  201. NSError *underlyingError = RPCError.userInfo[NSUnderlyingErrorKey];
  202. XCTAssertNotNil(underlyingError);
  203. XCTAssertNotNil(underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey]);
  204. XCTAssertNil(RPCResponse);
  205. }
  206. @end
  207. #endif