Skip to content

Commit 1e55456

Browse files
authored
Merge pull request #8543 from woocommerce/feat/8453-show-alert-for-inaccessible-login-url
REST API: Show alert for inaccessible login or admin URL
2 parents cdac7d8 + eff1bfa commit 1e55456

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

Networking/Networking/ApplicationPassword/ApplicationPasswordUseCase.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public enum ApplicationPasswordUseCaseError: Error {
88
case duplicateName
99
case applicationPasswordsDisabled
1010
case failedToConstructLoginOrAdminURLUsingSiteAddress
11+
case unauthorizedRequest
1112
}
1213

1314
public struct ApplicationPassword {
@@ -163,6 +164,8 @@ private extension DefaultApplicationPasswordUseCase {
163164
continuation.resume(throwing: ApplicationPasswordUseCaseError.applicationPasswordsDisabled)
164165
case .responseValidationFailed(reason: .unacceptableStatusCode(code: ErrorCode.duplicateNameErrorCode)):
165166
continuation.resume(throwing: ApplicationPasswordUseCaseError.duplicateName)
167+
case .responseValidationFailed(reason: .unacceptableStatusCode(code: ErrorCode.unauthorized)):
168+
continuation.resume(throwing: ApplicationPasswordUseCaseError.unauthorizedRequest)
166169
default:
167170
continuation.resume(throwing: error)
168171
}
@@ -209,6 +212,7 @@ private extension DefaultApplicationPasswordUseCase {
209212
static let notFound = 404
210213
static let applicationPasswordsDisabledErrorCode = 501
211214
static let duplicateNameErrorCode = 409
215+
static let unauthorized = 401
212216
}
213217

214218
enum Constants {

Networking/NetworkingTests/ApplicationPassword/DefaultApplicationPasswordUseCaseTests.swift

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import XCTest
22
@testable import Networking
3-
3+
import Alamofire
44

55
/// DefaultApplicationPasswordUseCase Unit Tests
66
///
@@ -43,4 +43,50 @@ final class DefaultApplicationPasswordUseCaseTests: XCTestCase {
4343
XCTAssertEqual(password.password.secretValue, "passwordvalue")
4444
XCTAssertEqual(password.wpOrgUsername, username)
4545
}
46+
47+
func test_applicationPasswordsDisabled_error_is_thrown_if_generating_password_fails_with_501_error() async throws {
48+
// Given
49+
let error = AFError.responseValidationFailed(reason: .unacceptableStatusCode(code: 501))
50+
network.simulateError(requestUrlSuffix: URLSuffix.generateApplicationPassword, error: error)
51+
let username = "demo"
52+
let siteAddress = "https://test.com"
53+
let sut = try DefaultApplicationPasswordUseCase(username: username,
54+
password: "qeWOhQ5RUV8W",
55+
siteAddress: siteAddress,
56+
network: network)
57+
58+
// When
59+
var failure: ApplicationPasswordUseCaseError?
60+
do {
61+
let _ = try await sut.generateNewPassword()
62+
} catch {
63+
failure = error as? ApplicationPasswordUseCaseError
64+
}
65+
66+
// Then
67+
XCTAssertTrue(failure == .applicationPasswordsDisabled)
68+
}
69+
70+
func test_unauthorizedRequest_error_is_thrown_if_generating_password_fails_with_401_error() async throws {
71+
// Given
72+
let error = AFError.responseValidationFailed(reason: .unacceptableStatusCode(code: 401))
73+
network.simulateError(requestUrlSuffix: URLSuffix.generateApplicationPassword, error: error)
74+
let username = "demo"
75+
let siteAddress = "https://test.com"
76+
let sut = try DefaultApplicationPasswordUseCase(username: username,
77+
password: "qeWOhQ5RUV8W",
78+
siteAddress: siteAddress,
79+
network: network)
80+
81+
// When
82+
var failure: ApplicationPasswordUseCaseError?
83+
do {
84+
let _ = try await sut.generateNewPassword()
85+
} catch {
86+
failure = error as? ApplicationPasswordUseCaseError
87+
}
88+
89+
// Then
90+
XCTAssertTrue(failure == .unauthorizedRequest)
91+
}
4692
}

WooCommerce/Classes/Authentication/PostSiteCredentialLoginChecker.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ private extension PostSiteCredentialLoginChecker {
5252
let errorUI = applicationPasswordDisabledUI(for: siteURL)
5353
navigationController.show(errorUI, sender: nil)
5454
}
55+
} catch ApplicationPasswordUseCaseError.unauthorizedRequest {
56+
await MainActor.run {
57+
self.showAlert(message: Localization.invalidLoginOrAdminURL, in: navigationController)
58+
}
5559
} catch {
5660
// show generic error
5761
await MainActor.run {
@@ -150,6 +154,12 @@ private extension PostSiteCredentialLoginChecker {
150154
onRetry()
151155
}
152156
alert.addAction(retryAction)
157+
} else {
158+
let supportAction = UIAlertAction(title: Localization.contactSupport, style: .default) { _ in
159+
navigationController.popViewController(animated: true)
160+
ServiceLocator.authenticationManager.presentSupport(from: navigationController, sourceTag: .loginSiteAddress)
161+
}
162+
alert.addAction(supportAction)
153163
}
154164
let restartAction = UIAlertAction(title: Localization.restartLoginButton, style: .cancel) { [weak self] _ in
155165
self?.stores.deauthenticate()
@@ -186,6 +196,11 @@ private extension PostSiteCredentialLoginChecker {
186196
"Error checking for the WooCommerce plugin.",
187197
comment: "Error message displayed when the WooCommerce plugin detail cannot be fetched after authentication"
188198
)
199+
static let invalidLoginOrAdminURL = NSLocalizedString(
200+
"Application password cannot be generated due to a custom login or admin URL on your site.",
201+
comment: "Message to display when the constructed admin or login URL for the logged-in site is not accessible"
202+
)
203+
static let contactSupport = NSLocalizedString("Contact Support", comment: "Button to contact support for login")
189204
static let retryButton = NSLocalizedString("Try Again", comment: "Button to refetch application password for the current site")
190205
static let restartLoginButton = NSLocalizedString("Log In With Another Account", comment: "Button to restart the login flow.")
191206
}

WooCommerce/WooCommerceTests/Authentication/PostSiteCredentialLoginCheckerTests.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ final class PostSiteCredentialLoginCheckerTests: XCTestCase {
183183
stores.whenReceivingAction(ofType: WordPressSiteAction.self) { action in
184184
switch action {
185185
case .fetchSiteInfo(_, let completion):
186-
let site = Site.fake().copy(isWooCommerceActive: false)
187186
completion(.failure(NetworkError.timeout))
188187
}
189188
}

0 commit comments

Comments
 (0)