change func signitures to use Credentials protocol from RSWeb instead of username/password

This commit is contained in:
Maurice Parker 2019-05-04 11:48:48 -05:00
parent 38202ece8d
commit 595db517a7
12 changed files with 133 additions and 16 deletions

View File

@ -241,17 +241,17 @@ public final class Account: DisplayNameProvider, UnreadCountProvider, Container,
// MARK: - API
public func storeCredentials(username: String, password: String) {
self.username = username
public func storeCredentials(_ credentials: Credentials) {
self.username = credentials.username
// self.password = password
}
public static func validateCredentials(transport: Transport = URLSession.webserviceTransport(), type: AccountType, username: String, password: String, completionHandler handler: @escaping (Result<Bool, Error>) -> Void) {
public static func validateCredentials(transport: Transport = URLSession.webserviceTransport(), type: AccountType, credentials: Credentials, completionHandler handler: @escaping (Result<Bool, Error>) -> Void) {
switch type {
case .onMyMac:
LocalAccountDelegate.validateCredentials(transport: transport, username: username, password: password, completionHandler: handler)
LocalAccountDelegate.validateCredentials(transport: transport, credentials: credentials, completionHandler: handler)
case .feedbin:
FeedbinAccountDelegate.validateCredentials(transport: transport, username: username, password: password, completionHandler: handler)
FeedbinAccountDelegate.validateCredentials(transport: transport, credentials: credentials, completionHandler: handler)
default:
break
}

View File

@ -7,6 +7,9 @@
objects = {
/* Begin PBXBuildFile section */
5107A099227DE42E00C7C3C5 /* AccountCredentialsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5107A098227DE42E00C7C3C5 /* AccountCredentialsTest.swift */; };
5107A09B227DE49500C7C3C5 /* TestAccountManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5107A09A227DE49500C7C3C5 /* TestAccountManager.swift */; };
5107A09D227DE77700C7C3C5 /* NilTransport.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5107A09C227DE77700C7C3C5 /* NilTransport.swift */; };
5144EA49227B497600D19003 /* FeedbinAPICaller.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5144EA48227B497600D19003 /* FeedbinAPICaller.swift */; };
5144EA4E227B829A00D19003 /* FeedbinAccountDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5144EA4D227B829A00D19003 /* FeedbinAccountDelegate.swift */; };
841973FE1F6DD1BC006346C4 /* RSCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 841973EF1F6DD19E006346C4 /* RSCore.framework */; };
@ -85,6 +88,9 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
5107A098227DE42E00C7C3C5 /* AccountCredentialsTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountCredentialsTest.swift; sourceTree = "<group>"; };
5107A09A227DE49500C7C3C5 /* TestAccountManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestAccountManager.swift; sourceTree = "<group>"; };
5107A09C227DE77700C7C3C5 /* NilTransport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NilTransport.swift; sourceTree = "<group>"; };
5144EA48227B497600D19003 /* FeedbinAPICaller.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedbinAPICaller.swift; sourceTree = "<group>"; };
5144EA4D227B829A00D19003 /* FeedbinAccountDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeedbinAccountDelegate.swift; sourceTree = "<group>"; };
841973E81F6DD19E006346C4 /* RSCore.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = RSCore.xcodeproj; path = ../RSCore/RSCore.xcodeproj; sourceTree = "<group>"; };
@ -244,8 +250,11 @@
848935031F62484F00CEBD24 /* AccountTests */ = {
isa = PBXGroup;
children = (
5107A09A227DE49500C7C3C5 /* TestAccountManager.swift */,
848935041F62485000CEBD24 /* AccountTests.swift */,
5107A098227DE42E00C7C3C5 /* AccountCredentialsTest.swift */,
848935061F62485000CEBD24 /* Info.plist */,
5107A09C227DE77700C7C3C5 /* NilTransport.swift */,
);
path = AccountTests;
sourceTree = "<group>";
@ -449,6 +458,9 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
5107A09B227DE49500C7C3C5 /* TestAccountManager.swift in Sources */,
5107A09D227DE77700C7C3C5 /* NilTransport.swift in Sources */,
5107A099227DE42E00C7C3C5 /* AccountCredentialsTest.swift in Sources */,
848935051F62485000CEBD24 /* AccountTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;

View File

@ -13,11 +13,12 @@ public protocol AccountDelegate {
// Local account does not; some synced accounts might.
var supportsSubFolders: Bool { get }
var server: String? { get }
static func validateCredentials(transport: Transport, credentials: Credentials, completionHandler handler: @escaping (Result<Bool, Error>) -> Void)
var refreshProgress: DownloadProgress { get }
static func validateCredentials(transport: Transport, username: String, password: String, completionHandler handler: @escaping (Result<Bool, Error>) -> Void)
func refreshAll(for: Account)
// Called at the end of accounts init method.

View File

@ -0,0 +1,28 @@
//
// AccountCredentialsTest.swift
// AccountTests
//
// Created by Maurice Parker on 5/4/19.
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
//
import XCTest
@testable import Account
class AccountCredentialsTest: XCTestCase {
private var account: Account!
override func setUp() {
account = TestAccountManager.shared.createAccount(type: .feedbin, transport: NilTransport())
}
override func tearDown() {
TestAccountManager.shared.deleteAccount(account)
}
func testExample() {
}
}

View File

@ -0,0 +1,18 @@
//
// NilTransport.swift
// AccountTests
//
// Created by Maurice Parker on 5/4/19.
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import RSWeb
struct NilTransport: Transport {
func send(request: URLRequest, completion: @escaping (Result<Data, Error>) -> Void) {
completion(.success(Data()))
}
}

View File

@ -0,0 +1,53 @@
//
// TestAccountManager.swift
// AccountTests
//
// Created by Maurice Parker on 5/4/19.
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
//
import Foundation
import RSWeb
@testable import Account
class TestAccountManager {
static let shared = TestAccountManager()
var accountsFolder: URL {
return FileManager.default.temporaryDirectory
}
func createAccount(type: AccountType, username: String? = nil, password: String? = nil, transport: Transport) -> Account {
let accountID = UUID().uuidString
let accountFolder = accountsFolder.appendingPathComponent("\(type.rawValue)_\(accountID)").absoluteString
do {
try FileManager.default.createDirectory(atPath: accountFolder, withIntermediateDirectories: true, attributes: nil)
} catch {
assertionFailure("Could not create folder for \(accountID) account.")
abort()
}
let account = Account(dataFolder: accountFolder, type: type, accountID: accountID, transport: transport)!
return account
}
func deleteAccount(_ account: Account) {
do {
try FileManager.default.removeItem(atPath: account.dataFolder)
}
catch {
assertionFailure("Could not create folder for OnMyMac account.")
abort()
}
}
}

View File

@ -19,10 +19,10 @@ final class FeedbinAPICaller: NSObject {
self.transport = transport
}
func validateCredentials(username: String, password: String, completionHandler handler: @escaping (Result<Bool, Error>) -> Void) {
func validateCredentials(credentials: Credentials, completionHandler handler: @escaping (Result<Bool, Error>) -> Void) {
let callURL = feedbinBaseURL.appendingPathComponent("authentication.json")
let request = URLRequest(url: callURL, username: username, password: password)
let request = URLRequest(url: callURL, credentials: credentials)
transport.send(request: request) { result in
switch result {

View File

@ -12,6 +12,7 @@ import RSWeb
final class FeedbinAccountDelegate: AccountDelegate {
let supportsSubFolders = false
let server: String? = "api.feedbin.com"
private let caller: FeedbinAPICaller
@ -21,10 +22,10 @@ final class FeedbinAccountDelegate: AccountDelegate {
var refreshProgress = DownloadProgress(numberOfTasks: 0)
static func validateCredentials(transport: Transport, username: String, password: String, completionHandler handler: @escaping (Result<Bool, Error>) -> Void) {
static func validateCredentials(transport: Transport, credentials: Credentials, completionHandler handler: @escaping (Result<Bool, Error>) -> Void) {
let caller = FeedbinAPICaller(transport: transport)
caller.validateCredentials(username: username, password: password) { result in
caller.validateCredentials(credentials: credentials) { result in
handler(result)
}

View File

@ -12,13 +12,15 @@ import RSWeb
final class LocalAccountDelegate: AccountDelegate {
let supportsSubFolders = false
let server: String? = nil
private let refresher = LocalAccountRefresher()
var refreshProgress: DownloadProgress {
return refresher.progress
}
static func validateCredentials(transport: Transport, username: String, password: String, completionHandler handler: (Result<Bool, Error>) -> Void) {
static func validateCredentials(transport: Transport, credentials: Credentials, completionHandler handler: (Result<Bool, Error>) -> Void) {
return handler(.success(false))
}

View File

@ -8,6 +8,7 @@
import AppKit
import Account
import RSWeb
class AccountsAddFeedbinWindowController: NSWindowController, NSTextFieldDelegate {
@ -52,7 +53,8 @@ class AccountsAddFeedbinWindowController: NSWindowController, NSTextFieldDelegat
progressIndicator.isHidden = false
progressIndicator.startAnimation(self)
Account.validateCredentials(type: .feedbin, username: usernameTextField.stringValue, password: passwordTextField.stringValue) { [weak self] result in
let credentials = BasicCredentials(username: usernameTextField.stringValue, password: passwordTextField.stringValue)
Account.validateCredentials(type: .feedbin, credentials: credentials) { [weak self] result in
guard let self = self else { return }
@ -65,7 +67,7 @@ class AccountsAddFeedbinWindowController: NSWindowController, NSTextFieldDelegat
if authenticated {
let account = AccountManager.shared.createAccount(type: .feedbin)
account.storeCredentials(username: self.usernameTextField.stringValue, password: self.passwordTextField.stringValue)
account.storeCredentials(credentials)
self.hostWindow?.endSheet(self.window!, returnCode: NSApplication.ModalResponse.OK)
} else {

@ -1 +1 @@
Subproject commit 6c9cab0e15bc4299e84866783fd59268e72d1771
Subproject commit 4ea1f13bbdea2c07d52a7b66909f1e6ae5ba13bf

@ -1 +1 @@
Subproject commit 039427d62d8efdfc43d541518afbd46d0147967d
Subproject commit eae66d829ff8beaf4ce0694d768bd5cb242fbacd