Skip to content

Commit 119c1fd

Browse files
committed
Fix crashes in tests
1 parent cb9ba8f commit 119c1fd

File tree

4 files changed

+24
-17
lines changed

4 files changed

+24
-17
lines changed

WooCommerce/Classes/AppDelegate.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,12 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
5757
let stores = ServiceLocator.stores
5858
let analytics = ServiceLocator.analytics
5959
let pushNotesManager = ServiceLocator.pushNotesManager
60+
61+
/// This is important to initialize early as there are a few code points where the authenticator is used.
62+
ServiceLocator.authenticationManager.initialize()
6063
stores.initializeAfterDependenciesAreInitialized()
61-
setupAnalytics(analytics)
6264

65+
setupAnalytics(analytics)
6366
setupCocoaLumberjack()
6467
setupLibraryLogger()
6568
setupLogLevel(.verbose)

WooCommerce/Classes/ViewRelated/AppCoordinator.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,11 @@ private extension AppCoordinator {
238238

239239
/// Configures the WPAuthenticator for usage in both logged-in and logged-out states.
240240
func configureAuthenticator() {
241-
authenticationManager.initialize()
241+
if isRunningTests {
242+
/// This is needed to fix crashes in unit tests.
243+
/// The authenticator is initialized in AppDelegate when running the app.
244+
authenticationManager.initialize()
245+
}
242246
authenticationManager.setLoggedOutAppSettings(loggedOutAppSettings)
243247
authenticationManager.displayAuthenticatorIfLoggedOut = { [weak self] in
244248
guard let self, self.isLoggedIn == false else { return nil }

WooCommerce/Classes/Yosemite/DefaultStoresManager.swift

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import class Networking.WordPressOrgNetwork
77
import KeychainAccess
88
import class WidgetKit.WidgetCenter
99
import Experiments
10-
import class WordPressAuthenticator.WordPressComBlogService
10+
import WordPressAuthenticator
1111
import enum NetworkingCore.RequestAuthenticationMode
1212

1313
// MARK: - DefaultStoresManager
@@ -765,17 +765,19 @@ private extension DefaultStoresManager {
765765
self.updateAndReloadWidgetInformation(with: site.siteID)
766766
/// Trigger the `v1.1/connect/site-info` API to get information about
767767
/// the site's Jetpack status and whether it's a WPCom site.
768-
let service = WordPressComBlogService()
769-
service.fetchUnauthenticatedSiteInfoForAddress(for: url, success: { [weak self] info in
768+
WordPressAuthenticator.fetchSiteInfo(for: url) { [weak self] result in
770769
guard let self else { return }
771-
let updatedSite = site.copy(isJetpackThePluginInstalled: info.hasJetpack,
772-
isJetpackConnected: info.isJetpackConnected,
773-
isWordPressComStore: info.isWPCom)
774-
sessionManager.defaultSite = updatedSite
775-
updateAndReloadWidgetInformation(with: site.siteID)
776-
}, failure: { error in
777-
DDLogError("⛔️ Cannot fetch generic site info: \(error)")
778-
})
770+
switch result {
771+
case .success(let info):
772+
let updatedSite = site.copy(isJetpackThePluginInstalled: info.hasJetpack,
773+
isJetpackConnected: info.isJetpackConnected,
774+
isWordPressComStore: info.isWPCom)
775+
self.sessionManager.defaultSite = updatedSite
776+
self.updateAndReloadWidgetInformation(with: site.siteID)
777+
case .failure(let error):
778+
DDLogError("⛔️ Cannot fetch generic site info: \(error)")
779+
}
780+
}
779781
case .failure(let error):
780782
DDLogError("⛔️ Cannot fetch WordPress site info: \(error)")
781783
}

WooCommerce/WordPressAuthenticator/Services/WordPressComBlogService.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import Foundation
22

33
// MARK: - WordPress.com BlogService
44
//
5-
public class WordPressComBlogService {
6-
7-
public init() {}
5+
class WordPressComBlogService {
86

97
/// Returns a new anonymous instance of WordPressComRestApi.
108
///
@@ -15,7 +13,7 @@ public class WordPressComBlogService {
1513
}
1614

1715

18-
public func fetchUnauthenticatedSiteInfoForAddress(for address: String, success: @escaping (WordPressComSiteInfo) -> Void, failure: @escaping (Error) -> Void) {
16+
func fetchUnauthenticatedSiteInfoForAddress(for address: String, success: @escaping (WordPressComSiteInfo) -> Void, failure: @escaping (Error) -> Void) {
1917
let remote = BlogServiceRemoteREST(wordPressComRestApi: anonymousAPI, siteID: 0)
2018
remote.fetchUnauthenticatedSiteInfo(forAddress: address, success: { response in
2119
guard let response = response else {

0 commit comments

Comments
 (0)