-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add WordPressKit unit tests #24776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add WordPressKit unit tests #24776
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a53f7e3
Copy the /Tests in the WordPressKit repo
crazytonyli 1765656
Copy the /WordPressKitTests in the WordPressKit repo
crazytonyli 4d4f783
Delete unneeded files
crazytonyli 9912685
Fix imports
crazytonyli 4b0cbdb
Fix code syntax issues
crazytonyli 21365b6
Remove error domain test
crazytonyli edb0a34
Mark unused variables as unused
crazytonyli 75370b3
Fix an invalid API doc
crazytonyli f0c0cad
Fix Sendable checks
crazytonyli 8319b5f
Add WordPressKitTests target to the Xcode project
crazytonyli d34e7c6
Add WordPressKitTests to the unit test test plan
crazytonyli cffdcaf
Fix swiftlint issues
crazytonyli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
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
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
103 changes: 103 additions & 0 deletions
103
Tests/WordPressKitTests/CoreAPITests/AppTransportSecuritySettingsTests.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import XCTest | ||
#if SWIFT_PACKAGE | ||
@testable import CoreAPI | ||
#else | ||
@testable import WordPressKit | ||
#endif | ||
|
||
final class AppTransportSecuritySettingsTests: XCTestCase { | ||
|
||
private var exampleURL = URL(string: "https://example.com")! | ||
|
||
func testReturnsTrueIfAllowsLocalNetworkingIsTrue() throws { | ||
// Given | ||
let provider = FakeInfoDictionaryObjectProvider(appTransportSecurity: [ | ||
"NSAllowsLocalNetworking": true, | ||
// This will be ignored | ||
"NSAllowsArbitraryLoads": true | ||
]) | ||
let appTransportSecurity = AppTransportSecuritySettings(provider) | ||
|
||
// When | ||
let secureAccessOnly = appTransportSecurity.secureAccessOnly(for: exampleURL) | ||
|
||
// Then | ||
XCTAssertTrue(secureAccessOnly) | ||
} | ||
|
||
func testReturnsFalseIfAllowsArbitraryLoadsIsTrue() throws { | ||
// Given | ||
let provider = FakeInfoDictionaryObjectProvider(appTransportSecurity: [ | ||
"NSAllowsArbitraryLoads": true | ||
]) | ||
let appTransportSecurity = AppTransportSecuritySettings(provider) | ||
|
||
// When | ||
let secureAccessOnly = appTransportSecurity.secureAccessOnly(for: exampleURL) | ||
|
||
// Then | ||
XCTAssertFalse(secureAccessOnly) | ||
} | ||
|
||
func testReturnsTrueByDefault() throws { | ||
// Given | ||
let provider = FakeInfoDictionaryObjectProvider(appTransportSecurity: nil) | ||
let appTransportSecurity = AppTransportSecuritySettings(provider) | ||
|
||
// When | ||
let secureAccessOnly = appTransportSecurity.secureAccessOnly(for: exampleURL) | ||
|
||
// Then | ||
XCTAssertTrue(secureAccessOnly) | ||
} | ||
|
||
func testReturnsTrueIfNothingIsDefined() throws { | ||
// Given | ||
let provider = FakeInfoDictionaryObjectProvider(appTransportSecurity: [String: Any]()) | ||
let appTransportSecurity = AppTransportSecuritySettings(provider) | ||
|
||
// When | ||
let secureAccessOnly = appTransportSecurity.secureAccessOnly(for: exampleURL) | ||
|
||
// Then | ||
XCTAssertTrue(secureAccessOnly) | ||
} | ||
|
||
func testReturnsFalseIfAllowsInsecureHTTPLoadsIsTrue() throws { | ||
// Given | ||
let provider = FakeInfoDictionaryObjectProvider(appTransportSecurity: [ | ||
"NSExceptionDomains": [ | ||
"shiki.me": [ | ||
"NSExceptionAllowsInsecureHTTPLoads": true | ||
] | ||
] | ||
]) | ||
let appTransportSecurity = AppTransportSecuritySettings(provider) | ||
let url = try XCTUnwrap(URL(string: "http://shiki.me")) | ||
|
||
// When | ||
let secureAccessOnly = appTransportSecurity.secureAccessOnly(for: url) | ||
|
||
// Then | ||
XCTAssertFalse(secureAccessOnly) | ||
} | ||
|
||
func testReturnsTrueIfAllowsInsecureHTTPLoadsIsNotProvided() throws { | ||
// Given | ||
let provider = FakeInfoDictionaryObjectProvider(appTransportSecurity: [ | ||
"NSExceptionDomains": [ | ||
"shiki.me": [String: Any]() | ||
], | ||
// This value will be ignored because there is an exception for shiki.me | ||
"NSAllowsArbitraryLoads": true | ||
]) | ||
let appTransportSecurity = AppTransportSecuritySettings(provider) | ||
let url = try XCTUnwrap(URL(string: "http://shiki.me")) | ||
|
||
// When | ||
let secureAccessOnly = appTransportSecurity.secureAccessOnly(for: url) | ||
|
||
// Then | ||
XCTAssertTrue(secureAccessOnly) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
Tests/WordPressKitTests/CoreAPITests/Bundle+SPMSupport.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import Foundation | ||
|
||
extension Bundle { | ||
/// Returns the `Bundle` for the target. | ||
/// | ||
/// If installed via CocoaPods, this will be `<target_name>.bundle`, otherwise it will be the framework bundle. | ||
@objc public class var coreAPITestsBundle: Bundle { | ||
#if SWIFT_PACKAGE | ||
return Bundle.module | ||
#else | ||
let defaultBundle = Bundle(for: BundleFinder.self) | ||
|
||
guard let bundleURL = defaultBundle.resourceURL, | ||
let resourceBundle = Bundle(url: bundleURL.appendingPathComponent("CoreAPITests.bundle")) else { | ||
return defaultBundle | ||
} | ||
|
||
return resourceBundle | ||
#endif | ||
} | ||
} | ||
|
||
#if !SWIFT_PACKAGE | ||
private class BundleFinder: NSObject {} | ||
#endif |
21 changes: 21 additions & 0 deletions
21
Tests/WordPressKitTests/CoreAPITests/FakeInfoDictionaryObjectProvider.swift
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#if SWIFT_PACKAGE | ||
@testable import CoreAPI | ||
#else | ||
@testable import WordPressKit | ||
#endif | ||
|
||
class FakeInfoDictionaryObjectProvider: InfoDictionaryObjectProvider { | ||
private let appTransportSecurity: [String: Any]? | ||
|
||
init(appTransportSecurity: [String: Any]?) { | ||
self.appTransportSecurity = appTransportSecurity | ||
} | ||
|
||
func object(forInfoDictionaryKey key: String) -> Any? { | ||
if key == "NSAppTransportSecurity" { | ||
return appTransportSecurity | ||
} | ||
|
||
return nil | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the new test target to the existing test plan file. I guess Xcode did not care about the order of the target which made the diff pretty difficult to review.