Skip to content

Commit e5759b3

Browse files
authored
Add WordPressKit unit tests (#24776)
* Copy the /Tests in the WordPressKit repo * Copy the /WordPressKitTests in the WordPressKit repo * Delete unneeded files * Fix imports * Fix code syntax issues * Remove error domain test * Mark unused variables as unused * Fix an invalid API doc * Fix Sendable checks * Add WordPressKitTests target to the Xcode project * Add WordPressKitTests to the unit test test plan * Fix swiftlint issues
1 parent 887ca95 commit e5759b3

460 files changed

Lines changed: 60701 additions & 18 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Modules/Package.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ let package = Package(
4747
.package(url: "https://github.com/wordpress-mobile/FSInteractiveMap", from: "0.3.0"),
4848
.package(url: "https://github.com/wordpress-mobile/MediaEditor-iOS", branch: "task/spm-support"),
4949
.package(url: "https://github.com/wordpress-mobile/NSObject-SafeExpectations", from: "0.0.6"),
50+
.package(url: "https://github.com/wordpress-mobile/wpxmlrpc", from: "0.9.0"),
5051
.package(url: "https://github.com/wordpress-mobile/NSURL-IDN", revision: "b34794c9a3f32312e1593d4a3d120572afa0d010"),
5152
.package(
5253
url: "https://github.com/wordpress-mobile/WordPressKit-iOS",
@@ -221,6 +222,7 @@ enum XcodeSupport {
221222
.library(name: "XcodeTarget_App", targets: ["XcodeTarget_App"]),
222223
.library(name: "XcodeTarget_Keystone", targets: ["XcodeTarget_Keystone"]),
223224
.library(name: "XcodeTarget_WordPressTests", targets: ["XcodeTarget_WordPressTests"]),
225+
.library(name: "XcodeTarget_WordPressKitTests", targets: ["XcodeTarget_WordPressKitTests"]),
224226
.library(name: "XcodeTarget_WordPressData", targets: ["XcodeTarget_WordPressData"]),
225227
.library(name: "XcodeTarget_WordPressAuthentificator", targets: ["XcodeTarget_WordPressAuthentificator"]),
226228
.library(name: "XcodeTarget_WordPressAuthentificatorTests", targets: ["XcodeTarget_WordPressAuthentificatorTests"]),
@@ -350,6 +352,10 @@ enum XcodeSupport {
350352
.product(name: "WordPressAPI", package: "wordpress-rs"),
351353
.product(name: "WordPressKit", package: "WordPressKit-iOS"),
352354
]),
355+
.xcodeTarget("XcodeTarget_WordPressKitTests", dependencies: testDependencies + [
356+
"wpxmlrpc",
357+
.product(name: "WordPressKit", package: "WordPressKit-iOS"),
358+
]),
353359
.xcodeTarget("XcodeTarget_WordPressAuthentificator", dependencies: wordPresAuthentificatorDependencies),
354360
.xcodeTarget("XcodeTarget_WordPressAuthentificatorTests", dependencies: wordPresAuthentificatorDependencies + testDependencies),
355361
.xcodeTarget("XcodeTarget_ShareExtension", dependencies: shareAndDraftExtensionsDependencies),

Tests/KeystoneTests/WordPressUnitTests.xctestplan

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,8 @@
3434
{
3535
"target" : {
3636
"containerPath" : "container:..\/Modules",
37-
"identifier" : "AsyncImageKitTests",
38-
"name" : "AsyncImageKitTests"
39-
}
40-
},
41-
{
42-
"target" : {
43-
"containerPath" : "container:WordPress.xcodeproj",
44-
"identifier" : "4AD953BA2C21451700D0EEFA",
45-
"name" : "WordPressAuthenticatorTests"
37+
"identifier" : "WordPressFluxTests",
38+
"name" : "WordPressFluxTests"
4639
}
4740
},
4841
{
@@ -62,15 +55,15 @@
6255
{
6356
"target" : {
6457
"containerPath" : "container:..\/Modules",
65-
"identifier" : "WordPressUIUnitTests",
66-
"name" : "WordPressUIUnitTests"
58+
"identifier" : "WordPressSharedTests",
59+
"name" : "WordPressSharedTests"
6760
}
6861
},
6962
{
7063
"target" : {
71-
"containerPath" : "container:..\/Modules",
72-
"identifier" : "WordPressSharedTests",
73-
"name" : "WordPressSharedTests"
64+
"containerPath" : "container:WordPress.xcodeproj",
65+
"identifier" : "4A8280FC2E5FE9B60037E180",
66+
"name" : "WordPressKitTests"
7467
}
7568
},
7669
{
@@ -80,11 +73,18 @@
8073
"name" : "JetpackStatsWidgetsCoreTests"
8174
}
8275
},
76+
{
77+
"target" : {
78+
"containerPath" : "container:WordPress.xcodeproj",
79+
"identifier" : "4AD953BA2C21451700D0EEFA",
80+
"name" : "WordPressAuthenticatorTests"
81+
}
82+
},
8383
{
8484
"target" : {
8585
"containerPath" : "container:..\/Modules",
86-
"identifier" : "WordPressFluxTests",
87-
"name" : "WordPressFluxTests"
86+
"identifier" : "WordPressUIUnitTests",
87+
"name" : "WordPressUIUnitTests"
8888
}
8989
},
9090
{
@@ -93,6 +93,13 @@
9393
"identifier" : "WordPressSharedObjCTests",
9494
"name" : "WordPressSharedObjCTests"
9595
}
96+
},
97+
{
98+
"target" : {
99+
"containerPath" : "container:..\/Modules",
100+
"identifier" : "AsyncImageKitTests",
101+
"name" : "AsyncImageKitTests"
102+
}
96103
}
97104
],
98105
"version" : 1
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import XCTest
2+
#if SWIFT_PACKAGE
3+
@testable import CoreAPI
4+
#else
5+
@testable import WordPressKit
6+
#endif
7+
8+
final class AppTransportSecuritySettingsTests: XCTestCase {
9+
10+
private var exampleURL = URL(string: "https://example.com")!
11+
12+
func testReturnsTrueIfAllowsLocalNetworkingIsTrue() throws {
13+
// Given
14+
let provider = FakeInfoDictionaryObjectProvider(appTransportSecurity: [
15+
"NSAllowsLocalNetworking": true,
16+
// This will be ignored
17+
"NSAllowsArbitraryLoads": true
18+
])
19+
let appTransportSecurity = AppTransportSecuritySettings(provider)
20+
21+
// When
22+
let secureAccessOnly = appTransportSecurity.secureAccessOnly(for: exampleURL)
23+
24+
// Then
25+
XCTAssertTrue(secureAccessOnly)
26+
}
27+
28+
func testReturnsFalseIfAllowsArbitraryLoadsIsTrue() throws {
29+
// Given
30+
let provider = FakeInfoDictionaryObjectProvider(appTransportSecurity: [
31+
"NSAllowsArbitraryLoads": true
32+
])
33+
let appTransportSecurity = AppTransportSecuritySettings(provider)
34+
35+
// When
36+
let secureAccessOnly = appTransportSecurity.secureAccessOnly(for: exampleURL)
37+
38+
// Then
39+
XCTAssertFalse(secureAccessOnly)
40+
}
41+
42+
func testReturnsTrueByDefault() throws {
43+
// Given
44+
let provider = FakeInfoDictionaryObjectProvider(appTransportSecurity: nil)
45+
let appTransportSecurity = AppTransportSecuritySettings(provider)
46+
47+
// When
48+
let secureAccessOnly = appTransportSecurity.secureAccessOnly(for: exampleURL)
49+
50+
// Then
51+
XCTAssertTrue(secureAccessOnly)
52+
}
53+
54+
func testReturnsTrueIfNothingIsDefined() throws {
55+
// Given
56+
let provider = FakeInfoDictionaryObjectProvider(appTransportSecurity: [String: Any]())
57+
let appTransportSecurity = AppTransportSecuritySettings(provider)
58+
59+
// When
60+
let secureAccessOnly = appTransportSecurity.secureAccessOnly(for: exampleURL)
61+
62+
// Then
63+
XCTAssertTrue(secureAccessOnly)
64+
}
65+
66+
func testReturnsFalseIfAllowsInsecureHTTPLoadsIsTrue() throws {
67+
// Given
68+
let provider = FakeInfoDictionaryObjectProvider(appTransportSecurity: [
69+
"NSExceptionDomains": [
70+
"shiki.me": [
71+
"NSExceptionAllowsInsecureHTTPLoads": true
72+
]
73+
]
74+
])
75+
let appTransportSecurity = AppTransportSecuritySettings(provider)
76+
let url = try XCTUnwrap(URL(string: "http://shiki.me"))
77+
78+
// When
79+
let secureAccessOnly = appTransportSecurity.secureAccessOnly(for: url)
80+
81+
// Then
82+
XCTAssertFalse(secureAccessOnly)
83+
}
84+
85+
func testReturnsTrueIfAllowsInsecureHTTPLoadsIsNotProvided() throws {
86+
// Given
87+
let provider = FakeInfoDictionaryObjectProvider(appTransportSecurity: [
88+
"NSExceptionDomains": [
89+
"shiki.me": [String: Any]()
90+
],
91+
// This value will be ignored because there is an exception for shiki.me
92+
"NSAllowsArbitraryLoads": true
93+
])
94+
let appTransportSecurity = AppTransportSecuritySettings(provider)
95+
let url = try XCTUnwrap(URL(string: "http://shiki.me"))
96+
97+
// When
98+
let secureAccessOnly = appTransportSecurity.secureAccessOnly(for: url)
99+
100+
// Then
101+
XCTAssertTrue(secureAccessOnly)
102+
}
103+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Foundation
2+
3+
extension Bundle {
4+
/// Returns the `Bundle` for the target.
5+
///
6+
/// If installed via CocoaPods, this will be `<target_name>.bundle`, otherwise it will be the framework bundle.
7+
@objc public class var coreAPITestsBundle: Bundle {
8+
#if SWIFT_PACKAGE
9+
return Bundle.module
10+
#else
11+
let defaultBundle = Bundle(for: BundleFinder.self)
12+
13+
guard let bundleURL = defaultBundle.resourceURL,
14+
let resourceBundle = Bundle(url: bundleURL.appendingPathComponent("CoreAPITests.bundle")) else {
15+
return defaultBundle
16+
}
17+
18+
return resourceBundle
19+
#endif
20+
}
21+
}
22+
23+
#if !SWIFT_PACKAGE
24+
private class BundleFinder: NSObject {}
25+
#endif
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#if SWIFT_PACKAGE
2+
@testable import CoreAPI
3+
#else
4+
@testable import WordPressKit
5+
#endif
6+
7+
class FakeInfoDictionaryObjectProvider: InfoDictionaryObjectProvider {
8+
private let appTransportSecurity: [String: Any]?
9+
10+
init(appTransportSecurity: [String: Any]?) {
11+
self.appTransportSecurity = appTransportSecurity
12+
}
13+
14+
func object(forInfoDictionaryKey key: String) -> Any? {
15+
if key == "NSAppTransportSecurity" {
16+
return appTransportSecurity
17+
}
18+
19+
return nil
20+
}
21+
}

0 commit comments

Comments
 (0)