Merge pull request #1086 from kielgillard/master

Make Account framework tests compile and pass.
This commit is contained in:
Maurice Parker 2019-09-30 02:24:50 -05:00 committed by GitHub
commit 2756da00e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 28 deletions

View File

@ -26,12 +26,12 @@ class AccountCredentialsTest: XCTestCase {
// Make sure any left over from failed tests are gone // Make sure any left over from failed tests are gone
do { do {
try account.removeBasicCredentials() try account.removeCredentials(type: .basic)
} catch { } catch {
XCTFail(error.localizedDescription) XCTFail(error.localizedDescription)
} }
var credentials: Credentials? = Credentials.basic(username: "maurice", password: "hardpasswd") var credentials: Credentials? = Credentials(type: .basic, username: "maurice", secret: "hardpasswd")
// Store the credentials // Store the credentials
do { do {
@ -43,19 +43,21 @@ class AccountCredentialsTest: XCTestCase {
// Retrieve them // Retrieve them
credentials = nil credentials = nil
do { do {
credentials = try account.retrieveBasicCredentials() credentials = try account.retrieveCredentials(type: .basic)
} catch { } catch {
XCTFail(error.localizedDescription) XCTFail(error.localizedDescription)
} }
switch credentials! { switch credentials!.type {
case .basic(let username, let password): case .basic:
XCTAssertEqual("maurice", username) XCTAssertEqual("maurice", credentials?.username)
XCTAssertEqual("hardpasswd", password) XCTAssertEqual("hardpasswd", credentials?.secret)
default:
XCTFail("Expected \(CredentialsType.basic), received \(credentials!.type)")
} }
// Update them // Update them
credentials = Credentials.basic(username: "maurice", password: "easypasswd") credentials = Credentials(type: .basic, username: "maurice", secret: "easypasswd")
do { do {
try account.storeCredentials(credentials!) try account.storeCredentials(credentials!)
} catch { } catch {
@ -65,27 +67,29 @@ class AccountCredentialsTest: XCTestCase {
// Retrieve them again // Retrieve them again
credentials = nil credentials = nil
do { do {
credentials = try account.retrieveBasicCredentials() credentials = try account.retrieveCredentials(type: .basic)
} catch { } catch {
XCTFail(error.localizedDescription) XCTFail(error.localizedDescription)
} }
switch credentials! { switch credentials!.type {
case .basic(let username, let password): case .basic:
XCTAssertEqual("maurice", username) XCTAssertEqual("maurice", credentials?.username)
XCTAssertEqual("easypasswd", password) XCTAssertEqual("easypasswd", credentials?.secret)
default:
XCTFail("Expected \(CredentialsType.basic), received \(credentials!.type)")
} }
// Delete them // Delete them
do { do {
try account.removeBasicCredentials() try account.removeCredentials(type: .basic)
} catch { } catch {
XCTFail(error.localizedDescription) XCTFail(error.localizedDescription)
} }
// Make sure they are gone // Make sure they are gone
do { do {
try credentials = account.retrieveBasicCredentials() try credentials = account.retrieveCredentials(type: .basic)
} catch { } catch {
XCTFail(error.localizedDescription) XCTFail(error.localizedDescription)
} }

View File

@ -27,7 +27,7 @@ class AccountFeedSyncTest: XCTestCase {
// Test initial folders // Test initial folders
let initialExpection = self.expectation(description: "Initial feeds") let initialExpection = self.expectation(description: "Initial feeds")
account.refreshAll() { account.refreshAll() { _ in
initialExpection.fulfill() initialExpection.fulfill()
} }
waitForExpectations(timeout: 5, handler: nil) waitForExpectations(timeout: 5, handler: nil)
@ -44,7 +44,7 @@ class AccountFeedSyncTest: XCTestCase {
testTransport.testFiles["https://api.feedbin.com/v2/subscriptions.json"] = "subscriptions_add.json" testTransport.testFiles["https://api.feedbin.com/v2/subscriptions.json"] = "subscriptions_add.json"
let addExpection = self.expectation(description: "Add feeds") let addExpection = self.expectation(description: "Add feeds")
account.refreshAll() { account.refreshAll() { _ in
addExpection.fulfill() addExpection.fulfill()
} }
waitForExpectations(timeout: 5, handler: nil) waitForExpectations(timeout: 5, handler: nil)

View File

@ -28,7 +28,7 @@ class AccountFolderContentsSyncTest: XCTestCase {
// Test initial folders // Test initial folders
let initialExpection = self.expectation(description: "Initial contents") let initialExpection = self.expectation(description: "Initial contents")
account.refreshAll() { account.refreshAll() { _ in
initialExpection.fulfill() initialExpection.fulfill()
} }
waitForExpectations(timeout: 5, handler: nil) waitForExpectations(timeout: 5, handler: nil)
@ -41,7 +41,7 @@ class AccountFolderContentsSyncTest: XCTestCase {
testTransport.testFiles["https://api.feedbin.com/v2/taggings.json"] = "taggings_add.json" testTransport.testFiles["https://api.feedbin.com/v2/taggings.json"] = "taggings_add.json"
let addExpection = self.expectation(description: "Add contents") let addExpection = self.expectation(description: "Add contents")
account.refreshAll() { account.refreshAll() { _ in
addExpection.fulfill() addExpection.fulfill()
} }
waitForExpectations(timeout: 5, handler: nil) waitForExpectations(timeout: 5, handler: nil)
@ -53,7 +53,7 @@ class AccountFolderContentsSyncTest: XCTestCase {
testTransport.testFiles["https://api.feedbin.com/v2/taggings.json"] = "taggings_delete.json" testTransport.testFiles["https://api.feedbin.com/v2/taggings.json"] = "taggings_delete.json"
let deleteExpection = self.expectation(description: "Delete contents") let deleteExpection = self.expectation(description: "Delete contents")
account.refreshAll() { account.refreshAll() { _ in
deleteExpection.fulfill() deleteExpection.fulfill()
} }
waitForExpectations(timeout: 5, handler: nil) waitForExpectations(timeout: 5, handler: nil)

View File

@ -25,7 +25,7 @@ class AccountFolderSyncTest: XCTestCase {
// Test initial folders // Test initial folders
let initialExpection = self.expectation(description: "Initial tags") let initialExpection = self.expectation(description: "Initial tags")
account.refreshAll() { account.refreshAll() { _ in
initialExpection.fulfill() initialExpection.fulfill()
} }
waitForExpectations(timeout: 5, handler: nil) waitForExpectations(timeout: 5, handler: nil)
@ -43,7 +43,7 @@ class AccountFolderSyncTest: XCTestCase {
testTransport.testFiles["https://api.feedbin.com/v2/tags.json"] = "tags_delete.json" testTransport.testFiles["https://api.feedbin.com/v2/tags.json"] = "tags_delete.json"
let deleteExpection = self.expectation(description: "Delete tags") let deleteExpection = self.expectation(description: "Delete tags")
account.refreshAll() { account.refreshAll() { _ in
deleteExpection.fulfill() deleteExpection.fulfill()
} }
waitForExpectations(timeout: 5, handler: nil) waitForExpectations(timeout: 5, handler: nil)
@ -62,7 +62,7 @@ class AccountFolderSyncTest: XCTestCase {
testTransport.testFiles["https://api.feedbin.com/v2/tags.json"] = "tags_add.json" testTransport.testFiles["https://api.feedbin.com/v2/tags.json"] = "tags_add.json"
let addExpection = self.expectation(description: "Add tags") let addExpection = self.expectation(description: "Add tags")
account.refreshAll() { account.refreshAll() { _ in
addExpection.fulfill() addExpection.fulfill()
} }
waitForExpectations(timeout: 5, handler: nil) waitForExpectations(timeout: 5, handler: nil)

View File

@ -16,30 +16,43 @@ final class TestTransport: Transport {
} }
var testFiles = [String: String]() var testFiles = [String: String]()
var testStatusCodes = [String: Int]()
func send(request: URLRequest, completion: @escaping (Result<(HTTPHeaders, Data?), Error>) -> Void) { private func httpResponse(for request: URLRequest, statusCode: Int = 200) -> HTTPURLResponse {
guard let url = request.url else {
fatalError("Attempting to mock a http response for a request without a URL \(request).")
}
return HTTPURLResponse(url: url, statusCode: statusCode, httpVersion: "HTTP/1.1", headerFields: nil)!
}
func send(request: URLRequest, completion: @escaping (Result<(HTTPURLResponse, Data?), Error>) -> Void) {
guard let urlString = request.url?.absoluteString else { guard let urlString = request.url?.absoluteString else {
completion(.failure(TestTransportError.invalidState)) completion(.failure(TestTransportError.invalidState))
return return
} }
let response = httpResponse(for: request, statusCode: testStatusCodes[urlString] ?? 200)
if let testFileName = testFiles[urlString] { if let testFileName = testFiles[urlString] {
let testFileURL = Bundle(for: TestTransport.self).resourceURL!.appendingPathComponent(testFileName) let testFileURL = Bundle(for: TestTransport.self).resourceURL!.appendingPathComponent(testFileName)
let data = try! Data(contentsOf: testFileURL) let data = try! Data(contentsOf: testFileURL)
DispatchQueue.global(qos: .background).async { DispatchQueue.global(qos: .background).async {
completion(.success((HTTPHeaders(), data))) completion(.success((response, data)))
} }
} else { } else {
DispatchQueue.global(qos: .background).async { DispatchQueue.global(qos: .background).async {
completion(.success((HTTPHeaders(), nil))) completion(.success((response, nil)))
} }
} }
} }
func send(request: URLRequest, payload: Data, completion: @escaping (Result<(HTTPHeaders, Data?), Error>) -> Void) { func send(request: URLRequest, method: String, completion: @escaping (Result<Void, Error>) -> Void) {
fatalError("Unimplemented.")
} }
func send(request: URLRequest, method: String, payload: Data, completion: @escaping (Result<(HTTPURLResponse, Data?), Error>) -> Void) {
fatalError("Unimplemented.")
}
} }