Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions WordPressKitTests/ReaderSiteServiceRemoteTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,12 @@ class ReaderSiteServiceRemoteTests: XCTestCase {
XCTAssertTrue(failure)
}

func testCheckSiteExistsAtURLSuccess() {
func testCheckSiteExistsAtURLSuccess() throws {
let testURLString = "http://www.wordpress.com"
let testURL = URL(string: testURLString)!
let stubPath = try XCTUnwrap(OHPathForFile("empty.json", type(of: self)))
stub(condition: {request in request.url?.absoluteString == testURLString}) { _ in
let stubPath = OHPathForFile("empty.json", type(of: self))
return fixture(filePath: stubPath!, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
return fixture(filePath: stubPath, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The header dictionary for content type JSON is repeated all over the place.

It could be DRYed in some form. I decided to keep it out because this PR is already noisy. Besides, the focus of this stream of work is to prepare for SPM. DRYing this definition would be useful, but doesn't bring us closer to that goal.

}

let expect = self.expectation(description: "One callback should be invoked")
Expand All @@ -308,12 +308,16 @@ class ReaderSiteServiceRemoteTests: XCTestCase {
self.waitForExpectations(timeout: 2, handler: nil)
}

func testCheckSiteExistsAtURLFailure() {
func testCheckSiteExistsAtURLFailure() throws {
let testURLString = "http://www.wordpress.com"
let testURL = URL(string: testURLString)!
let stubPath = try XCTUnwrap(OHPathForFile("empty.json", type(of: self)))
stub(condition: {request in request.url?.absoluteString == testURLString}) { _ in
let stubPath = OHPathForFile("empty.json", type(of: self))
return fixture(filePath: stubPath!, status: 400, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
return fixture(
filePath: stubPath,
status: 400,
headers: ["Content-Type" as NSObject: "application/json" as AnyObject]
)
}

let expect = self.expectation(description: "One callback should be invoked")
Expand Down
29 changes: 23 additions & 6 deletions WordPressKitTests/RemoteTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,20 @@ extension RemoteTestCase {
contentType: ResponseContentType,
status: Int32 = 200
) {
// There are hundreds of usages of the various `stubRemoteResponse` overloads.
// The pattern here should be to XCTUnwrap and throw.
// In the interest of moving along with the work, let's fail the tests at this level if the file is not found.
guard let stubPath = OHPathForFile(filename, type(of: self)) else {
return XCTFail("Could not find file at path '\(filename)'.")
}

stub(condition: condition) { _ in
let stubPath = OHPathForFile(filename, type(of: self))
var headers: [NSObject: AnyObject]?

if contentType != .NoContentType {
headers = ["Content-Type" as NSObject: contentType.rawValue as AnyObject]
}
return OHHTTPStubs.fixture(filePath: stubPath!, status: status, headers: headers)
return OHHTTPStubs.fixture(filePath: stubPath, status: status, headers: headers)
}
}

Expand All @@ -74,16 +80,22 @@ extension RemoteTestCase {
/// - status: The status code to use for the response. Defaults to 200.
///
func stubRemoteResponse(_ endpoint: String, filename: String, contentType: ResponseContentType, status: Int32 = 200) {
// There are hundreds of usages of the various `stubRemoteResponse` overloads.
// The pattern here should be to XCTUnwrap and throw.
// In the interest of moving along with the work, let's fail the tests at this level if the file is not found.
guard let stubPath = OHPathForFile(filename, type(of: self)) else {
return XCTFail("Could not find file at path '\(filename)'.")
}

stub(condition: { request in
return request.url?.absoluteString.range(of: endpoint) != nil
}) { _ in
let stubPath = OHPathForFile(filename, type(of: self))
var headers: [NSObject: AnyObject]?

if contentType != .NoContentType {
headers = ["Content-Type" as NSObject: contentType.rawValue as AnyObject]
}
return fixture(filePath: stubPath!, status: status, headers: headers)
return fixture(filePath: stubPath, status: status, headers: headers)
}
}

Expand Down Expand Up @@ -134,15 +146,20 @@ extension RemoteTestCase {
return HTTPStubsResponse(error: notConnectedError)
}

let stubPath = OHPathForFile(files[callCounter], type(of: self))
guard let stubPath = OHPathForFile(files[callCounter], type(of: self)) else {
XCTFail("Could not find file at path '\(files[callCounter])'.")
let error = NSError(domain: "RemoteTestCase", code: 0, userInfo: nil)
return HTTPStubsResponse(error: error)
}
Comment on lines +149 to +153
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to fail in this way, also returning an error because we're inside a closure and the file path is computed based on the callCounter value.


callCounter += 1

var headers: [NSObject: AnyObject]?
if contentType != .NoContentType {
headers = ["Content-Type" as NSObject: contentType.rawValue as AnyObject]
}

return fixture(filePath: stubPath!, status: status, headers: headers)
return fixture(filePath: stubPath, status: status, headers: headers)
}
}

Expand Down
124 changes: 79 additions & 45 deletions WordPressKitTests/WordPressComOAuthClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ class WordPressComOAuthClientTests: XCTestCase {
}
}

func testAuthenticateUsernameNo2FASuccessCase() {
func testAuthenticateUsernameNo2FASuccessCase() throws {
let stubPath = try XCTUnwrap(OHPathForFile("WordPressComOAuthSuccess.json", type(of: self)))
stub(condition: isOauthTokenRequest(url: .oAuthTokenUrl)) { _ in
let stubPath = OHPathForFile("WordPressComOAuthSuccess.json", type(of: self))
return fixture(filePath: stubPath!, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
return fixture(filePath: stubPath, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
}

let expect = expectation(description: "One callback should be invoked")
Expand All @@ -52,10 +52,10 @@ class WordPressComOAuthClientTests: XCTestCase {
waitForExpectations(timeout: 2, handler: nil)
}

func testAuthenticateUsernameNo2FASuccessCase_withMFAClosure() {
func testAuthenticateUsernameNo2FASuccessCase_withMFAClosure() throws {
let stubPath = try XCTUnwrap(OHPathForFile("WordPressComOAuthSuccess.json", type(of: self)))
stub(condition: isOauthTokenRequest(url: .oAuthTokenUrl)) { _ in
let stubPath = OHPathForFile("WordPressComOAuthSuccess.json", type(of: self))
return fixture(filePath: stubPath!, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
return fixture(filePath: stubPath, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
}

let expect = expectation(description: "One callback should be invoked")
Expand All @@ -81,10 +81,12 @@ class WordPressComOAuthClientTests: XCTestCase {
waitForExpectations(timeout: 2, handler: nil)
}

func testAuthenticateUsernameNo2FAFailureWrongPasswordCase() {
func testAuthenticateUsernameNo2FAFailureWrongPasswordCase() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComOAuthWrongPasswordFail.json", type(of: self))
)
stub(condition: isOauthTokenRequest(url: .oAuthTokenUrl)) { _ in
let stubPath = OHPathForFile("WordPressComOAuthWrongPasswordFail.json", type(of: self))
return fixture(filePath: stubPath!, status: 400, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
return fixture(filePath: stubPath, status: 400, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
}

let expect = expectation(description: "One callback should be invoked")
Expand All @@ -106,10 +108,12 @@ class WordPressComOAuthClientTests: XCTestCase {
waitForExpectations(timeout: 2, handler: nil)
}

func testAuthenticateUsernameNo2FAFailureWrongPasswordCase_withMFAClosure() {
func testAuthenticateUsernameNo2FAFailureWrongPasswordCase_withMFAClosure() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComOAuthWrongPasswordFail.json", type(of: self))
)
stub(condition: isOauthTokenRequest(url: .oAuthTokenUrl)) { _ in
let stubPath = OHPathForFile("WordPressComOAuthWrongPasswordFail.json", type(of: self))
return fixture(filePath: stubPath!, status: 400, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
return fixture(filePath: stubPath, status: 400, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
}

let expect = expectation(description: "One callback should be invoked")
Expand All @@ -134,10 +138,16 @@ class WordPressComOAuthClientTests: XCTestCase {
waitForExpectations(timeout: 2, handler: nil)
}

func testAuthenticateUsername2FAWrong2FACase() {
func testAuthenticateUsername2FAWrong2FACase() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComOAuthNeeds2FAFail.json", type(of: self))
)
stub(condition: isOauthTokenRequest(url: .oAuthTokenUrl)) { _ in
let stubPath = OHPathForFile("WordPressComOAuthNeeds2FAFail.json", type(of: self))
return fixture(filePath: stubPath!, status: 400, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
return fixture(
filePath: stubPath,
status: 400,
headers: ["Content-Type" as NSObject: "application/json" as AnyObject]
)
}

let expect = expectation(description: "Call should complete")
Expand Down Expand Up @@ -179,10 +189,16 @@ class WordPressComOAuthClientTests: XCTestCase {
waitForExpectations(timeout: 2, handler: nil)
}

func testAuthenticateUsername2FAWrong2FACase_withMFAClosure() {
func testAuthenticateUsername2FAWrong2FACase_withMFAClosure() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComOAuthNeeds2FAFail.json", type(of: self))
)
stub(condition: isOauthTokenRequest(url: .oAuthTokenUrl)) { _ in
let stubPath = OHPathForFile("WordPressComOAuthNeeds2FAFail.json", type(of: self))
return fixture(filePath: stubPath!, status: 400, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
return fixture(
filePath: stubPath,
status: 400,
headers: ["Content-Type" as NSObject: "application/json" as AnyObject]
)
}

let expect = expectation(description: "Call should complete")
Expand Down Expand Up @@ -227,10 +243,12 @@ class WordPressComOAuthClientTests: XCTestCase {
waitForExpectations(timeout: 2, handler: nil)
}

func testAuthenticateUsernameRequiresWebauthnMultifactorAuthentication() {
func testAuthenticateUsernameRequiresWebauthnMultifactorAuthentication() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComOAuthNeedsWebauthnMFA.json", type(of: self))
)
stub(condition: isOauthTokenRequest(url: .oAuthTokenUrl)) { _ in
let stubPath = OHPathForFile("WordPressComOAuthNeedsWebauthnMFA.json", type(of: self))
return fixture(filePath: stubPath!, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
return fixture(filePath: stubPath, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
}

let expect = expectation(description: "Call should complete")
Expand All @@ -256,10 +274,12 @@ class WordPressComOAuthClientTests: XCTestCase {
waitForExpectations(timeout: 2, handler: nil)
}

func testRequestOneTimeCodeWithUsername() {
func testRequestOneTimeCodeWithUsername() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComOAuthNeeds2FAFail.json", type(of: self))
)
stub(condition: isOauthTokenRequest(url: .oAuthTokenUrl)) { _ in
let stubPath = OHPathForFile("WordPressComOAuthNeeds2FAFail.json", type(of: self))
return fixture(filePath: stubPath!, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
return fixture(filePath: stubPath, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
}

let expect = expectation(description: "One callback should be invoked")
Expand All @@ -275,10 +295,12 @@ class WordPressComOAuthClientTests: XCTestCase {
waitForExpectations(timeout: 2, handler: nil)
}

func testRequestSocial2FACodeWithUserID() {
func testRequestSocial2FACodeWithUserID() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComSocial2FACodeSuccess.json", type(of: self))
)
stub(condition: isOauthTokenRequest(url: .socialLoginNewSMS2FA)) { _ in
let stubPath = OHPathForFile("WordPressComSocial2FACodeSuccess.json", type(of: self))
return fixture(filePath: stubPath!, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
return fixture(filePath: stubPath, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
}

let expect = expectation(description: "One callback should be invoked")
Expand All @@ -296,10 +318,12 @@ class WordPressComOAuthClientTests: XCTestCase {
waitForExpectations(timeout: 2, handler: nil)
}

func testAuthenticateWithIDToken() {
func testAuthenticateWithIDToken() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComAuthenticateWithIDTokenBearerTokenSuccess.json", type(of: self))
)
stub(condition: isOauthTokenRequest(url: .socialLogin)) { _ in
let stubPath = OHPathForFile("WordPressComAuthenticateWithIDTokenBearerTokenSuccess.json", type(of: self))
return fixture(filePath: stubPath!, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
return fixture(filePath: stubPath, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
}

let expect = expectation(description: "One callback should be invoked")
Expand Down Expand Up @@ -329,10 +353,12 @@ class WordPressComOAuthClientTests: XCTestCase {
waitForExpectations(timeout: 2, handler: nil)
}

func testAuthenticateWithIDToken2FANeeded() {
func testAuthenticateWithIDToken2FANeeded() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComAuthenticateWithIDToken2FANeededSuccess.json", type(of: self))
)
stub(condition: isOauthTokenRequest(url: .socialLogin)) { _ in
let stubPath = OHPathForFile("WordPressComAuthenticateWithIDToken2FANeededSuccess.json", type(of: self))
return fixture(filePath: stubPath!, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
return fixture(filePath: stubPath, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
}

let expect = expectation(description: "One callback should be invoked")
Expand Down Expand Up @@ -363,10 +389,12 @@ class WordPressComOAuthClientTests: XCTestCase {
waitForExpectations(timeout: 2, handler: nil)
}

func testAuthenticateWithIDTokenUserNeedsConnection() {
func testAuthenticateWithIDTokenUserNeedsConnection() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComAuthenticateWithIDTokenExistingUserNeedsConnection.json", type(of: self))
)
stub(condition: isOauthTokenRequest(url: .socialLogin)) { _ in
let stubPath = OHPathForFile("WordPressComAuthenticateWithIDTokenExistingUserNeedsConnection.json", type(of: self))
return fixture(filePath: stubPath!, status: 400, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
return fixture(filePath: stubPath, status: 400, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
}

let expect = expectation(description: "One callback should be invoked")
Expand Down Expand Up @@ -395,10 +423,12 @@ class WordPressComOAuthClientTests: XCTestCase {
waitForExpectations(timeout: 2, handler: nil)
}

func testAuthenticateSocialLoginUser() {
func testAuthenticateSocialLoginUser() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComAuthenticateWithIDTokenBearerTokenSuccess.json", type(of: self))
)
stub(condition: isOauthTokenRequest(url: .socialLogin2FA)) { _ in
let stubPath = OHPathForFile("WordPressComAuthenticateWithIDTokenBearerTokenSuccess.json", type(of: self))
return fixture(filePath: stubPath!, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
return fixture(filePath: stubPath, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
}

let expect = expectation(description: "One callback should be invoked")
Expand All @@ -416,10 +446,12 @@ class WordPressComOAuthClientTests: XCTestCase {
waitForExpectations(timeout: 2, handler: nil)
}

func testRequestWebauthnChallengeReturnsCompleteChallengeInfo() {
func testRequestWebauthnChallengeReturnsCompleteChallengeInfo() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComOAuthRequestChallenge.json", type(of: self))
)
stub(condition: isOauthTokenRequest(url: .requestWebauthnChallenge)) { _ in
let stubPath = OHPathForFile("WordPressComOAuthRequestChallenge.json", type(of: self))
return fixture(filePath: stubPath!, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
return fixture(filePath: stubPath, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
}

let expect = expectation(description: "One callback should be invoked")
Expand All @@ -437,10 +469,12 @@ class WordPressComOAuthClientTests: XCTestCase {
waitForExpectations(timeout: 2, handler: nil)
}

func testAuthenticateWebauthSignatureReturnsOauthToken() {
func testAuthenticateWebauthSignatureReturnsOauthToken() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComOAuthAuthenticateSignature.json", type(of: self))
)
stub(condition: isOauthTokenRequest(url: .verifySignature)) { _ in
let stubPath = OHPathForFile("WordPressComOAuthAuthenticateSignature.json", type(of: self))
return fixture(filePath: stubPath!, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
return fixture(filePath: stubPath, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
}

let expect = expectation(description: "One callback should be invoked")
Expand Down
Loading