Skip to content

Commit

Permalink
Move remaining tests to CoreAPITests/
Browse files Browse the repository at this point in the history
  • Loading branch information
mokagio committed Apr 8, 2024
1 parent dc583ad commit 111b82d
Show file tree
Hide file tree
Showing 26 changed files with 242 additions and 199 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ let package = Package(
],
path: "Tests/CoreAPITests",
resources: [
// .process("Stubs") // Relative to path
.process("Stubs") // Relative to path
]
),
]
Expand Down
30 changes: 30 additions & 0 deletions Sources/CoreAPI/WordPressComRestApiErrorDomain.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Foundation

/// Error domain of `NSError` instances that are converted from `WordPressComRestApiEndpointError`
/// and `WordPressAPIError<WordPressComRestApiEndpointError>` instances.
///
/// See `extension WordPressComRestApiEndpointError: CustomNSError` for context.
let WordPressComRestApiErrorDomain = "WordPressKit.WordPressComRestApiError"

// WordPressComRestApiErrorDomain is accessible only in Swift and, since it's a global, cannot be made @objc.
// As a workaround, here is a builder to init NSError with that domain and a method to check the domain.
@objc
public extension NSError {

@objc
static func wordPressComRestApiError(
code: Int,
userInfo: [String: Any]?
) -> NSError {
NSError(
domain: WordPressComRestApiErrorDomain,
code: code,
userInfo: userInfo
)
}

@objc
func hasWordPressComRestApiErrorDomain() -> Bool {
domain == WordPressComRestApiErrorDomain
}
}
13 changes: 6 additions & 7 deletions Sources/WordPressKit/Services/MediaServiceRemoteREST.m
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ - (NSError *)processMediaUploadErrors:(NSArray *)errorList {
errorMessage = errorInfo[@"message"];
}
NSDictionary *errorDictionary = @{NSLocalizedDescriptionKey: errorMessage};
error = [NSError errorWithDomain:WordPressComRestApiErrorDomain code:WordPressComRestApiErrorCodeUploadFailed userInfo:errorDictionary];
error = [NSError wordPressComRestApiErrorWithCode:WordPressComRestApiErrorCodeUploadFailed
userInfo:errorDictionary];
}
return error;
}
Expand Down Expand Up @@ -296,9 +297,8 @@ - (void)deleteMedia:(RemoteMedia *)media
}
} else {
if (failure) {
NSError *error = [NSError errorWithDomain:WordPressComRestApiErrorDomain
code:WordPressComRestApiErrorCodeUnknown
userInfo:nil];
NSError *error = [NSError wordPressComRestApiErrorWithCode:WordPressComRestApiErrorCodeUnknown
userInfo:nil];
failure(error);
}
}
Expand Down Expand Up @@ -369,9 +369,8 @@ -(void)getVideoPressToken:(NSString *)videoPressID
}
} else {
if (failure) {
NSError *error = [NSError errorWithDomain:WordPressComRestApiErrorDomain
code:WordPressComRestApiErrorCodeUnknown
userInfo:nil];
NSError *error = [NSError wordPressComRestApiErrorWithCode:WordPressComRestApiErrorCodeUnknown
userInfo:nil];
failure(error);
}
}
Expand Down
9 changes: 4 additions & 5 deletions Sources/WordPressKit/Services/WordPressComServiceRemote.m
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,9 @@ - (void)createWPComBlogWithUrl:(NSString *)blogUrl
NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
userInfo[WordPressComRestApi.ErrorKeyErrorMessage] = localizedErrorMessage;
userInfo[NSLocalizedDescriptionKey] = localizedErrorMessage;
NSError *errorWithLocalizedMessage = [[NSError alloc] initWithDomain:WordPressComRestApiErrorDomain
code:WordPressComRestApiErrorCodeUnknown
userInfo:userInfo];

NSError *errorWithLocalizedMessage = [NSError wordPressComRestApiErrorWithCode:WordPressComRestApiErrorCodeUnknown
userInfo:userInfo];

