FIRStartPasskeyEnrollmentResponseTests.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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/FIRStartPasskeyEnrollmentRequest.h"
  21. #import "FirebaseAuth/Sources/Backend/RPC/FIRStartPasskeyEnrollmentResponse.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 kIDToken
  36. @brief Token representing the user's identity.
  37. */
  38. static NSString *const kIDToken = @"idToken";
  39. /**
  40. @var kTestRpID
  41. @brief Fake Relying Party ID used for testing.
  42. */
  43. static NSString *const kTestRpID = @"1234567890";
  44. /**
  45. @var kTestChallenge
  46. @brief Fake challenge used for testing.
  47. */
  48. static NSString *const kTestChallenge = @"challengebytes";
  49. /**
  50. @var kTestUserID
  51. @brief Fake user id used for testing.
  52. */
  53. static NSString *const kTestUserID = @"user-id";
  54. /**
  55. @var kUsersKey
  56. @brief the name of the "users" property in the response.
  57. */
  58. static NSString *const kUsersKey = @"users";
  59. /**
  60. @var kTestRpKey
  61. @brief the name of the "rp" property in the response.
  62. */
  63. static NSString *const kTestRpKey = @"rp";
  64. /**
  65. @var kTestChallengeKey
  66. @brief the name of the "challenge" property in the response.
  67. */
  68. static NSString *const kTestChallengeKey = @"challenge";
  69. /**
  70. @var kTestUserKey
  71. @brief the name of the "user" property in the response.
  72. */
  73. static NSString *const kTestUserKey = @"user";
  74. /**
  75. @var kTestIDKey
  76. @brief the name of the "id" property in the response.
  77. */
  78. static NSString *const kTestIDKey = @"id";
  79. /**
  80. @class FIRStartPasskeyEnrollmentResponseTests
  81. @brief Tests for @c FIRStartPasskeyEnrollmentResponse.
  82. */
  83. @interface FIRStartPasskeyEnrollmentResponseTests : XCTestCase
  84. @end
  85. @implementation FIRStartPasskeyEnrollmentResponseTests {
  86. /**
  87. @brief This backend RPC issuer is used to fake network responses for each test in the suite.
  88. In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
  89. */
  90. FIRFakeBackendRPCIssuer *_RPCIssuer;
  91. /**
  92. @brief This is the request configuration used for testing.
  93. */
  94. FIRAuthRequestConfiguration *_requestConfiguration;
  95. }
  96. - (void)setUp {
  97. [super setUp];
  98. FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
  99. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
  100. _RPCIssuer = RPCIssuer;
  101. _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey
  102. appID:kTestFirebaseAppID];
  103. }
  104. - (void)tearDown {
  105. _RPCIssuer = nil;
  106. _requestConfiguration = nil;
  107. [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
  108. [super tearDown];
  109. }
  110. /** @fn testSuccessfulStartPasskeyEnrollmentResponse
  111. @brief This test simulates a successful @c StartPasskeyEnrollment flow.
  112. */
  113. - (void)testSuccessfulStartPasskeyEnrollmentResponse {
  114. FIRStartPasskeyEnrollmentRequest *request =
  115. [[FIRStartPasskeyEnrollmentRequest alloc] initWithIDToken:kIDToken
  116. requestConfiguration:_requestConfiguration];
  117. __block BOOL callbackInvoked;
  118. __block FIRStartPasskeyEnrollmentResponse *RPCResponse;
  119. __block NSError *RPCError;
  120. [FIRAuthBackend startPasskeyEnrollment:request
  121. callback:^(FIRStartPasskeyEnrollmentResponse *_Nullable response,
  122. NSError *_Nullable error) {
  123. callbackInvoked = YES;
  124. RPCResponse = response;
  125. RPCError = error;
  126. }];
  127. [_RPCIssuer respondWithJSON:@{
  128. @"credentialCreationOptions" : @{
  129. kTestChallengeKey : kTestChallenge,
  130. kTestRpKey : @{kTestIDKey : kTestRpID},
  131. kTestUserKey : @{kTestIDKey : kTestUserID},
  132. },
  133. }];
  134. XCTAssert(callbackInvoked);
  135. XCTAssertNil(RPCError);
  136. XCTAssertNotNil(RPCResponse);
  137. XCTAssertEqualObjects(RPCResponse.rpID, kTestRpID);
  138. XCTAssertEqualObjects(RPCResponse.challenge, kTestChallenge);
  139. XCTAssertEqualObjects(RPCResponse.userID, kTestUserID);
  140. }
  141. /** @fn testStartPasskeyEnrollmentResponseMissingCreationOptionsError
  142. @brief This test simulates an unexpected response returned from server in @c
  143. StartPasskeyEnrollment flow.
  144. */
  145. - (void)testStartPasskeyEnrollmentResponseMissingCreationOptionsError {
  146. FIRStartPasskeyEnrollmentRequest *request =
  147. [[FIRStartPasskeyEnrollmentRequest alloc] initWithIDToken:kIDToken
  148. requestConfiguration:_requestConfiguration];
  149. __block BOOL callbackInvoked;
  150. __block FIRStartPasskeyEnrollmentResponse *RPCResponse;
  151. __block NSError *RPCError;
  152. [FIRAuthBackend startPasskeyEnrollment:request
  153. callback:^(FIRStartPasskeyEnrollmentResponse *_Nullable response,
  154. NSError *_Nullable error) {
  155. callbackInvoked = YES;
  156. RPCResponse = response;
  157. RPCError = error;
  158. }];
  159. [_RPCIssuer respondWithJSON:@{
  160. @"wrongkey" : @{},
  161. }];
  162. [self errorValidationHelperWithCallbackInvoked:callbackInvoked
  163. rpcError:RPCError
  164. rpcResponse:RPCResponse];
  165. }
  166. /** @fn testStartPasskeyEnrollmentResponseMissingRpIdError
  167. @brief This test simulates an unexpected response returned from server in @c
  168. StartPasskeyEnrollment flow.
  169. */
  170. - (void)testStartPasskeyEnrollmentResponseMissingRpIdError {
  171. FIRStartPasskeyEnrollmentRequest *request =
  172. [[FIRStartPasskeyEnrollmentRequest alloc] initWithIDToken:kIDToken
  173. requestConfiguration:_requestConfiguration];
  174. __block BOOL callbackInvoked;
  175. __block FIRStartPasskeyEnrollmentResponse *RPCResponse;
  176. __block NSError *RPCError;
  177. [FIRAuthBackend startPasskeyEnrollment:request
  178. callback:^(FIRStartPasskeyEnrollmentResponse *_Nullable response,
  179. NSError *_Nullable error) {
  180. callbackInvoked = YES;
  181. RPCResponse = response;
  182. RPCError = error;
  183. }];
  184. [_RPCIssuer respondWithJSON:@{
  185. @"credentialCreationOptions" : @{
  186. kTestChallengeKey : kTestChallenge,
  187. kTestRpKey : @{},
  188. kTestUserKey : @{kTestIDKey : kTestUserID},
  189. },
  190. }];
  191. [self errorValidationHelperWithCallbackInvoked:callbackInvoked
  192. rpcError:RPCError
  193. rpcResponse:RPCResponse];
  194. }
  195. /** @fn testStartPasskeyEnrollmentResponseMissingUserIdError
  196. @brief This test simulates an unexpected response returned from server in @c
  197. StartPasskeyEnrollment flow.
  198. */
  199. - (void)testStartPasskeyEnrollmentResponseMissingUserIdError {
  200. FIRStartPasskeyEnrollmentRequest *request =
  201. [[FIRStartPasskeyEnrollmentRequest alloc] initWithIDToken:kIDToken
  202. requestConfiguration:_requestConfiguration];
  203. __block BOOL callbackInvoked;
  204. __block FIRStartPasskeyEnrollmentResponse *RPCResponse;
  205. __block NSError *RPCError;
  206. [FIRAuthBackend startPasskeyEnrollment:request
  207. callback:^(FIRStartPasskeyEnrollmentResponse *_Nullable response,
  208. NSError *_Nullable error) {
  209. callbackInvoked = YES;
  210. RPCResponse = response;
  211. RPCError = error;
  212. }];
  213. [_RPCIssuer respondWithJSON:@{
  214. @"credentialCreationOptions" : @{
  215. kTestChallengeKey : kTestChallenge,
  216. kTestRpKey : @{kTestIDKey : kTestRpID},
  217. kTestUserKey : @{},
  218. },
  219. }];
  220. [self errorValidationHelperWithCallbackInvoked:callbackInvoked
  221. rpcError:RPCError
  222. rpcResponse:RPCResponse];
  223. }
  224. /** @fn testStartPasskeyEnrollmentResponseMissingChallengeError
  225. @brief This test simulates an unexpected response returned from server in @c
  226. StartPasskeyEnrollment flow.
  227. */
  228. - (void)testStartPasskeyEnrollmentResponseMissingChallengeError {
  229. FIRStartPasskeyEnrollmentRequest *request =
  230. [[FIRStartPasskeyEnrollmentRequest alloc] initWithIDToken:kIDToken
  231. requestConfiguration:_requestConfiguration];
  232. __block BOOL callbackInvoked;
  233. __block FIRStartPasskeyEnrollmentResponse *RPCResponse;
  234. __block NSError *RPCError;
  235. [FIRAuthBackend startPasskeyEnrollment:request
  236. callback:^(FIRStartPasskeyEnrollmentResponse *_Nullable response,
  237. NSError *_Nullable error) {
  238. callbackInvoked = YES;
  239. RPCResponse = response;
  240. RPCError = error;
  241. }];
  242. [_RPCIssuer respondWithJSON:@{
  243. @"credentialCreationOptions" : @{
  244. kTestRpKey : @{kTestIDKey : kTestRpID},
  245. kTestUserKey : @{kTestIDKey : kTestUserID},
  246. },
  247. }];
  248. [self errorValidationHelperWithCallbackInvoked:callbackInvoked
  249. rpcError:RPCError
  250. rpcResponse:RPCResponse];
  251. }
  252. /** @fn errorValidationHelperWithCallbackInvoked:rpcError:rpcResponse:
  253. @brief Helper function to validate the unexpected response returned from server in @c
  254. StartPasskeyEnrollment flow.
  255. */
  256. - (void)errorValidationHelperWithCallbackInvoked:(BOOL)callbackInvoked
  257. rpcError:(NSError *)RPCError
  258. rpcResponse:(FIRStartPasskeyEnrollmentResponse *)RPCResponse {
  259. XCTAssert(callbackInvoked);
  260. XCTAssertNotNil(RPCError);
  261. XCTAssertEqualObjects(RPCError.domain, FIRAuthErrorDomain);
  262. XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInternalError);
  263. XCTAssertNotNil(RPCError.userInfo[NSUnderlyingErrorKey]);
  264. NSError *underlyingError = RPCError.userInfo[NSUnderlyingErrorKey];
  265. XCTAssertNotNil(underlyingError);
  266. XCTAssertNotNil(underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey]);
  267. XCTAssertNil(RPCResponse);
  268. }
  269. @end
  270. #endif