Merge branch 'feature/more-tests-pr' into feature/multi-key-support
This commit is contained in:
commit
f0c21dd880
43 changed files with 756 additions and 34 deletions
|
|
@ -1,5 +1,5 @@
|
|||
//
|
||||
// CoreDataStack.swift
|
||||
// PersistenceController.swift
|
||||
// passKit
|
||||
//
|
||||
// Created by Mingshen Sun on 12/28/24.
|
||||
|
|
@ -18,19 +18,19 @@ public class PersistenceController {
|
|||
|
||||
let container: NSPersistentContainer
|
||||
|
||||
init(isUnitTest: Bool = false) {
|
||||
init(storeURL: URL? = nil) {
|
||||
self.container = NSPersistentContainer(name: Self.modelName, managedObjectModel: .sharedModel)
|
||||
let description = container.persistentStoreDescriptions.first
|
||||
description?.shouldMigrateStoreAutomatically = false
|
||||
description?.shouldInferMappingModelAutomatically = false
|
||||
if isUnitTest {
|
||||
description?.url = URL(fileURLWithPath: "/dev/null")
|
||||
} else {
|
||||
description?.url = URL(fileURLWithPath: Globals.dbPath)
|
||||
}
|
||||
description?.url = storeURL ?? URL(fileURLWithPath: Globals.dbPath)
|
||||
setup()
|
||||
}
|
||||
|
||||
static func forUnitTests() -> PersistenceController {
|
||||
PersistenceController(storeURL: URL(fileURLWithPath: "/dev/null"))
|
||||
}
|
||||
|
||||
func setup() {
|
||||
container.loadPersistentStores { _, error in
|
||||
if error != nil {
|
||||
|
|
@ -34,6 +34,10 @@ public class PGPAgent {
|
|||
pgpInterface = nil
|
||||
}
|
||||
|
||||
public func isInitialized() -> Bool {
|
||||
pgpInterface != nil
|
||||
}
|
||||
|
||||
public func getKeyID() throws -> [String] {
|
||||
try checkAndInit()
|
||||
return pgpInterface?.keyID ?? []
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ public enum AppError: Error, Equatable {
|
|||
case keyImport
|
||||
case readingFile(fileName: String)
|
||||
case passwordDuplicated
|
||||
case cannotDeleteNonEmptyDirectory
|
||||
case gitReset
|
||||
case gitCommit
|
||||
case gitCreateSignature
|
||||
|
|
|
|||
|
|
@ -273,9 +273,15 @@ public class PasswordStore {
|
|||
}
|
||||
|
||||
public func delete(passwordEntity: PasswordEntity) throws {
|
||||
if !passwordEntity.children.isEmpty {
|
||||
throw AppError.cannotDeleteNonEmptyDirectory
|
||||
}
|
||||
|
||||
let deletedFileURL = passwordEntity.fileURL(in: storeURL)
|
||||
let deletedFilePath = passwordEntity.path
|
||||
try gitRm(path: passwordEntity.path)
|
||||
if !passwordEntity.isDir {
|
||||
try gitRm(path: passwordEntity.path)
|
||||
}
|
||||
try deletePasswordEntities(passwordEntity: passwordEntity)
|
||||
try deleteDirectoryTree(at: deletedFileURL)
|
||||
try gitCommit(message: "RemovePassword.".localize(deletedFilePath))
|
||||
|
|
@ -283,6 +289,11 @@ public class PasswordStore {
|
|||
}
|
||||
|
||||
public func edit(passwordEntity: PasswordEntity, password: Password, keyID: String? = nil) throws -> PasswordEntity? {
|
||||
guard !passwordEntity.isDir else {
|
||||
// caller should ensure this, so this is not a user-facing error
|
||||
throw AppError.other(message: "Cannot edit a directory")
|
||||
}
|
||||
|
||||
var newPasswordEntity: PasswordEntity? = passwordEntity
|
||||
let url = passwordEntity.fileURL(in: storeURL)
|
||||
|
||||
|
|
@ -320,11 +331,11 @@ public class PasswordStore {
|
|||
saveUpdatedContext()
|
||||
}
|
||||
|
||||
public func saveUpdatedContext() {
|
||||
private func saveUpdatedContext() {
|
||||
PersistenceController.shared.save()
|
||||
}
|
||||
|
||||
public func deleteCoreData() {
|
||||
private func deleteCoreData() {
|
||||
PasswordEntity.deleteAll(in: context)
|
||||
PersistenceController.shared.save()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue