| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- /*
- * Copyright 2023 Google LLC
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #import <XCTest/XCTest.h>
- #if TARGET_OS_IOS || TARGET_OS_TV || TARGET_OS_OSX || TARGET_OS_MACCATALYST
- #import "FirebaseAuth/Sources/Public/FirebaseAuth/FIRAuthErrors.h"
- #import "FirebaseAuth/Sources/Backend/FIRAuthBackend.h"
- #import "FirebaseAuth/Sources/Backend/RPC/FIRStartPasskeyEnrollmentRequest.h"
- #import "FirebaseAuth/Sources/Backend/RPC/FIRStartPasskeyEnrollmentResponse.h"
- #import "FirebaseAuth/Sources/Utilities/FIRAuthInternalErrors.h"
- #import "FirebaseAuth/Tests/Unit/FIRFakeBackendRPCIssuer.h"
- /**
- @var kTestAPIKey
- @brief Fake API key used for testing.
- */
- static NSString *const kTestAPIKey = @"APIKey";
- /**
- @var kTestFirebaseAppID
- @brief Fake Firebase app ID used for testing.
- */
- static NSString *const kTestFirebaseAppID = @"appID";
- /**
- @var kIDToken
- @brief Token representing the user's identity.
- */
- static NSString *const kIDToken = @"idToken";
- /**
- @var kTestRpID
- @brief Fake Relying Party ID used for testing.
- */
- static NSString *const kTestRpID = @"1234567890";
- /**
- @var kTestChallenge
- @brief Fake challenge used for testing.
- */
- static NSString *const kTestChallenge = @"challengebytes";
- /**
- @var kTestUserID
- @brief Fake user id used for testing.
- */
- static NSString *const kTestUserID = @"user-id";
- /**
- @var kUsersKey
- @brief the name of the "users" property in the response.
- */
- static NSString *const kUsersKey = @"users";
- /**
- @var kTestRpKey
- @brief the name of the "rp" property in the response.
- */
- static NSString *const kTestRpKey = @"rp";
- /**
- @var kTestChallengeKey
- @brief the name of the "challenge" property in the response.
- */
- static NSString *const kTestChallengeKey = @"challenge";
- /**
- @var kTestUserKey
- @brief the name of the "user" property in the response.
- */
- static NSString *const kTestUserKey = @"user";
- /**
- @var kTestIDKey
- @brief the name of the "id" property in the response.
- */
- static NSString *const kTestIDKey = @"id";
- /**
- @class FIRStartPasskeyEnrollmentResponseTests
- @brief Tests for @c FIRStartPasskeyEnrollmentResponse.
- */
- @interface FIRStartPasskeyEnrollmentResponseTests : XCTestCase
- @end
- @implementation FIRStartPasskeyEnrollmentResponseTests {
- /**
- @brief This backend RPC issuer is used to fake network responses for each test in the suite.
- In the @c setUp method we initialize this and set @c FIRAuthBackend's RPC issuer to it.
- */
- FIRFakeBackendRPCIssuer *_RPCIssuer;
- /**
- @brief This is the request configuration used for testing.
- */
- FIRAuthRequestConfiguration *_requestConfiguration;
- }
- - (void)setUp {
- [super setUp];
- FIRFakeBackendRPCIssuer *RPCIssuer = [[FIRFakeBackendRPCIssuer alloc] init];
- [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:RPCIssuer];
- _RPCIssuer = RPCIssuer;
- _requestConfiguration = [[FIRAuthRequestConfiguration alloc] initWithAPIKey:kTestAPIKey
- appID:kTestFirebaseAppID];
- }
- - (void)tearDown {
- _RPCIssuer = nil;
- _requestConfiguration = nil;
- [FIRAuthBackend setDefaultBackendImplementationWithRPCIssuer:nil];
- [super tearDown];
- }
- /** @fn testSuccessfulStartPasskeyEnrollmentResponse
- @brief This test simulates a successful @c StartPasskeyEnrollment flow.
- */
- - (void)testSuccessfulStartPasskeyEnrollmentResponse {
- FIRStartPasskeyEnrollmentRequest *request =
- [[FIRStartPasskeyEnrollmentRequest alloc] initWithIDToken:kIDToken
- requestConfiguration:_requestConfiguration];
- __block BOOL callbackInvoked;
- __block FIRStartPasskeyEnrollmentResponse *RPCResponse;
- __block NSError *RPCError;
- [FIRAuthBackend startPasskeyEnrollment:request
- callback:^(FIRStartPasskeyEnrollmentResponse *_Nullable response,
- NSError *_Nullable error) {
- callbackInvoked = YES;
- RPCResponse = response;
- RPCError = error;
- }];
- [_RPCIssuer respondWithJSON:@{
- @"credentialCreationOptions" : @{
- kTestChallengeKey : kTestChallenge,
- kTestRpKey : @{kTestIDKey : kTestRpID},
- kTestUserKey : @{kTestIDKey : kTestUserID},
- },
- }];
- XCTAssert(callbackInvoked);
- XCTAssertNil(RPCError);
- XCTAssertNotNil(RPCResponse);
- XCTAssertEqualObjects(RPCResponse.rpID, kTestRpID);
- XCTAssertEqualObjects(RPCResponse.challenge, kTestChallenge);
- XCTAssertEqualObjects(RPCResponse.userID, kTestUserID);
- }
- /** @fn testStartPasskeyEnrollmentResponseMissingCreationOptionsError
- @brief This test simulates an unexpected response returned from server in @c
- StartPasskeyEnrollment flow.
- */
- - (void)testStartPasskeyEnrollmentResponseMissingCreationOptionsError {
- FIRStartPasskeyEnrollmentRequest *request =
- [[FIRStartPasskeyEnrollmentRequest alloc] initWithIDToken:kIDToken
- requestConfiguration:_requestConfiguration];
- __block BOOL callbackInvoked;
- __block FIRStartPasskeyEnrollmentResponse *RPCResponse;
- __block NSError *RPCError;
- [FIRAuthBackend startPasskeyEnrollment:request
- callback:^(FIRStartPasskeyEnrollmentResponse *_Nullable response,
- NSError *_Nullable error) {
- callbackInvoked = YES;
- RPCResponse = response;
- RPCError = error;
- }];
- [_RPCIssuer respondWithJSON:@{
- @"wrongkey" : @{},
- }];
- [self errorValidationHelperWithCallbackInvoked:callbackInvoked
- rpcError:RPCError
- rpcResponse:RPCResponse];
- }
- /** @fn testStartPasskeyEnrollmentResponseMissingRpIdError
- @brief This test simulates an unexpected response returned from server in @c
- StartPasskeyEnrollment flow.
- */
- - (void)testStartPasskeyEnrollmentResponseMissingRpIdError {
- FIRStartPasskeyEnrollmentRequest *request =
- [[FIRStartPasskeyEnrollmentRequest alloc] initWithIDToken:kIDToken
- requestConfiguration:_requestConfiguration];
- __block BOOL callbackInvoked;
- __block FIRStartPasskeyEnrollmentResponse *RPCResponse;
- __block NSError *RPCError;
- [FIRAuthBackend startPasskeyEnrollment:request
- callback:^(FIRStartPasskeyEnrollmentResponse *_Nullable response,
- NSError *_Nullable error) {
- callbackInvoked = YES;
- RPCResponse = response;
- RPCError = error;
- }];
- [_RPCIssuer respondWithJSON:@{
- @"credentialCreationOptions" : @{
- kTestChallengeKey : kTestChallenge,
- kTestRpKey : @{},
- kTestUserKey : @{kTestIDKey : kTestUserID},
- },
- }];
- [self errorValidationHelperWithCallbackInvoked:callbackInvoked
- rpcError:RPCError
- rpcResponse:RPCResponse];
- }
- /** @fn testStartPasskeyEnrollmentResponseMissingUserIdError
- @brief This test simulates an unexpected response returned from server in @c
- StartPasskeyEnrollment flow.
- */
- - (void)testStartPasskeyEnrollmentResponseMissingUserIdError {
- FIRStartPasskeyEnrollmentRequest *request =
- [[FIRStartPasskeyEnrollmentRequest alloc] initWithIDToken:kIDToken
- requestConfiguration:_requestConfiguration];
- __block BOOL callbackInvoked;
- __block FIRStartPasskeyEnrollmentResponse *RPCResponse;
- __block NSError *RPCError;
- [FIRAuthBackend startPasskeyEnrollment:request
- callback:^(FIRStartPasskeyEnrollmentResponse *_Nullable response,
- NSError *_Nullable error) {
- callbackInvoked = YES;
- RPCResponse = response;
- RPCError = error;
- }];
- [_RPCIssuer respondWithJSON:@{
- @"credentialCreationOptions" : @{
- kTestChallengeKey : kTestChallenge,
- kTestRpKey : @{kTestIDKey : kTestRpID},
- kTestUserKey : @{},
- },
- }];
- [self errorValidationHelperWithCallbackInvoked:callbackInvoked
- rpcError:RPCError
- rpcResponse:RPCResponse];
- }
- /** @fn testStartPasskeyEnrollmentResponseMissingChallengeError
- @brief This test simulates an unexpected response returned from server in @c
- StartPasskeyEnrollment flow.
- */
- - (void)testStartPasskeyEnrollmentResponseMissingChallengeError {
- FIRStartPasskeyEnrollmentRequest *request =
- [[FIRStartPasskeyEnrollmentRequest alloc] initWithIDToken:kIDToken
- requestConfiguration:_requestConfiguration];
- __block BOOL callbackInvoked;
- __block FIRStartPasskeyEnrollmentResponse *RPCResponse;
- __block NSError *RPCError;
- [FIRAuthBackend startPasskeyEnrollment:request
- callback:^(FIRStartPasskeyEnrollmentResponse *_Nullable response,
- NSError *_Nullable error) {
- callbackInvoked = YES;
- RPCResponse = response;
- RPCError = error;
- }];
- [_RPCIssuer respondWithJSON:@{
- @"credentialCreationOptions" : @{
- kTestRpKey : @{kTestIDKey : kTestRpID},
- kTestUserKey : @{kTestIDKey : kTestUserID},
- },
- }];
- [self errorValidationHelperWithCallbackInvoked:callbackInvoked
- rpcError:RPCError
- rpcResponse:RPCResponse];
- }
- /** @fn errorValidationHelperWithCallbackInvoked:rpcError:rpcResponse:
- @brief Helper function to validate the unexpected response returned from server in @c
- StartPasskeyEnrollment flow.
- */
- - (void)errorValidationHelperWithCallbackInvoked:(BOOL)callbackInvoked
- rpcError:(NSError *)RPCError
- rpcResponse:(FIRStartPasskeyEnrollmentResponse *)RPCResponse {
- XCTAssert(callbackInvoked);
- XCTAssertNotNil(RPCError);
- XCTAssertEqualObjects(RPCError.domain, FIRAuthErrorDomain);
- XCTAssertEqual(RPCError.code, FIRAuthErrorCodeInternalError);
- XCTAssertNotNil(RPCError.userInfo[NSUnderlyingErrorKey]);
- NSError *underlyingError = RPCError.userInfo[NSUnderlyingErrorKey];
- XCTAssertNotNil(underlyingError);
- XCTAssertNotNil(underlyingError.userInfo[FIRAuthErrorUserInfoDeserializedResponseKey]);
- XCTAssertNil(RPCResponse);
- }
- @end
- #endif
|