zashi-ios-wallet-private/secantTests/UtilTests/UserPreferencesStorageTests...

227 lines
7.9 KiB
Swift
Raw Normal View History

//
// UserPreferencesStorageTests.swift
// secantTests
//
// Created by Lukáš Korba on 22.03.2022.
//
import XCTest
@testable import secant_testnet
class UserPreferencesStorageTests: XCTestCase {
// swiftlint:disable:next implicitly_unwrapped_optional
var storage: UserPreferencesStorage!
override func setUp() async throws {
try await super.setUp()
guard let userDefaults = UserDefaults.init(suiteName: "test") else {
XCTFail("UserPreferencesStorageTests: UserDefaults.init(suiteName: \"test\") failed to initialize")
return
}
storage = UserPreferencesStorage(
appSessionFrom: 12345678.0,
convertedCurrency: "USD",
fiatConvertion: true,
recoveryPhraseTestCompleted: true,
sessionAutoshielded: false,
Add crash reporter to secant (#531) * [#525] Adds functions to configure, testCrash and check if it can start. This adds a build phase where a dummy file is added to the project to make the build and Plist copy happy. When building in the CI there will be a script to replace this Plist file with the real one that then will be copied to the bundle Crashlytics will be "off" by default and then be turned on when starting up to be an Opt-Out thing. This is the only way it can be turned off later. reference: https://firebase.google.com/docs/crashlytics/customize-crash-reports?platform=ios#enable_opt-in_reporting The app will start with crash reporting turned off and will set it up on by default on the application's code. Then if the user wants to opt-out of crash reporting, it can. Otherwise, it won't be possible. Adds opting out of crash reporting as a stored user preference. This adds a value inside UserPreferencesStorage and its live and mock counterparts. also creates a builer for `CrashReporterClient` that has a Dependency to `@Dependency(\.userStoredPreferences)` and sets the references for the client to set the appropriate values into the user storage `UserPreferencesStorage` as been adapted to be a TCA Dependency. `SettingsStore` now as a `Toogle()` to turn off and on crash reporting. But it doesn't work yet because I haven't found out how to make a TCA Binding that can rely on an initial value that is not hardcoded but injected from somewhere else. See https://www.pointfree.co/episodes/ep158-safer-conciser-forms-part-1 https://www.pointfree.co/episodes/ep158-safer-conciser-forms-part-2 Adds Test Crash button and enable crash reporting Adds upload-symbols run script phase Closes #525 Add a custom build environment variable "UPLOAD_CRASHLYTICS_SYMBOLS" that will let Xcode skip the upload_symbols script for debug builds Fix Initialization tests * bump build
2023-02-15 13:18:18 -08:00
userOptedOutOfCrashReporting: true,
userDefaults: .live(userDefaults: userDefaults)
)
await storage.removeAll()
}
override func tearDown() async throws {
try await super.tearDown()
await storage.removeAll()
storage = nil
}
// MARK: - Default values in the live UserDefaults environment
func testAppSessionFrom_defaultValue() throws {
XCTAssertEqual(12345678.0, storage.activeAppSessionFrom, "User Preferences: `activeAppSessionFrom` default doesn't match.")
}
func testConvertedCurrency_defaultValue() throws {
XCTAssertEqual("USD", storage.currency, "User Preferences: `currency` default doesn't match.")
}
func testFiatConvertion_defaultValue() throws {
XCTAssertEqual(true, storage.isFiatConverted, "User Preferences: `isFiatConverted` default doesn't match.")
}
func testRecoveryPhraseTestCompleted_defaultValue() throws {
XCTAssertEqual(true, storage.isRecoveryPhraseTestCompleted, "User Preferences: `isRecoveryPhraseTestCompleted` default doesn't match.")
}
func testSessionAutoshielded_defaultValue() throws {
XCTAssertEqual(false, storage.isSessionAutoshielded, "User Preferences: `isSessionAutoshielded` default doesn't match.")
}
// MARK: - Set new values in the live UserDefaults environment
func testAppSessionFrom_setNewValue() async throws {
await storage.setActiveAppSessionFrom(87654321.0)
XCTAssertEqual(87654321.0, storage.activeAppSessionFrom, "User Preferences: `activeAppSessionFrom` default doesn't match.")
}
func testConvertedCurrency_setNewValue() async throws {
await storage.setCurrency("CZK")
XCTAssertEqual("CZK", storage.currency, "User Preferences: `currency` default doesn't match.")
}
func testFiatConvertion_setNewValue() async throws {
await storage.setIsFiatConverted(false)
XCTAssertEqual(false, storage.isFiatConverted, "User Preferences: `isFiatConverted` default doesn't match.")
}
func testRecoveryPhraseTestCompleted_setNewValue() async throws {
await storage.setIsRecoveryPhraseTestCompleted(false)
XCTAssertEqual(false, storage.isRecoveryPhraseTestCompleted, "User Preferences: `isRecoveryPhraseTestCompleted` default doesn't match.")
}
func testSessionAutoshielded_setNewValue() async throws {
await storage.setIsSessionAutoshielded(true)
XCTAssertEqual(true, storage.isSessionAutoshielded, "User Preferences: `isSessionAutoshielded` default doesn't match.")
}
// MARK: - Mocked user defaults vs. default values
func testAppSessionFrom_mocked() throws {
let mockedUD = UserDefaultsClient(
objectForKey: { _ in 87654321.0 },
remove: { _ in },
setValue: { _, _ in },
synchronize: { true }
)
let mockedStorage = UserPreferencesStorage(
appSessionFrom: 12345678.0,
convertedCurrency: "USD",
fiatConvertion: true,
recoveryPhraseTestCompleted: true,
sessionAutoshielded: false,
Add crash reporter to secant (#531) * [#525] Adds functions to configure, testCrash and check if it can start. This adds a build phase where a dummy file is added to the project to make the build and Plist copy happy. When building in the CI there will be a script to replace this Plist file with the real one that then will be copied to the bundle Crashlytics will be "off" by default and then be turned on when starting up to be an Opt-Out thing. This is the only way it can be turned off later. reference: https://firebase.google.com/docs/crashlytics/customize-crash-reports?platform=ios#enable_opt-in_reporting The app will start with crash reporting turned off and will set it up on by default on the application's code. Then if the user wants to opt-out of crash reporting, it can. Otherwise, it won't be possible. Adds opting out of crash reporting as a stored user preference. This adds a value inside UserPreferencesStorage and its live and mock counterparts. also creates a builer for `CrashReporterClient` that has a Dependency to `@Dependency(\.userStoredPreferences)` and sets the references for the client to set the appropriate values into the user storage `UserPreferencesStorage` as been adapted to be a TCA Dependency. `SettingsStore` now as a `Toogle()` to turn off and on crash reporting. But it doesn't work yet because I haven't found out how to make a TCA Binding that can rely on an initial value that is not hardcoded but injected from somewhere else. See https://www.pointfree.co/episodes/ep158-safer-conciser-forms-part-1 https://www.pointfree.co/episodes/ep158-safer-conciser-forms-part-2 Adds Test Crash button and enable crash reporting Adds upload-symbols run script phase Closes #525 Add a custom build environment variable "UPLOAD_CRASHLYTICS_SYMBOLS" that will let Xcode skip the upload_symbols script for debug builds Fix Initialization tests * bump build
2023-02-15 13:18:18 -08:00
userOptedOutOfCrashReporting: true,
userDefaults: mockedUD
)
XCTAssertEqual(87654321.0, mockedStorage.activeAppSessionFrom, "User Preferences: `activeAppSessionFrom` default doesn't match.")
}
func testConvertedCurrency_mocked() throws {
let mockedUD = UserDefaultsClient(
objectForKey: { _ in "CZK" },
remove: { _ in },
setValue: { _, _ in },
synchronize: { true }
)
let mockedStorage = UserPreferencesStorage(
appSessionFrom: 12345678.0,
convertedCurrency: "USD",
fiatConvertion: true,
recoveryPhraseTestCompleted: true,
sessionAutoshielded: false,
Add crash reporter to secant (#531) * [#525] Adds functions to configure, testCrash and check if it can start. This adds a build phase where a dummy file is added to the project to make the build and Plist copy happy. When building in the CI there will be a script to replace this Plist file with the real one that then will be copied to the bundle Crashlytics will be "off" by default and then be turned on when starting up to be an Opt-Out thing. This is the only way it can be turned off later. reference: https://firebase.google.com/docs/crashlytics/customize-crash-reports?platform=ios#enable_opt-in_reporting The app will start with crash reporting turned off and will set it up on by default on the application's code. Then if the user wants to opt-out of crash reporting, it can. Otherwise, it won't be possible. Adds opting out of crash reporting as a stored user preference. This adds a value inside UserPreferencesStorage and its live and mock counterparts. also creates a builer for `CrashReporterClient` that has a Dependency to `@Dependency(\.userStoredPreferences)` and sets the references for the client to set the appropriate values into the user storage `UserPreferencesStorage` as been adapted to be a TCA Dependency. `SettingsStore` now as a `Toogle()` to turn off and on crash reporting. But it doesn't work yet because I haven't found out how to make a TCA Binding that can rely on an initial value that is not hardcoded but injected from somewhere else. See https://www.pointfree.co/episodes/ep158-safer-conciser-forms-part-1 https://www.pointfree.co/episodes/ep158-safer-conciser-forms-part-2 Adds Test Crash button and enable crash reporting Adds upload-symbols run script phase Closes #525 Add a custom build environment variable "UPLOAD_CRASHLYTICS_SYMBOLS" that will let Xcode skip the upload_symbols script for debug builds Fix Initialization tests * bump build
2023-02-15 13:18:18 -08:00
userOptedOutOfCrashReporting: true,
userDefaults: mockedUD
)
XCTAssertEqual("CZK", mockedStorage.currency, "User Preferences: `currency` default doesn't match.")
}
func testFiatConvertion_mocked() throws {
let mockedUD = UserDefaultsClient(
objectForKey: { _ in false },
remove: { _ in },
setValue: { _, _ in },
synchronize: { true }
)
let mockedStorage = UserPreferencesStorage(
appSessionFrom: 12345678.0,
convertedCurrency: "USD",
fiatConvertion: true,
recoveryPhraseTestCompleted: true,
sessionAutoshielded: false,
Add crash reporter to secant (#531) * [#525] Adds functions to configure, testCrash and check if it can start. This adds a build phase where a dummy file is added to the project to make the build and Plist copy happy. When building in the CI there will be a script to replace this Plist file with the real one that then will be copied to the bundle Crashlytics will be "off" by default and then be turned on when starting up to be an Opt-Out thing. This is the only way it can be turned off later. reference: https://firebase.google.com/docs/crashlytics/customize-crash-reports?platform=ios#enable_opt-in_reporting The app will start with crash reporting turned off and will set it up on by default on the application's code. Then if the user wants to opt-out of crash reporting, it can. Otherwise, it won't be possible. Adds opting out of crash reporting as a stored user preference. This adds a value inside UserPreferencesStorage and its live and mock counterparts. also creates a builer for `CrashReporterClient` that has a Dependency to `@Dependency(\.userStoredPreferences)` and sets the references for the client to set the appropriate values into the user storage `UserPreferencesStorage` as been adapted to be a TCA Dependency. `SettingsStore` now as a `Toogle()` to turn off and on crash reporting. But it doesn't work yet because I haven't found out how to make a TCA Binding that can rely on an initial value that is not hardcoded but injected from somewhere else. See https://www.pointfree.co/episodes/ep158-safer-conciser-forms-part-1 https://www.pointfree.co/episodes/ep158-safer-conciser-forms-part-2 Adds Test Crash button and enable crash reporting Adds upload-symbols run script phase Closes #525 Add a custom build environment variable "UPLOAD_CRASHLYTICS_SYMBOLS" that will let Xcode skip the upload_symbols script for debug builds Fix Initialization tests * bump build
2023-02-15 13:18:18 -08:00
userOptedOutOfCrashReporting: true,
userDefaults: mockedUD
)
XCTAssertEqual(false, mockedStorage.isFiatConverted, "User Preferences: `isFiatConverted` default doesn't match.")
}
func testRecoveryPhraseTestCompleted_mocked() throws {
let mockedUD = UserDefaultsClient(
objectForKey: { _ in false },
remove: { _ in },
setValue: { _, _ in },
synchronize: { true }
)
let mockedStorage = UserPreferencesStorage(
appSessionFrom: 12345678.0,
convertedCurrency: "USD",
fiatConvertion: true,
recoveryPhraseTestCompleted: true,
sessionAutoshielded: false,
Add crash reporter to secant (#531) * [#525] Adds functions to configure, testCrash and check if it can start. This adds a build phase where a dummy file is added to the project to make the build and Plist copy happy. When building in the CI there will be a script to replace this Plist file with the real one that then will be copied to the bundle Crashlytics will be "off" by default and then be turned on when starting up to be an Opt-Out thing. This is the only way it can be turned off later. reference: https://firebase.google.com/docs/crashlytics/customize-crash-reports?platform=ios#enable_opt-in_reporting The app will start with crash reporting turned off and will set it up on by default on the application's code. Then if the user wants to opt-out of crash reporting, it can. Otherwise, it won't be possible. Adds opting out of crash reporting as a stored user preference. This adds a value inside UserPreferencesStorage and its live and mock counterparts. also creates a builer for `CrashReporterClient` that has a Dependency to `@Dependency(\.userStoredPreferences)` and sets the references for the client to set the appropriate values into the user storage `UserPreferencesStorage` as been adapted to be a TCA Dependency. `SettingsStore` now as a `Toogle()` to turn off and on crash reporting. But it doesn't work yet because I haven't found out how to make a TCA Binding that can rely on an initial value that is not hardcoded but injected from somewhere else. See https://www.pointfree.co/episodes/ep158-safer-conciser-forms-part-1 https://www.pointfree.co/episodes/ep158-safer-conciser-forms-part-2 Adds Test Crash button and enable crash reporting Adds upload-symbols run script phase Closes #525 Add a custom build environment variable "UPLOAD_CRASHLYTICS_SYMBOLS" that will let Xcode skip the upload_symbols script for debug builds Fix Initialization tests * bump build
2023-02-15 13:18:18 -08:00
userOptedOutOfCrashReporting: true,
userDefaults: mockedUD
)
XCTAssertEqual(false, mockedStorage.isRecoveryPhraseTestCompleted, "User Preferences: `isRecoveryPhraseTestCompleted` default doesn't match.")
}
func testSessionAutoshielded_mocked() throws {
let mockedUD = UserDefaultsClient(
objectForKey: { _ in true },
remove: { _ in },
setValue: { _, _ in },
synchronize: { true }
)
let mockedStorage = UserPreferencesStorage(
appSessionFrom: 12345678.0,
convertedCurrency: "USD",
fiatConvertion: true,
recoveryPhraseTestCompleted: true,
sessionAutoshielded: false,
Add crash reporter to secant (#531) * [#525] Adds functions to configure, testCrash and check if it can start. This adds a build phase where a dummy file is added to the project to make the build and Plist copy happy. When building in the CI there will be a script to replace this Plist file with the real one that then will be copied to the bundle Crashlytics will be "off" by default and then be turned on when starting up to be an Opt-Out thing. This is the only way it can be turned off later. reference: https://firebase.google.com/docs/crashlytics/customize-crash-reports?platform=ios#enable_opt-in_reporting The app will start with crash reporting turned off and will set it up on by default on the application's code. Then if the user wants to opt-out of crash reporting, it can. Otherwise, it won't be possible. Adds opting out of crash reporting as a stored user preference. This adds a value inside UserPreferencesStorage and its live and mock counterparts. also creates a builer for `CrashReporterClient` that has a Dependency to `@Dependency(\.userStoredPreferences)` and sets the references for the client to set the appropriate values into the user storage `UserPreferencesStorage` as been adapted to be a TCA Dependency. `SettingsStore` now as a `Toogle()` to turn off and on crash reporting. But it doesn't work yet because I haven't found out how to make a TCA Binding that can rely on an initial value that is not hardcoded but injected from somewhere else. See https://www.pointfree.co/episodes/ep158-safer-conciser-forms-part-1 https://www.pointfree.co/episodes/ep158-safer-conciser-forms-part-2 Adds Test Crash button and enable crash reporting Adds upload-symbols run script phase Closes #525 Add a custom build environment variable "UPLOAD_CRASHLYTICS_SYMBOLS" that will let Xcode skip the upload_symbols script for debug builds Fix Initialization tests * bump build
2023-02-15 13:18:18 -08:00
userOptedOutOfCrashReporting: true,
userDefaults: mockedUD
)
XCTAssertEqual(true, mockedStorage.isSessionAutoshielded, "User Preferences: `isSessionAutoshielded` default doesn't match.")
}
// MARK: - Remove all keys from the live UD environment
func testRemoveAll() async throws {
guard let userDefaults = UserDefaults.init(suiteName: "test") else {
XCTFail("User Preferences: UserDefaults.init(suiteName: \"test\") failed to initialize")
return
}
// fill in the data
UserPreferencesStorage.Constants.allCases.forEach {
userDefaults.set("anyValue", forKey: $0.rawValue)
}
// remove it
await storage?.removeAll()
// check the presence
UserPreferencesStorage.Constants.allCases.forEach {
XCTAssertNil(
userDefaults.object(forKey: $0.rawValue),
"User Preferences: key \($0.rawValue) should be removed but it's still present in User Defaults"
)
}
}
}