failure(errorWithLocalizedMessage);
} else {
success(responseObject);
Expand Down Expand Up @@ -229,7 +228,7 @@ - (void)createWPComBlogWithUrl:(NSString *)blogUrl

- (NSError *)errorWithLocalizedMessage:(NSError *)error {
NSError *errorWithLocalizedMessage = error;
if ([error.domain isEqualToString:WordPressComRestApiErrorDomain] &&
if ([error hasWordPressComRestApiErrorDomain] &&
[error.userInfo objectForKey:WordPressComRestApi.ErrorKeyErrorCode] != nil) {

NSString *localizedErrorMessage = [self errorMessageForError:error];
Expand Down
7 changes: 0 additions & 7 deletions Sources/WordPressKit/Utility/Constants.h

This file was deleted.

7 changes: 0 additions & 7 deletions Sources/WordPressKit/Utility/Constants.m

This file was deleted.

43 changes: 0 additions & 43 deletions Sources/WordPressKit/Utility/HTTPProtocolHelpers.swift
Original file line number Diff line number Diff line change
@@ -1,48 +1,5 @@
import Foundation

extension String.Encoding {
/// See: https://www.iana.org/assignments/character-sets/character-sets.xhtml
init?(ianaCharsetName: String) {
let encoding: CFStringEncoding = CFStringConvertIANACharSetNameToEncoding(ianaCharsetName as CFString)
guard encoding != kCFStringEncodingInvalidId,
let builtInEncoding = CFStringBuiltInEncodings(rawValue: encoding)
else {
return nil
}

switch builtInEncoding {
case .macRoman:
self = .macOSRoman
case .windowsLatin1:
self = .windowsCP1252
case .isoLatin1:
self = .isoLatin1
case .nextStepLatin:
self = .nextstep
case .ASCII:
self = .ascii
case .unicode:
self = .unicode
case .UTF8:
self = .utf8
case .nonLossyASCII:
self = .nonLossyASCII
case .UTF16BE:
self = .utf16BigEndian
case .UTF16LE:
self = .utf16LittleEndian
case .UTF32:
self = .utf32
case .UTF32BE:
self = .utf32BigEndian
case .UTF32LE:
self = .utf32LittleEndian
@unknown default:
return nil
}
}
}

extension HTTPURLResponse {

/// Return parameter value in a header field.
Expand Down
1 change: 0 additions & 1 deletion Sources/WordPressKit/WordPressKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,3 @@ FOUNDATION_EXPORT const unsigned char WordPressKitVersionString[];
#import <WordPressKit/NSString+MD5.h>

#import <WordPressKit/WPKitLogging.h>
#import <WordPressKit/Constants.h>
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import XCTest
#if SWIFT_PACKAGE
@testable import CoreAPI
#else
@testable import WordPressKit
#endif

final class AppTransportSecuritySettingsTests: XCTestCase {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import Foundation
#if SWIFT_PACKAGE
@testable import CoreAPI
#else
@testable import WordPressKit
#endif

class FakeInfoDictionaryObjectProvider: InfoDictionaryObjectProvider {
private let appTransportSecurity: [String: Any]?
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Foundation
import XCTest
import wpxmlrpc

#if SWIFT_PACKAGE
@testable import CoreAPI
#else
@testable import WordPressKit
#endif
import wpxmlrpc
import XCTest

class HTTPRequestBuilderTests: XCTestCase {

Expand Down Expand Up @@ -413,7 +415,7 @@ class HTTPRequestBuilderTests: XCTestCase {
}

func testXMLRPCUpload() throws {
let file = try XCTUnwrap(Bundle(for: type(of: self)).url(forResource: "me-settings-success", withExtension: "json"))
let file = try XCTUnwrap(Bundle.coreAPITestsBundle.url(forResource: "me-settings-success", withExtension: "json"))
let fileContentBase64 = try Data(contentsOf: file).base64EncodedString()
let fileStream = try XCTUnwrap(InputStream(url: file))
let request = try HTTPRequestBuilder(url: URL(string: "https://w.org/xmlrpc.php")!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import Foundation
import Alamofire
import XCTest
import CryptoKit

#if SWIFT_PACKAGE
@testable import CoreAPI
#else
@testable import WordPressKit
#endif

class MutliparFormDataTests: XCTestCase {
struct Form: Codable {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import Foundation
import XCTest
import OHHTTPStubs

#if SWIFT_PACKAGE
@testable import CoreAPI
import OHHTTPStubsSwift
#else
@testable import WordPressKit
#endif

class NonceRetrievalTests: XCTestCase {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Foundation
import XCTest

#if SWIFT_PACKAGE
@testable import CoreAPI
#else
@testable import WordPressKit
#endif

class RSDParserTests: XCTestCase {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import Foundation
import XCTest
import OHHTTPStubs

#if SWIFT_PACKAGE
@testable import CoreAPI
import OHHTTPStubsSwift
#else
@testable import WordPressKit
#endif

class WordPressComOAuthClientTests: XCTestCase {

Expand All @@ -27,7 +31,7 @@ class WordPressComOAuthClientTests: XCTestCase {
}

func testAuthenticateUsernameNo2FASuccessCase() throws {
let stubPath = try XCTUnwrap(OHPathForFile("WordPressComOAuthSuccess.json", type(of: self)))
let stubPath = try XCTUnwrap(OHPathForFileInBundle("WordPressComOAuthSuccess.json", Bundle.coreAPITestsBundle))
stub(condition: isOauthTokenRequest(url: .oAuthTokenUrl)) { _ in
return fixture(filePath: stubPath, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
}
Expand All @@ -53,7 +57,7 @@ class WordPressComOAuthClientTests: XCTestCase {
}

func testAuthenticateUsernameNo2FASuccessCase_withMFAClosure() throws {
let stubPath = try XCTUnwrap(OHPathForFile("WordPressComOAuthSuccess.json", type(of: self)))
let stubPath = try XCTUnwrap(OHPathForFileInBundle("WordPressComOAuthSuccess.json", Bundle.coreAPITestsBundle))
stub(condition: isOauthTokenRequest(url: .oAuthTokenUrl)) { _ in
return fixture(filePath: stubPath, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
}
Expand Down Expand Up @@ -83,7 +87,7 @@ class WordPressComOAuthClientTests: XCTestCase {

func testAuthenticateUsernameNo2FAFailureWrongPasswordCase() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComOAuthWrongPasswordFail.json", type(of: self))
OHPathForFileInBundle("WordPressComOAuthWrongPasswordFail.json", Bundle.coreAPITestsBundle)
)
stub(condition: isOauthTokenRequest(url: .oAuthTokenUrl)) { _ in
return fixture(filePath: stubPath, status: 400, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
Expand All @@ -110,7 +114,7 @@ class WordPressComOAuthClientTests: XCTestCase {

func testAuthenticateUsernameNo2FAFailureWrongPasswordCase_withMFAClosure() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComOAuthWrongPasswordFail.json", type(of: self))
OHPathForFileInBundle("WordPressComOAuthWrongPasswordFail.json", Bundle.coreAPITestsBundle)
)
stub(condition: isOauthTokenRequest(url: .oAuthTokenUrl)) { _ in
return fixture(filePath: stubPath, status: 400, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
Expand Down Expand Up @@ -140,7 +144,7 @@ class WordPressComOAuthClientTests: XCTestCase {

func testAuthenticateUsername2FAWrong2FACase() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComOAuthNeeds2FAFail.json", type(of: self))
OHPathForFileInBundle("WordPressComOAuthNeeds2FAFail.json", Bundle.coreAPITestsBundle)
)
stub(condition: isOauthTokenRequest(url: .oAuthTokenUrl)) { _ in
return fixture(
Expand Down Expand Up @@ -191,7 +195,7 @@ class WordPressComOAuthClientTests: XCTestCase {

func testAuthenticateUsername2FAWrong2FACase_withMFAClosure() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComOAuthNeeds2FAFail.json", type(of: self))
OHPathForFileInBundle("WordPressComOAuthNeeds2FAFail.json", Bundle.coreAPITestsBundle)
)
stub(condition: isOauthTokenRequest(url: .oAuthTokenUrl)) { _ in
return fixture(
Expand Down Expand Up @@ -245,7 +249,7 @@ class WordPressComOAuthClientTests: XCTestCase {

func testAuthenticateUsernameRequiresWebauthnMultifactorAuthentication() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComOAuthNeedsWebauthnMFA.json", type(of: self))
OHPathForFileInBundle("WordPressComOAuthNeedsWebauthnMFA.json", Bundle.coreAPITestsBundle)
)
stub(condition: isOauthTokenRequest(url: .oAuthTokenUrl)) { _ in
return fixture(filePath: stubPath, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
Expand Down Expand Up @@ -276,7 +280,7 @@ class WordPressComOAuthClientTests: XCTestCase {

func testRequestOneTimeCodeWithUsername() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComOAuthNeeds2FAFail.json", type(of: self))
OHPathForFileInBundle("WordPressComOAuthNeeds2FAFail.json", Bundle.coreAPITestsBundle)
)
stub(condition: isOauthTokenRequest(url: .oAuthTokenUrl)) { _ in
return fixture(filePath: stubPath, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
Expand All @@ -297,7 +301,7 @@ class WordPressComOAuthClientTests: XCTestCase {

func testRequestSocial2FACodeWithUserID() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComSocial2FACodeSuccess.json", type(of: self))
OHPathForFileInBundle("WordPressComSocial2FACodeSuccess.json", Bundle.coreAPITestsBundle)
)
stub(condition: isOauthTokenRequest(url: .socialLoginNewSMS2FA)) { _ in
return fixture(filePath: stubPath, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
Expand All @@ -320,7 +324,7 @@ class WordPressComOAuthClientTests: XCTestCase {

func testAuthenticateWithIDToken() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComAuthenticateWithIDTokenBearerTokenSuccess.json", type(of: self))
OHPathForFileInBundle("WordPressComAuthenticateWithIDTokenBearerTokenSuccess.json", Bundle.coreAPITestsBundle)
)
stub(condition: isOauthTokenRequest(url: .socialLogin)) { _ in
return fixture(filePath: stubPath, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
Expand Down Expand Up @@ -355,7 +359,7 @@ class WordPressComOAuthClientTests: XCTestCase {

func testAuthenticateWithIDToken2FANeeded() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComAuthenticateWithIDToken2FANeededSuccess.json", type(of: self))
OHPathForFileInBundle("WordPressComAuthenticateWithIDToken2FANeededSuccess.json", Bundle.coreAPITestsBundle)
)
stub(condition: isOauthTokenRequest(url: .socialLogin)) { _ in
return fixture(filePath: stubPath, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
Expand Down Expand Up @@ -391,7 +395,7 @@ class WordPressComOAuthClientTests: XCTestCase {

func testAuthenticateWithIDTokenUserNeedsConnection() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComAuthenticateWithIDTokenExistingUserNeedsConnection.json", type(of: self))
OHPathForFileInBundle("WordPressComAuthenticateWithIDTokenExistingUserNeedsConnection.json", Bundle.coreAPITestsBundle)
)
stub(condition: isOauthTokenRequest(url: .socialLogin)) { _ in
return fixture(filePath: stubPath, status: 400, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
Expand Down Expand Up @@ -425,7 +429,7 @@ class WordPressComOAuthClientTests: XCTestCase {

func testAuthenticateSocialLoginUser() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComAuthenticateWithIDTokenBearerTokenSuccess.json", type(of: self))
OHPathForFileInBundle("WordPressComAuthenticateWithIDTokenBearerTokenSuccess.json", Bundle.coreAPITestsBundle)
)
stub(condition: isOauthTokenRequest(url: .socialLogin2FA)) { _ in
return fixture(filePath: stubPath, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
Expand All @@ -448,7 +452,7 @@ class WordPressComOAuthClientTests: XCTestCase {

func testRequestWebauthnChallengeReturnsCompleteChallengeInfo() throws {
let stubPath = try XCTUnwrap(
OHPathForFile("WordPressComOAuthRequestChallenge.json", type(of: self))
OHPathForFileInBundle("WordPressComOAuthRequestChallenge.json", Bundle.coreAPITestsBundle)
)
stub(condition: isOauthTokenRequest(url: .requestWebauthnChallenge)) { _ in
return fixture(filePath: stubPath, headers: ["Content-Type" as NSObject: "application/json" as AnyObject])
Expand All @@ -471,7 +475,7 @@ class WordPressComOAuthClientTests: XCTestCase {

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

0 comments on commit 111b82d

Please sign in to comment.