-
Notifications
You must be signed in to change notification settings - Fork 154
[PM-27134] feat: Add Register Passkey screen to TestHarness #2946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
morganzellers-bw
wants to merge
4
commits into
pm-40519-testharness-passkeys-sdk-foundation
from
pm-27134-testharness-register-passkeys-ui
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
TestHarnessShared/Core/Autofill/Passkey/Fixtures/MakeCredentialResult+Fixtures.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import BitwardenSdk | ||
| import Foundation | ||
|
|
||
| extension MakeCredentialResult { | ||
| static func fixture( | ||
| attestationObject: Data = Data([0x01]), | ||
| authenticatorData: Data = Data([0x02]), | ||
| credentialId: Data = Data([0x03]), | ||
| ) -> MakeCredentialResult { | ||
| MakeCredentialResult( | ||
| authenticatorData: authenticatorData, | ||
| attestationObject: attestationObject, | ||
| credentialId: credentialId, | ||
| extensions: MakeCredentialExtensionsOutput(prf: nil), | ||
| ) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
TestHarnessShared/UI/Autofill/Passkey/RegisterPasskeyAction.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // MARK: - RegisterPasskeyAction | ||
|
|
||
| /// Actions that can be processed by an `RegisterPasskeyProcessor`. | ||
| /// | ||
| enum RegisterPasskeyAction: Equatable { | ||
| /// The display name text field changed. | ||
| case displayNameChanged(String) | ||
|
|
||
| /// The relying party ID text field changed. | ||
| case rpIdChanged(String) | ||
|
|
||
| /// The username text field changed. | ||
| case userNameChanged(String) | ||
| } |
8 changes: 8 additions & 0 deletions
8
TestHarnessShared/UI/Autofill/Passkey/RegisterPasskeyEffect.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| // MARK: - RegisterPasskeyEffect | ||
|
|
||
| /// Effects that can be processed by an `RegisterPasskeyProcessor`. | ||
| /// | ||
| enum RegisterPasskeyEffect: Equatable { | ||
| /// The user tapped the register passkey button. | ||
| case registerPasskey | ||
| } |
77 changes: 77 additions & 0 deletions
77
TestHarnessShared/UI/Autofill/Passkey/RegisterPasskeyProcessor.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import BitwardenKit | ||
|
|
||
| // MARK: - RegisterPasskeyProcessor | ||
|
|
||
| /// The processor for the register passkey test screen. | ||
| /// | ||
| final class RegisterPasskeyProcessor: StateProcessor< | ||
| RegisterPasskeyState, | ||
| RegisterPasskeyAction, | ||
| RegisterPasskeyEffect, | ||
| > { | ||
| // MARK: Private Properties | ||
|
|
||
| /// The coordinator that handles navigation. | ||
| private let coordinator: AnyCoordinator<RootRoute, Void> | ||
|
|
||
| /// The service used to perform passkey registration through the Bitwarden SDK. | ||
| private let passkeyService: PasskeyService | ||
|
|
||
| // MARK: Initialization | ||
|
|
||
| /// Initializes an `RegisterPasskeyProcessor`. | ||
| /// | ||
| /// - Parameters: | ||
| /// - coordinator: The coordinator that handles navigation. | ||
| /// - passkeyService: The service used to perform passkey registration through the | ||
| /// Bitwarden SDK. | ||
| /// | ||
| init( | ||
| coordinator: AnyCoordinator<RootRoute, Void>, | ||
| passkeyService: PasskeyService, | ||
| ) { | ||
| self.coordinator = coordinator | ||
| self.passkeyService = passkeyService | ||
| super.init(state: RegisterPasskeyState()) | ||
| } | ||
|
|
||
| // MARK: Methods | ||
|
|
||
| override func perform(_ effect: RegisterPasskeyEffect) async { | ||
| switch effect { | ||
| case .registerPasskey: | ||
| await registerPasskey() | ||
| } | ||
| } | ||
|
|
||
| override func receive(_ action: RegisterPasskeyAction) { | ||
| switch action { | ||
| case let .displayNameChanged(newValue): | ||
| state.displayName = newValue | ||
| state.status = .idle | ||
| case let .rpIdChanged(newValue): | ||
| state.rpId = newValue | ||
| state.status = .idle | ||
| case let .userNameChanged(newValue): | ||
| state.userName = newValue | ||
| state.status = .idle | ||
| } | ||
| } | ||
|
|
||
| // MARK: Private | ||
|
|
||
| /// Orchestrates state transitions and calls `passkeyService.registerPasskey`. | ||
| private func registerPasskey() async { | ||
| state.status = .inProgress | ||
| do { | ||
| let result = try await passkeyService.registerPasskey( | ||
| rpId: state.rpId, | ||
| userName: state.userName, | ||
| displayName: state.displayName, | ||
| ) | ||
| state.status = .success(credentialId: result.credentialId.base64EncodedString()) | ||
| } catch { | ||
| state.status = .failure(error.localizedDescription) | ||
| } | ||
| } | ||
| } | ||
109 changes: 109 additions & 0 deletions
109
TestHarnessShared/UI/Autofill/Passkey/RegisterPasskeyProcessorTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import BitwardenKit | ||
| import BitwardenKitMocks | ||
| import BitwardenSdk | ||
| import TestHelpers | ||
| import XCTest | ||
|
|
||
| @testable import TestHarnessShared | ||
|
|
||
| // MARK: - RegisterPasskeyProcessorTests | ||
|
|
||
| /// Tests for `RegisterPasskeyProcessor`. | ||
| /// | ||
| class RegisterPasskeyProcessorTests: BitwardenTestCase { | ||
| // MARK: Properties | ||
|
|
||
| var coordinator: MockCoordinator<RootRoute, Void>! | ||
| var passkeyService: MockPasskeyService! | ||
| var subject: RegisterPasskeyProcessor! | ||
|
|
||
| // MARK: Setup & Teardown | ||
|
|
||
| @MainActor | ||
| override func setUp() { | ||
| super.setUp() | ||
| coordinator = MockCoordinator() | ||
| passkeyService = MockPasskeyService() | ||
| subject = RegisterPasskeyProcessor( | ||
| coordinator: coordinator.asAnyCoordinator(), | ||
| passkeyService: passkeyService, | ||
| ) | ||
| } | ||
|
|
||
| override func tearDown() { | ||
| super.tearDown() | ||
| coordinator = nil | ||
| passkeyService = nil | ||
| subject = nil | ||
| } | ||
|
|
||
| // MARK: Action Tests | ||
|
|
||
| /// `receive(.displayNameChanged)` updates the display name in state. | ||
| @MainActor | ||
| func test_receive_displayNameChanged() { | ||
| subject.receive(.displayNameChanged("Test User")) | ||
| XCTAssertEqual(subject.state.displayName, "Test User") | ||
| } | ||
|
|
||
| /// `receive(.rpIdChanged)` updates the RP ID in state. | ||
| @MainActor | ||
| func test_receive_rpIdChanged() { | ||
| subject.receive(.rpIdChanged("bitwarden.com")) | ||
| XCTAssertEqual(subject.state.rpId, "bitwarden.com") | ||
| } | ||
|
|
||
| /// `receive(.userNameChanged)` updates the username in state. | ||
| @MainActor | ||
| func test_receive_userNameChanged() { | ||
| subject.receive(.userNameChanged("testuser")) | ||
| XCTAssertEqual(subject.state.userName, "testuser") | ||
| } | ||
|
|
||
| // MARK: Effect Tests | ||
|
|
||
| /// `perform(.registerPasskey)` passes the current state values to the SDK passkey service. | ||
| @MainActor | ||
| func test_perform_registerPasskey_passesStateValues() async { | ||
| subject.receive(.rpIdChanged("example.com")) | ||
| subject.receive(.userNameChanged("alice")) | ||
| subject.receive(.displayNameChanged("Alice Smith")) | ||
| passkeyService.registerPasskeyReturnValue = .fixture() | ||
|
|
||
| await subject.perform(.registerPasskey) | ||
|
|
||
| XCTAssertEqual( | ||
| passkeyService.registerPasskeyReceivedArguments?.rpId, | ||
| "example.com", | ||
| ) | ||
| XCTAssertEqual( | ||
| passkeyService.registerPasskeyReceivedArguments?.userName, | ||
| "alice", | ||
| ) | ||
| XCTAssertEqual( | ||
| passkeyService.registerPasskeyReceivedArguments?.displayName, | ||
| "Alice Smith", | ||
| ) | ||
| } | ||
|
|
||
| /// `perform(.registerPasskey)` sets status to `.failure` when registration throws. | ||
| @MainActor | ||
| func test_perform_registerPasskey_failure() async { | ||
| passkeyService.registerPasskeyThrowableError = BitwardenTestError.example | ||
|
|
||
| await subject.perform(.registerPasskey) | ||
|
|
||
| XCTAssertEqual(subject.state.status, .failure(BitwardenTestError.example.localizedDescription)) | ||
| } | ||
|
|
||
| /// `perform(.registerPasskey)` sets status to `.success` with the resulting credential ID | ||
| /// when registration succeeds. | ||
| @MainActor | ||
| func test_perform_registerPasskey_success() async { | ||
| passkeyService.registerPasskeyReturnValue = .fixture(credentialId: Data([0x01, 0x02, 0x03])) | ||
|
|
||
| await subject.perform(.registerPasskey) | ||
|
|
||
| XCTAssertEqual(subject.state.status, .success(credentialId: Data([0x01, 0x02, 0x03]).base64EncodedString())) | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
TestHarnessShared/UI/Autofill/Passkey/RegisterPasskeyState.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import Foundation | ||
|
|
||
| // MARK: - RegisterPasskeyState | ||
|
|
||
| /// The state for the register passkey test screen. | ||
| /// | ||
| struct RegisterPasskeyState: Equatable { | ||
| // MARK: Types | ||
|
|
||
| /// The current status of an passkey registration attempt. | ||
| enum RegistrationStatus: Equatable { | ||
| /// Registration failed with the associated error description. | ||
| case failure(String) | ||
|
|
||
| /// No registration attempt has been made. | ||
| case idle | ||
|
|
||
| /// A registration request is in progress. | ||
| case inProgress | ||
|
|
||
| /// Registration completed successfully, producing the credential ID below. | ||
| case success(credentialId: String) | ||
| } | ||
|
|
||
| // MARK: Properties | ||
|
|
||
| /// The display name for the passkey credential. | ||
| var displayName: String = "" | ||
|
|
||
| /// The relying party identifier (RP ID) for passkey registration. | ||
| var rpId: String = "bitwarden.pw" | ||
|
|
||
| /// The current registration status. | ||
| var status: RegistrationStatus = .idle | ||
|
|
||
| /// The title of the screen. | ||
| var title: String = Localizations.registerPasskey | ||
|
|
||
| /// The username for the passkey credential. | ||
| var userName: String = "" | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π¨ SUGGESTED: Editing a field while a registration is in flight resets
statusto.idle, re-enabling the register button.Details and fix
The text fields stay editable during registration (only the button is
.disabled), so typing mid-flight setsstatusback to.idle. That hides theProgressViewand re-enables the Register button (RegisterPasskeyView.swift:83), which lets a second concurrentregisterPasskeyrun and register a duplicate credential for the same RP β later ambiguous forassertPasskey(credentialId: nil, rpId:).Guarding the reset keeps the in-progress state authoritative: