Move 'keyHandler' in KeyFileManager from constructor to function

This commit is contained in:
Danny Moesch 2019-07-20 23:02:38 +02:00
parent 2ece3ef07a
commit 3eaf00f9fd
2 changed files with 6 additions and 8 deletions

View file

@ -15,19 +15,17 @@ public class KeyFileManager {
private let keyType: CryptographicKey private let keyType: CryptographicKey
private let keyPath: String private let keyPath: String
private let keyHandler: KeyHandler
private convenience init(keyType: CryptographicKey) { private convenience init(keyType: CryptographicKey) {
self.init(keyType: keyType, keyPath: keyType.getFileSharingPath()) self.init(keyType: keyType, keyPath: keyType.getFileSharingPath())
} }
public init(keyType: CryptographicKey, keyPath: String, keyHandler: @escaping KeyHandler = AppKeychain.add) { public init(keyType: CryptographicKey, keyPath: String) {
self.keyType = keyType self.keyType = keyType
self.keyPath = keyPath self.keyPath = keyPath
self.keyHandler = keyHandler
} }
public func importKeyAndDeleteFile() throws { public func importKeyAndDeleteFile(keyHandler: KeyHandler = AppKeychain.shared.add) throws {
guard let keyFileContent = FileManager.default.contents(atPath: keyPath) else { guard let keyFileContent = FileManager.default.contents(atPath: keyPath) else {
throw AppError.ReadingFile(URL(fileURLWithPath: keyPath).lastPathComponent) throw AppError.ReadingFile(URL(fileURLWithPath: keyPath).lastPathComponent)
} }

View file

@ -12,7 +12,7 @@ import XCTest
class KeyFileManagerTest: XCTestCase { class KeyFileManagerTest: XCTestCase {
private static let filePath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("test.txt").path private static let filePath = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("test.txt").path
private static let keyFileManager = KeyFileManager(keyType: PgpKey.PUBLIC, keyPath: filePath) { _, _ in } private static let keyFileManager = KeyFileManager(keyType: PgpKey.PUBLIC, keyPath: filePath)
override func tearDown() { override func tearDown() {
try? FileManager.default.removeItem(atPath: KeyFileManagerTest.filePath) try? FileManager.default.removeItem(atPath: KeyFileManagerTest.filePath)
@ -22,17 +22,17 @@ class KeyFileManagerTest: XCTestCase {
func testImportKeyAndDeleteFile() throws { func testImportKeyAndDeleteFile() throws {
let fileContent = "content".data(using: .ascii) let fileContent = "content".data(using: .ascii)
var storage: [String: Data] = [:] var storage: [String: Data] = [:]
let keyFileManager = KeyFileManager(keyType: PgpKey.PRIVATE, keyPath: KeyFileManagerTest.filePath) { storage[$1] = $0 } let keyFileManager = KeyFileManager(keyType: PgpKey.PRIVATE, keyPath: KeyFileManagerTest.filePath)
FileManager.default.createFile(atPath: KeyFileManagerTest.filePath, contents: fileContent, attributes: nil) FileManager.default.createFile(atPath: KeyFileManagerTest.filePath, contents: fileContent, attributes: nil)
try keyFileManager.importKeyAndDeleteFile() try keyFileManager.importKeyAndDeleteFile { storage[$1] = $0 }
XCTAssertFalse(FileManager.default.fileExists(atPath: KeyFileManagerTest.filePath)) XCTAssertFalse(FileManager.default.fileExists(atPath: KeyFileManagerTest.filePath))
XCTAssertTrue(storage[PgpKey.PRIVATE.getKeychainKey()] == fileContent) XCTAssertTrue(storage[PgpKey.PRIVATE.getKeychainKey()] == fileContent)
} }
func testErrorReadingFile() throws { func testErrorReadingFile() throws {
XCTAssertThrowsError(try KeyFileManagerTest.keyFileManager.importKeyAndDeleteFile()) { XCTAssertThrowsError(try KeyFileManagerTest.keyFileManager.importKeyAndDeleteFile { _, _ in }) {
XCTAssertEqual($0 as! AppError, AppError.ReadingFile("test.txt")) XCTAssertEqual($0 as! AppError, AppError.ReadingFile("test.txt"))
} }
} }