mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-03 06:55:59 +00:00
Add AppLockSetupPINScreen. (#1930)
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
//
|
||||
// Copyright 2022 New Vector Ltd
|
||||
//
|
||||
// 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
|
||||
|
||||
@testable import ElementX
|
||||
|
||||
@MainActor
|
||||
class AppLockSetupPINScreenViewModelTests: XCTestCase {
|
||||
var appLockService: AppLockService!
|
||||
var keychainController: KeychainControllerMock!
|
||||
var viewModel: AppLockSetupPINScreenViewModelProtocol!
|
||||
|
||||
var context: AppLockSetupPINScreenViewModelType.Context { viewModel.context }
|
||||
|
||||
override func setUp() {
|
||||
AppSettings.reset()
|
||||
keychainController = KeychainControllerMock()
|
||||
appLockService = AppLockService(keychainController: keychainController, appSettings: AppSettings())
|
||||
}
|
||||
|
||||
override func tearDown() {
|
||||
AppSettings.reset()
|
||||
}
|
||||
|
||||
func testCreatePIN() async throws {
|
||||
viewModel = AppLockSetupPINScreenViewModel(initialMode: .create, appLockService: appLockService)
|
||||
XCTAssertEqual(context.viewState.mode, .create, "The mode should start as creation.")
|
||||
|
||||
let createDeferred = deferFulfillment(context.$viewState, message: "A valid PIN needs confirming.") { $0.mode == .confirm }
|
||||
context.pinCode = "2023"
|
||||
context.send(viewAction: .submitPINCode)
|
||||
try await createDeferred.fulfill()
|
||||
XCTAssertEqual(context.viewState.mode, .confirm, "The mode should transition to confirmation.")
|
||||
|
||||
let confirmDeferred = deferFulfillment(viewModel.actions, message: "The screen should be finished.") { $0 == .complete }
|
||||
context.pinCode = "2023"
|
||||
context.send(viewAction: .submitPINCode)
|
||||
try await confirmDeferred.fulfill()
|
||||
}
|
||||
|
||||
func testCreateWeakPIN() async throws {
|
||||
viewModel = AppLockSetupPINScreenViewModel(initialMode: .create, appLockService: appLockService)
|
||||
XCTAssertEqual(context.viewState.mode, .create, "The mode should start as creation.")
|
||||
XCTAssertNil(context.alertInfo, "There shouldn't be an alert to begin with.")
|
||||
|
||||
context.pinCode = "0000"
|
||||
context.send(viewAction: .submitPINCode)
|
||||
|
||||
XCTAssertEqual(context.alertInfo?.id, .weakPIN, "The weak PIN should be rejected.")
|
||||
XCTAssertEqual(context.viewState.mode, .create, "The mode shouldn't transition after an invalid PIN code.")
|
||||
}
|
||||
|
||||
func testCreatePINMismatch() async throws {
|
||||
viewModel = AppLockSetupPINScreenViewModel(initialMode: .create, appLockService: appLockService)
|
||||
XCTAssertEqual(context.viewState.mode, .create, "The mode should start as creation.")
|
||||
XCTAssertNil(context.alertInfo, "There shouldn't be an alert to begin with.")
|
||||
|
||||
let createDeferred = deferFulfillment(context.$viewState, message: "A valid PIN needs confirming.") { $0.mode == .confirm }
|
||||
context.pinCode = "2023"
|
||||
context.send(viewAction: .submitPINCode)
|
||||
try await createDeferred.fulfill()
|
||||
XCTAssertEqual(context.viewState.mode, .confirm, "The mode should transition to confirmation.")
|
||||
XCTAssertNil(context.alertInfo, "There shouldn't be an alert after a valid initial PIN.")
|
||||
|
||||
context.pinCode = "2024"
|
||||
context.send(viewAction: .submitPINCode)
|
||||
|
||||
XCTAssertEqual(context.alertInfo?.id, .pinMismatch, "A PIN mismatch should be rejected.")
|
||||
}
|
||||
|
||||
func testUnlock() async throws {
|
||||
viewModel = AppLockSetupPINScreenViewModel(initialMode: .unlock, appLockService: appLockService)
|
||||
let pinCode = "2023"
|
||||
keychainController.pinCodeReturnValue = pinCode
|
||||
keychainController.containsPINCodeReturnValue = true
|
||||
|
||||
let deferred = deferFulfillment(viewModel.actions, message: "The screen should be finished.") { $0 == .complete }
|
||||
context.pinCode = pinCode
|
||||
context.send(viewAction: .submitPINCode)
|
||||
try await deferred.fulfill()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// Copyright 2023 New Vector Ltd
|
||||
//
|
||||
// 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
|
||||
|
||||
@testable import ElementX
|
||||
|
||||
class PINTextFieldTests: XCTestCase {
|
||||
func testSanitize() {
|
||||
let textField = PINTextField(pinCode: .constant(""))
|
||||
XCTAssertEqual(textField.sanitize("2"), "2")
|
||||
XCTAssertEqual(textField.sanitize("2023"), "2023")
|
||||
XCTAssertEqual(textField.sanitize("20233"), "2023")
|
||||
XCTAssertEqual(textField.sanitize("20x"), "20")
|
||||
XCTAssertEqual(textField.sanitize("20!"), "20")
|
||||
XCTAssertEqual(textField.sanitize("boop"), "")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:c78cada10d7c15137a635b0e841fe20e124814921d953296fca5ed16dea93fef
|
||||
size 98997
|
||||
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:f2aa8303cb959ebeae8a6b6bced04f2c3ecdfe429c97f153a38a2d650dddadf4
|
||||
size 99386
|
||||
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:e53aa446554e19bc47b31f59038ec3caeeaba8c4c15a1884f5e510f30eac4c61
|
||||
size 72867
|
||||
@@ -0,0 +1,3 @@
|
||||
version https://git-lfs.github.com/spec/v1
|
||||
oid sha256:fcab4fcacf9a140239e887d4133d281d7defdd89a8ab6e6f3a2f5fba8815fde8
|
||||
size 98245
|
||||
Reference in New Issue
Block a user