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
| func retrivePassword(for account: String) -> String? { | |
| let query: [String: Any] = [kSecClass as String: kSecClassGenericPassword, | |
| kSecAttrAccount as String: account, | |
| kSecMatchLimit as String: kSecMatchLimitOne, | |
| kSecReturnData as String: kCFBooleanTrue] | |
| var retrivedData: AnyObject? = nil | |
| let _ = SecItemCopyMatching(query as CFDictionary, &retrivedData) | |
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
| func testPaswordRetrive() { | |
| let password = "123456" | |
| let account = "User" | |
| keyChainService.save(password, for: account) | |
| XCTAssertEqual(keyChainService.retrivePassword(for: account), password) | |
| } |
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
| authManager.login(key, user) |
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
| func saveEncryptedPassword(_ password: String, for account: String) { | |
| let salt = Array("salty".utf8) | |
| let key = try! HKDF(password: Array(password.utf8), salt: salt, variant: .sha256).calculate().toHexString() | |
| keychainService.save(key, for: account) | |
| } |
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
| protocol CoreDataStack { | |
| var persistentContainer: NSPersistentContainer! {get} | |
| var managedObjectContext: NSManagedObjectContext! {get} | |
| func saveContext() | |
| } |
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
| class MainCoreDataStack: CoreDataStack { | |
| var persistentContainer: NSPersistentContainer! | |
| var managedObjectContext: NSManagedObjectContext! | |
| init() { | |
| persistentContainer = NSPersistentContainer(name: "MediumMockingProject") | |
| persistentContainer.loadPersistentStores(completionHandler: { (storeDescription, error) in | |
| if let error = error as NSError? { | |
| fatalError("Unresolved error \(error), \(error.userInfo)") | |
| } |
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
| class MockCoreDataStack: CoreDataStack { | |
| var managedObjectContext: NSManagedObjectContext! | |
| var persistentContainer: NSPersistentContainer! | |
| init() { | |
| let managedObjectModel = NSManagedObjectModel.mergedModel(from: [Bundle.main])! | |
| persistentContainer = NSPersistentContainer(name: "TestingContainer", managedObjectModel: managedObjectModel) | |
| let description = NSPersistentStoreDescription() | |
| description.type = NSInMemoryStoreType | |
| persistentContainer.persistentStoreDescriptions = [description] |
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
| class DatabaseService { | |
| var persistentContainer: NSPersistentContainer! | |
| var managedObjectContext: NSManagedObjectContext! | |
| init(stack: CoreDataStack = MainCoreDataStack()) { | |
| persistentContainer = stack.persistentContainer | |
| managedObjectContext = stack.managedObjectContext | |
| } | |
| func insertProject(id: String, name: String) -> Project { |
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
| var databaseService: DatabaseService! | |
| override func setUp() { | |
| super.setUp() | |
| databaseService = DatabaseService(stack: MockCoreDataStack()) | |
| } |
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
| func testProjecInsert() { | |
| let allItems = databaseService.getAllProjects() | |
| XCTAssertEqual(allItems.count, 0) | |
| XCTAssertNotNil(databaseService.insertProject(id: "A123", name: "First Project")) | |
| XCTAssertNotNil(databaseService.insertProject(id: "A123", name: "Second Project")) | |
| let itemsAfterInserts = databaseService.getAllProjects() | |
| XCTAssertEqual(itemsAfterInserts.count, 2) | |
| } |